Skip to content

Commit

Permalink
chore: refactor build config plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshg committed Jan 26, 2025
1 parent 4ea2a0f commit f5fbc83
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ kopy { functions = KopyFunctions.Copy }
val javaAgent by configurations.registering { isTransitive = false }

tasks {
val buildConfigExtn = extensions.create<BuildConfigExtension>("buildConfig")
val buildConfig = register<BuildConfig>("buildConfig", buildConfigExtn)
buildConfig.configure { enabled = buildConfigExtn.enabled.get() }
kotlin.sourceSets.main { kotlin.srcDirs(buildConfig) }

// Configure "compileJava" and "compileTestJava" tasks.
withType<JavaCompile>().configureEach { configureJavac(project) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ redacted {
kopy { functions = KopyFunctions.Copy }

tasks {
if (isSharedProject) {
val buildConfigExtn = extensions.create<BuildConfigExtension>("buildConfig")
val buildConfig by register<BuildConfig>("buildConfig", buildConfigExtn)
kotlin.sourceSets.commonMain { kotlin.srcDirs(buildConfig) }
// compileKotlinMetadata { dependsOn(buildConfig) }
// maybeRegister<Task>("prepareKotlinIdeaImport") { dependsOn(buildConfig) }
}
val buildConfigExtn = extensions.create<BuildConfigExtension>("buildConfig")
val buildConfig = register<BuildConfig>("buildConfig", buildConfigExtn)
buildConfig.configure { enabled = buildConfigExtn.enabled.get() }
kotlin.sourceSets.commonMain { kotlin.srcDirs(buildConfig) }
// compileKotlinMetadata { dependsOn(buildConfig) }

withType<KspAATask>().configureEach { configureKspConfig() }

Expand Down
10 changes: 6 additions & 4 deletions plugins/project/src/main/kotlin/tasks/BuildConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tasks

import com.github.ajalt.mordant.rendering.TextColors
import com.javiersc.semver.project.gradle.plugin.Commit
import gg.jte.generated.precompiled.StaticTemplates
import javax.inject.Inject
Expand Down Expand Up @@ -32,7 +33,7 @@ abstract class BuildConfig @Inject constructor(@Nested val extn: BuildConfigExte
val pkg = fqName.substringBeforeLast(".", "")

val file = dir.resolve("$className.kt")
logger.quiet("Generated build config file: ${file.name}")
logger.quiet(TextColors.magenta("Generated build config file: ${file.name}"))

// Get git commit info
val gitCommit = run {
Expand Down Expand Up @@ -76,10 +77,11 @@ abstract class BuildConfig @Inject constructor(@Nested val extn: BuildConfigExte
}

open class BuildConfigExtension @Inject constructor(layout: ProjectLayout, objects: ObjectFactory) {
@get:Input val enabled = objects.property<Boolean>().convention(false)
@get:Input val classFqName = objects.property<String>().convention("BuildConfig")
@get:Input val projectVersion = objects.property<String>()
@get:Input val projectName = objects.property<String>()
@get:Input val projectDesc = objects.property<String>()
@get:Input val projectVersion = objects.property<String>().convention("")
@get:Input val projectName = objects.property<String>().convention("")
@get:Input val projectDesc = objects.property<String>().convention("")
@get:Input val catalogVersions = objects.mapProperty<String, String>().convention(emptyMap())
@get:Input val dependencies = objects.listProperty<String>().convention(emptyList())
@Internal val gitCommit = objects.property<Commit>()
Expand Down
13 changes: 11 additions & 2 deletions sandbox/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ plugins {
alias(libs.plugins.shadow)
}

kotlin { jvmTarget(project) }

description = "Sandbox App"

buildConfig {
enabled = true
projectName = rootProject.name
projectVersion = project.version.toString()
projectDesc = rootProject.description
gitCommit = semver.commits.get().first()
catalogVersions = project.versionCatalogMapOf()
}

kotlin { jvmTarget(project) }

application { mainClass = "MainKt" }
4 changes: 4 additions & 0 deletions sandbox/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@file:Suppress("UnstableApiUsage")

import com.javiersc.semver.settings.gradle.plugin.SemverSettingsExtension

pluginManagement {
repositories {
gradlePluginPortal()
Expand Down Expand Up @@ -46,6 +48,8 @@ dependencyResolutionManagement {
}
}

configure<SemverSettingsExtension> { gitDir = file("$rootDir/../.git") }

plugins { id("dev.suresh.plugin.repos") version "+" }

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
Expand Down
4 changes: 2 additions & 2 deletions sandbox/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fun main() {
println("Hello, sandbox project! 🏖")
println("Java: ${System.getProperty("java.runtime.version")}, Kotlin: ${KotlinVersion.CURRENT}")
println("Hello, ${BuildConfig.name}! 🏖")
println("Java: ${BuildConfig.java}, Kotlin: ${BuildConfig.kotlin}")
}

0 comments on commit f5fbc83

Please sign in to comment.