Skip to content
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

Merged
merged 8 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

plugins {
Expand All @@ -9,7 +7,6 @@ plugins {
alias(libs.plugins.publish)
alias(libs.plugins.dokka)
alias(libs.plugins.spotless)
alias(libs.plugins.download)
}

gradlePlugin {
Expand All @@ -28,7 +25,6 @@ tasks.named("validatePlugins") {
}

dependencies {
compileOnly gradleApi()
compileOnly libs.androidGradleApi
compileOnly libs.kotlinGradlePlugin
implementation libs.kotlinx.serialization
Expand All @@ -53,6 +49,7 @@ kotlin {
tasks.named("test") {
dependsOn(':publishAllPublicationsToTestingRepository')
systemProperty('licenseeVersion', VERSION_NAME)
systemProperty('generatedSpdxFile', file("build/generated/spdx/app/cash/licensee/licensesSpdx.kt").path)
systemProperty('line.separator', '\n')

testLogging {
Expand Down Expand Up @@ -110,17 +107,7 @@ tasks.named('check') { check ->
check.dependsOn(checkFixtureJars)
}

tasks.register('updateLicenses', de.undercouch.gradle.tasks.download.Download) { task ->
task.src('https://spdx.org/licenses/licenses.json')
def destDir = file('src/main/resources/app/cash/licensee')
task.dest(destDir)
task.doLast {
def destFile = new File(destDir, 'licenses.json')
def json = new JsonSlurper().parse(destFile)
json.licenses = json.licenses.sort { a,b -> a.licenseId <=> b.licenseId }
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

destFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(json))
}
}
sourceSets.main.kotlin.srcDir(tasks.register('generateSpdx', app.cash.licensee.GenerateSpdxIdTask))

publishing {
repositories {
Expand All @@ -134,7 +121,7 @@ publishing {
spotless {
kotlin {
target('**/*.kt')
targetExclude("**/src/test/test-build-logic/build/**")
targetExclude("**/src/test/test-build-logic/build/**", "**/generated-sources/**")
ktlint(libs.ktlint.get().version).editorConfigOverride([
'ktlint_standard_filename': 'disabled',
])
Expand Down
12 changes: 12 additions & 0 deletions gradle/build-logic/build.gradle.kts
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)
}
13 changes: 13 additions & 0 deletions gradle/build-logic/settings.gradle
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS

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())
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Yup, since 2.2.

I think I actually want to revert to continuing the embed the JSON

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val fileSpec = FileSpec.builder("app.cash.licensee", "licensesSpdx")
fileSpec.addType(addSpdxIdInterface())
return fileSpec.build()
return FileSpec.builder("app.cash.licensee", "licensesSpdx")
.addType(addSpdxIdInterface())
.build()

}

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>,
)
132 changes: 132 additions & 0 deletions gradle/build-logic/src/main/kotlin/app/cash/licensee/SpdxLicenses.kt
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package app.cash.licensee
@file:Suppress("HttpUrlsUsage")
package app.cash.licensee


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
}
}
Loading