Skip to content

Commit

Permalink
Merge pull request #13 from usefulness/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored Nov 11, 2023
2 parents 6b241f4 + 566d272 commit e114486
Show file tree
Hide file tree
Showing 28 changed files with 334 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
with:
build-root-directory: sample
gradle-version: ${{ matrix.gradle }}
arguments: assemble -PagpVersion=${{ matrix.agp }} --continue
arguments: check -PagpVersion=${{ matrix.agp }} --continue

- name: Upload reports
if: ${{ failure() }}
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ Options can be configured in the `licenseeForAndroid` extension:

```groovy
licenseeForAndroid {
enableKotlinCodeGeneration = false
enableAndroidAssetGeneration = true
androidAssetFileName = "licensee_artifacts.json"
singularVariantName = null
enableKotlinCodeGeneration = false
generatedPackageName = "io.github.usefulness.licensee"
enableAndroidAssetGeneration = true
androidAssetFileName = "licensee_artifacts.json"
singularVariantName = null
}
```

- `enableKotlinCodeGeneration` - Generates a static list of open source assets
- `generatedPackageName` - Generate Kotlin code under given package
- `enableAndroidAssetGeneration` - Enable asset generation. Will copy licensee report to android asset directory making it available as `androidAssetFileName`
- `androidAssetFileName` - The name of the asset file the licensee report gets copied to.
- `singularVariantName` - The name of the build variant that all variants will use to have always the same licensed, regardless of app variant. (i.e. "productionRelease")
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ maven-binarycompatiblity = "0.13.2"
[libraries]
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" }
junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" }
assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" }
agp-gradle-api = { module = "com.android.tools.build:gradle-api", version.ref = "google-agp" }
kotlin-gradle-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "maven-kotlin" }
Expand Down
2 changes: 2 additions & 0 deletions licensee-for-android/api/licensee-for-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ public abstract class io/github/usefulness/licensee/CodeGenerationTask : org/gra
public final fun action ()V
public abstract fun getInputFile ()Lorg/gradle/api/file/RegularFileProperty;
public abstract fun getOutputDirectory ()Lorg/gradle/api/file/DirectoryProperty;
public abstract fun getPackageName ()Lorg/gradle/api/provider/Property;
}

public class io/github/usefulness/licensee/LicenseeForAndroidExtension {
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public final fun getAndroidAssetFileName ()Lorg/gradle/api/provider/Property;
public final fun getEnableAndroidAssetGeneration ()Lorg/gradle/api/provider/Property;
public final fun getEnableKotlinCodeGeneration ()Lorg/gradle/api/provider/Property;
public final fun getGeneratedPackageName ()Lorg/gradle/api/provider/Property;
public final fun getSingularVariantName ()Lorg/gradle/api/provider/Property;
}

Expand Down
1 change: 1 addition & 0 deletions licensee-for-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation(libs.kotlinx.serialization.json.okio)
implementation(libs.com.squareup.okio)

testRuntimeOnly(libs.junit.platform.launcher)
testRuntimeOnly(libs.junit.jupiter.engine)

