diff --git a/command-line-tools/build.gradle.kts b/command-line-tools/build.gradle.kts index 70afd02..3501c85 100644 --- a/command-line-tools/build.gradle.kts +++ b/command-line-tools/build.gradle.kts @@ -1,3 +1,4 @@ + import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask repositories { diff --git a/command-line-tools/tagger-cli/build.gradle.kts b/command-line-tools/tagger-cli/build.gradle.kts index 0449cb3..08dc6b3 100644 --- a/command-line-tools/tagger-cli/build.gradle.kts +++ b/command-line-tools/tagger-cli/build.gradle.kts @@ -1,4 +1,6 @@ +import org.apache.tools.ant.filters.ReplaceTokens import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { alias(libs.plugins.org.jetbrains.kotlin.multiplatform) @@ -11,6 +13,8 @@ repositories { mavenCentral() } +val generatedDirectory = project.layout.buildDirectory.dir("generated-sources/templates/kotlin/main") + kotlin { jvm { withJava() } js(IR) { @@ -19,6 +23,7 @@ kotlin { binaries.executable() testTask { useMocha { timeout = "10s" } + environment("EXPECTED_VERSION", "${project.version}") environment("GIT_CONFIG_GLOBAL", "/dev/null") environment("GIT_CONFIG_SYSTEM", "/dev/null") } @@ -54,6 +59,7 @@ dependencies { tasks { withType(Test::class) { useJUnitPlatform() + environment("EXPECTED_VERSION", project.version) environment("GIT_CONFIG_GLOBAL", "/dev/null") environment("GIT_CONFIG_SYSTEM", "/dev/null") } @@ -97,6 +103,20 @@ tasks { dependsOn(jsPublish) mustRunAfter(check) } + val copyTemplates by registering(Copy::class) { + inputs.property("version", rootProject.version) + filteringCharset = "UTF-8" + from(project.projectDir.resolve("src/commonMain/templates")) { + filter("tokens" to mapOf("TAGGER_VERSION" to rootProject.version)) + } + into(generatedDirectory) + } + withType { + dependsOn(copyTemplates) + } + kotlin.sourceSets { + commonMain { kotlin.srcDir(copyTemplates) } + } } fun Project.isSnapshot() = version.toString().contains("SNAPSHOT") diff --git a/command-line-tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tagger.kt b/command-line-tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tagger.kt index 2d5601e..821e12c 100644 --- a/command-line-tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tagger.kt +++ b/command-line-tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tagger.kt @@ -3,9 +3,14 @@ package com.zegreatrob.tools.tagger.cli import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.options.versionOption class Tagger : CliktCommand() { + init { + versionOption(Versions.taggerVersion) + } + private val quiet by option("--quiet", "-q") .flag(default = false) diff --git a/command-line-tools/tagger-cli/src/commonMain/templates/Versions.kt b/command-line-tools/tagger-cli/src/commonMain/templates/Versions.kt new file mode 100644 index 0000000..b67b022 --- /dev/null +++ b/command-line-tools/tagger-cli/src/commonMain/templates/Versions.kt @@ -0,0 +1,5 @@ +package com.zegreatrob.tools.tagger.cli + +object Versions { + val taggerVersion: String = "@TAGGER_VERSION@" +} diff --git a/command-line-tools/tagger-cli/src/commonTest/kotlin/com/zegreatrob/tools/tagger/cli/TaggerTest.kt b/command-line-tools/tagger-cli/src/commonTest/kotlin/com/zegreatrob/tools/tagger/cli/TaggerTest.kt index 63d4f1e..69c6972 100644 --- a/command-line-tools/tagger-cli/src/commonTest/kotlin/com/zegreatrob/tools/tagger/cli/TaggerTest.kt +++ b/command-line-tools/tagger-cli/src/commonTest/kotlin/com/zegreatrob/tools/tagger/cli/TaggerTest.kt @@ -1,8 +1,10 @@ package com.zegreatrob.tools.tagger.cli import com.github.ajalt.clikt.testing.test +import com.zegreatrob.tools.test.git.getEnvironmentVariable import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull class TaggerTest { @Test @@ -24,4 +26,17 @@ class TaggerTest { assertEquals("", it) } } + + @Test + fun versionWillReturnAppropriateVersion() { + val expectedVersion = getEnvironmentVariable("EXPECTED_VERSION") + assertNotNull(expectedVersion, "Test not setup correctly - include build version") + cli() + .test("-q --version") + .output + .trim() + .let { + assertEquals("tagger version $expectedVersion", it) + } + } }