From adccf7b81990f553a42440f5ba8d7e41f855a697 Mon Sep 17 00:00:00 2001 From: Peter vR Date: Wed, 29 Dec 2021 14:07:57 -0700 Subject: [PATCH] expose Gradle Providers for calculated version & tagName for other extensions to use (#7) --- .../io/github/nefilim/gradle/semver/SemVerExtension.kt | 9 +++++++++ .../io/github/nefilim/gradle/semver/SemVerPlugin.kt | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerExtension.kt b/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerExtension.kt index eaf256b..ca0b7e0 100644 --- a/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerExtension.kt +++ b/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerExtension.kt @@ -10,6 +10,7 @@ import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.Property +import org.gradle.api.provider.Provider import javax.inject.Inject open class SemVerExtension @Inject constructor(objects: ObjectFactory) { @@ -17,6 +18,9 @@ open class SemVerExtension @Inject constructor(objects: ObjectFactory) { private val tagPrefix: Property = objects.property(String::class.java) private val initialVersion: Property = objects.property(Version::class.java) private val overrideVersion: Property = objects.property(Version::class.java) + private val calculatedVersionInternal: Property = objects.property(Version::class.java) + val calculatedVersion: Provider = calculatedVersionInternal + val calculatedTagName: Provider = calculatedVersionInternal.flatMap { v -> tagPrefix.map { "$it$v"} } private val main: BranchHandler = objects.newInstance(BranchHandler::class.java, objects, GitRef.MainBranch.DefaultScope, GitRef.MainBranch.DefaultStage) private val develop: BranchHandler = objects.newInstance(BranchHandler::class.java, objects, GitRef.DevelopBranch.DefaultScope, GitRef.DevelopBranch.DefaultStage) @@ -65,6 +69,11 @@ open class SemVerExtension @Inject constructor(objects: ObjectFactory) { action.execute(hotfix) } + fun setVersion(version: Version) { + calculatedVersionInternal.set(version) + calculatedVersionInternal.disallowChanges() + } + fun buildPluginConfig(): PluginConfig { return PluginConfig( verbose.get(), diff --git a/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerPlugin.kt b/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerPlugin.kt index 90a9ea3..8442596 100644 --- a/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerPlugin.kt +++ b/src/main/kotlin/io/github/nefilim/gradle/semver/SemVerPlugin.kt @@ -23,12 +23,15 @@ public class SemVerPlugin: Plugin { val context = SemVerPluginContext(target.git, config, target) context.verbose("semver configuration: $config") - target.version = config.overrideVersion.getOrElse { + val calculatedVersion = config.overrideVersion.getOrElse { context.calculateVersionFlow().getOrHandle { context.error("failed to calculate version: $it".red()) throw Exception("$it") } } + target.version = calculatedVersion.toString() + println("setting calculated version property to ${calculatedVersion}") + semVerExtension.setVersion(calculatedVersion) context.generateVersionFile() if (target == target.rootProject)