Skip to content

Commit

Permalink
use libs
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnBuhanan committed May 5, 2024
1 parent f6d2a33 commit 087ef4e
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 192 deletions.
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation(libs.kotlin.gradle)
implementation(libs.kspGradlePlugin)
implementation(localGroovy())
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

gradlePlugin {
Expand Down
6 changes: 2 additions & 4 deletions buildSrc/src/main/kotlin/LemmusAndroidApplicationPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import com.lemmus.extension.androidApplication
import com.lemmus.extension.anvil
import com.lemmus.extension.applyOnce
import com.lemmus.extension.circuit
import com.lemmus.extension.coreLibraryDesugaring
import com.lemmus.extension.coroutines
import com.lemmus.extension.dynamicNamespace
import com.lemmus.extension.findDependency
import com.lemmus.extension.jetpackCompose
import com.lemmus.extension.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies
Expand All @@ -34,11 +33,10 @@ class LemmusAndroidApplicationPlugin : Plugin<Project> {
circuit()
anvil()
jetpackCompose()
coreLibraryDesugaring()
coroutines()

dependencies {
"implementation"(findDependency("timber"))
"implementation"(libs.timber)
}

extensions.create("lemmus", LemmusAndroidApplicationExtension::class.java, this)
Expand Down
6 changes: 2 additions & 4 deletions buildSrc/src/main/kotlin/LemmusAndroidLibraryPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import com.lemmus.extension.androidLibrary
import com.lemmus.extension.anvil
import com.lemmus.extension.applyOnce
import com.lemmus.extension.circuit
import com.lemmus.extension.coreLibraryDesugaring
import com.lemmus.extension.coroutines
import com.lemmus.extension.dynamicNamespace
import com.lemmus.extension.feature
import com.lemmus.extension.findDependency
import com.lemmus.extension.jetpackCompose
import com.lemmus.extension.libs
import com.lemmus.extension.moshi
import com.lemmus.extension.retrofit
import org.gradle.api.Plugin
Expand All @@ -33,12 +32,11 @@ class LemmusAndroidLibraryPlugin : Plugin<Project> {
// checkstyle()
// configureLint()
// gradleDependenciesSorter()
coreLibraryDesugaring()
dynamicNamespace()
coroutines()

dependencies {
"implementation"(findDependency("timber"))
"implementation"(libs.timber)
}

extensions.create("lemmus", LemmusAndroidLibraryExtension::class.java, this)
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/LemmusKotlinLibraryPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.lemmus.extension.allProjects
import com.lemmus.extension.anvil
import com.lemmus.extension.applyOnce
import com.lemmus.extension.findDependency
import com.lemmus.extension.kotlinJvm
import com.lemmus.extension.kotlinLibrary
import com.lemmus.extension.libs
import com.lemmus.extension.moshi
import com.lemmus.extension.retrofit
import org.gradle.api.Plugin
Expand All @@ -32,7 +32,7 @@ class LemmusKotlinLibraryPlugin : Plugin<Project> {

private fun Project.circuitRuntime() {
dependencies {
"implementation"(findDependency("circuitRuntime"))
"implementation"(libs.circuitRuntime)
}
}
}
Expand Down
45 changes: 0 additions & 45 deletions buildSrc/src/main/kotlin/VersionCatalogExt.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ fun Project.androidApplication() {
defaultConfig {
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")

targetSdk = findVersionInt("targetSdk")
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"
applicationId = "com.lemmus"
testApplicationId = "com.lemmus.test"
}

aaptOptions {
noCompress += "apk"
}

testOptions {
unitTests.all {
// Explicitly disable stacktrace recovery during test runs so that tests can rely on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun Project.androidLibrary() {
configure<LibraryExtension> {
kotlinAndroid(this)
defaultConfig {
targetSdk = findVersionInt("targetSdk")
targetSdk = libs.versions.targetSdk.get().toInt()
}
sourceSets {
getByName("main") {
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/com/lemmus/extension/Anvil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fun Project.anvil() {
applyOnce("com.squareup.anvil")

dependencies {
"implementation"(findDependency("dagger.lib"))
"implementation"(libs.dagger.lib)
"implementation"(project(":common:di"))

"kapt"(findDependency("dagger.compiler"))
"kapt"(libs.dagger.compiler)
}
}
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/com/lemmus/extension/Circuit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ fun Project.circuit() {
applyOnce<KspGradleSubplugin>()

dependencies {
"implementation"(findBundle("circuit"))
"implementation"(findDependency("circuitCodegenAnnotations"))
"implementation"(libs.bundles.circuit)
"implementation"(libs.circuitCodegenAnnotations)

"ksp"(findDependency("circuitCodegen"))
"ksp"(libs.circuitCodegen)
}
}
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/com/lemmus/extension/Coroutines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.gradle.kotlin.dsl.dependencies

fun Project.coroutines() {
dependencies {
"implementation"(findDependency("kotlin.coroutines.core"))
"implementation"(findDependency("kotlin.coroutines.android"))
"implementation"(libs.kotlin.coroutines.core)
"implementation"(libs.kotlin.coroutines.android)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import org.gradle.kotlin.dsl.findByType

fun Project.jetpackCompose() {
dependencies {
"implementation"(findBundle("compose"))
"implementation"(libs.bundles.compose)
}

val compilerVersion = findVersion("composeCompiler")
val compilerVersion = libs.versions.composeCompiler.get()
extensions.findByType(LibraryExtension::class)?.apply {
jetpackComposeCompiler(compilerVersion)
}
Expand Down
38 changes: 2 additions & 36 deletions buildSrc/src/main/kotlin/com/lemmus/extension/KotlinAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

package com.lemmus.extension

import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.CommonExtension
import com.android.build.gradle.LibraryExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

Expand All @@ -23,15 +19,13 @@ internal fun Project.kotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = findVersionInt("compileSdk")
compileSdk = libs.versions.compileSdk.get().toInt()

defaultConfig {
minSdk = findVersionInt("minSdk")
minSdk = libs.versions.minSdk.get().toInt()
}

compileOptions {
// Up to Java 11 APIs are available through desugaring
// https://developer.android.com/studio/write/java11-minimal-support-table
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
Expand All @@ -40,29 +34,6 @@ internal fun Project.kotlinAndroid(
kotlin()
}

internal fun Project.coreLibraryDesugaring() {
extensions.findByType(LibraryExtension::class)?.apply {
// coreLibraryDesugaring(this)
}
extensions.findByType(ApplicationExtension::class)?.apply {
// coreLibraryDesugaring(this)
}
}

internal fun Project.coreLibraryDesugaring(
commonExtension: CommonExtension<*, *, *, *, *>,
) {
commonExtension.apply {
compileOptions {
isCoreLibraryDesugaringEnabled = true
}
}

dependencies {
"coreLibraryDesugaring"(findDependency("desugarJdkLibs"))
}
}

/**
* Configure base Kotlin options for JVM (non-Android)
*/
Expand All @@ -82,12 +53,7 @@ private fun Project.kotlin() {
// Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
// Set JVM target to 11
jvmTarget = JavaVersion.VERSION_17.toString()
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
// val warningsAsErrors: String? by project
// allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs = listOf(
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/com/lemmus/extension/Moshi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun Project.moshi() {
applyOnce<KspGradleSubplugin>()

dependencies {
"ksp"(findDependency("moshiCodeGen"))
"implementation"(findDependency("moshi"))
"ksp"(libs.moshiCodeGen)
"implementation"(libs.moshi)
}
}
70 changes: 3 additions & 67 deletions buildSrc/src/main/kotlin/com/lemmus/extension/ProjectExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,8 @@

package com.lemmus.extension

import java.util.*
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project
import org.gradle.api.artifacts.ExternalModuleDependencyBundle
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.the

/**
* Wraps VersionCatalog#findBundle to give a clean error message in the event of a failure
*/
fun VersionCatalog.resolveBundle(bundle: String): Provider<ExternalModuleDependencyBundle>? =
resolveCatalogItem(VersionCatalog::findBundle, bundle)

/**
* Wraps VersionCatalog#findDependency to give a clean error message in the event of a failure
*/
fun VersionCatalog.resolveDependency(dependency: String): Provider<MinimalExternalModuleDependency>? =
resolveCatalogItem(VersionCatalog::findLibrary, dependency)

/**
* Wraps VersionCatalog#findVersion to give a clean error message in the event of a failure
*/
fun VersionCatalog.resolveVersion(version: String): String? =
resolveCatalogItem(VersionCatalog::findVersion, version)?.requiredVersion

fun VersionCatalog.resolveVersionInt(version: String): Int? = resolveVersion(version)?.toInt()

private fun <T> VersionCatalog.resolveCatalogItem(
finder: VersionCatalog.(String) -> Optional<T>,
identifier: String,
): T? {
return try {
finder(identifier).get()
} catch (ex: NoSuchElementException) {
null
}
}

val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

fun Project.findVersion(version: String): String {
return libs.resolveVersion(version) ?: throw NullPointerException(
exceptionTemplate("version", version)
)
}

fun Project.findVersionInt(versionInt: String): Int {
return libs.resolveVersionInt(versionInt) ?: throw NullPointerException(
exceptionTemplate("versionInt", versionInt)
)
}

fun Project.findDependency(dependency: String): Provider<MinimalExternalModuleDependency> {
return libs.resolveDependency(dependency) ?: throw NullPointerException(
exceptionTemplate("dependency", dependency)
)
}

fun Project.findBundle(bundle: String): Provider<ExternalModuleDependencyBundle> {
return libs.resolveBundle(bundle) ?: throw NullPointerException(
exceptionTemplate("bundle", bundle)
)
}

private fun exceptionTemplate(type: String, name: String) =
"Couldn't resolve $type $name is it defined in the version catalog? (toml file)"
internal val Project.libs get() = the<LibrariesForLibs>()
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/com/lemmus/extension/Retrofit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.gradle.kotlin.dsl.dependencies

internal fun Project.retrofit() {
dependencies {
"implementation"(findDependency("retrofit"))
"implementation"(findDependency("retrofit.converter.moshi"))
"implementation"(libs.retrofit)
"implementation"(libs.retrofit.converter.moshi)
}
}
15 changes: 0 additions & 15 deletions settings.gradle

This file was deleted.

Loading

0 comments on commit 087ef4e

Please sign in to comment.