Skip to content

Commit

Permalink
Merge pull request #361 from usefulness/updates
Browse files Browse the repository at this point in the history
Enable Gradle Isolated Projects
  • Loading branch information
mateuszkwiecinski authored Aug 31, 2024
2 parents 006a141 + d199b94 commit d67bb82
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 71 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
*.iml

# Gradle
/.gradle
.gradle
build

.kotlin
64 changes: 0 additions & 64 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
}
4 changes: 2 additions & 2 deletions diffuse/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id("setup-compilation")
}

configurations {
r8
register("r8")
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion formats/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id("setup-compilation")
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ POM_DEVELOPER_NAME=Jake Wharton

org.gradle.caching=true
org.gradle.parallel=true
org.gradle.unsafe.isolated-projects=true
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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" }
31 changes: 31 additions & 0 deletions gradle/plugins/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
}
}
14 changes: 14 additions & 0 deletions gradle/plugins/settings.gradle
Original file line number Diff line number Diff line change
@@ -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"
76 changes: 76 additions & 0 deletions gradle/plugins/src/main/kotlin/SetupCompilationPlugin.kt
Original file line number Diff line number Diff line change
@@ -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<Project> {

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<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.fromTarget(javaTarget.toString()))
freeCompilerArgs.add("-opt-in=kotlin.contracts.ExperimentalContracts")
}
}

pluginManager.withPlugin("java") {
tasks.withType<JavaCompile>().configureEach {
options.release.set(javaTarget)
}
tasks.named<ProcessResources>("processResources") {
from(isolated.rootProject.projectDirectory.file("LICENSE"))
}
}


pluginManager.apply(KtlintGradlePlugin::class.java)
extensions.configure<KtlintGradleExtension>("ktlint") {
ktlintVersion.set(getVersionCatalogVersion("ktlint"))
}

tasks.withType<Test>().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
2 changes: 1 addition & 1 deletion io/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id("setup-compilation")
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion reports/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id("setup-compilation")
}

dependencies {
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ include ':diffuse'
include ':formats'
include ':io'
include ':reports'

includeBuild("gradle/plugins")

0 comments on commit d67bb82

Please sign in to comment.