-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Dependency Handling with Logging and Safety Checks
- Loading branch information
Showing
12 changed files
with
186 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
plugin/library-convention/src/main/kotlin/dev/teogor/ceres/models/DependencyType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.teogor.ceres.models | ||
|
||
enum class DependencyType(val gradleNotation: String) { | ||
KSP("ksp"), | ||
KSP_ANDROID_TEST("kspAndroidTest"), | ||
IMPLEMENTATION("implementation"), | ||
API("api"), | ||
ANDROID_TEST_IMPLEMENTATION("androidTestImplementation"), | ||
TEST_IMPLEMENTATION("testImplementation"), | ||
COMPILE_ONLY("compileOnly"), | ||
} |
54 changes: 54 additions & 0 deletions
54
plugin/library-convention/src/main/kotlin/dev/teogor/ceres/models/LibrarySpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dev.teogor.ceres.models | ||
|
||
data class LibrarySpec( | ||
val name: String, | ||
val module: String, | ||
val dependencyTypes: List<DependencyType> = emptyList(), | ||
val isBom: Boolean = false, | ||
) | ||
|
||
val roomRuntime = LibrarySpec( | ||
name = "room.runtime", | ||
module = "androidx.room:room-runtime", | ||
dependencyTypes = listOf( | ||
DependencyType.IMPLEMENTATION, | ||
), | ||
) | ||
val roomKtx = LibrarySpec( | ||
name = "room.ktx", | ||
module = "androidx.room:room-ktx", | ||
dependencyTypes = listOf( | ||
DependencyType.IMPLEMENTATION, | ||
), | ||
) | ||
val roomCompiler = LibrarySpec( | ||
name = "room.compiler", | ||
module = "androidx.room:room-compiler", | ||
dependencyTypes = listOf( | ||
DependencyType.KSP, | ||
), | ||
) | ||
val hiltAndroid = LibrarySpec( | ||
name = "hilt.android", | ||
module = "com.google.dagger:hilt-android", | ||
dependencyTypes = listOf( | ||
DependencyType.IMPLEMENTATION, | ||
), | ||
) | ||
val hiltAndroidCompiler = LibrarySpec( | ||
name = "hilt.android.compiler", | ||
module = "com.google.dagger:hilt-android-compiler", | ||
dependencyTypes = listOf( | ||
DependencyType.KSP, | ||
DependencyType.KSP_ANDROID_TEST, | ||
), | ||
) | ||
val androidxComposeBom = LibrarySpec( | ||
name = "androidx.compose.bom", | ||
module = "androidx.compose:compose-bom", | ||
dependencyTypes = listOf( | ||
DependencyType.IMPLEMENTATION, | ||
DependencyType.ANDROID_TEST_IMPLEMENTATION, | ||
), | ||
isBom = true, | ||
) |
67 changes: 67 additions & 0 deletions
67
plugin/library-convention/src/main/kotlin/dev/teogor/ceres/utils/DependencyHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package dev.teogor.ceres.utils | ||
|
||
import dev.teogor.ceres.models.LibrarySpec | ||
import java.util.Optional | ||
import org.gradle.api.artifacts.MinimalExternalModuleDependency | ||
import org.gradle.api.artifacts.VersionCatalog | ||
import org.gradle.api.logging.Logger | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.kotlin.dsl.DependencyHandlerScope | ||
|
||
fun VersionCatalog.findLibrary( | ||
librarySpec: LibrarySpec, | ||
): Optional<Provider<MinimalExternalModuleDependency>> { | ||
return findLibrary(librarySpec.name) | ||
} | ||
|
||
fun DependencyHandlerScope.add( | ||
dependencies: List<LibrarySpec>, | ||
logger: Logger, | ||
libs: VersionCatalog, | ||
) { | ||
dependencies.forEach { libraryDependency -> | ||
val library = libs.findLibrary(libraryDependency) | ||
if (!library.isPresent) { | ||
logger.error("Library alias named \"${libraryDependency.name}\" not found in the version catalog. Please ensure it's properly defined.") | ||
|
||
libs.resolveLibraryDependency(libraryDependency.name)?.let { | ||
logger.warn("Found library with name \"${it.first}\". Using recommended alias \"${libraryDependency.name}\" is encouraged for consistency and clarity.") | ||
add(libraryDependency, it.second) | ||
} | ||
} else { | ||
add(libraryDependency, library.get().get()) | ||
} | ||
} | ||
} | ||
|
||
fun DependencyHandlerScope.add( | ||
librarySpec: LibrarySpec, | ||
moduleProvider: MinimalExternalModuleDependency, | ||
) { | ||
librarySpec.dependencyTypes.forEach { | ||
if (librarySpec.isBom) { | ||
add( | ||
configurationName = it.gradleNotation, | ||
dependencyNotation = platform(moduleProvider), | ||
) | ||
} else { | ||
add( | ||
configurationName = it.gradleNotation, | ||
dependencyNotation = moduleProvider, | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun VersionCatalog.resolveLibraryDependency(module: String): Pair<String, MinimalExternalModuleDependency>? { | ||
val regex = "[._-]".toRegex() | ||
return libraryAliases | ||
.map { libraryAlias -> | ||
Pair(libraryAlias, findLibrary(libraryAlias).get().getOrNull()) | ||
} | ||
.filter { it.second != null } | ||
.map { it.first to it.second!! } | ||
.firstOrNull { | ||
regex.replace(it.second.module.name, ".") == module | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters