Skip to content

Commit

Permalink
Everything seems to be working fine now
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Nov 10, 2023
1 parent 9a26655 commit ca39410
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.okio.decodeFromBufferedSource
import okio.buffer
import okio.source
import org.gradle.api.DefaultTask
Expand All @@ -17,7 +19,7 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import se.premex.gross.core.LicenseParser
import se.premex.gross.core.Artifact

@CacheableTask
abstract class CodeGenerationTask : DefaultTask() {
Expand All @@ -42,7 +44,7 @@ abstract class CodeGenerationTask : DefaultTask() {
.addType(licenseeTypesGenerator.artifactTypeSpec)
.build().writeTo(outputDirectory.asFile.get())

val artifacts = LicenseParser().decode(inputFile.asFile.get().source().buffer())
val artifacts = Json.decodeFromBufferedSource<List<Artifact>>(inputFile.asFile.get().source().buffer())

val artifactCodeGenerator = ArtifactCodeGenerator(
packageName = packageName,
Expand Down
46 changes: 22 additions & 24 deletions licensee-for-android/src/main/kotlin/se/premex/gross/GrossPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package se.premex.gross

import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.AndroidComponentsExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.reporting.ReportingExtension
import java.util.Locale

public class GrossPlugin : Plugin<Project> {
override fun apply(project: Project) {
Expand All @@ -16,57 +15,56 @@ public class GrossPlugin : Plugin<Project> {
val reportingExtension: ReportingExtension =
project.extensions.getByType(ReportingExtension::class.java)

listOf(
"com.android.application",
"com.android.library",
"com.android.dynamic-feature",
)
.forEach { pluginId ->
project.pluginManager.withPlugin(pluginId) {
configureAndroidPlugin(project, extension, reportingExtension)
project.pluginManager.withPlugin("app.cash.licensee") {
listOf(
"com.android.application",
"com.android.library",
"com.android.dynamic-feature",
)
.forEach { pluginId ->
project.pluginManager.withPlugin(pluginId) {
configureAndroidPlugin(project, extension, reportingExtension)
}
}
}
}
}

private fun configureAndroidPlugin(project: Project, extension: GrossExtension, reportingExtension: ReportingExtension) {
val androidComponentsExtension =
project.extensions.getByName("androidComponents") as ApplicationAndroidComponentsExtension
project.extensions.getByName("androidComponents") as AndroidComponentsExtension<*, *, *>

androidComponentsExtension.onVariants { variant ->
val capitalizedVariantName = variant.name.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase(Locale.getDefault())
} else {
it.toString()
}
}
val target = variant.name
val targetCapitalized = target.replaceFirstChar(Char::titlecase)
val licenseeTaskName = "licenseeAndroid$targetCapitalized"

val artifactsFile = reportingExtension.file("licensee/${variant.name}/artifacts.json")
val artifactsFile = reportingExtension.file("licensee/android$targetCapitalized/artifacts.json")
if (extension.enableAndroidAssetGeneration.get()) {
val copyArtifactsTask =
project.tasks.register("copy${capitalizedVariantName}LicenseeReportToAssets", AssetCopyTask::class.java) {
project.tasks.register("copy${targetCapitalized}LicenseeReportToAssets", AssetCopyTask::class.java) {
it.inputFile.set(artifactsFile)
it.targetFileName.set(extension.androidAssetFileName.get())
}
variant.sources.assets!!.addGeneratedSourceDirectory(
copyArtifactsTask,
AssetCopyTask::outputDirectory,
)
copyArtifactsTask.configure { it.dependsOn("licensee$capitalizedVariantName") }
copyArtifactsTask.configure { it.dependsOn(licenseeTaskName) }
}

if (extension.enableKotlinCodeGeneration.get()) {
val codeGenerationTask =
project.tasks.register("${capitalizedVariantName}LicenseeReportToKotlin", CodeGenerationTask::class.java) {
project.tasks.register("${target}LicenseeReportToKotlin", CodeGenerationTask::class.java) {
it.inputFile.set(artifactsFile)
}

// Do NOT use `.kotlin` here: https://issuetracker.google.com/issues/268248348
variant.sources.java!!.addGeneratedSourceDirectory(
codeGenerationTask,
CodeGenerationTask::outputDirectory,
)

codeGenerationTask.configure { it.dependsOn("licensee$capitalizedVariantName") }
codeGenerationTask.configure { it.dependsOn(licenseeTaskName) }
}
}
}
Expand Down

This file was deleted.

13 changes: 7 additions & 6 deletions sample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ android {
}

buildTypes {
release {
minifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
named("release") {
minifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
matchingFallbacks += ["debug"]
}
}
lint {
checkReleaseBuilds = false
}

buildFeatures {
compose = true
Expand Down
7 changes: 4 additions & 3 deletions sample/app/src/main/kotlin/se/premex/gross/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.premex.gross
package io.githhub.usefulness.licensee.android.app

import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -9,7 +9,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.AddCircle
import androidx.compose.material.icons.rounded.Create
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarItem
Expand All @@ -19,6 +18,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import se.premex.gross.AssetsOssView
import se.premex.gross.ProgrammaticOssView
import se.premex.gross.ui.theme.GrossTheme

enum class Views {
Expand All @@ -27,7 +28,7 @@ enum class Views {
}

class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down
5 changes: 3 additions & 2 deletions sample/app/src/main/kotlin/se/premex/gross/OssArtifactView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import io.githhub.usefulness.licensee.android.app.R
import kotlinx.serialization.ExperimentalSerializationApi
import se.premex.gross.ui.AssetLicenseParser
import se.premex.gross.ui.ErrorView
import se.premex.gross.ui.LoadingView
Expand All @@ -17,6 +18,7 @@ import se.premex.gross.ui.OssViewState
import se.premex.gross.ui.State
import java.io.IOException

@OptIn(ExperimentalSerializationApi::class)
@Composable
fun AssetsOssView() {
val assetManager = LocalContext.current.assets
Expand All @@ -27,8 +29,7 @@ fun AssetsOssView() {

LaunchedEffect(key1 = assetManager) {
try {
uiState.value =
OssViewState(viewState = State.Success(data = licenseParser.readFromAssets()))
uiState.value = OssViewState(viewState = State.Success(data = licenseParser.readFromAssets()))
} catch (ioException: IOException) {
uiState.value =
OssViewState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import io.githhub.usefulness.licensee.android.app.R
import se.premex.gross.core.Artifact
import se.premex.gross.core.Scm
import se.premex.gross.core.SpdxLicenses
Expand Down

0 comments on commit ca39410

Please sign in to comment.