-
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 all commits
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 | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,3 @@ | ||||||||||||||||||
import groovy.json.JsonOutput | ||||||||||||||||||
import groovy.json.JsonSlurper | ||||||||||||||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion | ||||||||||||||||||
|
||||||||||||||||||
plugins { | ||||||||||||||||||
|
@@ -10,7 +8,6 @@ plugins { | |||||||||||||||||
alias(libs.plugins.publish) | ||||||||||||||||||
alias(libs.plugins.dokka) | ||||||||||||||||||
alias(libs.plugins.spotless) | ||||||||||||||||||
alias(libs.plugins.download) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
gradlePlugin { | ||||||||||||||||||
|
@@ -29,7 +26,6 @@ tasks.named("validatePlugins") { | |||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
dependencies { | ||||||||||||||||||
compileOnly gradleApi() | ||||||||||||||||||
compileOnly libs.androidGradleApi | ||||||||||||||||||
compileOnly libs.kotlinGradlePlugin | ||||||||||||||||||
implementation libs.kotlinx.serialization | ||||||||||||||||||
|
@@ -56,6 +52,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 { | ||||||||||||||||||
|
@@ -113,18 +110,27 @@ 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 } | ||||||||||||||||||
destFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(json)) | ||||||||||||||||||
Directory jsonFolder = layout.projectDirectory.dir("src/main/resources/app/cash/licensee") | ||||||||||||||||||
RegularFile jsonFile = jsonFolder.file("licenses.json") | ||||||||||||||||||
|
||||||||||||||||||
tasks.register("downloadLicensesJson", Copy) { | ||||||||||||||||||
group = "generatespdx" | ||||||||||||||||||
|
||||||||||||||||||
from(resources.text.fromUri("https://spdx.org/licenses/licenses.json").asFile()) { | ||||||||||||||||||
rename { | ||||||||||||||||||
jsonFile.asFile.name | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
into(jsonFolder) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
TaskProvider<app.cash.licensee.GenerateSpdxIdTask> generateSpdx = tasks.register('generateSpdx', app.cash.licensee.GenerateSpdxIdTask) { | ||||||||||||||||||
// Do not wire the output from writeSortedSpdx here to split downloading the remote file and code gen. | ||||||||||||||||||
inputJson.set(jsonFile) | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+127
to
+130
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
|
||||||||||||||||||
|
||||||||||||||||||
sourceSets.main.kotlin.srcDir(generateSpdx) | ||||||||||||||||||
|
||||||||||||||||||
publishing { | ||||||||||||||||||
repositories { | ||||||||||||||||||
maven { | ||||||||||||||||||
|
@@ -137,7 +143,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', | ||||||||||||||||||
// Making something an expression body should be a choice around readability. | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
`kotlin-dsl` | ||
alias(libs.plugins.kotlin.jvm) | ||
alias(libs.plugins.kotlin.serialization) | ||
} | ||
|
||
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,7 @@ | ||||||
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() | ||||||
google() | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,141 @@ | ||||||||||||||||||
/* | ||||||||||||||||||
* 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.file.RegularFileProperty | ||||||||||||||||||
import org.gradle.api.tasks.CacheableTask | ||||||||||||||||||
import org.gradle.api.tasks.InputFile | ||||||||||||||||||
import org.gradle.api.tasks.OutputDirectory | ||||||||||||||||||
import org.gradle.api.tasks.PathSensitive | ||||||||||||||||||
import org.gradle.api.tasks.PathSensitivity | ||||||||||||||||||
import org.gradle.api.tasks.TaskAction | ||||||||||||||||||
|
||||||||||||||||||
@CacheableTask | ||||||||||||||||||
abstract class GenerateSpdxIdTask : DefaultTask() { | ||||||||||||||||||
@get:InputFile | ||||||||||||||||||
@get:PathSensitive(PathSensitivity.RELATIVE) | ||||||||||||||||||
abstract val inputJson: RegularFileProperty | ||||||||||||||||||
|
||||||||||||||||||
@get:OutputDirectory | ||||||||||||||||||
abstract val generatedSpdx: DirectoryProperty | ||||||||||||||||||
|
||||||||||||||||||
init { | ||||||||||||||||||
group = "generateSpdx" | ||||||||||||||||||
generatedSpdx.convention(project.layout.buildDirectory.dir("generated/spdx")) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@TaskAction | ||||||||||||||||||
fun write() { | ||||||||||||||||||
val parsed = SpdxLicenses.parseJson(inputJson.asFile.get().readText(), 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 = SpdxId.nestedClass("Companion") | ||||||||||||||||||
|
||||||||||||||||||
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>, | ||
) |
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.