Skip to content

Commit

Permalink
expose Gradle Providers for calculated version & tagName for other ex…
Browse files Browse the repository at this point in the history
…tensions to use (#7)
  • Loading branch information
nefilim authored Dec 29, 2021
1 parent 8e773e4 commit adccf7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ 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) {
private val verbose: Property<Boolean> = objects.property(Boolean::class.java)
private val tagPrefix: Property<String> = objects.property(String::class.java)
private val initialVersion: Property<Version> = objects.property(Version::class.java)
private val overrideVersion: Property<Version> = objects.property(Version::class.java)
private val calculatedVersionInternal: Property<Version> = objects.property(Version::class.java)
val calculatedVersion: Provider<Version> = calculatedVersionInternal
val calculatedTagName: Provider<String> = 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)
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ public class SemVerPlugin: Plugin<Project> {
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)
Expand Down

0 comments on commit adccf7b

Please sign in to comment.