Skip to content

Commit

Permalink
Replace all usages of findProperty (#652)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ansman and ZacSweers authored Aug 23, 2024
1 parent 71641da commit 45d1705
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 25 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> { findProperty("moshix.javaReleaseVersion") as? String? }
.orElse(libs.versions.jvmTarget)
Expand Down
8 changes: 4 additions & 4 deletions moshi-ir/moshi-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ tasks.withType<KotlinCompile>().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()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Properties> {
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<Properties, LocalProperties.Parameters> {
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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 26 additions & 16 deletions moshi-ir/moshi-kotlin-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KotlinCompile>().configureEach {
compilerOptions { freeCompilerArgs.addAll("-opt-in=kotlin.ExperimentalStdlibApi") }
if (name == "compileTestKotlin" && project.findProperty("kotlin.experimental.tryK2") != "true") {
finalizedBy(proguardRuleValidator)
val proguardRuleValidator =
tasks.register<ProguardRuleValidator>("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<KotlinCompile>().named { it == "compileTestKotlin" })
}
}

dependencies {
testImplementation("junit:junit:4.13.2")
Expand Down
1 change: 0 additions & 1 deletion moshi-ir/moshi-kotlin-tests/gradle.properties

This file was deleted.

0 comments on commit 45d1705

Please sign in to comment.