From 45d170560662902d19e355296d3a3bfb68e26bca Mon Sep 17 00:00:00 2001 From: Nicklas Ansman Date: Thu, 22 Aug 2024 22:16:05 -0400 Subject: [PATCH] Replace all usages of findProperty (#652) * Replace all usages of findProperty It is not compatible with project isolation, `providers.gradleProperty` is however. This PR also change all build script related usages, though this isn't needed for consumers of course. * Spotless * Build's working again * Add new API * Make proguard rule validator modern * Support local properties --------- Co-authored-by: Zac Sweers --- .github/workflows/ci.yml | 5 +-- build.gradle.kts | 1 + moshi-ir/moshi-gradle-plugin/build.gradle.kts | 8 ++-- .../moshix/ir/gradle/LocalProperties.kt | 33 +++++++++++++++ .../moshix/ir/gradle/MoshiGradleSubplugin.kt | 14 ++++++- moshi-ir/moshi-kotlin-tests/build.gradle.kts | 42 ++++++++++++------- moshi-ir/moshi-kotlin-tests/gradle.properties | 1 - 7 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/LocalProperties.kt delete mode 100644 moshi-ir/moshi-kotlin-tests/gradle.properties diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1347dc42..f880d235 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,12 +17,11 @@ concurrency: jobs: build: - name: 'KSP ${{ matrix.ksp_enabled }} / K2 ${{ matrix.k2_enabled }}' + name: 'KSP ${{ matrix.ksp_enabled }} runs-on: ubuntu-latest strategy: matrix: ksp_enabled: [ true, false ] - k2_enabled: [ true, false ] fail-fast: false steps: @@ -39,7 +38,7 @@ jobs: uses: gradle/actions/setup-gradle@v4 - name: Build project - run: ./gradlew build check -Pmoshix.useKsp=${{ matrix.ksp_enabled }} -Pkotlin.experimental.tryK2=${{ matrix.k2_enabled }} --quiet + run: ./gradlew build check -Pmoshix.useKsp=${{ matrix.ksp_enabled }} --quiet publish-snapshot: needs: 'build' diff --git a/build.gradle.kts b/build.gradle.kts index 7d5e1457..c0236376 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -76,6 +76,7 @@ spotless { subprojects { pluginManager.withPlugin("java") { // javaReleaseVersion can be set to override the global version + // Can't use providers.gradleProperty() because it doesn't work on subprojects val jvmTargetProvider = provider { findProperty("moshix.javaReleaseVersion") as? String? } .orElse(libs.versions.jvmTarget) diff --git a/moshi-ir/moshi-gradle-plugin/build.gradle.kts b/moshi-ir/moshi-gradle-plugin/build.gradle.kts index e70a66f4..9db8399b 100644 --- a/moshi-ir/moshi-gradle-plugin/build.gradle.kts +++ b/moshi-ir/moshi-gradle-plugin/build.gradle.kts @@ -67,15 +67,15 @@ tasks.withType().configureEach { tasks.matching { it.name == "sourcesJar" }.configureEach { dependsOn(copyVersionTemplatesProvider) } gradlePlugin { - website = project.findProperty("POM_URL") as String - vcsUrl = project.findProperty("POM_SCM_URL") as String + website = providers.gradleProperty("POM_URL").get() + vcsUrl = providers.gradleProperty("POM_SCM_URL").get() plugins { register("moshiPlugin") { id = "dev.zacsweers.moshix" - displayName = project.findProperty("POM_NAME") as String + displayName = providers.gradleProperty("POM_NAME").get() implementationClass = "dev.zacsweers.moshix.ir.gradle.MoshiGradleSubplugin" - description = project.findProperty("POM_DESCRIPTION") as String + description = providers.gradleProperty("POM_DESCRIPTION").get() } } } diff --git a/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/LocalProperties.kt b/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/LocalProperties.kt new file mode 100644 index 00000000..2755aefd --- /dev/null +++ b/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/LocalProperties.kt @@ -0,0 +1,33 @@ +package dev.zacsweers.moshix.ir.gradle + +import java.util.Properties +import org.gradle.api.Project +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Provider +import org.gradle.api.provider.ValueSource +import org.gradle.api.provider.ValueSourceParameters + +internal fun Project.createPropertiesProvider(filePath: String): Provider { + return project.providers.of(LocalProperties::class.java) { + it.parameters.propertiesFile.set(project.layout.projectDirectory.file(filePath)) + } +} + +/** Implementation of provider holding a local properties file's parsed [Properties]. */ +internal abstract class LocalProperties : ValueSource { + interface Parameters : ValueSourceParameters { + val propertiesFile: RegularFileProperty + } + + override fun obtain(): Properties? { + val provider = parameters.propertiesFile + if (!provider.isPresent) { + return null + } + val propertiesFile = provider.asFile.get() + if (!propertiesFile.exists()) { + return null + } + return Properties().apply { propertiesFile.inputStream().use(::load) } + } +} diff --git a/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/MoshiGradleSubplugin.kt b/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/MoshiGradleSubplugin.kt index 2bcae837..380f5dd9 100644 --- a/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/MoshiGradleSubplugin.kt +++ b/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/MoshiGradleSubplugin.kt @@ -22,11 +22,23 @@ class MoshiGradleSubplugin : KotlinCompilerPluginSupportPlugin { private companion object { val SUPPORTED_PLATFORMS = setOf(KotlinPlatformType.androidJvm, KotlinPlatformType.jvm) + const val GENERATE_PROGUARD_RULES_KEY = "moshix.generateProguardRules" } override fun apply(target: Project) { target.extensions.create("moshi", MoshiPluginExtension::class.java) - if (target.findProperty("moshix.generateProguardRules")?.toString()?.toBoolean() != false) { + + val localGradlePropertyProvider = + target.createPropertiesProvider("gradle.properties").map { + it.getProperty(GENERATE_PROGUARD_RULES_KEY) + } + + if ( + localGradlePropertyProvider + .orElse(target.providers.gradleProperty(GENERATE_PROGUARD_RULES_KEY)) + .orNull + ?.toBoolean() != false + ) { try { target.pluginManager.apply("com.google.devtools.ksp") } catch (e: Exception) { diff --git a/moshi-ir/moshi-kotlin-tests/build.gradle.kts b/moshi-ir/moshi-kotlin-tests/build.gradle.kts index c48ff568..95aadda0 100644 --- a/moshi-ir/moshi-kotlin-tests/build.gradle.kts +++ b/moshi-ir/moshi-kotlin-tests/build.gradle.kts @@ -22,28 +22,38 @@ plugins { alias(libs.plugins.ksp) } +kotlin.compilerOptions.optIn.add("kotlin.ExperimentalStdlibApi") + moshi { enableSealed.set(true) } -val proguardRuleValidator = - tasks.register("validateProguardRules") { - doNotTrackState("This is a validation task that should always run") - notCompatibleWithConfigurationCache("This task always runs") - doLast { - logger.lifecycle("Validating proguard rules") - val proguardRulesDir = project.file("build/generated/ksp/test/resources/META-INF/proguard") - check(proguardRulesDir.exists() && proguardRulesDir.listFiles()!!.isNotEmpty()) { - "No proguard rules found! Did you forget to apply the KSP Gradle plugin?" - } - logger.lifecycle("Proguard rules properly generated ✅ ") +@CacheableTask +abstract class ProguardRuleValidator : DefaultTask() { + @get:InputFiles + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val proguardRulesDir: DirectoryProperty + + @get:OutputFile abstract val output: RegularFileProperty + + @TaskAction + fun validate() { + logger.lifecycle("Validating proguard rules") + val proguardRulesDir = this@ProguardRuleValidator.proguardRulesDir.asFile.get() + check(proguardRulesDir.exists() && proguardRulesDir.walkTopDown().any()) { + "No proguard rules found! Did you forget to apply the KSP Gradle plugin?" } + logger.lifecycle("Proguard rules properly generated ✅ ") + output.get().asFile.writeText("validated") } +} -tasks.withType().configureEach { - compilerOptions { freeCompilerArgs.addAll("-opt-in=kotlin.ExperimentalStdlibApi") } - if (name == "compileTestKotlin" && project.findProperty("kotlin.experimental.tryK2") != "true") { - finalizedBy(proguardRuleValidator) +val proguardRuleValidator = + tasks.register("validateProguardRules") { + proguardRulesDir.set( + project.layout.buildDirectory.dir("generated/ksp/test/resources/META-INF/proguard") + ) + output.set(project.layout.buildDirectory.file("moshix/validation/validated.txt")) + dependsOn(tasks.withType().named { it == "compileTestKotlin" }) } -} dependencies { testImplementation("junit:junit:4.13.2") diff --git a/moshi-ir/moshi-kotlin-tests/gradle.properties b/moshi-ir/moshi-kotlin-tests/gradle.properties deleted file mode 100644 index c90f60c8..00000000 --- a/moshi-ir/moshi-kotlin-tests/gradle.properties +++ /dev/null @@ -1 +0,0 @@ -moshix.generateProguardRules=true \ No newline at end of file