Skip to content

Commit 9f78366

Browse files
committed
Changed the environment variable indicator from $ to env:, updated the tests and the docs
1 parent 42ba797 commit 9f78366

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

docs/configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ attributes. Valid values: `MajorMinorPatchTag`, `MajorMinorPatch`, `MajorMinor`,
7474
### assembly-file-versioning-format
7575
Set this to any of the available [variables](/more-info/variables) in combination (but not necessary) with
7676
a process scoped environment variable. It overwrites the value of `assembly-file-versioning-scheme`. To reference
77-
an environment variable, use `$`
78-
Example Syntax #1: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER`
77+
an environment variable, use `env:`
78+
Example Syntax #1: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER`
7979
if available in the environment otherwise the `fallback_string`
80-
Example Syntax #2: `'{Major}.{Minor}.{Patch}.{$JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER`
80+
Example Syntax #2: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER`
8181
if available in the environment otherwise the parsing fails.
8282
String interpolation is supported as in `assembly-informational-format`
8383

src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void FormatWith_EnvVarToken()
4444
{
4545
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
4646
var propertyObject = new { };
47-
var target = "{$GIT_VERSION_TEST_VAR}";
47+
var target = "{env:GIT_VERSION_TEST_VAR}";
4848
var expected = "Env Var Value";
4949
var actual = target.FormatWith(propertyObject);
5050
Assert.AreEqual(expected, actual);
@@ -55,7 +55,7 @@ public void FormatWith_EnvVarTokenWithFallback()
5555
{
5656
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
5757
var propertyObject = new { };
58-
var target = "{$GIT_VERSION_TEST_VAR??fallback}";
58+
var target = "{env:GIT_VERSION_TEST_VAR??fallback}";
5959
var expected = "Env Var Value";
6060
var actual = target.FormatWith(propertyObject);
6161
Assert.AreEqual(expected, actual);
@@ -66,7 +66,7 @@ public void FormatWith_UnsetEnvVarTokenWithFallback()
6666
{
6767
Environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null);
6868
var propertyObject = new { };
69-
var target = "{$GIT_VERSION_UNSET_TEST_VAR??fallback}";
69+
var target = "{env:GIT_VERSION_UNSET_TEST_VAR??fallback}";
7070
var expected = "fallback";
7171
var actual = target.FormatWith(propertyObject);
7272
Assert.AreEqual(expected, actual);
@@ -78,7 +78,7 @@ public void FormatWith_MultipleEnvVars()
7878
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_1", "Val-1");
7979
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_2", "Val-2");
8080
var propertyObject = new { };
81-
var target = "{$GIT_VERSION_TEST_VAR_1} and {$GIT_VERSION_TEST_VAR_2}";
81+
var target = "{env:GIT_VERSION_TEST_VAR_1} and {env:GIT_VERSION_TEST_VAR_2}";
8282
var expected = "Val-1 and Val-2";
8383
var actual = target.FormatWith(propertyObject);
8484
Assert.AreEqual(expected, actual);
@@ -88,9 +88,9 @@ public void FormatWith_MultipleEnvVars()
8888
public void FormatWith_MultipleEnvChars()
8989
{
9090
var propertyObject = new { };
91-
//Test the greediness of the regex in matching $ char
92-
var target = "{$$GIT_VERSION_TEST_VAR_1} and {$DUMMY_VAR??fallback}";
93-
var expected = "{$$GIT_VERSION_TEST_VAR_1} and fallback";
91+
//Test the greediness of the regex in matching env: char
92+
var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR??fallback}";
93+
var expected = "{env:env:GIT_VERSION_TEST_VAR_1} and fallback";
9494
var actual = target.FormatWith(propertyObject);
9595
Assert.AreEqual(expected, actual);
9696
}
@@ -99,18 +99,19 @@ public void FormatWith_MultipleEnvChars()
9999
public void FormatWith_MultipleFallbackChars()
100100
{
101101
var propertyObject = new { };
102-
//Test the greediness of the regex in matching $ and ?? chars
103-
var target = "{$$GIT_VERSION_TEST_VAR_1} and {$DUMMY_VAR???fallback}";
102+
//Test the greediness of the regex in matching env: and ?? chars
103+
var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR???fallback}";
104104
var actual = target.FormatWith(propertyObject);
105105
Assert.AreEqual(target, actual);
106106
}
107107

108108
[Test]
109109
public void FormatWith_SingleFallbackChar()
110110
{
111+
Environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "Dummy-Val");
111112
var propertyObject = new { };
112113
//Test the sanity of the regex when there is a grammar mismatch
113-
var target = "SomeNumbers and {$DUMMY_VAR?fallback}";
114+
var target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR?fallback}";
114115
var actual = target.FormatWith(propertyObject);
115116
Assert.AreEqual(target, actual);
116117
}

src/GitVersionCore/StringFormatWith.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitVersion
88

99
static class StringFormatWithExtension
1010
{
11-
private static readonly Regex TokensRegex = new Regex(@"{\$??\w+(\?\?\w+)??}", RegexOptions.Compiled);
11+
private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\?\?\w+)??}", RegexOptions.Compiled);
1212

1313
/// <summary>
1414
/// Formats a string template with the given source object.
@@ -27,16 +27,18 @@ public static string FormatWith<T>(this string template, T source)
2727

2828
// {MajorMinorPatch}+{Branch}
2929
var objType = source.GetType();
30+
const string ENV_VAR_INDICATOR = "env:";
3031
foreach (Match match in TokensRegex.Matches(template))
3132
{
3233
var memberAccessExpression = TrimBraces(match.Value);
3334
string propertyValue = null;
3435

3536
// Support evaluation of environment variables in the format string
36-
// For example: {$JENKINS_BUILD_NUMBER??fall-back-string}
37-
if (memberAccessExpression.StartsWith("$"))
37+
// For example: {env:JENKINS_BUILD_NUMBER??fall-back-string}
38+
39+
if (memberAccessExpression.StartsWith(ENV_VAR_INDICATOR))
3840
{
39-
memberAccessExpression = memberAccessExpression.TrimStart('$').TrimEnd('$');
41+
memberAccessExpression = memberAccessExpression.TrimStart(ENV_VAR_INDICATOR.ToCharArray()).TrimEnd(ENV_VAR_INDICATOR.ToCharArray());
4042
string envVar = memberAccessExpression, fallback = null;
4143
string[] components = (memberAccessExpression.Contains("??")) ? memberAccessExpression.Split(new string[] { "??" }, StringSplitOptions.None) : null;
4244
if (components != null)

0 commit comments

Comments
 (0)