From d93f470bc7514edc4e469cfb5d665e2c40b1b61d Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Tue, 13 Mar 2018 17:45:22 +0100 Subject: [PATCH 1/7] Fix issue Montonically increasing AssemblyFileVersion to satisfy the MSI (#1366) --- docs/configuration.md | 10 ++++++ src/GitVersionCore/Configuration/Config.cs | 3 ++ .../Configuration/ConfigurationProvider.cs | 1 + src/GitVersionCore/EffectiveConfiguration.cs | 3 ++ src/GitVersionCore/GitVersionContext.cs | 3 +- src/GitVersionCore/GitVersionCore.csproj | 3 +- .../Helpers/EnvironmentHelper.cs | 12 +++++++ .../OutputVariables/VariableProvider.cs | 23 +++++++++++- src/GitVersionCore/StringFormatWith.cs | 35 ++++++++++++++++--- 9 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/GitVersionCore/Helpers/EnvironmentHelper.cs diff --git a/docs/configuration.md b/docs/configuration.md index 04ed44ae24..807ce72d2c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -71,6 +71,16 @@ while still updating the `AssemblyVersion` and `AssemblyInformationVersion` attributes. Valid values: `MajorMinorPatchTag`, `MajorMinorPatch`, `MajorMinor`, `Major`, `None`. +### assembly-file-versioning-format +Set this to any of the available [variables](/more-info/variables) in combination (but not necessary) with +a process scoped environment variable. It overwrites the value of `assembly-file-versioning-scheme`. To reference +an environment variable, use `$` +Example Syntax #1: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER` +if available in the environment otherwise the `fallback_string` +Example Syntax #2: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER` +if available in the environment otherwise the parsing fails. +String interpolation is supported as in `assembly-informational-format` + ### assembly-informational-format Set this to any of the available [variables](/more-info/variables) to change the value of the `AssemblyInformationalVersion` attribute. Default set to diff --git a/src/GitVersionCore/Configuration/Config.cs b/src/GitVersionCore/Configuration/Config.cs index a1d7e3f010..3e6b4067d0 100644 --- a/src/GitVersionCore/Configuration/Config.cs +++ b/src/GitVersionCore/Configuration/Config.cs @@ -26,6 +26,9 @@ public Config() [YamlMember(Alias = "assembly-informational-format")] public string AssemblyInformationalFormat { get; set; } + [YamlMember(Alias = "assembly-file-versioning-format")] + public string AssemblyFileVersioningFormat { get; set; } + [YamlMember(Alias = "mode")] public VersioningMode? VersioningMode { get; set; } diff --git a/src/GitVersionCore/Configuration/ConfigurationProvider.cs b/src/GitVersionCore/Configuration/ConfigurationProvider.cs index b4fc482c8e..dd023c7298 100644 --- a/src/GitVersionCore/Configuration/ConfigurationProvider.cs +++ b/src/GitVersionCore/Configuration/ConfigurationProvider.cs @@ -88,6 +88,7 @@ public static void ApplyDefaultsTo(Config config) config.AssemblyVersioningScheme = config.AssemblyVersioningScheme ?? AssemblyVersioningScheme.MajorMinorPatch; config.AssemblyFileVersioningScheme = config.AssemblyFileVersioningScheme ?? AssemblyFileVersioningScheme.MajorMinorPatch; config.AssemblyInformationalFormat = config.AssemblyInformationalFormat; + config.AssemblyFileVersioningFormat = config.AssemblyFileVersioningFormat; config.TagPrefix = config.TagPrefix ?? DefaultTagPrefix; config.VersioningMode = config.VersioningMode ?? VersioningMode.ContinuousDelivery; config.ContinuousDeploymentFallbackTag = config.ContinuousDeploymentFallbackTag ?? "ci"; diff --git a/src/GitVersionCore/EffectiveConfiguration.cs b/src/GitVersionCore/EffectiveConfiguration.cs index 3ce643e31c..732fc0e252 100644 --- a/src/GitVersionCore/EffectiveConfiguration.cs +++ b/src/GitVersionCore/EffectiveConfiguration.cs @@ -12,6 +12,7 @@ public EffectiveConfiguration( AssemblyVersioningScheme assemblyVersioningScheme, AssemblyFileVersioningScheme assemblyFileVersioningScheme, string assemblyInformationalFormat, + string assemblyFileVersioningFormat, VersioningMode versioningMode, string gitTagPrefix, string tag, string nextVersion, IncrementStrategy increment, string branchPrefixToTrim, @@ -35,6 +36,7 @@ public EffectiveConfiguration( AssemblyVersioningScheme = assemblyVersioningScheme; AssemblyFileVersioningScheme = assemblyFileVersioningScheme; AssemblyInformationalFormat = assemblyInformationalFormat; + AssemblyFileVersioningFormat = assemblyFileVersioningFormat; VersioningMode = versioningMode; GitTagPrefix = gitTagPrefix; Tag = tag; @@ -67,6 +69,7 @@ public EffectiveConfiguration( public AssemblyVersioningScheme AssemblyVersioningScheme { get; private set; } public AssemblyFileVersioningScheme AssemblyFileVersioningScheme { get; private set; } public string AssemblyInformationalFormat { get; private set; } + public string AssemblyFileVersioningFormat { get; private set; } /// /// Git tag prefix diff --git a/src/GitVersionCore/GitVersionContext.cs b/src/GitVersionCore/GitVersionContext.cs index 67bd267ef0..2ce7cf0acf 100644 --- a/src/GitVersionCore/GitVersionContext.cs +++ b/src/GitVersionCore/GitVersionContext.cs @@ -122,6 +122,7 @@ void CalculateEffectiveConfiguration() var assemblyVersioningScheme = FullConfiguration.AssemblyVersioningScheme.Value; var assemblyFileVersioningScheme = FullConfiguration.AssemblyFileVersioningScheme.Value; var assemblyInformationalFormat = FullConfiguration.AssemblyInformationalFormat; + var assemblyFileVersioningFormat = FullConfiguration.AssemblyFileVersioningFormat; var gitTagPrefix = FullConfiguration.TagPrefix; var majorMessage = FullConfiguration.MajorVersionBumpMessage; var minorMessage = FullConfiguration.MinorVersionBumpMessage; @@ -132,7 +133,7 @@ void CalculateEffectiveConfiguration() var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? FullConfiguration.CommitMessageIncrementing.Value; Configuration = new EffectiveConfiguration( - assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix, + assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, tag, nextVersion, incrementStrategy, currentBranchConfig.Regex, preventIncrementForMergedBranchVersion, diff --git a/src/GitVersionCore/GitVersionCore.csproj b/src/GitVersionCore/GitVersionCore.csproj index acebe812b1..d95644ffd2 100644 --- a/src/GitVersionCore/GitVersionCore.csproj +++ b/src/GitVersionCore/GitVersionCore.csproj @@ -121,6 +121,7 @@ + @@ -232,4 +233,4 @@ - + \ No newline at end of file diff --git a/src/GitVersionCore/Helpers/EnvironmentHelper.cs b/src/GitVersionCore/Helpers/EnvironmentHelper.cs new file mode 100644 index 0000000000..84d35afe0d --- /dev/null +++ b/src/GitVersionCore/Helpers/EnvironmentHelper.cs @@ -0,0 +1,12 @@ +using System; + +namespace GitVersion.Helpers +{ + public class EnvironmentHelper + { + public static string GetEnvironmentVariableForProcess(string envVar) + { + return Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Process); + } + } +} diff --git a/src/GitVersionCore/OutputVariables/VariableProvider.cs b/src/GitVersionCore/OutputVariables/VariableProvider.cs index ef6acb5c55..ace1013b7a 100644 --- a/src/GitVersionCore/OutputVariables/VariableProvider.cs +++ b/src/GitVersionCore/OutputVariables/VariableProvider.cs @@ -61,6 +61,27 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, } } + string assemblyFileVersioningFormat; + string assemblyFileSemVer; + + if (!(string.IsNullOrEmpty(config.AssemblyFileVersioningFormat))) + { + //assembly-file-versioning-format value if provided in the config, overwrites the exisiting AssemblyFileSemVer + try + { + assemblyFileVersioningFormat = config.AssemblyFileVersioningFormat.FormatWith(semverFormatValues); + assemblyFileSemVer = assemblyFileVersioningFormat; + } + catch (ArgumentException formex) + { + throw new WarningException(string.Format("Unable to format AssemblyFileVersioningFormat. Check your format string: {0}", formex.Message)); + } + } + else + { + assemblyFileSemVer = semverFormatValues.AssemblyFileSemVer; + } + var variables = new VersionVariables( semverFormatValues.Major, semverFormatValues.Minor, @@ -76,7 +97,7 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, semverFormatValues.LegacySemVerPadded, semverFormatValues.FullSemVer, semverFormatValues.AssemblySemVer, - semverFormatValues.AssemblyFileSemVer, + assemblyFileSemVer, semverFormatValues.PreReleaseTag, semverFormatValues.PreReleaseTagWithDash, semverFormatValues.PreReleaseLabel, diff --git a/src/GitVersionCore/StringFormatWith.cs b/src/GitVersionCore/StringFormatWith.cs index a33c1ded82..78e11c5dae 100644 --- a/src/GitVersionCore/StringFormatWith.cs +++ b/src/GitVersionCore/StringFormatWith.cs @@ -8,7 +8,7 @@ namespace GitVersion static class StringFormatWithExtension { - private static readonly Regex TokensRegex = new Regex(@"{\w+}", RegexOptions.Compiled); + private static readonly Regex TokensRegex = new Regex(@"{\$??\w+(\?\?\w+)??}", RegexOptions.Compiled); /// /// Formats a string template with the given source object. @@ -30,13 +30,39 @@ public static string FormatWith(this string template, T source) foreach (Match match in TokensRegex.Matches(template)) { var memberAccessExpression = TrimBraces(match.Value); - Func expression = CompileDataBinder(objType, memberAccessExpression); - string propertyValue = expression(source); + string propertyValue = null; + + // Support evaluation of environment variables in the format string + // For example: {$JENKINS_BUILD_NUMBER??fall-back-string} + if (memberAccessExpression.StartsWith("$")) + { + memberAccessExpression = memberAccessExpression.TrimStart('$').TrimEnd('$'); + string envVar = memberAccessExpression, fallback = null; + string[] components = (memberAccessExpression.Contains("??")) ? memberAccessExpression.Split(new string[] { "??" }, StringSplitOptions.None) : null; + if (components != null) + { + envVar = components[0]; + fallback = components[1]; + } + + propertyValue = Helpers.EnvironmentHelper.GetEnvironmentVariableForProcess(envVar); + if (propertyValue == null) + { + if (fallback != null) + propertyValue = fallback; + else + throw new ArgumentException(string.Format("Environment variable {0} not found and no fallback string provided", envVar)); + } + } + else + { + Func expression = CompileDataBinder(objType, memberAccessExpression); + propertyValue = expression(source); + } template = template.Replace(match.Value, propertyValue); } return template; - } @@ -73,6 +99,5 @@ static Func CompileDataBinder(Type type, string expr) return Expression.Lambda>(body, param).Compile(); } - } } \ No newline at end of file From d0f4994053b9f36c49f4c3aefb572b0df381847a Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Tue, 13 Mar 2018 18:00:11 +0100 Subject: [PATCH 2/7] Fix failing testcases --- src/GitVersionCore.Tests/CommitDateTests.cs | 2 +- src/GitVersionCore.Tests/TestEffectiveConfiguration.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore.Tests/CommitDateTests.cs b/src/GitVersionCore.Tests/CommitDateTests.cs index 4872031fdd..dfe98c1751 100644 --- a/src/GitVersionCore.Tests/CommitDateTests.cs +++ b/src/GitVersionCore.Tests/CommitDateTests.cs @@ -27,7 +27,7 @@ public void CommitDateFormatTest(string format, string expectedOutcome) }, new EffectiveConfiguration( - AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit, + AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit, "", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty(), false, true, format) ); diff --git a/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs b/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs index 1fc326056a..61ca6a9963 100644 --- a/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs +++ b/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs @@ -10,6 +10,7 @@ public class TestEffectiveConfiguration : EffectiveConfiguration public TestEffectiveConfiguration( AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme assemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, + string assemblyFileVersioningSchemeFormat = null, string assemblyInformationalFormat = null, VersioningMode versioningMode = VersioningMode.ContinuousDelivery, string gitTagPrefix = "v", @@ -32,7 +33,7 @@ public TestEffectiveConfiguration( bool tracksReleaseBranches = false, bool isRelease = false, string commitDateFormat = "yyyy-MM-dd") : - base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch, + base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyFileVersioningSchemeFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch, branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag, trackMergeTarget, majorMessage, minorMessage, patchMessage, noBumpMessage, From 8a16bcb2219c52c8b1cc128c8bd58a19eab2a50f Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Thu, 15 Mar 2018 13:48:31 +0100 Subject: [PATCH 3/7] Adding a unit test for FormatWith method --- .../GitVersionCore.Tests.csproj | 1 + .../StringFormatWithExtensionTests.cs | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs diff --git a/src/GitVersionCore.Tests/GitVersionCore.Tests.csproj b/src/GitVersionCore.Tests/GitVersionCore.Tests.csproj index d1f5a93bf4..6c7f1ad723 100644 --- a/src/GitVersionCore.Tests/GitVersionCore.Tests.csproj +++ b/src/GitVersionCore.Tests/GitVersionCore.Tests.csproj @@ -143,6 +143,7 @@ + diff --git a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs new file mode 100644 index 0000000000..32c8a9a5b8 --- /dev/null +++ b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs @@ -0,0 +1,75 @@ +using System; + +using GitVersion; +using NUnit.Framework; + +namespace GitVersionCore.Tests +{ + [TestFixture] + + public class StringFormatWithExtensionTests + { + [Test] + public void FormatWith_NoTokens() + { + var propertyObject = new { }; + var target = "Some String without tokens"; + var expected = target; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_SingleSimpleToken() + { + var propertyObject = new { SomeProperty = "SomeValue" }; + var target = "{SomeProperty}"; + var expected = "SomeValue"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_MultipleTokensAndVerbatimText() + { + var propertyObject = new { SomeProperty = "SomeValue", AnotherProperty = "Other Value" }; + var target = "{SomeProperty} some text {AnotherProperty}"; + var expected = "SomeValue some text Other Value"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_EnvVarToken() + { + Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value"); + var propertyObject = new { }; + var target = "{$GIT_VERSION_TEST_VAR}"; + var expected = "Env Var Value"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_EnvVarTokenWithFallback() + { + Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value"); + var propertyObject = new { }; + var target = "{$GIT_VERSION_TEST_VAR??fallback}"; + var expected = "Env Var Value"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_UnsetEnvVarTokenWithFallback() + { + Environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null); + var propertyObject = new { }; + var target = "{$GIT_VERSION_UNSET_TEST_VAR??fallback}"; + var expected = "fallback"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + } +} From b4f6e8af409cff8f2bbe6f71aa8cde3cf4ead985 Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Wed, 21 Mar 2018 15:31:01 +0100 Subject: [PATCH 4/7] Added support for assembly-versioning-format, updated testcase for FormatWith, updated docs --- docs/configuration.md | 3 + src/GitVersionCore.Tests/CommitDateTests.cs | 2 +- .../StringFormatWithExtensionTests.cs | 43 +++++++++++++ .../TestEffectiveConfiguration.cs | 5 +- src/GitVersionCore/Configuration/Config.cs | 3 + .../Configuration/ConfigurationProvider.cs | 1 + src/GitVersionCore/EffectiveConfiguration.cs | 3 + src/GitVersionCore/GitVersionContext.cs | 3 +- .../OutputVariables/VariableProvider.cs | 62 ++++++++----------- 9 files changed, 86 insertions(+), 39 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 807ce72d2c..296166f6b4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -81,6 +81,9 @@ Example Syntax #2: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER}'`. Uses `J if available in the environment otherwise the parsing fails. String interpolation is supported as in `assembly-informational-format` +### assembly-versioning-format +Follows the same semantics as `assembly-file-versioning-format` and overwrites the value of `assembly-versioning-scheme`. + ### assembly-informational-format Set this to any of the available [variables](/more-info/variables) to change the value of the `AssemblyInformationalVersion` attribute. Default set to diff --git a/src/GitVersionCore.Tests/CommitDateTests.cs b/src/GitVersionCore.Tests/CommitDateTests.cs index dfe98c1751..8520786baf 100644 --- a/src/GitVersionCore.Tests/CommitDateTests.cs +++ b/src/GitVersionCore.Tests/CommitDateTests.cs @@ -27,7 +27,7 @@ public void CommitDateFormatTest(string format, string expectedOutcome) }, new EffectiveConfiguration( - AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit, + AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit, "", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty(), false, true, format) ); diff --git a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs index 32c8a9a5b8..1d77278c33 100644 --- a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs +++ b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs @@ -71,5 +71,48 @@ public void FormatWith_UnsetEnvVarTokenWithFallback() var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); } + + [Test] + public void FormatWith_MultipleEnvVars() + { + Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_1", "Val-1"); + Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_2", "Val-2"); + var propertyObject = new { }; + var target = "{$GIT_VERSION_TEST_VAR_1} and {$GIT_VERSION_TEST_VAR_2}"; + var expected = "Val-1 and Val-2"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_MultipleEnvChars() + { + var propertyObject = new { }; + //Test the greediness of the regex in matching $ char + var target = "{$$GIT_VERSION_TEST_VAR_1} and {$DUMMY_VAR??fallback}"; + var expected = "{$$GIT_VERSION_TEST_VAR_1} and fallback"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } + + [Test] + public void FormatWith_MultipleFallbackChars() + { + var propertyObject = new { }; + //Test the greediness of the regex in matching $ and ?? chars + var target = "{$$GIT_VERSION_TEST_VAR_1} and {$DUMMY_VAR???fallback}"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(target, actual); + } + + [Test] + public void FormatWith_SingleFallbackChar() + { + var propertyObject = new { }; + //Test the sanity of the regex when there is a grammar mismatch + var target = "SomeNumbers and {$DUMMY_VAR?fallback}"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(target, actual); + } } } diff --git a/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs b/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs index 61ca6a9963..876de76ac1 100644 --- a/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs +++ b/src/GitVersionCore.Tests/TestEffectiveConfiguration.cs @@ -10,7 +10,8 @@ public class TestEffectiveConfiguration : EffectiveConfiguration public TestEffectiveConfiguration( AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme assemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, - string assemblyFileVersioningSchemeFormat = null, + string assemblyVersioningFormat = null, + string assemblyFileVersioningFormat = null, string assemblyInformationalFormat = null, VersioningMode versioningMode = VersioningMode.ContinuousDelivery, string gitTagPrefix = "v", @@ -33,7 +34,7 @@ public TestEffectiveConfiguration( bool tracksReleaseBranches = false, bool isRelease = false, string commitDateFormat = "yyyy-MM-dd") : - base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyFileVersioningSchemeFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch, + base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch, branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag, trackMergeTarget, majorMessage, minorMessage, patchMessage, noBumpMessage, diff --git a/src/GitVersionCore/Configuration/Config.cs b/src/GitVersionCore/Configuration/Config.cs index 3e6b4067d0..733b294292 100644 --- a/src/GitVersionCore/Configuration/Config.cs +++ b/src/GitVersionCore/Configuration/Config.cs @@ -26,6 +26,9 @@ public Config() [YamlMember(Alias = "assembly-informational-format")] public string AssemblyInformationalFormat { get; set; } + [YamlMember(Alias = "assembly-versioning-format")] + public string AssemblyVersioningFormat { get; set; } + [YamlMember(Alias = "assembly-file-versioning-format")] public string AssemblyFileVersioningFormat { get; set; } diff --git a/src/GitVersionCore/Configuration/ConfigurationProvider.cs b/src/GitVersionCore/Configuration/ConfigurationProvider.cs index dd023c7298..6c78f4b037 100644 --- a/src/GitVersionCore/Configuration/ConfigurationProvider.cs +++ b/src/GitVersionCore/Configuration/ConfigurationProvider.cs @@ -88,6 +88,7 @@ public static void ApplyDefaultsTo(Config config) config.AssemblyVersioningScheme = config.AssemblyVersioningScheme ?? AssemblyVersioningScheme.MajorMinorPatch; config.AssemblyFileVersioningScheme = config.AssemblyFileVersioningScheme ?? AssemblyFileVersioningScheme.MajorMinorPatch; config.AssemblyInformationalFormat = config.AssemblyInformationalFormat; + config.AssemblyVersioningFormat = config.AssemblyVersioningFormat; config.AssemblyFileVersioningFormat = config.AssemblyFileVersioningFormat; config.TagPrefix = config.TagPrefix ?? DefaultTagPrefix; config.VersioningMode = config.VersioningMode ?? VersioningMode.ContinuousDelivery; diff --git a/src/GitVersionCore/EffectiveConfiguration.cs b/src/GitVersionCore/EffectiveConfiguration.cs index 732fc0e252..abf2cd9aec 100644 --- a/src/GitVersionCore/EffectiveConfiguration.cs +++ b/src/GitVersionCore/EffectiveConfiguration.cs @@ -12,6 +12,7 @@ public EffectiveConfiguration( AssemblyVersioningScheme assemblyVersioningScheme, AssemblyFileVersioningScheme assemblyFileVersioningScheme, string assemblyInformationalFormat, + string assemblyVersioningFormat, string assemblyFileVersioningFormat, VersioningMode versioningMode, string gitTagPrefix, string tag, string nextVersion, IncrementStrategy increment, @@ -36,6 +37,7 @@ public EffectiveConfiguration( AssemblyVersioningScheme = assemblyVersioningScheme; AssemblyFileVersioningScheme = assemblyFileVersioningScheme; AssemblyInformationalFormat = assemblyInformationalFormat; + AssemblyVersioningFormat = assemblyVersioningFormat; AssemblyFileVersioningFormat = assemblyFileVersioningFormat; VersioningMode = versioningMode; GitTagPrefix = gitTagPrefix; @@ -69,6 +71,7 @@ public EffectiveConfiguration( public AssemblyVersioningScheme AssemblyVersioningScheme { get; private set; } public AssemblyFileVersioningScheme AssemblyFileVersioningScheme { get; private set; } public string AssemblyInformationalFormat { get; private set; } + public string AssemblyVersioningFormat { get; private set; } public string AssemblyFileVersioningFormat { get; private set; } /// diff --git a/src/GitVersionCore/GitVersionContext.cs b/src/GitVersionCore/GitVersionContext.cs index 2ce7cf0acf..4478d330b4 100644 --- a/src/GitVersionCore/GitVersionContext.cs +++ b/src/GitVersionCore/GitVersionContext.cs @@ -122,6 +122,7 @@ void CalculateEffectiveConfiguration() var assemblyVersioningScheme = FullConfiguration.AssemblyVersioningScheme.Value; var assemblyFileVersioningScheme = FullConfiguration.AssemblyFileVersioningScheme.Value; var assemblyInformationalFormat = FullConfiguration.AssemblyInformationalFormat; + var assemblyVersioningFormat = FullConfiguration.AssemblyVersioningFormat; var assemblyFileVersioningFormat = FullConfiguration.AssemblyFileVersioningFormat; var gitTagPrefix = FullConfiguration.TagPrefix; var majorMessage = FullConfiguration.MajorVersionBumpMessage; @@ -133,7 +134,7 @@ void CalculateEffectiveConfiguration() var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? FullConfiguration.CommitMessageIncrementing.Value; Configuration = new EffectiveConfiguration( - assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, + assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, tag, nextVersion, incrementStrategy, currentBranchConfig.Regex, preventIncrementForMergedBranchVersion, diff --git a/src/GitVersionCore/OutputVariables/VariableProvider.cs b/src/GitVersionCore/OutputVariables/VariableProvider.cs index ace1013b7a..b7832d91a9 100644 --- a/src/GitVersionCore/OutputVariables/VariableProvider.cs +++ b/src/GitVersionCore/OutputVariables/VariableProvider.cs @@ -44,43 +44,16 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config); string informationalVersion; + CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues, semverFormatValues.DefaultInformationalVersion, + "AssemblyInformationalVersion", out informationalVersion); - if (string.IsNullOrEmpty(config.AssemblyInformationalFormat)) - { - informationalVersion = semverFormatValues.DefaultInformationalVersion; - } - else - { - try - { - informationalVersion = config.AssemblyInformationalFormat.FormatWith(semverFormatValues); - } - catch (ArgumentException formex) - { - throw new WarningException(string.Format("Unable to format AssemblyInformationalVersion. Check your format string: {0}", formex.Message)); - } - } - - string assemblyFileVersioningFormat; string assemblyFileSemVer; + CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues, semverFormatValues.AssemblyFileSemVer, + "AssemblyFileVersioningFormat", out assemblyFileSemVer); - if (!(string.IsNullOrEmpty(config.AssemblyFileVersioningFormat))) - { - //assembly-file-versioning-format value if provided in the config, overwrites the exisiting AssemblyFileSemVer - try - { - assemblyFileVersioningFormat = config.AssemblyFileVersioningFormat.FormatWith(semverFormatValues); - assemblyFileSemVer = assemblyFileVersioningFormat; - } - catch (ArgumentException formex) - { - throw new WarningException(string.Format("Unable to format AssemblyFileVersioningFormat. Check your format string: {0}", formex.Message)); - } - } - else - { - assemblyFileSemVer = semverFormatValues.AssemblyFileSemVer; - } + string assemblySemVer; + CheckAndFormatString(config.AssemblyVersioningFormat, semverFormatValues, semverFormatValues.AssemblySemVer, + "AssemblyVersioningFormat", out assemblySemVer); var variables = new VersionVariables( semverFormatValues.Major, @@ -96,7 +69,7 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, semverFormatValues.LegacySemVer, semverFormatValues.LegacySemVerPadded, semverFormatValues.FullSemVer, - semverFormatValues.AssemblySemVer, + assemblySemVer, assemblyFileSemVer, semverFormatValues.PreReleaseTag, semverFormatValues.PreReleaseTagWithDash, @@ -121,5 +94,24 @@ static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion) semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag ?? 0; semanticVersion.BuildMetaData.CommitsSinceTag = null; } + + static void CheckAndFormatString(string formatString, T source, string defaultValue, string formatVarName, out string formattedString) + { + if (string.IsNullOrEmpty(formatString)) + { + formattedString = defaultValue; + } + else + { + try + { + formattedString = formatString.FormatWith(source); + } + catch (ArgumentException formex) + { + throw new WarningException(string.Format("Unable to format {0}. Check your format string: {1}", formatVarName, formex.Message)); + } + } + } } } \ No newline at end of file From 9f783661937f625ba74661ce0c4f15cf703fa439 Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Thu, 22 Mar 2018 15:42:07 +0100 Subject: [PATCH 5/7] Changed the environment variable indicator from $ to env:, updated the tests and the docs --- docs/configuration.md | 6 +++--- .../StringFormatWithExtensionTests.cs | 21 ++++++++++--------- src/GitVersionCore/StringFormatWith.cs | 10 +++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 296166f6b4..f2a6f02c18 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -74,10 +74,10 @@ attributes. Valid values: `MajorMinorPatchTag`, `MajorMinorPatch`, `MajorMinor`, ### assembly-file-versioning-format Set this to any of the available [variables](/more-info/variables) in combination (but not necessary) with a process scoped environment variable. It overwrites the value of `assembly-file-versioning-scheme`. To reference -an environment variable, use `$` -Example Syntax #1: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER` +an environment variable, use `env:` +Example Syntax #1: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER` if available in the environment otherwise the `fallback_string` -Example Syntax #2: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER` +Example Syntax #2: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER` if available in the environment otherwise the parsing fails. String interpolation is supported as in `assembly-informational-format` diff --git a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs index 1d77278c33..f9b69595ea 100644 --- a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs +++ b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs @@ -44,7 +44,7 @@ public void FormatWith_EnvVarToken() { Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value"); var propertyObject = new { }; - var target = "{$GIT_VERSION_TEST_VAR}"; + var target = "{env:GIT_VERSION_TEST_VAR}"; var expected = "Env Var Value"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -55,7 +55,7 @@ public void FormatWith_EnvVarTokenWithFallback() { Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value"); var propertyObject = new { }; - var target = "{$GIT_VERSION_TEST_VAR??fallback}"; + var target = "{env:GIT_VERSION_TEST_VAR??fallback}"; var expected = "Env Var Value"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -66,7 +66,7 @@ public void FormatWith_UnsetEnvVarTokenWithFallback() { Environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null); var propertyObject = new { }; - var target = "{$GIT_VERSION_UNSET_TEST_VAR??fallback}"; + var target = "{env:GIT_VERSION_UNSET_TEST_VAR??fallback}"; var expected = "fallback"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -78,7 +78,7 @@ public void FormatWith_MultipleEnvVars() Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_1", "Val-1"); Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_2", "Val-2"); var propertyObject = new { }; - var target = "{$GIT_VERSION_TEST_VAR_1} and {$GIT_VERSION_TEST_VAR_2}"; + var target = "{env:GIT_VERSION_TEST_VAR_1} and {env:GIT_VERSION_TEST_VAR_2}"; var expected = "Val-1 and Val-2"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -88,9 +88,9 @@ public void FormatWith_MultipleEnvVars() public void FormatWith_MultipleEnvChars() { var propertyObject = new { }; - //Test the greediness of the regex in matching $ char - var target = "{$$GIT_VERSION_TEST_VAR_1} and {$DUMMY_VAR??fallback}"; - var expected = "{$$GIT_VERSION_TEST_VAR_1} and fallback"; + //Test the greediness of the regex in matching env: char + var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR??fallback}"; + var expected = "{env:env:GIT_VERSION_TEST_VAR_1} and fallback"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); } @@ -99,8 +99,8 @@ public void FormatWith_MultipleEnvChars() public void FormatWith_MultipleFallbackChars() { var propertyObject = new { }; - //Test the greediness of the regex in matching $ and ?? chars - var target = "{$$GIT_VERSION_TEST_VAR_1} and {$DUMMY_VAR???fallback}"; + //Test the greediness of the regex in matching env: and ?? chars + var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR???fallback}"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(target, actual); } @@ -108,9 +108,10 @@ public void FormatWith_MultipleFallbackChars() [Test] public void FormatWith_SingleFallbackChar() { + Environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "Dummy-Val"); var propertyObject = new { }; //Test the sanity of the regex when there is a grammar mismatch - var target = "SomeNumbers and {$DUMMY_VAR?fallback}"; + var target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR?fallback}"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(target, actual); } diff --git a/src/GitVersionCore/StringFormatWith.cs b/src/GitVersionCore/StringFormatWith.cs index 78e11c5dae..1653910fa1 100644 --- a/src/GitVersionCore/StringFormatWith.cs +++ b/src/GitVersionCore/StringFormatWith.cs @@ -8,7 +8,7 @@ namespace GitVersion static class StringFormatWithExtension { - private static readonly Regex TokensRegex = new Regex(@"{\$??\w+(\?\?\w+)??}", RegexOptions.Compiled); + private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\?\?\w+)??}", RegexOptions.Compiled); /// /// Formats a string template with the given source object. @@ -27,16 +27,18 @@ public static string FormatWith(this string template, T source) // {MajorMinorPatch}+{Branch} var objType = source.GetType(); + const string ENV_VAR_INDICATOR = "env:"; foreach (Match match in TokensRegex.Matches(template)) { var memberAccessExpression = TrimBraces(match.Value); string propertyValue = null; // Support evaluation of environment variables in the format string - // For example: {$JENKINS_BUILD_NUMBER??fall-back-string} - if (memberAccessExpression.StartsWith("$")) + // For example: {env:JENKINS_BUILD_NUMBER??fall-back-string} + + if (memberAccessExpression.StartsWith(ENV_VAR_INDICATOR)) { - memberAccessExpression = memberAccessExpression.TrimStart('$').TrimEnd('$'); + memberAccessExpression = memberAccessExpression.TrimStart(ENV_VAR_INDICATOR.ToCharArray()).TrimEnd(ENV_VAR_INDICATOR.ToCharArray()); string envVar = memberAccessExpression, fallback = null; string[] components = (memberAccessExpression.Contains("??")) ? memberAccessExpression.Split(new string[] { "??" }, StringSplitOptions.None) : null; if (components != null) From 2642ef1263a9856d126d9041b4d9184a9b3e46ab Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Fri, 23 Mar 2018 15:39:32 +0100 Subject: [PATCH 6/7] Updated the regex to include spaces around null propagation operator for readability, updated the tests & docs --- docs/configuration.md | 2 +- .../StringFormatWithExtensionTests.cs | 20 ++++++++++++++----- src/GitVersionCore/StringFormatWith.cs | 6 +++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index f2a6f02c18..c58116bb93 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -75,7 +75,7 @@ attributes. Valid values: `MajorMinorPatchTag`, `MajorMinorPatch`, `MajorMinor`, Set this to any of the available [variables](/more-info/variables) in combination (but not necessary) with a process scoped environment variable. It overwrites the value of `assembly-file-versioning-scheme`. To reference an environment variable, use `env:` -Example Syntax #1: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER` +Example Syntax #1: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER ?? fallback_string}'`. Uses `JENKINS_BUILD_NUMBER` if available in the environment otherwise the `fallback_string` Example Syntax #2: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER` if available in the environment otherwise the parsing fails. diff --git a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs index f9b69595ea..1981e192fb 100644 --- a/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs +++ b/src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs @@ -55,7 +55,7 @@ public void FormatWith_EnvVarTokenWithFallback() { Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value"); var propertyObject = new { }; - var target = "{env:GIT_VERSION_TEST_VAR??fallback}"; + var target = "{env:GIT_VERSION_TEST_VAR ?? fallback}"; var expected = "Env Var Value"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -66,7 +66,7 @@ public void FormatWith_UnsetEnvVarTokenWithFallback() { Environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null); var propertyObject = new { }; - var target = "{env:GIT_VERSION_UNSET_TEST_VAR??fallback}"; + var target = "{env:GIT_VERSION_UNSET_TEST_VAR ?? fallback}"; var expected = "fallback"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -89,7 +89,7 @@ public void FormatWith_MultipleEnvChars() { var propertyObject = new { }; //Test the greediness of the regex in matching env: char - var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR??fallback}"; + var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ?? fallback}"; var expected = "{env:env:GIT_VERSION_TEST_VAR_1} and fallback"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(expected, actual); @@ -100,7 +100,7 @@ public void FormatWith_MultipleFallbackChars() { var propertyObject = new { }; //Test the greediness of the regex in matching env: and ?? chars - var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR???fallback}"; + var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ??? fallback}"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(target, actual); } @@ -111,9 +111,19 @@ public void FormatWith_SingleFallbackChar() Environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "Dummy-Val"); var propertyObject = new { }; //Test the sanity of the regex when there is a grammar mismatch - var target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR?fallback}"; + var target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR??fallback}"; var actual = target.FormatWith(propertyObject); Assert.AreEqual(target, actual); } + + [Test] + public void FormatWIth_NullPropagationWithMultipleSpaces() + { + var propertyObject = new { SomeProperty = "Some Value" }; + var target = "{SomeProperty} and {env:DUMMY_ENV_VAR ?? fallback}"; + var expected = "Some Value and fallback"; + var actual = target.FormatWith(propertyObject); + Assert.AreEqual(expected, actual); + } } } diff --git a/src/GitVersionCore/StringFormatWith.cs b/src/GitVersionCore/StringFormatWith.cs index 1653910fa1..6289f5a642 100644 --- a/src/GitVersionCore/StringFormatWith.cs +++ b/src/GitVersionCore/StringFormatWith.cs @@ -8,7 +8,7 @@ namespace GitVersion static class StringFormatWithExtension { - private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\?\?\w+)??}", RegexOptions.Compiled); + private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\s+(\?\?)??\s+\w+)??}", RegexOptions.Compiled); /// /// Formats a string template with the given source object. @@ -43,8 +43,8 @@ public static string FormatWith(this string template, T source) string[] components = (memberAccessExpression.Contains("??")) ? memberAccessExpression.Split(new string[] { "??" }, StringSplitOptions.None) : null; if (components != null) { - envVar = components[0]; - fallback = components[1]; + envVar = components[0].Trim(); + fallback = components[1].Trim(); } propertyValue = Helpers.EnvironmentHelper.GetEnvironmentVariableForProcess(envVar); From 588ccadde3c6b2b705a05f304fa62f06c8502195 Mon Sep 17 00:00:00 2001 From: Ruh Ullah Shah Date: Mon, 26 Mar 2018 15:10:53 +0200 Subject: [PATCH 7/7] Added named group for env: in the FormatWith Regex, removed out parameter from CheckAndFormatString --- .../OutputVariables/VariableProvider.cs | 21 ++++++++++--------- src/GitVersionCore/StringFormatWith.cs | 9 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/GitVersionCore/OutputVariables/VariableProvider.cs b/src/GitVersionCore/OutputVariables/VariableProvider.cs index b7832d91a9..5781041b9e 100644 --- a/src/GitVersionCore/OutputVariables/VariableProvider.cs +++ b/src/GitVersionCore/OutputVariables/VariableProvider.cs @@ -43,17 +43,14 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config); - string informationalVersion; - CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues, semverFormatValues.DefaultInformationalVersion, - "AssemblyInformationalVersion", out informationalVersion); + string informationalVersion = CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues, + semverFormatValues.DefaultInformationalVersion, "AssemblyInformationalVersion"); - string assemblyFileSemVer; - CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues, semverFormatValues.AssemblyFileSemVer, - "AssemblyFileVersioningFormat", out assemblyFileSemVer); + string assemblyFileSemVer = CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues, + semverFormatValues.AssemblyFileSemVer, "AssemblyFileVersioningFormat"); - string assemblySemVer; - CheckAndFormatString(config.AssemblyVersioningFormat, semverFormatValues, semverFormatValues.AssemblySemVer, - "AssemblyVersioningFormat", out assemblySemVer); + string assemblySemVer = CheckAndFormatString(config.AssemblyVersioningFormat, semverFormatValues, + semverFormatValues.AssemblySemVer, "AssemblyVersioningFormat"); var variables = new VersionVariables( semverFormatValues.Major, @@ -95,8 +92,10 @@ static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion) semanticVersion.BuildMetaData.CommitsSinceTag = null; } - static void CheckAndFormatString(string formatString, T source, string defaultValue, string formatVarName, out string formattedString) + static string CheckAndFormatString(string formatString, T source, string defaultValue, string formatVarName) { + string formattedString; + if (string.IsNullOrEmpty(formatString)) { formattedString = defaultValue; @@ -112,6 +111,8 @@ static void CheckAndFormatString(string formatString, T source, string defau throw new WarningException(string.Format("Unable to format {0}. Check your format string: {1}", formatVarName, formex.Message)); } } + + return formattedString; } } } \ No newline at end of file diff --git a/src/GitVersionCore/StringFormatWith.cs b/src/GitVersionCore/StringFormatWith.cs index 6289f5a642..693eb5b814 100644 --- a/src/GitVersionCore/StringFormatWith.cs +++ b/src/GitVersionCore/StringFormatWith.cs @@ -8,7 +8,7 @@ namespace GitVersion static class StringFormatWithExtension { - private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\s+(\?\?)??\s+\w+)??}", RegexOptions.Compiled); + private static readonly Regex TokensRegex = new Regex(@"{(?env:)??\w+(\s+(\?\?)??\s+\w+)??}", RegexOptions.Compiled); /// /// Formats a string template with the given source object. @@ -27,18 +27,17 @@ public static string FormatWith(this string template, T source) // {MajorMinorPatch}+{Branch} var objType = source.GetType(); - const string ENV_VAR_INDICATOR = "env:"; foreach (Match match in TokensRegex.Matches(template)) { var memberAccessExpression = TrimBraces(match.Value); string propertyValue = null; // Support evaluation of environment variables in the format string - // For example: {env:JENKINS_BUILD_NUMBER??fall-back-string} + // For example: {env:JENKINS_BUILD_NUMBER ?? fall-back-string} - if (memberAccessExpression.StartsWith(ENV_VAR_INDICATOR)) + if (match.Groups["env"].Success) { - memberAccessExpression = memberAccessExpression.TrimStart(ENV_VAR_INDICATOR.ToCharArray()).TrimEnd(ENV_VAR_INDICATOR.ToCharArray()); + memberAccessExpression = memberAccessExpression.Substring(memberAccessExpression.IndexOf(':') + 1); string envVar = memberAccessExpression, fallback = null; string[] components = (memberAccessExpression.Contains("??")) ? memberAccessExpression.Split(new string[] { "??" }, StringSplitOptions.None) : null; if (components != null)