| author | Da Risk <da_risk@geekorum.com> |
| Mon, 05 May 2025 11:45:00 -0400 | |
| changeset 81 | 7926a1662eb2 |
| parent 79 | 79794afbbf95 |
| permissions | -rw-r--r-- |
| 29 | 1 |
/* |
2 |
* AboutOss is an utility library to retrieve and display |
|
3 |
* opensource licenses in Android applications. |
|
4 |
* |
|
|
34
ce299aacc068
build: update license headers
Da Risk <da_risk@geekorum.com>
parents:
29
diff
changeset
|
5 |
* Copyright (C) 2023-2025 by Frederic-Charles Barthelery. |
| 29 | 6 |
* |
7 |
* This file is part of AboutOss. |
|
8 |
* |
|
9 |
* AboutOss is free software: you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation, either version 3 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* AboutOss is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with AboutOss. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
package com.geekorum.aboutoss.core.licensee |
|
23 |
||
24 |
import kotlinx.serialization.Serializable |
|
25 |
import kotlinx.serialization.json.Json |
|
26 |
import okio.Source |
|
27 |
import okio.buffer |
|
28 |
||
| 79 | 29 |
/** |
30 |
* Parse licences data generated by the [licensee](https://github.com/cashapp/licensee) gradle plugin. |
|
31 |
*/ |
|
| 29 | 32 |
class LicenseeParser( |
33 |
input: Source |
|
34 |
): AutoCloseable {
|
|
35 |
private val buffered = input.buffer() |
|
36 |
||
| 79 | 37 |
/** |
38 |
* Read licensee data |
|
39 |
* |
|
40 |
* @return a map of dependency name to license |
|
41 |
*/ |
|
| 29 | 42 |
fun readLicensee(): Map<String, String> {
|
43 |
val json = Json {
|
|
44 |
ignoreUnknownKeys = true |
|
45 |
} |
|
46 |
val items: List<LicenseItem> = json.decodeFromString(buffered.readUtf8()) |
|
47 |
||
48 |
return items.associate {
|
|
49 |
val name = it.name ?: "${it.groupId}:${it.artifactId}"
|
|
50 |
val license = it.spdxLicenses.firstNotNullOfOrNull {
|
|
51 |
"${it.name}\n\n${it.url}"
|
|
52 |
} ?: it.unknownLicenses.firstNotNullOf {
|
|
53 |
"${it.name}\n\n${it.url}"
|
|
54 |
} |
|
55 |
name to license |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
override fun close() {
|
|
60 |
buffered.close() |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
||
65 |
@Serializable |
|
66 |
private data class LicenseItem( |
|
67 |
val groupId: String, |
|
68 |
val artifactId: String, |
|
69 |
val version: String, |
|
70 |
val spdxLicenses: List<SpdxLicense> = emptyList(), |
|
71 |
val unknownLicenses: List<UnknownLicense> = emptyList(), |
|
72 |
val name: String? = null, |
|
73 |
val scm: Scm? = null, |
|
74 |
) |
|
75 |
||
76 |
@Serializable |
|
77 |
private data class SpdxLicense( |
|
78 |
val identifier: String, |
|
79 |
val name: String, |
|
80 |
val url: String, |
|
81 |
) |
|
82 |
||
83 |
@Serializable |
|
84 |
private data class UnknownLicense( |
|
85 |
val name: String, |
|
86 |
val url: String |
|
87 |
) |
|
88 |
||
89 |
@Serializable |
|
90 |
private data class Scm( |
|
91 |
val url: String, |
|
92 |
) |