diff --git a/src/main/kotlin/org/danilopianini/gradle/gitsemver/GitSemVer.kt b/src/main/kotlin/org/danilopianini/gradle/gitsemver/GitSemVer.kt index 922058fb..78d84e4c 100644 --- a/src/main/kotlin/org/danilopianini/gradle/gitsemver/GitSemVer.kt +++ b/src/main/kotlin/org/danilopianini/gradle/gitsemver/GitSemVer.kt @@ -8,7 +8,6 @@ import org.gradle.api.Project */ class GitSemVer : Plugin { - @OptIn(ExperimentalUnsignedTypes::class) override fun apply(project: Project) { with(project) { /* @@ -17,12 +16,21 @@ class GitSemVer : Plugin { val extension = project.createExtension(GitSemVerExtension.EXTENSION_NAME, project) project.afterEvaluate { with(extension) { - assignGitSemanticVersion() + properties["forceVersion"]?.let { + require(SemanticVersion.semVerRegex.matches(it.toString())) { + "The version '$it' is not a valid semantic versioning format" + } + project.logger.lifecycle("Forcing version to $it") + project.version = it.toString() + } ?: run { assignGitSemanticVersion() } } } tasks.register("printGitSemVer") { it.doLast { - println("Version computed by ${GitSemVer::class.java.simpleName}: ${extension.computeVersion()}") + println( + "Version computed by ${GitSemVer::class.java.simpleName}: " + + "${properties["forceVersion"] ?: extension.computeVersion()}", + ) } } } diff --git a/src/test/kotlin/org/danilopianini/gradle/gitsemver/test/Tests.kt b/src/test/kotlin/org/danilopianini/gradle/gitsemver/test/Tests.kt index 6994047f..bf6e4270 100644 --- a/src/test/kotlin/org/danilopianini/gradle/gitsemver/test/Tests.kt +++ b/src/test/kotlin/org/danilopianini/gradle/gitsemver/test/Tests.kt @@ -1,5 +1,6 @@ package org.danilopianini.gradle.gitsemver.test +import io.kotest.assertions.throwables.shouldThrowUnit import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe @@ -7,6 +8,7 @@ import io.kotest.matchers.string.shouldContain import io.kotest.matchers.string.shouldNotContain import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.UnexpectedBuildFailure import java.util.concurrent.TimeUnit internal class Tests : StringSpec( @@ -99,6 +101,16 @@ internal class Tests : StringSpec( } workingDirectory.runGradle() shouldNotContain "1.2.3" } + "force the version" { + val result = configuredPlugin().runGradle("-PforceVersion=1.2.3", "printGitSemVer", "--stacktrace") + print(result) + result shouldContain "1.2.3" + } + "force the version with a non compliant version" { + shouldThrowUnit { + configuredPlugin().runGradle("-PforceVersion=a.b.c", "printGitSemVer", "--stacktrace") + } + } } ) { companion object {