Skip to content

Commit

Permalink
Use kotlin apis only (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored Nov 20, 2023
1 parent 0f9f92e commit c581b5f
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import com.project.starter.quality.internal.configureAndroidCoverage
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

internal fun CommonExtension<*, *, *, *, *>.configureAndroidPlugin(rootConfig: RootConfigExtension) {
defaultConfig.apply {
Expand All @@ -39,8 +40,8 @@ internal inline fun <reified TStarter, reified TAgp> Project.configureAndroidPro
val projectLint = registerProjectLintTask()
val projectTest = registerProjectTestTask()
val projectCoverage = registerProjectCoverageTask()
tasks.withType(KotlinCompile::class.java).configureEach {
it.kotlinOptions.jvmTarget = rootConfig.javaVersion.toString()
tasks.withType(KotlinJvmCompile::class.java).configureEach {
it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(rootConfig.javaVersion.toString()))
}

withExtension<TStarter> { projectConfig ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AndroidApplicationPlugin : Plugin<Project> {

override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.application")
pluginManager.apply("kotlin-android")
pluginManager.apply("org.jetbrains.kotlin.android")
pluginManager.apply("com.starter.quality")
pluginManager.apply(ConfigurationPlugin::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AndroidLibraryPlugin : Plugin<Project> {

override fun apply(target: Project): Unit = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("kotlin-android")
pluginManager.apply("org.jetbrains.kotlin.android")
pluginManager.apply("com.starter.quality")
pluginManager.apply(ConfigurationPlugin::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class AndroidApplicationPluginTest : WithGradleProjectTest() {
resolve("src/main/kotlin/com/example/ValidKotlinFile1.kt") {
writeText(kotlinClass("ValidKotlinFile1"))
}
resolve("src/release/kotlin/ReleaseModel.kt") {
resolve("src/release/kotlin/com/example/ReleaseModel.kt") {
writeText(kotlinClass("ReleaseModel"))
}
resolve("src/test/kotlin/com/example/Test1.kt") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal class AndroidLibraryPluginTest : WithGradleProjectTest() {
resolve("src/main/kotlin/com/example/ValidKotlinFile1.kt") {
writeText(kotlinClass("ValidKotlinFile1"))
}
resolve("src/release/kotlin/ReleaseModel.kt") {
resolve("src/release/kotlin/com/example/ReleaseModel.kt") {
writeText(kotlinClass("ReleaseModel"))
}
resolve("src/test/kotlin/com/example/Test1.kt") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ internal class AndroidQualityPluginTest : WithGradleProjectTest() {
"""
import org.gradle.api.JavaVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
id('com.starter.quality')
Expand Down Expand Up @@ -110,7 +111,7 @@ internal class AndroidQualityPluginTest : WithGradleProjectTest() {
options.release.set(targetJavaVersion.majorVersion.toInteger())
}
tasks.withType(KotlinCompile).configureEach {
kotlinOptions.jvmTarget = targetJavaVersion
compilerOptions.jvmTarget = JvmTarget.@Companion.fromTarget(targetJavaVersion.toString())
}
""".trimIndent()
Expand Down
21 changes: 19 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ commonConfig {
}

allprojects {
pluginManager.withPlugin("kotlin") { plugin ->
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { plugin ->
def kotlinVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project)
configurations.matching { it.name != "detekt" }.configureEach {
resolutionStrategy.eachDependency {
Expand All @@ -24,9 +24,26 @@ allprojects {
}
}
kotlin {
jvmToolchain(21)
jvmToolchain(libs.versions.java.compilation.get().toInteger())
}
}
pluginManager.withPlugin("java-gradle-plugin") {
configurations {
register("testRuntimeDependencies") {
attributes {
// KGP publishes multiple variants https://kotlinlang.org/docs/whatsnew17.html#support-for-gradle-plugin-variants
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.class, Usage.JAVA_RUNTIME))
attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.class, Category.LIBRARY))
}
}
}

// Required to put the Kotlin plugin on the classpath for the functional test suite
tasks.withType(PluginUnderTestMetadata).configureEach {
pluginClasspath.from(configurations.testRuntimeDependencies)
}

}
tasks.withType(Test).configureEach {
doLast {
Thread.sleep(2000) // https://github.com/gradle/gradle/issues/16603
Expand Down
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
google-agp = "8.1.1"
java-compilation = "21"
google-agp = "8.1.4"
gradle-starter = "0.66.0"
gradle-gradlepublish = "1.2.1"
gradle-jacocotestkit = "1.0.12"
Expand All @@ -17,6 +18,8 @@ maven-dokka = "1.9.10"
[libraries]
agp-gradle-implementation = { module = "com.android.tools.build:gradle", version.ref = "google-agp" } # TODO remove
agp-gradle-api = { module = "com.android.tools.build:gradle-api", version.ref = "google-agp" }
jetbrains-kotlin-jvm-implementation = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "mavencentral-kotlin" }
jetbrains-kotlin-jvm-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "mavencentral-kotlin" }
publishplugin-gradle = { module = "com.gradle.publish:plugin-publish-plugin", version.ref = "gradle-gradlepublish" }
usefulness-issuechecker = { module = "com.github.usefulness:issuechecker", version.ref = "mavencentral-issuechecker" }
usefulness-ktlint = { module = "io.github.usefulness:ktlint-gradle-plugin", version.ref = "mavencentral-ktlint-gradle" }
Expand Down
5 changes: 3 additions & 2 deletions gradle/plugins/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile

plugins {
Expand All @@ -12,15 +13,15 @@ dependencies {
}

kotlin {
jvmToolchain(21)
jvmToolchain(libs.versions.java.compilation.get().toInteger())
}

def targetJavaVersion = JavaVersion.VERSION_11
tasks.withType(JavaCompile).configureEach {
options.release.set(targetJavaVersion.majorVersion.toInteger())
}
tasks.withType(KotlinCompile).configureEach {
kotlinOptions.jvmTarget = targetJavaVersion
compilerOptions.jvmTarget = JvmTarget.@Companion.fromTarget(targetJavaVersion.toString())
}

gradlePlugin {
Expand Down
3 changes: 2 additions & 1 deletion jvm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ plugins {
}

dependencies {
api 'org.jetbrains.kotlin:kotlin-gradle-plugin'
runtimeOnly(libs.jetbrains.kotlin.jvm.implementation)
api(libs.jetbrains.kotlin.jvm.api)
implementation project(":versioning")
implementation project(":quality")
implementation project(":config")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.compile.JavaCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

class KotlinLibraryPlugin : Plugin<Project> {

override fun apply(target: Project) = with(target) {
pluginManager.apply("kotlin")
pluginManager.apply("org.jetbrains.kotlin.jvm")
pluginManager.apply("com.starter.quality")
pluginManager.apply(ConfigurationPlugin::class.java)

extensions.create("projectConfig", KotlinLibraryConfigExtension::class.java)

tasks.withType(KotlinCompile::class.java).configureEach {
it.kotlinOptions.jvmTarget = rootConfig.javaVersion.toString()
tasks.withType(KotlinJvmCompile::class.java).configureEach {
it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(rootConfig.javaVersion.toString()))
}
tasks.withType(JavaCompile::class.java).configureEach {
it.options.release.set(rootConfig.javaVersion.majorVersion.toInt())
Expand Down
3 changes: 2 additions & 1 deletion multiplatform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ plugins {
}

dependencies {
api "org.jetbrains.kotlin:kotlin-gradle-plugin"
runtimeOnly(libs.jetbrains.kotlin.jvm.implementation)
api(libs.jetbrains.kotlin.jvm.api)
implementation project(":jvm")
implementation project(":quality")
implementation project(":config")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import com.project.starter.modules.tasks.ProjectCoverageTask.Companion.registerP
import com.project.starter.modules.tasks.ProjectTestTask.Companion.registerProjectTestTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

class MultiplatformLibraryPlugin : Plugin<Project> {

Expand All @@ -21,8 +22,8 @@ class MultiplatformLibraryPlugin : Plugin<Project> {
registerProjectTestTask {
it.dependsOn("allTests")
}
tasks.withType(KotlinCompile::class.java).configureEach {
it.kotlinOptions.jvmTarget = rootConfig.javaVersion.toString()
tasks.withType(KotlinJvmCompile::class.java).configureEach {
it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(rootConfig.javaVersion.toString()))
}

configureMultiplatformCoverage()
Expand Down
7 changes: 5 additions & 2 deletions quality/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ plugins {
}

dependencies {
api 'org.jetbrains.kotlin:kotlin-gradle-plugin'
compileOnly(libs.jetbrains.kotlin.jvm.api)
compileOnly(libs.agp.gradle.api)
api libs.usefulness.ktlint
api libs.detekt.gradle
implementation libs.usefulness.issuechecker
implementation project(":config")
compileOnly libs.agp.gradle.api

testImplementation project(":testing")

testRuntimeDependencies(libs.jetbrains.kotlin.jvm.implementation)
testRuntimeDependencies(libs.agp.gradle.implementation)
}

tasks.named("test") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.project.starter.quality.tasks.ProjectCodeStyleTask.Companion.addProje
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSetContainer
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer

class QualityPlugin : Plugin<Project> {

Expand Down Expand Up @@ -51,7 +51,7 @@ class QualityPlugin : Plugin<Project> {
}

private fun Project.configureFormatOnRecompile() {
pluginManager.withPlugin("kotlin") {
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
tasks.named("compileKotlin") {
if (rootConfig.quality.formatOnCompile) {
it.dependsOn("$path:formatKotlin")
Expand Down Expand Up @@ -79,8 +79,8 @@ internal inline fun Project.onAndroid(crossinline function: AndroidComponentsExt
project.extensions.findByName("androidComponents")?.let { (it as? AndroidComponentsExtension<*, *, *>)?.function() }
}

internal inline fun Project.onMultiplatform(crossinline function: KotlinMultiplatformExtension.() -> Unit) {
project.extensions.findByName("kotlin")?.let { (it as? KotlinMultiplatformExtension)?.function() }
internal inline fun Project.onMultiplatform(crossinline function: KotlinSourceSetContainer.() -> Unit) {
project.extensions.findByName("kotlin")?.let { (it as? KotlinSourceSetContainer)?.function() }
}

internal inline fun Project.onJvm(crossinline function: SourceSetContainer.() -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class QualityPluginTest : WithGradleProjectTest() {
writeText(
"""
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
id('com.starter.quality')
Expand All @@ -29,7 +30,7 @@ internal class QualityPluginTest : WithGradleProjectTest() {
options.release.set(targetJavaVersion.majorVersion.toInteger())
}
tasks.withType(KotlinCompile).configureEach {
kotlinOptions.jvmTarget = targetJavaVersion
compilerOptions.jvmTarget = JvmTarget.@Companion.fromTarget(targetJavaVersion.toString())
}
repositories.mavenCentral()
Expand Down Expand Up @@ -77,6 +78,7 @@ internal class QualityPluginTest : WithGradleProjectTest() {
"""
import org.gradle.api.JavaVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
id('com.starter.config')
Expand All @@ -99,7 +101,7 @@ internal class QualityPluginTest : WithGradleProjectTest() {
options.release.set(targetJavaVersion.majorVersion.toInteger())
}
tasks.withType(KotlinCompile).configureEach {
kotlinOptions.jvmTarget = targetJavaVersion
compilerOptions.jvmTarget = JvmTarget.@Companion.fromTarget(targetJavaVersion.toString())
}
""".trimIndent()
rootDirectory.resolve("build.gradle").writeText(buildscript)
Expand Down

0 comments on commit c581b5f

Please sign in to comment.