-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Download and generate Spdx licenses #316
Changes from 1 commit
8fb66d5
c6d15c8
8874cbd
bef2c7b
a63231c
06a9652
c0ffaf2
d3da860
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
plugins { | ||
`kotlin-dsl` | ||
kotlin("plugin.serialization") version embeddedKotlinVersion | ||
} | ||
|
||
dependencies { | ||
implementation(libs.kotlinx.serialization) | ||
implementation(libs.kotlinpoet) | ||
|
||
testImplementation(kotlin("test-junit")) | ||
testImplementation(libs.assertk) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||
import org.gradle.api.initialization.resolve.RepositoriesMode | ||||||
|
||||||
dependencyResolutionManagement { | ||||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
repositories { | ||||||
mavenCentral() | ||||||
} | ||||||
versionCatalogs.register("libs") { | ||||||
from(files("../libs.versions.toml")) | ||||||
} | ||||||
} | ||||||
|
||||||
rootProject.name = 'build-logic' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import org.gradle.api.initialization.resolve.RepositoriesMode | ||
|
||
dependencyResolutionManagement { | ||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) | ||
repositories { | ||
mavenCentral() | ||
google() | ||
} | ||
} | ||
JakeWharton marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,138 @@ | ||||||||||||||||||
/* | ||||||||||||||||||
* Copyright (C) 2024 Square, Inc. | ||||||||||||||||||
* | ||||||||||||||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||
* you may not use this file except in compliance with the License. | ||||||||||||||||||
* You may obtain a copy of the License at | ||||||||||||||||||
* | ||||||||||||||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||
* | ||||||||||||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||
* See the License for the specific language governing permissions and | ||||||||||||||||||
* limitations under the License. | ||||||||||||||||||
*/ | ||||||||||||||||||
package app.cash.licensee | ||||||||||||||||||
|
||||||||||||||||||
import com.squareup.kotlinpoet.ClassName | ||||||||||||||||||
import com.squareup.kotlinpoet.FileSpec | ||||||||||||||||||
import com.squareup.kotlinpoet.FunSpec | ||||||||||||||||||
import com.squareup.kotlinpoet.KModifier | ||||||||||||||||||
import com.squareup.kotlinpoet.LIST | ||||||||||||||||||
import com.squareup.kotlinpoet.MemberName.Companion.member | ||||||||||||||||||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||||||||||||||||||
import com.squareup.kotlinpoet.PropertySpec | ||||||||||||||||||
import com.squareup.kotlinpoet.STRING | ||||||||||||||||||
import com.squareup.kotlinpoet.TypeSpec | ||||||||||||||||||
import org.gradle.api.DefaultTask | ||||||||||||||||||
import org.gradle.api.file.DirectoryProperty | ||||||||||||||||||
import org.gradle.api.provider.Property | ||||||||||||||||||
import org.gradle.api.tasks.CacheableTask | ||||||||||||||||||
import org.gradle.api.tasks.Input | ||||||||||||||||||
import org.gradle.api.tasks.OutputDirectory | ||||||||||||||||||
import org.gradle.api.tasks.TaskAction | ||||||||||||||||||
|
||||||||||||||||||
@CacheableTask | ||||||||||||||||||
abstract class GenerateSpdxIdTask : DefaultTask() { | ||||||||||||||||||
@get:Input | ||||||||||||||||||
abstract val inputJson: Property<String> | ||||||||||||||||||
|
||||||||||||||||||
@get:OutputDirectory | ||||||||||||||||||
abstract val generatedSpdx: DirectoryProperty | ||||||||||||||||||
|
||||||||||||||||||
init { | ||||||||||||||||||
inputJson.convention(project.resources.text.fromUri("https://spdx.org/licenses/licenses.json").asString()) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whaaaaaaat is going on here. Gradle has an entire HTTP client hidden in a property factory? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I actually want to revert to continuing the embed the JSON, except we can embed it into the build-logic for this task to use. I'm generally wary of depending on the internet and would prefer updates be explicit rather than it magically getting new values based on whenever you build. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yup, since 2.2.
So we download the json (manually/ automatically) using Gradle but keep the generator to generate Kotlin code from the Json to have typed Spdx Ids? Sounds okay for me too. Yeah, and it can't fail a release due connections problems/new entries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. And that's basically the goal. We don't want it to change out from underneath us. Also means old builds are reproducible, although not sure why that would ever be needed. |
||||||||||||||||||
generatedSpdx.convention(project.layout.buildDirectory.dir("generated/spdx")) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@TaskAction | ||||||||||||||||||
fun write() { | ||||||||||||||||||
val parsed = SpdxLicenses.parseJson(inputJson.get(), defaultFallbackUrls) | ||||||||||||||||||
parsed.generate().writeTo(generatedSpdx.asFile.get()) | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
private fun SpdxLicenses.generate(): FileSpec { | ||||||||||||||||||
val fileSpec = FileSpec.builder("app.cash.licensee", "licensesSpdx") | ||||||||||||||||||
|
||||||||||||||||||
fileSpec.addType(addSpdxIdInterface()) | ||||||||||||||||||
|
||||||||||||||||||
return fileSpec.build() | ||||||||||||||||||
Comment on lines
+60
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
private val SpdxId: ClassName = ClassName("app.cash.licensee", "SpdxId") | ||||||||||||||||||
private val SpdxIdCompanion: ClassName = ClassName("app.cash.licensee", "SpdxId", "Companion") | ||||||||||||||||||
JakeWharton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
private val SpdxLicenseJson.identifier: String get() = if (id == "0BSD") { | ||||||||||||||||||
"ZeroBSD" // hardcoded because it is the only id starting with a digit. | ||||||||||||||||||
} else { | ||||||||||||||||||
id | ||||||||||||||||||
.replace("-", "_") | ||||||||||||||||||
.replace(".", "") | ||||||||||||||||||
.replace("+", "Plus") | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
private fun SpdxLicenses.addSpdxIdInterface(): TypeSpec = TypeSpec.classBuilder("SpdxId").apply { | ||||||||||||||||||
primaryConstructor( | ||||||||||||||||||
FunSpec.constructorBuilder() | ||||||||||||||||||
.addParameter("id", STRING) | ||||||||||||||||||
.addParameter("name", STRING) | ||||||||||||||||||
.addParameter("url", STRING) | ||||||||||||||||||
.build(), | ||||||||||||||||||
) | ||||||||||||||||||
addProperty(PropertySpec.builder("id", STRING).initializer("id").build()) | ||||||||||||||||||
addProperty(PropertySpec.builder("name", STRING).initializer("name").build()) | ||||||||||||||||||
addProperty(PropertySpec.builder("url", STRING).initializer("url").build()) | ||||||||||||||||||
addSuperinterface(java.io.Serializable::class) | ||||||||||||||||||
|
||||||||||||||||||
addType(spdxIdCompanion()) | ||||||||||||||||||
}.build() | ||||||||||||||||||
|
||||||||||||||||||
private fun SpdxLicenses.spdxIdCompanion(): TypeSpec = TypeSpec.companionObjectBuilder().apply { | ||||||||||||||||||
for ((_, license) in identifierToLicense) { | ||||||||||||||||||
addProperty( | ||||||||||||||||||
PropertySpec.builder(license.identifier, SpdxId) | ||||||||||||||||||
.addAnnotation(JvmStatic::class) | ||||||||||||||||||
.addKdoc(license.name) | ||||||||||||||||||
.initializer("%T(%S, %S, %S)", SpdxId, license.id, license.name, license.targetUrl) | ||||||||||||||||||
.build(), | ||||||||||||||||||
) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
addFunction(findByIdentifier()) | ||||||||||||||||||
addFunction(findByUrl()) | ||||||||||||||||||
}.build() | ||||||||||||||||||
|
||||||||||||||||||
private fun SpdxLicenses.findByIdentifier(): FunSpec = FunSpec.builder("findByIdentifier").apply { | ||||||||||||||||||
addAnnotation(JvmStatic::class) | ||||||||||||||||||
addParameter("id", STRING) | ||||||||||||||||||
returns(SpdxId.copy(nullable = true)) | ||||||||||||||||||
|
||||||||||||||||||
beginControlFlow("return when (id)") | ||||||||||||||||||
for ((_, license) in identifierToLicense) { | ||||||||||||||||||
addCode("%S -> %M\n", license.id, SpdxIdCompanion.member(license.identifier)) | ||||||||||||||||||
} | ||||||||||||||||||
addCode("else -> null\n") | ||||||||||||||||||
endControlFlow() | ||||||||||||||||||
}.build() | ||||||||||||||||||
|
||||||||||||||||||
private fun SpdxLicenses.findByUrl(): FunSpec = FunSpec.builder("findByUrl").apply { | ||||||||||||||||||
addParameter("url", STRING) | ||||||||||||||||||
addModifiers(KModifier.INTERNAL) | ||||||||||||||||||
returns(LIST.parameterizedBy(SpdxId)) | ||||||||||||||||||
|
||||||||||||||||||
beginControlFlow("return when (url)") | ||||||||||||||||||
for ((urls, licenses) in simplified) { | ||||||||||||||||||
for (url in urls) { | ||||||||||||||||||
addCode("%S,\n", url) | ||||||||||||||||||
} | ||||||||||||||||||
addCode(" -> listOf(") | ||||||||||||||||||
for (license in licenses) { | ||||||||||||||||||
addCode("\n%M,", SpdxIdCompanion.member(license.identifier)) | ||||||||||||||||||
} | ||||||||||||||||||
addCode("\n)\n") | ||||||||||||||||||
} | ||||||||||||||||||
addCode("else -> emptyList()\n") | ||||||||||||||||||
endControlFlow() | ||||||||||||||||||
}.build() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.licensee | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class SpdxLicensesJson( | ||
val licenses: List<SpdxLicenseJson>, | ||
) | ||
|
||
@Serializable | ||
internal data class SpdxLicenseJson( | ||
@SerialName("licenseId") val id: String, | ||
val name: String, | ||
@SerialName("reference") val spdxUrl: String, | ||
@SerialName("seeAlso") val otherUrls: List<String>, | ||
) |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,132 @@ | ||||||||||
/* | ||||||||||
* Copyright (C) 2024 Square, Inc. | ||||||||||
* | ||||||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
* you may not use this file except in compliance with the License. | ||||||||||
* You may obtain a copy of the License at | ||||||||||
* | ||||||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
* | ||||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
* See the License for the specific language governing permissions and | ||||||||||
* limitations under the License. | ||||||||||
*/ | ||||||||||
package app.cash.licensee | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
import kotlinx.serialization.json.Json | ||||||||||
|
||||||||||
internal class SpdxLicenses( | ||||||||||
val identifierToLicense: Map<String, SpdxLicenseJson>, | ||||||||||
private val urlToLicense: Map<String, List<SpdxLicenseJson>>, | ||||||||||
) { | ||||||||||
fun findByIdentifier(id: String): SpdxLicenseJson? = identifierToLicense[id] | ||||||||||
fun findByUrl(url: String): List<SpdxLicenseJson>? = urlToLicense[url] | ||||||||||
|
||||||||||
val simplified: List<Pair<List<String>, List<SpdxLicenseJson>>> = urlToLicense.simplify() | ||||||||||
|
||||||||||
companion object { | ||||||||||
private val format: Json = Json { | ||||||||||
ignoreUnknownKeys = true | ||||||||||
} | ||||||||||
|
||||||||||
internal fun parseJson( | ||||||||||
json: String, | ||||||||||
withFallbackUrls: FallbackBuilder.() -> Unit, | ||||||||||
): SpdxLicenses { | ||||||||||
val licenses = format.decodeFromString( | ||||||||||
SpdxLicensesJson.serializer(), | ||||||||||
json, | ||||||||||
).licenses.sortedBy { | ||||||||||
it.id | ||||||||||
} | ||||||||||
|
||||||||||
val identifierToLicense = licenses.associateBy { it.id } | ||||||||||
|
||||||||||
val mapUrls = licenses.mapUrls() | ||||||||||
val urlToLicense = mapUrls.mapValues { | ||||||||||
it.value.toList() | ||||||||||
} | ||||||||||
val withFallbacks = mapUrls.addFallbackUrls( | ||||||||||
identifierToLicense, | ||||||||||
urlToLicense, | ||||||||||
withFallbackUrls, | ||||||||||
) | ||||||||||
|
||||||||||
return SpdxLicenses( | ||||||||||
identifierToLicense, | ||||||||||
withFallbacks, | ||||||||||
) | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
internal fun List<SpdxLicenseJson>.mapUrls(): Map<String, List<SpdxLicenseJson>> { | ||||||||||
val urlToIds: MutableMap<String, MutableList<SpdxLicenseJson>> = mutableMapOf() | ||||||||||
|
||||||||||
for (license in this) { | ||||||||||
urlToIds.computeIfAbsent(license.spdxUrl) { mutableListOf() }.add(license) | ||||||||||
|
||||||||||
for (otherUrl in license.otherUrls) { | ||||||||||
if (otherUrl.startsWith("http://")) { | ||||||||||
// Assume an 'https' variant is reachable. | ||||||||||
val httpsUrl = "https" + otherUrl.substring(4) | ||||||||||
urlToIds.computeIfAbsent(httpsUrl) { mutableListOf() }.add(license) | ||||||||||
} | ||||||||||
urlToIds.computeIfAbsent(otherUrl) { mutableListOf() }.add(license) | ||||||||||
} | ||||||||||
} | ||||||||||
return urlToIds.mapValues { it.value.toList() } | ||||||||||
} | ||||||||||
|
||||||||||
internal fun Map<String, List<SpdxLicenseJson>>.addFallbackUrls( | ||||||||||
idToLicense: Map<String, SpdxLicenseJson>, | ||||||||||
urlToLicenses: Map<String, List<SpdxLicenseJson>>, | ||||||||||
action: FallbackBuilder.() -> Unit, | ||||||||||
): Map<String, List<SpdxLicenseJson>> = toMutableMap().apply { | ||||||||||
FallbackBuilder( | ||||||||||
idToLicense, | ||||||||||
urlToLicenses, | ||||||||||
this, | ||||||||||
).action() | ||||||||||
} | ||||||||||
|
||||||||||
internal class FallbackBuilder( | ||||||||||
private val findByIdentifier: Map<String, SpdxLicenseJson>, | ||||||||||
private val findByUrl: Map<String, List<SpdxLicenseJson>>, | ||||||||||
private val result: MutableMap<String, List<SpdxLicenseJson>>, | ||||||||||
) { | ||||||||||
fun putLicense(vararg spdxIds: String, urls: MutableList<String>.() -> Unit) { | ||||||||||
val licenses = spdxIds.map { | ||||||||||
requireNotNull(findByIdentifier[it]) { | ||||||||||
"No SPDX identifier '$it' in the embedded set" | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
for (url in buildList(urls)) { | ||||||||||
require(findByUrl[url].orEmpty().isEmpty()) { | ||||||||||
"$url is canonical and does not need to be a fallback" | ||||||||||
} | ||||||||||
require(result.put(url, licenses) == null) { | ||||||||||
"$url specified twice" | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
internal fun Map<String, List<SpdxLicenseJson>>.simplify(): List<Pair<List<String>, List<SpdxLicenseJson>>> = | ||||||||||
entries.groupBy({ it.value }, { it.key }).toList().map { | ||||||||||
it.second.sorted() to it.first | ||||||||||
}.sortedBy { | ||||||||||
it.first.first() | ||||||||||
} | ||||||||||
|
||||||||||
internal val SpdxLicenseJson.targetUrl get() = (otherUrls.firstOrNull() ?: spdxUrl).let { firstUrl -> | ||||||||||
if (firstUrl.startsWith("http://")) { | ||||||||||
// Assume an 'https' variant is reachable. | ||||||||||
"https" + firstUrl.substring(4) | ||||||||||
} else { | ||||||||||
firstUrl | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I COMPLETELY forgot that I was doing this. Maybe we do need sort... I thought they were finally sorting because they said they would.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverting bef2c7b is easy, but on the other hand, we could test it with the next license update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I lost track of this PR.
Yeah we need to revert. It's definitely not sorted, despite me asking.