diff --git a/.gitignore b/.gitignore index 55ad31b0..b40c6c9b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,7 @@ *.iml # Gradle -/.gradle +.gradle build + +.kotlin diff --git a/build.gradle b/build.gradle index 9d158c1e..e69de29b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,64 +0,0 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - -plugins { - alias(libs.plugins.usefulness.ktlint.gradle) apply(false) - alias(libs.plugins.kotlin.jvm) apply(false) -} - -subprojects { - def javaTarget = JavaVersion.VERSION_11 - - pluginManager.withPlugin(libs.plugins.kotlin.jvm.get().pluginId) { - pluginManager.apply(libs.plugins.usefulness.ktlint.gradle.get().pluginId) - - kotlin { - jvmToolchain(21) - } - tasks.withType(KotlinCompile).configureEach { - compilerOptions { - jvmTarget = JvmTarget.fromTarget(javaTarget.majorVersion) - freeCompilerArgs = [ - "-progressive", - '-opt-in=kotlin.contracts.ExperimentalContracts', - ] - } - } - } - - pluginManager.withPlugin("java") { - tasks.withType(JavaCompile).configureEach { - options.release.set(javaTarget.majorVersion.toInteger()) - } - tasks.named("processResources", ProcessResources) { - from(rootProject.file("LICENSE")) - } - } - - pluginManager.withPlugin("io.github.usefulness.ktlint-gradle-plugin") { - ktlint { - ktlintVersion = libs.versions.ktlint.asProvider().get() - } - } - - tasks.withType(Test).configureEach { - useJUnitPlatform() - } - - configurations.configureEach { - resolutionStrategy.eachDependency { - if (requested.group == "com.android.tools.build" && requested.name == "aapt2-proto") { - useVersion(libs.versions.aapt2Proto.get()) - because("we need to keep dependencies in sync with bundletool") - } - if (requested.group == "com.google.protobuf" && requested.name == "protobuf-java") { - useVersion(libs.versions.protobufJava.get()) - because("we need to keep dependencies in sync with bundletool") - } - if (requested.group == "com.google.guava" && requested.name == "guava") { - useVersion(libs.versions.guava.get()) - because("we need to keep dependencies in sync with bundletool") - } - } - } -} diff --git a/diffuse/build.gradle b/diffuse/build.gradle index 318d6763..0a84250d 100644 --- a/diffuse/build.gradle +++ b/diffuse/build.gradle @@ -1,9 +1,9 @@ plugins { - alias(libs.plugins.kotlin.jvm) + id("setup-compilation") } configurations { - r8 + register("r8") } dependencies { diff --git a/formats/build.gradle b/formats/build.gradle index 69429e0f..c497def2 100644 --- a/formats/build.gradle +++ b/formats/build.gradle @@ -1,5 +1,5 @@ plugins { - alias(libs.plugins.kotlin.jvm) + id("setup-compilation") } dependencies { diff --git a/gradle.properties b/gradle.properties index ce592da7..877fa8b3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,3 +16,4 @@ POM_DEVELOPER_NAME=Jake Wharton org.gradle.caching=true org.gradle.parallel=true +org.gradle.unsafe.isolated-projects=true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 62163709..a1e29fb0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,4 +1,6 @@ [versions] +java-compilation = "21" +java-target = "11" bundletool = "1.17.1" # Keep this values in sync with bundletool"s dependencies. aapt2Proto = "7.3.0-alpha07-8248216" @@ -40,7 +42,8 @@ diffUtils = { module = "io.github.java-diff-utils:java-diff-utils", version.ref picnic = { module = "com.jakewharton.picnic:picnic", version.ref = "picnic" } r8 = { module = "com.android.tools:r8", version.ref = "r8" } ktlint-cli = { module = "com.pinterest.ktlint:ktlint-cli", version.ref = "ktlint" } +ktlint-gradle = { module = "io.github.usefulness:ktlint-gradle-plugin", version.ref = "ktlint-gradle-plugin" } [plugins] -usefulness-ktlint-gradle = { id = "io.github.usefulness.ktlint-gradle-plugin", version.ref = "ktlint-gradle-plugin" } kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlin-sam-with-receiver = { id = "org.jetbrains.kotlin.plugin.sam.with.receiver", version.ref = "kotlin" } diff --git a/gradle/plugins/build.gradle b/gradle/plugins/build.gradle new file mode 100644 index 00000000..d492c7e1 --- /dev/null +++ b/gradle/plugins/build.gradle @@ -0,0 +1,31 @@ +import org.gradle.kotlin.dsl.ProjectExtensionsKt + +plugins { + id 'java-gradle-plugin' + alias(libs.plugins.kotlin.jvm) + alias(libs.plugins.kotlin.sam.with.receiver) +} + +samWithReceiver { + annotation("org.gradle.api.HasImplicitReceiver") +} + +kotlin { + jvmToolchain(libs.versions.java.compilation.get().toInteger()) +} + +dependencies { + implementation(ProjectExtensionsKt.gradleKotlinDsl(project)) + implementation("org.jetbrains.kotlin:kotlin-gradle-plugin") + implementation(libs.ktlint.gradle) + implementation(gradleApi()) +} + +gradlePlugin { + plugins { + publishingPlugin { + id = 'setup-compilation' + implementationClass = 'SetupCompilationPlugin' + } + } +} diff --git a/gradle/plugins/settings.gradle b/gradle/plugins/settings.gradle new file mode 100644 index 00000000..d0128810 --- /dev/null +++ b/gradle/plugins/settings.gradle @@ -0,0 +1,14 @@ +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + gradlePluginPortal() + google() + } + versionCatalogs { + create("libs") { + from(files("../libs.versions.toml")) + } + } +} + +rootProject.name = "plugins" diff --git a/gradle/plugins/src/main/kotlin/SetupCompilationPlugin.kt b/gradle/plugins/src/main/kotlin/SetupCompilationPlugin.kt new file mode 100644 index 00000000..cbf1295c --- /dev/null +++ b/gradle/plugins/src/main/kotlin/SetupCompilationPlugin.kt @@ -0,0 +1,76 @@ +import io.github.usefulness.KtlintGradleExtension +import io.github.usefulness.KtlintGradlePlugin +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.VersionCatalogsExtension +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.api.tasks.testing.Test +import org.gradle.kotlin.dsl.named +import org.gradle.kotlin.dsl.withType +import org.gradle.language.jvm.tasks.ProcessResources +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.dsl.kotlinExtension +import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile + +class SetupCompilationPlugin : Plugin { + + override fun apply(project: Project) = with(project) { + pluginManager.apply("org.jetbrains.kotlin.jvm") + + val javaTarget = getVersionCatalogVersion("java-target").toInt() + + + kotlinExtension.apply { + jvmToolchain(21) + } + + tasks.withType().configureEach { + compilerOptions { + jvmTarget.set(JvmTarget.fromTarget(javaTarget.toString())) + freeCompilerArgs.add("-opt-in=kotlin.contracts.ExperimentalContracts") + } + } + + pluginManager.withPlugin("java") { + tasks.withType().configureEach { + options.release.set(javaTarget) + } + tasks.named("processResources") { + from(isolated.rootProject.projectDirectory.file("LICENSE")) + } + } + + + pluginManager.apply(KtlintGradlePlugin::class.java) + extensions.configure("ktlint") { + ktlintVersion.set(getVersionCatalogVersion("ktlint")) + } + + tasks.withType().configureEach { + useJUnitPlatform() + } + + configurations.configureEach { + resolutionStrategy.eachDependency { + if (requested.group == "com.android.tools.build" && requested.name == "aapt2-proto") { + useVersion(getVersionCatalogVersion("aapt2Proto").toString()) + because("we need to keep dependencies in sync with bundletool") + } + if (requested.group == "com.google.protobuf" && requested.name == "protobuf-java") { + useVersion(getVersionCatalogVersion("protobufJava").toString()) + because("we need to keep dependencies in sync with bundletool") + } + if (requested.group == "com.google.guava" && requested.name == "guava") { + useVersion(getVersionCatalogVersion("guava").toString()) + because("we need to keep dependencies in sync with bundletool") + } + } + } + } +} + +internal val Project.libs + get() = extensions.getByType(VersionCatalogsExtension::class.java).named("libs") + +internal fun Project.getVersionCatalogVersion(name: String) = + libs.findVersion(name).get().requiredVersion diff --git a/io/build.gradle b/io/build.gradle index a9aa8269..b1fedce2 100644 --- a/io/build.gradle +++ b/io/build.gradle @@ -1,5 +1,5 @@ plugins { - alias(libs.plugins.kotlin.jvm) + id("setup-compilation") } dependencies { diff --git a/reports/build.gradle b/reports/build.gradle index 6dbc68a6..4ef0e06a 100644 --- a/reports/build.gradle +++ b/reports/build.gradle @@ -1,5 +1,5 @@ plugins { - alias(libs.plugins.kotlin.jvm) + id("setup-compilation") } dependencies { diff --git a/settings.gradle b/settings.gradle index e087bf0b..39f4d963 100644 --- a/settings.gradle +++ b/settings.gradle @@ -27,3 +27,5 @@ include ':diffuse' include ':formats' include ':io' include ':reports' + +includeBuild("gradle/plugins")