From b53b75af77ce23f56d78fd16df93611901283e19 Mon Sep 17 00:00:00 2001 From: Gabriel Ittner Date: Mon, 27 May 2024 08:42:17 +0200 Subject: [PATCH] update config --- .github/renovate.json5 | 16 +--- .gitignore | 1 + gradle.properties | 3 - gradle/libs.versions.toml | 7 +- kotlinw | 60 +++++++++----- .../gradle/plugin/SettingsExtension.kt | 1 + .../gradle/plugin/SettingsPlugin.kt | 8 -- plugins/plugins.gradle.kts | 4 +- .../gradle/plugin/FreeleticsBaseExtension.kt | 1 + .../gradle/plugin/FreeleticsBasePlugin.kt | 5 +- .../plugin/FreeleticsGradlePluginPlugin.kt | 1 + .../plugin/FreeleticsMultiplatformPlugin.kt | 1 + .../freeletics/gradle/setup/ComposeSetup.kt | 81 +------------------ .../com/freeletics/gradle/util/Extensions.kt | 23 +++++- .../com/freeletics/gradle/util/KgpBridge.kt | 65 --------------- 15 files changed, 78 insertions(+), 199 deletions(-) delete mode 100644 plugins/src/main/kotlin/com/freeletics/gradle/util/KgpBridge.kt diff --git a/.github/renovate.json5 b/.github/renovate.json5 index 7a12b1d5..bc5c9fa5 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -62,26 +62,16 @@ matchPackagePatterns: [ "org.jetbrains.kotlin", "com.google.devtools.ksp", - "dev.zacsweers.kctfork", - "androidx.compose.compiler", - "org.jetbrains.compose.compiler", ], excludePackagePatterns: [ "org.jetbrains.kotlinx", ], - groupName: "Kotlin, KSP and Compose Compiler" + groupName: "Kotlin and KSP" }, { matchPackagePatterns: [ - "androidx.compose.runtime", - "androidx.compose.ui", - "androidx.compose.foundation", - "androidx.compose.material", - "org.jetbrains.compose$", - "org.jetbrains.compose.runtime", - "org.jetbrains.compose.ui", - "org.jetbrains.compose.foundation", - "org.jetbrains.compose.material", + "androidx.compose", + "org.jetbrains.compose", ], groupName: "Compose" } diff --git a/.gitignore b/.gitignore index b3ae2034..620c0028 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ local.properties .DS_Store build/ kotlin-js-store/ +.kotlin/ docs/index.md docs/changelog.md diff --git a/gradle.properties b/gradle.properties index 8b190d0e..191759ae 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,6 @@ org.gradle.jvmargs=-Xms1g -Xmx2g -XX:MaxMetaspaceSize=1g org.gradle.parallel=true -# Do not apply dependency analysis plugin automatically to sub-projects -systemProp.dependency.analysis.autoapply=false -# Print buildHealth result dependency.analysis.print.build.health=true GROUP=com.freeletics.gradle diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a7d8a49c..7e37b874 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] java-target = "17" -java-toolchain = "21" +java-toolchain = "22" fgp = "0.12.2" @@ -8,7 +8,6 @@ kotlin = "2.0.0" coroutines = "1.8.1" android-gradle = "8.4.0" android-gradle-common = "31.3.2" -jb-compose="1.6.2" ksp="2.0.0-1.0.21" anvil = "2.5.0-beta09" moshix = "0.26.0" @@ -32,11 +31,11 @@ kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = " coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } kotlin-gradle-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "kotlin" } +kotlin-gradle-annotations = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-annotations", version.ref = "kotlin" } kotlin-native-utils = { module = "org.jetbrains.kotlin:kotlin-native-utils", version.ref = "kotlin" } android-gradle = { module = "com.android.tools.build:gradle-api", version.ref = "android-gradle" } -compose-gradle = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "jb-compose" } -ksp = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" } +ksp-gradle = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" } anvil-gradle = { module = "com.squareup.anvil:gradle-plugin", version.ref = "anvil" } moshix-gradle = { module = "dev.zacsweers.moshix:moshi-gradle-plugin", version.ref = "moshix" } paparazzi-gradle = { module = "com.freeletics.fork.paparazzi:paparazzi-gradle-plugin", version.ref = "paparazzi" } diff --git a/kotlinw b/kotlinw index eaf65f30..1b9cc8c7 100755 --- a/kotlinw +++ b/kotlinw @@ -1,32 +1,43 @@ -#!/usr/bin/env bash +#!/bin/sh set -e -ROOT_DIR="$(dirname $0)" +ROOT_DIR="$(dirname "$0")" -JDK_VERSION="$(grep -s 'java-gradle' $ROOT_DIR/gradle/libs.versions.toml | cut -d'"' -f2)" +JDK_VERSION="$(grep -s 'java-gradle' "$ROOT_DIR/gradle/libs.versions.toml" | cut -d'"' -f2)" if [ -z "$JDK_VERSION" ]; then - JDK_VERSION="21" + JDK_VERSION="$(grep -s 'java-target' "$ROOT_DIR/gradle/libs.versions.toml" | cut -d'"' -f2)" +fi +if [ -z "$JDK_VERSION" ]; then + JDK_VERSION="22" fi -KOTLIN_VERSION="1.9.0" +KOTLIN_VERSION="$(grep -s -m 1 '^kotlin \?=' "$ROOT_DIR/gradle/libs.versions.toml" | cut -d'"' -f2)" if [ -f "$ROOT_DIR/gradle/libs.versions.toml" ]; then - KOTLIN_VERSION="$(grep -m 1 '^kotlin \?=' $ROOT_DIR/gradle/libs.versions.toml | cut -d'"' -f2)" + KOTLIN_VERSION="2.0.0" fi -INSTALLATION_DIR="${HOME}/.kotlinw/${KOTLIN_VERSION}" -BINARY_DIR="${INSTALLATION_DIR}/kotlinc/bin" +# OS specific support (must be 'true' or 'false'). +case "$(uname -s)" in + Linux*) MACHINE=Linux;; + Darwin*) MACHINE=Mac;; + *) MACHINE="UNSUPPORTED" +esac -if [[ "$OSTYPE" == "linux-gnu"* ]]; then - if [ -d "/usr/lib/jvm/zulu-$JDK_VERSION-amd64" ]; then - JAVA_HOME="/usr/lib/jvm/zulu-$JDK_VERSION-amd64/" - elif [ -d "/usr/lib/jvm/zulu$JDK_VERSION-ca-amd64" ]; then - JAVA_HOME="/usr/lib/jvm/zulu$JDK_VERSION-ca-amd64/" - else - java --version +if [ "$MACHINE" = "Linux" ]; then + export JAVA_HOME="/usr/lib/jvm/zulu-$JDK_VERSION-amd64/" + if [ ! -d "$JAVA_HOME" ]; then + export JAVA_HOME="/usr/lib/jvm/zulu$JDK_VERSION-ca-amd64/" + if [ ! -d "$JAVA_HOME" ]; then + echo "Installing JDK ${JDK_VERSION} (you may be prompted for your password)..." + curl -s https://repos.azul.com/azul-repo.key | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/azul.gpg + echo "deb [signed-by=/usr/share/keyrings/azul.gpg] https://repos.azul.com/zulu/deb stable main" | sudo tee /etc/apt/sources.list.d/zulu.list + sudo apt update + sudo apt install -y "zulu$JDK_VERSION-jdk" + fi fi -elif [[ "$OSTYPE" == "darwin"* ]]; then - JAVA_HOME="/Library/Java/JavaVirtualMachines/zulu-$JDK_VERSION.jdk/Contents/Home" - if [ ! -d "/Library/Java/JavaVirtualMachines/zulu-${JDK_VERSION}.jdk/Contents/Home" ]; then +elif [ "$MACHINE" = "Mac" ]; then + export JAVA_HOME="/Library/Java/JavaVirtualMachines/zulu-$JDK_VERSION.jdk/Contents/Home" + if [ ! -d "$JAVA_HOME" ]; then echo "Installing JDK ${JDK_VERSION} (you may be prompted for your password)..." brew tap mdogan/zulu brew install --cask "zulu-jdk${JDK_VERSION}" @@ -36,9 +47,12 @@ else exit 1 fi +INSTALLATION_DIR="${HOME}/.kotlinw/${KOTLIN_VERSION}" +BINARY_DIR="${INSTALLATION_DIR}/kotlinc/bin" + if [ ! -f "${BINARY_DIR}/kotlin" ]; then echo "Downloading Kotlin ${KOTLIN_VERSION}" - mkdir -p ${INSTALLATION_DIR} + mkdir -p "${INSTALLATION_DIR}" temp_file=$(mktemp /tmp/kotlin.zip.XXXXXX) curl -sLo "${temp_file}" "https://github.com/JetBrains/kotlin/releases/download/v${KOTLIN_VERSION}/kotlin-compiler-${KOTLIN_VERSION}.zip" unzip -q "${temp_file}" -d "${INSTALLATION_DIR}" @@ -48,4 +62,10 @@ fi # this works around an issue where the Kotlin compiler used by ktlint accesses code that JDK 12+ don't allow access to export JAVA_OPTS="--add-opens java.base/java.lang=ALL-UNNAMED" -"${BINARY_DIR}/kotlin" "$@" +SCRIPT_FILE="$1" +# will remove the first element of the $@ arguments array since we read it already above +shift +# uses kotlinc instead of kotlin because the latter doesn't allow specifying a jvm target and defaults to Java 8 +# the -- between SCRIPT_FILE and the other arguments is there so that the arguments are treated as arguments to the +# script and not to kotlinc +"${BINARY_DIR}/kotlinc" "-jvm-target" "$JDK_VERSION" "-script" "$SCRIPT_FILE" "--" "${@}" diff --git a/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsExtension.kt b/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsExtension.kt index f5c31de6..74793e52 100644 --- a/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsExtension.kt +++ b/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsExtension.kt @@ -86,6 +86,7 @@ public abstract class SettingsExtension(private val settings: Settings) { @JvmOverloads public fun snapshots(androidXBuildId: String? = null) { settings.dependencyResolutionManagement { management -> + @Suppress("UnstableApiUsage") management.repositories { handler -> handler.maven { it.setUrl("https://oss.sonatype.org/content/repositories/snapshots/") diff --git a/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsPlugin.kt b/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsPlugin.kt index 90bde259..cdcad88b 100644 --- a/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsPlugin.kt +++ b/plugin-settings/src/main/kotlin/com/freeletics/gradle/plugin/SettingsPlugin.kt @@ -5,7 +5,6 @@ import com.gradle.develocity.agent.gradle.DevelocityPlugin import org.gradle.api.Plugin import org.gradle.api.credentials.PasswordCredentials import org.gradle.api.initialization.Settings -import org.gradle.api.initialization.resolve.RepositoriesMode import org.gradle.caching.http.HttpBuildCache import org.gradle.kotlin.dsl.jvm import org.gradle.toolchains.foojay.FoojayToolchainResolver @@ -103,13 +102,6 @@ public abstract class SettingsPlugin : Plugin { } } - // TODO https://youtrack.jetbrains.com/issue/KT-51379 - val mpp = target.booleanProperty("fgp.kotlin.multiplatformProject", false) - if (!mpp) { - @Suppress("UnstableApiUsage") - management.repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) - } - val prefix = "fgp.version.override." management.versionCatalogs { val libs = it.maybeCreate("libs") diff --git a/plugins/plugins.gradle.kts b/plugins/plugins.gradle.kts index 23b5fac7..d4699f0c 100644 --- a/plugins/plugins.gradle.kts +++ b/plugins/plugins.gradle.kts @@ -7,9 +7,9 @@ dependencies { compileOnly(libs.android.gradle) compileOnly(libs.kotlin.gradle) compileOnly(libs.kotlin.gradle.api) + compileOnly(libs.kotlin.gradle.annotations) compileOnly(libs.kotlin.native.utils) - compileOnly(libs.compose.gradle) - compileOnly(libs.ksp) + compileOnly(libs.ksp.gradle) compileOnly(libs.anvil.gradle) compileOnly(libs.moshix.gradle) compileOnly(libs.paparazzi.gradle) diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBaseExtension.kt b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBaseExtension.kt index 4f4b5837..7d95b111 100644 --- a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBaseExtension.kt +++ b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBaseExtension.kt @@ -3,6 +3,7 @@ package com.freeletics.gradle.plugin import com.freeletics.gradle.setup.configureDagger import com.freeletics.gradle.setup.configureMoshi import com.freeletics.gradle.setup.setupCompose +import com.freeletics.gradle.util.compilerOptions import com.freeletics.gradle.util.getDependency import com.freeletics.gradle.util.kotlin import org.gradle.api.Project diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBasePlugin.kt b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBasePlugin.kt index c0b0c882..233579f6 100644 --- a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBasePlugin.kt +++ b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsBasePlugin.kt @@ -1,8 +1,8 @@ package com.freeletics.gradle.plugin -import com.freeletics.gradle.util.KotlinAndroidProjectExtensionDelegate import com.freeletics.gradle.util.addMaybe import com.freeletics.gradle.util.booleanProperty +import com.freeletics.gradle.util.compilerOptions import com.freeletics.gradle.util.getBundleOrNull import com.freeletics.gradle.util.getVersionOrNull import com.freeletics.gradle.util.java @@ -14,6 +14,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.jvm.tasks.Jar import org.gradle.jvm.toolchain.JvmVendorSpec +import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions import org.jetbrains.kotlin.gradle.dsl.KotlinVersion @@ -66,7 +67,7 @@ public abstract class FreeleticsBasePlugin : Plugin { toolchain.vendor.set(JvmVendorSpec.AZUL) } - val isAndroid = this is KotlinAndroidProjectExtensionDelegate + val isAndroid = this is KotlinAndroidProjectExtension compilerOptions { val version = getVersionOrNull("kotlin-language") diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsGradlePluginPlugin.kt b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsGradlePluginPlugin.kt index 33fc5d9d..bea310cf 100644 --- a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsGradlePluginPlugin.kt +++ b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsGradlePluginPlugin.kt @@ -1,5 +1,6 @@ package com.freeletics.gradle.plugin +import com.freeletics.gradle.util.compilerOptions import com.freeletics.gradle.util.kotlin import org.gradle.api.Plugin import org.gradle.api.Project diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsMultiplatformPlugin.kt b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsMultiplatformPlugin.kt index 0264558d..d69545ed 100644 --- a/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsMultiplatformPlugin.kt +++ b/plugins/src/main/kotlin/com/freeletics/gradle/plugin/FreeleticsMultiplatformPlugin.kt @@ -1,6 +1,7 @@ package com.freeletics.gradle.plugin import com.freeletics.gradle.setup.defaultTestSetup +import com.freeletics.gradle.util.compilerOptions import com.freeletics.gradle.util.freeleticsExtension import com.freeletics.gradle.util.kotlin import com.freeletics.gradle.util.kotlinMultiplatform diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/setup/ComposeSetup.kt b/plugins/src/main/kotlin/com/freeletics/gradle/setup/ComposeSetup.kt index 0564dfec..7e050d9b 100644 --- a/plugins/src/main/kotlin/com/freeletics/gradle/setup/ComposeSetup.kt +++ b/plugins/src/main/kotlin/com/freeletics/gradle/setup/ComposeSetup.kt @@ -1,25 +1,12 @@ package com.freeletics.gradle.setup -import com.freeletics.gradle.util.android import com.freeletics.gradle.util.booleanProperty -import com.freeletics.gradle.util.getDependency -import com.freeletics.gradle.util.getVersion +import com.freeletics.gradle.util.compilerOptions import com.freeletics.gradle.util.kotlin -import com.freeletics.gradle.util.stringProperty import org.gradle.api.Project -import org.jetbrains.compose.ComposeExtension -import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin internal fun Project.setupCompose() { - if (kotlinGradlePluginVersion().startsWith("2.")) { - setupComposeKotlin2() - } else if ((plugins.hasPlugin("com.android.library") || plugins.hasPlugin("com.android.application")) && - !plugins.hasPlugin("org.jetbrains.kotlin.multiplatform") - ) { - setupComposeAndroid() - } else { - setupComposeJetbrains() - } + plugins.apply("org.jetbrains.kotlin.plugin.compose") val enableMetrics = project.booleanProperty("fgp.compose.enableCompilerMetrics", false) if (enableMetrics.get()) { @@ -55,67 +42,3 @@ internal fun Project.setupCompose() { } } } - -private fun Project.kotlinGradlePluginVersion(): String { - val id = listOf( - "org.jetbrains.kotlin.jvm", - "org.jetbrains.kotlin.android", - "org.jetbrains.kotlin.multiplatform", - ).firstOrNull { plugins.hasPlugin(it) } ?: throw IllegalStateException("No Kotlin plugin found") - return (plugins.getPlugin(id) as KotlinBasePlugin).pluginVersion -} - -private fun Project.setupComposeKotlin2() { - plugins.apply("org.jetbrains.kotlin.plugin.compose") - - // TODO remove after RC1 - if (kotlinGradlePluginVersion() == "2.0.0-RC1") { - kotlin { - compilerOptions { - freeCompilerArgs.addAll( - "-P", - "plugin:androidx.compose.compiler.plugins.kotlin:" + - "suppressKotlinVersionCompatibilityCheck=2.0.0-RC1", - ) - } - } - } -} - -private fun Project.setupComposeAndroid() { - android { - buildFeatures.compose = true - - composeOptions { - kotlinCompilerExtensionVersion = project.getVersion("androidx.compose.compiler") - } - } - - suppressVersionInconsistency() -} - -private fun Project.setupComposeJetbrains() { - plugins.apply("org.jetbrains.compose") - - extensions.configure(ComposeExtension::class.java) { - val composeCompiler = getDependency("jetbrains-compose-compiler").get() - it.kotlinCompilerPlugin.set("${composeCompiler.group}:${composeCompiler.name}:${composeCompiler.version}") - } - - suppressVersionInconsistency() -} - -private fun Project.suppressVersionInconsistency() { - val suppressComposeCompilerCheck = stringProperty("fgp.compose.kotlinVersion").orNull - if (suppressComposeCompilerCheck != null) { - kotlin { - compilerOptions { - freeCompilerArgs.addAll( - "-P", - "plugin:androidx.compose.compiler.plugins.kotlin:" + - "suppressKotlinVersionCompatibilityCheck=$suppressComposeCompilerCheck", - ) - } - } - } -} diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/util/Extensions.kt b/plugins/src/main/kotlin/com/freeletics/gradle/util/Extensions.kt index c385920f..d8c016d4 100644 --- a/plugins/src/main/kotlin/com/freeletics/gradle/util/Extensions.kt +++ b/plugins/src/main/kotlin/com/freeletics/gradle/util/Extensions.kt @@ -10,10 +10,14 @@ import com.freeletics.gradle.plugin.FreeleticsAndroidExtension import com.freeletics.gradle.plugin.FreeleticsBaseExtension import com.freeletics.gradle.plugin.FreeleticsJvmExtension import com.freeletics.gradle.plugin.FreeleticsMultiplatformExtension -import com.freeletics.gradle.util.KotlinProjectExtensionDelegate.Companion.kotlinProjectExtensionDelegate import org.gradle.api.Project import org.gradle.api.plugins.JavaPluginExtension +import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi +import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension internal val Project.freeleticsExtension: FreeleticsBaseExtension get() = extensions.getByType(FreeleticsBaseExtension::class.java) @@ -33,8 +37,21 @@ internal fun Project.java(action: JavaPluginExtension.() -> Unit) { } } -internal fun Project.kotlin(action: KotlinProjectExtensionDelegate.() -> Unit) { - kotlinProjectExtensionDelegate().action() +internal fun Project.kotlin(action: KotlinProjectExtension.() -> Unit) { + (project.extensions.getByName("kotlin") as KotlinProjectExtension).action() +} + +@OptIn(ExperimentalKotlinGradlePluginApi::class) +internal fun KotlinProjectExtension.compilerOptions(configure: KotlinCommonCompilerOptions.() -> Unit) { + when (this) { + is KotlinJvmProjectExtension -> compilerOptions(configure) + is KotlinAndroidProjectExtension -> compilerOptions(configure) + is KotlinMultiplatformExtension -> { + compilerOptions(configure) + targets.configureEach { compilerOptions(configure) } + } + else -> throw IllegalStateException("Unsupported kotlin extension ${this::class}") + } } internal fun Project.kotlinMultiplatform(action: KotlinMultiplatformExtension.() -> Unit) { diff --git a/plugins/src/main/kotlin/com/freeletics/gradle/util/KgpBridge.kt b/plugins/src/main/kotlin/com/freeletics/gradle/util/KgpBridge.kt deleted file mode 100644 index b0d9b613..00000000 --- a/plugins/src/main/kotlin/com/freeletics/gradle/util/KgpBridge.kt +++ /dev/null @@ -1,65 +0,0 @@ -package com.freeletics.gradle.util - -import org.gradle.api.Action -import org.gradle.api.Project -import org.gradle.jvm.toolchain.JavaToolchainSpec -import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension -import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask - -// TODO can be removed after KotlinMultiplatformExtension supports `compilerOptions` -// which should happen in 1.9.20 https://youtrack.jetbrains.com/issue/KT-55515 -internal sealed class KotlinProjectExtensionDelegate( - private val extension: KotlinProjectExtension, -) { - fun explicitApi() { - extension.explicitApi() - } - - fun jvmToolchain(action: Action) { - extension.jvmToolchain(action) - } - - abstract fun compilerOptions(configure: KotlinCommonCompilerOptions.() -> Unit) - - internal companion object { - fun Project.kotlinProjectExtensionDelegate(): KotlinProjectExtensionDelegate { - return when (val extension = extensions.getByName("kotlin")) { - is KotlinJvmProjectExtension -> KotlinJvmProjectExtensionDelegate(extension) - is KotlinAndroidProjectExtension -> KotlinAndroidProjectExtensionDelegate(extension) - is KotlinMultiplatformExtension -> KotlinMultiplatformProjectExtensionDelegate(this, extension) - else -> throw IllegalStateException("Unsupported kotlint extension ${extension::class}") - } - } - } -} - -internal class KotlinAndroidProjectExtensionDelegate( - private val extension: KotlinAndroidProjectExtension, -) : KotlinProjectExtensionDelegate(extension) { - override fun compilerOptions(configure: KotlinCommonCompilerOptions.() -> Unit) { - extension.compilerOptions(configure) - } -} - -internal class KotlinJvmProjectExtensionDelegate( - private val extension: KotlinJvmProjectExtension, -) : KotlinProjectExtensionDelegate(extension) { - override fun compilerOptions(configure: KotlinCommonCompilerOptions.() -> Unit) { - extension.compilerOptions(configure) - } -} -internal class KotlinMultiplatformProjectExtensionDelegate( - private val project: Project, - extension: KotlinMultiplatformExtension, -) : KotlinProjectExtensionDelegate(extension) { - - override fun compilerOptions(configure: KotlinCommonCompilerOptions.() -> Unit) { - project.tasks.withType(KotlinCompilationTask::class.java).configureEach { - it.compilerOptions(configure) - } - } -}