Skip to content

Add assembly-file-versioning-format with support for environment variables #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ 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 `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}.{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`

### 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
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/CommitDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IVersionFilter>(), false, true, format)
);

Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
<Compile Include="Helpers\PathHelper.cs" />
<Compile Include="IntegrationTests\MasterScenarios.cs" />
<Compile Include="SemanticVersionTests.cs" />
<Compile Include="StringFormatWithExtensionTests.cs" />
<Compile Include="TestableVersionVariables.cs" />
<Compile Include="TestEffectiveConfiguration.cs" />
<Compile Include="TestFileSystem.cs" />
Expand Down
129 changes: 129 additions & 0 deletions src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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 = "{env: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 = "{env: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 = "{env:GIT_VERSION_UNSET_TEST_VAR ?? fallback}";
var expected = "fallback";
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 = "{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);
}

[Test]
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 expected = "{env:env: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 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);
}

[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 = "{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);
}
}
}
4 changes: 3 additions & 1 deletion src/GitVersionCore.Tests/TestEffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class TestEffectiveConfiguration : EffectiveConfiguration
public TestEffectiveConfiguration(
AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
AssemblyFileVersioningScheme assemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch,
string assemblyVersioningFormat = null,
string assemblyFileVersioningFormat = null,
string assemblyInformationalFormat = null,
VersioningMode versioningMode = VersioningMode.ContinuousDelivery,
string gitTagPrefix = "v",
Expand All @@ -32,7 +34,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, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag,
trackMergeTarget,
majorMessage, minorMessage, patchMessage, noBumpMessage,
Expand Down
6 changes: 6 additions & 0 deletions src/GitVersionCore/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ 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; }

[YamlMember(Alias = "mode")]
public VersioningMode? VersioningMode { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions src/GitVersionCore/Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ 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;
config.ContinuousDeploymentFallbackTag = config.ContinuousDeploymentFallbackTag ?? "ci";
Expand Down
6 changes: 6 additions & 0 deletions src/GitVersionCore/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public EffectiveConfiguration(
AssemblyVersioningScheme assemblyVersioningScheme,
AssemblyFileVersioningScheme assemblyFileVersioningScheme,
string assemblyInformationalFormat,
string assemblyVersioningFormat,
string assemblyFileVersioningFormat,
VersioningMode versioningMode, string gitTagPrefix,
string tag, string nextVersion, IncrementStrategy increment,
string branchPrefixToTrim,
Expand All @@ -35,6 +37,8 @@ public EffectiveConfiguration(
AssemblyVersioningScheme = assemblyVersioningScheme;
AssemblyFileVersioningScheme = assemblyFileVersioningScheme;
AssemblyInformationalFormat = assemblyInformationalFormat;
AssemblyVersioningFormat = assemblyVersioningFormat;
AssemblyFileVersioningFormat = assemblyFileVersioningFormat;
VersioningMode = versioningMode;
GitTagPrefix = gitTagPrefix;
Tag = tag;
Expand Down Expand Up @@ -67,6 +71,8 @@ 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; }

/// <summary>
/// Git tag prefix
Expand Down
4 changes: 3 additions & 1 deletion src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ 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;
var minorMessage = FullConfiguration.MinorVersionBumpMessage;
Expand All @@ -132,7 +134,7 @@ void CalculateEffectiveConfiguration()
var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? FullConfiguration.CommitMessageIncrementing.Value;

Configuration = new EffectiveConfiguration(
assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix,
assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix,
tag, nextVersion, incrementStrategy,
currentBranchConfig.Regex,
preventIncrementForMergedBranchVersion,
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<Compile Include="GitVersionException.cs" />
<Compile Include="GitVersionInformationResources\GitVersionInformationGenerator.cs" />
<Compile Include="Helpers\EncodingHelper.cs" />
<Compile Include="Helpers\EnvironmentHelper.cs" />
<Compile Include="Helpers\FileSystem.cs" />
<Compile Include="Helpers\IFileSystem.cs" />
<Compile Include="Helpers\IThreadSleep.cs" />
Expand Down Expand Up @@ -232,4 +233,4 @@
</Target>
<Import Project="..\packages\PepitaPackage.1.21.4\build\PepitaPackage.targets" Condition="Exists('..\packages\PepitaPackage.1.21.4\build\PepitaPackage.targets')" />
<Import Project="..\packages\Fody.2.0.8\build\portable-net+sl+win+wpa+wp\Fody.targets" Condition="Exists('..\packages\Fody.2.0.8\build\portable-net+sl+win+wpa+wp\Fody.targets')" />
</Project>
</Project>
12 changes: 12 additions & 0 deletions src/GitVersionCore/Helpers/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace GitVersion.Helpers
{
public class EnvironmentHelper
{
public static string GetEnvironmentVariableForProcess(string envVar)
{
return Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Process);
}
}
}
50 changes: 32 additions & 18 deletions src/GitVersionCore/OutputVariables/VariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,14 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion,

var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config);

string informationalVersion;
string informationalVersion = CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues,
semverFormatValues.DefaultInformationalVersion, "AssemblyInformationalVersion");

if (string.IsNullOrEmpty(config.AssemblyInformationalFormat))
{
informationalVersion = semverFormatValues.DefaultInformationalVersion;
}
else
{
try
{
informationalVersion = config.AssemblyInformationalFormat.FormatWith<SemanticVersionFormatValues>(semverFormatValues);
}
catch (ArgumentException formex)
{
throw new WarningException(string.Format("Unable to format AssemblyInformationalVersion. Check your format string: {0}", formex.Message));
}
}
string assemblyFileSemVer = CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues,
semverFormatValues.AssemblyFileSemVer, "AssemblyFileVersioningFormat");

string assemblySemVer = CheckAndFormatString(config.AssemblyVersioningFormat, semverFormatValues,
semverFormatValues.AssemblySemVer, "AssemblyVersioningFormat");

var variables = new VersionVariables(
semverFormatValues.Major,
Expand All @@ -75,8 +66,8 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion,
semverFormatValues.LegacySemVer,
semverFormatValues.LegacySemVerPadded,
semverFormatValues.FullSemVer,
semverFormatValues.AssemblySemVer,
semverFormatValues.AssemblyFileSemVer,
assemblySemVer,
assemblyFileSemVer,
semverFormatValues.PreReleaseTag,
semverFormatValues.PreReleaseTagWithDash,
semverFormatValues.PreReleaseLabel,
Expand All @@ -100,5 +91,28 @@ static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion)
semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag ?? 0;
semanticVersion.BuildMetaData.CommitsSinceTag = null;
}

static string CheckAndFormatString<T>(string formatString, T source, string defaultValue, string formatVarName)
{
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));
}
}

return formattedString;
}
}
}
Loading