From 905f32c3be2da23c4c86bc77ef47454b30045f42 Mon Sep 17 00:00:00 2001 From: Dominik Grzelak Date: Sun, 24 Oct 2021 22:13:52 +0200 Subject: [PATCH] fix #392 | Allow to get the uncached version (#428) --- mkdocs.yml | 2 +- ...ScmVersionExposedApiIntegrationTest.groovy | 49 +++++++++++++++++++ .../axion/release/domain/VersionConfig.groovy | 25 +++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index f78b29b2..32e2b1bb 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -17,7 +17,7 @@ markdown_extensions: anchorlink: true extra: - version: '1.13.5' + version: '1.13.6' nav: - Home: 'index.md' diff --git a/src/integration/groovy/pl/allegro/tech/build/axion/release/ScmVersionExposedApiIntegrationTest.groovy b/src/integration/groovy/pl/allegro/tech/build/axion/release/ScmVersionExposedApiIntegrationTest.groovy index 20d3a2f0..aae7a957 100644 --- a/src/integration/groovy/pl/allegro/tech/build/axion/release/ScmVersionExposedApiIntegrationTest.groovy +++ b/src/integration/groovy/pl/allegro/tech/build/axion/release/ScmVersionExposedApiIntegrationTest.groovy @@ -60,4 +60,53 @@ class ScmVersionExposedApiIntegrationTest extends BaseIntegrationTest { result.output.contains("Branch: ${position.branch}") result.task(":outputPosition").outcome == TaskOutcome.SUCCESS } + + def "getUncached should respect changing prefix"() { + given: + repository.tag("v1.2.3") + repository.tag("prefix4.5.6") + repository.tag("another7.8.9") + buildFile(""" + task uncachedVersion { doLast { + println "Default prefix: \${scmVersion.uncached.decoratedVersion}" + scmVersion.tag.prefix = "prefix" + println "Custom prefix 1: \${scmVersion.uncached.decoratedVersion}" + scmVersion.tag.prefix = "another" + println "Custom prefix 2: \${scmVersion.uncached.decoratedVersion}" + } } + """) + + when: + def result = runGradle('uncachedVersion') + + then: + result.output.contains('Default prefix: 1.2.3') + result.output.contains('Custom prefix 1: 4.5.6') + result.output.contains('Custom prefix 2: 7.8.9') + result.task(":uncachedVersion").outcome == TaskOutcome.SUCCESS + } + + def "getUncached should not modify cached version"() { + given: + repository.tag("v1.2.3") + repository.tag("prefix4.5.6") + buildFile(""" + task uncachedVersion { doLast { + println "Default prefix: \${scmVersion.version}" + scmVersion.tag.prefix = "prefix" + println "Custom prefix: \${scmVersion.uncached.decoratedVersion}" + scmVersion.tag.prefix = "another" + println "Cached version: \${scmVersion.version}" + } } + """) + + when: + def result = runGradle('uncachedVersion') + + then: + result.output.contains('Default prefix: 1.2.3') + result.output.contains('Custom prefix: 4.5.6') + result.output.contains('Cached version: 1.2.3') + result.task(":uncachedVersion").outcome == TaskOutcome.SUCCESS + } } diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy index 19c0eb15..2577b557 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy @@ -190,11 +190,34 @@ class VersionConfig { } } + /** + * @deprecated Due to the fact it uses the cached context, which results in returning the same + * version even though project properties got changed. Use {@link #getUncached()} instead. + * @return uncached version, but based on a cached context + */ + @Deprecated @Nested VersionService.DecoratedVersion getUncachedVersion() { ensureContextExists() + return getVersionFromContext(context) + } + + /** + * Allows to calculate and get the version, omitting caching mechanisms. + * May be slower for large projects, use then {@link #getVersion()} instead. + * @since 1.13.4 + * @return uncached version + */ + @Nested + VersionService.DecoratedVersion getUncached() { + def context = GradleAwareContext.create(project, this) + return getVersionFromContext(context) + } + + private static VersionService.DecoratedVersion getVersionFromContext(Context context) { Properties rules = context.rules() - return context.versionService().currentDecoratedVersion(rules.version, rules.tag, rules.nextVersion) + def versionService = context.versionService() + return versionService.currentDecoratedVersion(rules.version, rules.tag, rules.nextVersion) } @Nested