refactor(build): use typed catalog in build-logic

This commit is contained in:
Harsh Shandilya 2023-11-28 11:49:47 +05:30
parent a9b0112279
commit f3bc261f37
No known key found for this signature in database
3 changed files with 13 additions and 18 deletions

View file

@ -91,4 +91,7 @@ dependencies {
implementation(libs.build.vcu) implementation(libs.build.vcu)
implementation(libs.build.versions) implementation(libs.build.versions)
implementation(libs.kotlinx.coroutines.core) implementation(libs.kotlinx.coroutines.core)
// Expose the generated version catalog API to the plugin.
implementation(files(libs::class.java.superclass.protectionDomain.codeSource.location))
} }

View file

@ -5,12 +5,11 @@ import app.passwordstore.gradle.flavors.configureSlimTests
import com.android.build.api.dsl.ApplicationExtension import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.LibraryExtension import com.android.build.api.dsl.LibraryExtension
import com.android.build.gradle.TestedExtension import com.android.build.gradle.TestedExtension
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.findByType import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType import org.gradle.kotlin.dsl.withType
object AndroidCommon { object AndroidCommon {
@ -47,11 +46,10 @@ object AndroidCommon {
} }
project.extensions.findByType<ApplicationExtension>()?.run { lint.configureLint(project) } project.extensions.findByType<ApplicationExtension>()?.run { lint.configureLint(project) }
project.extensions.findByType<LibraryExtension>()?.run { lint.configureLint(project) } project.extensions.findByType<LibraryExtension>()?.run { lint.configureLint(project) }
val lintDepKeys = listOf("thirdparty-compose-lints", "thirdparty-slack-lints") val libs = project.extensions.getByName("libs") as LibrariesForLibs
val catalog = project.extensions.getByType<VersionCatalogsExtension>() project.dependencies.apply {
val libs = catalog.named("libs") addProvider("lintChecks", libs.thirdparty.compose.lints)
lintDepKeys.forEach { key -> addProvider("lintChecks", libs.thirdparty.slack.lints)
project.dependencies.addProvider("lintChecks", libs.findLibrary(key).get())
} }
} }
} }

View file

@ -6,11 +6,9 @@
package app.passwordstore.gradle package app.passwordstore.gradle
import app.passwordstore.gradle.KotlinCommonPlugin.Companion.JVM_TOOLCHAIN_ACTION import app.passwordstore.gradle.KotlinCommonPlugin.Companion.JVM_TOOLCHAIN_ACTION
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.artifacts.VersionConstraint
import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType import org.gradle.kotlin.dsl.withType
@ -27,10 +25,9 @@ class KotlinAndroidPlugin : Plugin<Project> {
apply(KotlinCommonPlugin::class) apply(KotlinCommonPlugin::class)
} }
project.extensions.getByType<KotlinProjectExtension>().jvmToolchain(JVM_TOOLCHAIN_ACTION) project.extensions.getByType<KotlinProjectExtension>().jvmToolchain(JVM_TOOLCHAIN_ACTION)
val catalog = project.extensions.getByType<VersionCatalogsExtension>() val libs = project.extensions.getByName("libs") as LibrariesForLibs
val libs = catalog.named("libs") val composeCompilerVersion = libs.versions.composeCompiler.get()
val composeCompilerVersion = libs.getVersion("composeCompiler") val kotlinVersion = libs.versions.kotlin.get()
val kotlinVersion = libs.getVersion("kotlin")
val matches = COMPOSE_COMPILER_VERSION_REGEX.find(composeCompilerVersion) val matches = COMPOSE_COMPILER_VERSION_REGEX.find(composeCompilerVersion)
if (matches != null) { if (matches != null) {
@ -46,11 +43,8 @@ class KotlinAndroidPlugin : Plugin<Project> {
} }
} }
private fun VersionCatalog.getVersion(key: String) =
findVersion(key).map(VersionConstraint::toString).get()
private companion object { private companion object {
// Matches against 1.5.0-dev-k1.9.0-6a60475e07f // Matches against 1.5.0-dev-k1.9.0-6a60475e07f
val COMPOSE_COMPILER_VERSION_REGEX = "\\d.\\d.\\d-dev-k(\\d.\\d.\\d)-[a-z0-9]+".toRegex() val COMPOSE_COMPILER_VERSION_REGEX = "\\d.\\d.\\d-dev-k(\\d.\\d.\\d+)-[a-z0-9]+".toRegex()
} }
} }