Skip to content

Commit

Permalink
extract hilt setup into its own convention plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nisrulz committed Feb 8, 2024
1 parent 857a0b5 commit 2f9dba4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
5 changes: 1 addition & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)

id("app-convention")
id("hilt-convention")
}

android {
Expand All @@ -21,6 +19,5 @@ dependencies {
implementation(project(":presentation"))

// Dagger-Hilt
implementation(libs.bundles.hilt)
ksp(libs.hilt.compiler)
}
48 changes: 35 additions & 13 deletions build-logic/src/main/kotlin/VersionCatalogExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
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.plugin.use.PluginDependency

private val Project.catalog
get() = extensions.getByType<VersionCatalogsExtension>()

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

internal val VersionCatalog.compileSdk
get() = findVersionOrThrow("compileSdk").toInt()
Expand All @@ -15,19 +22,34 @@ internal val VersionCatalog.minSdk
internal val VersionCatalog.targetSdk
get() = findVersionOrThrow("targetSdk").toInt()

private fun VersionCatalog.findPluginOrThrow(name: String) =
findPlugin(name)
.orElseThrow { NoSuchElementException("Plugin $name not found in version catalog") }

private fun VersionCatalog.findBundleOrThrow(name: String) =
findBundle(name)
.orElseThrow { NoSuchElementException("Bundle $name not found in version catalog") }

private fun VersionCatalog.findLibraryOrThrow(name: String) =
findLibrary(name)
.orElseThrow { NoSuchElementException("Library $name not found in version catalog") }

private fun VersionCatalog.findVersionOrThrow(name: String) =
findVersion(name)
.orElseThrow { NoSuchElementException("Version $name not found in version catalog") }
.requiredVersion

internal fun Project.applyPlugin(
alias: String,
block: (Provider<PluginDependency>) -> Unit,
) {
libs.findPlugin(alias).ifPresent { plugin ->
block(plugin)
}
}

internal fun Project.applyBundle(
alias: String,
block: (Provider<ExternalModuleDependencyBundle>) -> Unit,
) {
libs.findBundle(alias).ifPresent { bundle ->
block(bundle)
}
}

internal fun Project.applyLibrary(
alias: String,
block: (Provider<MinimalExternalModuleDependency>) -> Unit,
) {
libs.findLibrary(alias).ifPresent { lib ->
block(lib)
}
}
10 changes: 10 additions & 0 deletions build-logic/src/main/kotlin/hilt-convention.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import gradle.kotlin.dsl.accessors._624aae704a5c30b505ab3598db099943.implementation

applyPlugin("ksp") { plugins { alias(it) } }
applyPlugin("hilt") { plugins { alias(it) } }

applyBundle("hilt") {
dependencies {
implementation(it)
}
}
4 changes: 1 addition & 3 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
id("library-convention")
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
id("hilt-convention")

alias(libs.plugins.kotlin.serialization)
}
Expand All @@ -26,7 +25,6 @@ dependencies {
implementation(project(":domain"))

// Dagger-Hilt
implementation(libs.bundles.hilt)
ksp(libs.hilt.compiler)

// Retrofit
Expand Down
5 changes: 1 addition & 4 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)

id("library-convention")
id("hilt-convention")
}

android {
Expand All @@ -12,7 +10,6 @@ android {

dependencies {
// Dagger-Hilt
implementation(libs.bundles.hilt)
ksp(libs.hilt.compiler)

// Testing
Expand Down
5 changes: 1 addition & 4 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)

id("library-convention")
id("hilt-convention")
}
android {
namespace = "com.nisrulz.example.spacexapi.presentation"
Expand All @@ -23,7 +21,6 @@ dependencies {
implementation(project(":data"))

// Dagger-Hilt
implementation(libs.bundles.hilt)
ksp(libs.hilt.compiler)

// Material
Expand Down

0 comments on commit 2f9dba4

Please sign in to comment.