Skip to content

Commit

Permalink
Support Bill of Materials (BOM)
Browse files Browse the repository at this point in the history
  • Loading branch information
Omico committed Dec 13, 2022
1 parent 32bd160 commit 509a9c9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/gradm-getting-started/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dependencies {
// Material
compileOnly(material)
// Square
compileOnly(platform(okhttp.bom))
compileOnly(okhttp.bom)
compileOnly(okhttp)
compileOnly(okhttp.dnsOverHttps)
compileOnly(okhttp.interceptor.logging)
Expand Down
1 change: 1 addition & 0 deletions examples/gradm-getting-started/gradm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ dependencies:
okhttp-bom:
alias: okhttp.bom
version: ${versions.okhttp}
bom: true
okhttp-dnsoverhttps:
alias: okhttp.dnsOverHttps
noSpecificVersion: true
Expand Down
3 changes: 3 additions & 0 deletions gradm-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ kotlin {
target.compilations.all {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = listOf(
"-opt-in=kotlin.RequiresOptIn",
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@
package me.omico.gradm.internal.codegen

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asTypeName
import me.omico.gradm.GRADM_DEPENDENCY_PACKAGE_NAME
import me.omico.gradm.VersionsMeta
import me.omico.gradm.internal.YamlDocument
import me.omico.gradm.internal.config.Dependency
import me.omico.gradm.internal.config.dependencies
import me.omico.gradm.path.gradmGeneratedDependenciesProjectPaths
import me.omico.gradm.path.sourceFolder
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency
import java.util.TreeMap
import org.gradle.api.artifacts.Dependency as GradleArtifactsDependency

internal data class CodegenDependency(
val hasParent: Boolean = false,
val bom: Boolean = false,
val group: String = "",
val artifact: String = "",
val version: String? = null,
Expand Down Expand Up @@ -127,6 +132,7 @@ private fun CodegenDependencies.addDependency(
getOrDefault(alias, CodegenDependency())
.copy(
hasParent = hasParent,
bom = dependency.bom,
group = dependency.group,
artifact = dependency.artifact,
version = dependency.version ?: versionsMeta[dependency.module],
Expand Down Expand Up @@ -181,9 +187,32 @@ private fun TypeSpec.Builder.addDependencyProperty(propertyName: String, classNa
.build()
.let(::addProperty)

@OptIn(ExperimentalKotlinPoetApi::class)
private fun TypeSpec.Builder.addDependency(name: String, dependency: CodegenDependency): TypeSpec.Builder =
apply {
if (!dependency.hasDependency) return@apply
if (dependency.bom) {
PropertySpec.builder(name.camelCase(), GradleArtifactsDependency::class)
.contextReceivers(DependencyHandler::class.asTypeName())
.getter(
FunSpec.getterBuilder()
.addStatement("return ${name.camelCase()}(\"${dependency.version}\")")
.build(),
)
.build()
.also(::addProperty)
FunSpec.builder(name.camelCase())
.addParameter("version", String::class)
.returns(GradleArtifactsDependency::class)
.contextReceivers(DependencyHandler::class.asTypeName())
.addStatement(
"return platform(\"${dependency.module}:\$version\")",
GradleArtifactsDependency::class,
)
.build()
.also(::addFunction)
return@apply
}
PropertySpec.builder(name.camelCase(), String::class)
.apply {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ private val gradleBuildScriptContent: String =
| target.compilations.all {
| kotlinOptions {
| jvmTarget = "11"
| freeCompilerArgs = listOf(
| "-Xcontext-receivers",
| )
| }
| }
|}
Expand Down Expand Up @@ -105,6 +108,9 @@ val gradleBuildScriptContentForBuildLogic: String =
| target.compilations.all {
| kotlinOptions {
| jvmTarget = "11"
| freeCompilerArgs = listOf(
| "-Xcontext-receivers",
| )
| }
| }
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ val YamlDocument.dependencies: List<Dependency>
repository = repository.url,
noUpdates = repository.noUpdates,
noSpecificVersion = noSpecificVersion,
bom = attributes.find("bom", false),
group = group,
artifact = artifact,
alias = attributes.require("alias"),
Expand All @@ -58,6 +59,7 @@ data class Dependency(
val repository: String,
val noUpdates: Boolean,
val noSpecificVersion: Boolean,
val bom: Boolean,
val group: String,
val artifact: String,
val alias: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal fun Plugin.toDependency(): Dependency =
repository = repository,
noUpdates = noUpdates,
noSpecificVersion = false,
bom = false,
group = group,
artifact = artifact,
alias = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ fun YamlScope.dependenciesMapping(document: YamlDocument) {
if (attributes.find("noSpecificVersion", false)) {
scalar("noSpecificVersion", true)
}
if (attributes.find("bom", false)) {
scalar("bom", true)
}
}
}
}
Expand Down

0 comments on commit 509a9c9

Please sign in to comment.