testImplementation(libs.junit.jupiter.api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.withIndent
import io.github.usefulness.licensee.core.Artifact

internal class ArtifactCodeGenerator(
Expand All @@ -13,59 +14,57 @@ internal class ArtifactCodeGenerator(
private val unknownLicensesTypeSpec: TypeSpec,
) {

@Suppress("SpreadOperator")
fun artifactCodeBlock(artifact: Artifact): CodeBlock {
val arguments = mutableListOf<Any>()
fun artifactCodeBlock(artifact: Artifact) = CodeBlock.builder()
.withIndent {
addStatement("Artifact(")
withIndent {
addStatement("groupId = %S,", artifact.groupId)
addStatement("artifactId = %S,", artifact.artifactId)
addStatement("version = %S,", artifact.version)
addStatement("name = %S,", artifact.name)

val statement = buildString {
appendLine(
"""Artifact(
|groupId = %S,
|artifactId = %S,
|version = %S,
""".trimMargin(),
)
arguments.add(artifact.groupId)
arguments.add(artifact.artifactId)
arguments.add(artifact.version)
if (artifact.name != null) {
appendLine("""name = %S,""")
arguments.add(artifact.name)
} else {
appendLine("""name = null,""")
}
if (artifact.spdxLicenses.isNullOrEmpty()) {
addStatement("spdxLicenses = %M(),", MemberName("kotlin.collections", "emptyList"))
} else {
addStatement("spdxLicenses = %M(", MemberName("kotlin.collections", "listOf"))
withIndent {
artifact.spdxLicenses.forEach { license ->
addStatement("%T(", ClassName(packageName, spdxLicensesTypeSpec.name!!))
withIndent {
addStatement("identifier = %S,", license.identifier)
addStatement("name = %S,", license.name)
addStatement("url = %S,", license.url)
}
addStatement("),")
}
}
addStatement("),")
}

appendLine("""spdxLicenses = %M(""")
arguments.add(MemberName("kotlin.collections", "listOf"))
artifact.spdxLicenses?.forEach {
appendLine("""%T(identifier = %S, name = %S, url = %S)""".trimMargin())
arguments.add(ClassName(packageName, spdxLicensesTypeSpec.name!!))
arguments.add(it.identifier)
arguments.add(it.name)
arguments.add(it.url)
}
appendLine("""),""")
if (artifact.scm == null) {
addStatement("scm = null,")
} else {
addStatement("scm = %T(url = %S),", ClassName(packageName, scmTypeSpec.name!!), artifact.scm.url)
}

if (artifact.scm != null) {
appendLine("""scm = %T(%S), """.trimMargin())
arguments.add(ClassName(packageName, scmTypeSpec.name!!))
arguments.add(artifact.scm.url)
} else {
appendLine("""scm = null, """.trimMargin())
if (artifact.unknownLicenses.isNullOrEmpty()) {
addStatement("unknownLicenses = %M(),", MemberName("kotlin.collections", "emptyList"))
} else {
addStatement("unknownLicenses = %M(", MemberName("kotlin.collections", "listOf"))
withIndent {
artifact.unknownLicenses.forEach { license ->
addStatement("%T(", ClassName(packageName, unknownLicensesTypeSpec.name!!))
withIndent {
addStatement("name = %S,", license.name)
addStatement("url = %S,", license.url)
}
addStatement("),")
}
}
addStatement("),")
}
}

appendLine("""unknownLicenses = %M(""")
arguments.add(MemberName("kotlin.collections", "listOf"))
artifact.unknownLicenses?.forEach {
appendLine("""%T(name = %S, url = %S)""".trimMargin())
arguments.add(ClassName(packageName, unknownLicensesTypeSpec.name!!))
arguments.add(it.name)
arguments.add(it.url)
}
appendLine("""),""")

appendLine("""|)""".trimMargin())
addStatement("),")
}
return CodeBlock.builder().addStatement(statement, *arguments.toTypedArray()).build()
}
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import okio.source
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
Expand All @@ -23,7 +25,6 @@ import org.gradle.api.tasks.TaskAction

@CacheableTask
public abstract class CodeGenerationTask : DefaultTask() {
private val packageName = "io.github.usefulness.licensee"

@get:OutputDirectory
public abstract val outputDirectory: DirectoryProperty
Expand All @@ -32,9 +33,13 @@ public abstract class CodeGenerationTask : DefaultTask() {
@get:InputFile
public abstract val inputFile: RegularFileProperty

@get:Input
public abstract val packageName: Property<String>

@TaskAction
@ExperimentalSerializationApi
public fun action() {
val packageName = packageName.get()
val licenseeTypesGenerator = LicenseeTypesGenerator(packageName)

FileSpec.builder(packageName, "Artifact")
Expand All @@ -57,7 +62,6 @@ public abstract class CodeGenerationTask : DefaultTask() {
addStatement("%M(", MemberName("kotlin.collections", "listOf"))
artifacts.forEach { artifact ->
add(artifactCodeGenerator.artifactCodeBlock(artifact))
addStatement(",")
}
addStatement(")")
}.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public open class LicenseeForAndroidExtension(objectFactory: ObjectFactory) {
*/
public val enableKotlinCodeGeneration: Property<Boolean> = objectFactory.property(default = false)

/**
* Generate kotlin code under given package
* Default: `io.github.usefulness.licensee`
*/
public val generatedPackageName: Property<String> = objectFactory.property(default = "io.github.usefulness.licensee")

/**
* Enable asset generation. Will copy licensee report to
* android asset directory making it available as 'androidAssetFileName'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class LicenseeForAndroidPlugin : Plugin<Project> {
CodeGenerationTask::class.java,
) {
it.inputFile.set(artifactsFile)
it.packageName.set(extension.generatedPackageName)
}

// Do NOT use `.kotlin` here: https://issuetracker.google.com/issues/268248348
Expand Down
Loading

0 comments on commit e114486

Please sign in to comment.