Skip to content

Commit

Permalink
feat: allow forcing the version via gradle property
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasfara authored and DanySK committed Feb 21, 2023
1 parent 1a9286e commit dba5b4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/kotlin/org/danilopianini/gradle/gitsemver/GitSemVer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.gradle.api.Project
*/
class GitSemVer : Plugin<Project> {

@OptIn(ExperimentalUnsignedTypes::class)
override fun apply(project: Project) {
with(project) {
/*
Expand All @@ -17,12 +16,21 @@ class GitSemVer : Plugin<Project> {
val extension = project.createExtension<GitSemVerExtension>(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()}",
)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/org/danilopianini/gradle/gitsemver/test/Tests.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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
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(
Expand Down Expand Up @@ -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<UnexpectedBuildFailure> {
configuredPlugin().runGradle("-PforceVersion=a.b.c", "printGitSemVer", "--stacktrace")
}
}
}
) {
companion object {
Expand Down

0 comments on commit dba5b4d

Please sign in to comment.