Skip to content
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

Add support and tests for AssemblyInformationalVersion configurability #660

Merged
merged 3 commits into from
Oct 17, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions src/GitVersionCore.Tests/ConfigProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void CanReadDocumentAndMigrate()

var config = ConfigurationProvider.Provide(repoPath, fileSystem);
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
config.AssemblyInformationalFormat.ShouldBe(null);
config.NextVersion.ShouldBe("2.0.0");
config.TagPrefix.ShouldBe("[vV|version-]");
config.VersioningMode.ShouldBe(VersioningMode.ContinuousDelivery);
Expand Down Expand Up @@ -110,13 +111,28 @@ public void CanWriteOutEffectiveConfiguration()
Approvals.Verify(config);
}

[Test]
public void CanUpdateAssemblyInformationalVersioningScheme()
{
const string text = @"
assembly-versioning-scheme: MajorMinor
assembly-informational-format: '{NugetVersion}'";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to see a test which uses multiple format values. i.e '{Major}.{Minor}.{Patch}'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


SetupConfigFileContent(text);

var config = ConfigurationProvider.Provide(repoPath, fileSystem);
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
config.AssemblyInformationalFormat.ShouldBe("{NugetVersion}");
}

[Test]
public void CanReadDefaultDocument()
{
const string text = "";
SetupConfigFileContent(text);
var config = ConfigurationProvider.Provide(repoPath, fileSystem);
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinorPatch);
config.AssemblyInformationalFormat.ShouldBe(null);
config.Branches["dev(elop)?(ment)?$"].Tag.ShouldBe("unstable");
config.Branches["releases?[/-]"].Tag.ShouldBe("beta");
config.TagPrefix.ShouldBe(ConfigurationProvider.DefaultTagPrefix);
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersionCore.Tests/TestEffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class TestEffectiveConfiguration : EffectiveConfiguration
{
public TestEffectiveConfiguration(
AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
string assemblyInformationalFormat = null,
VersioningMode versioningMode = VersioningMode.ContinuousDelivery,
string gitTagPrefix = "v",
string tag = "",
Expand All @@ -21,7 +22,7 @@ public TestEffectiveConfiguration(
CommitMessageIncrementMode commitMessageMode = CommitMessageIncrementMode.Enabled,
int legacySemVerPadding = 4,
int buildMetaDataPadding = 4) :
base(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
base(assemblyVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag,
trackMergeTarget,
majorMessage, minorMessage, patchMessage,
Expand Down
2 changes: 0 additions & 2 deletions src/GitVersionCore/AssemblyVersionsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public static string GetAssemblyVersion(
default:
throw new ArgumentException(string.Format("Unexpected value ({0}).", scheme), "scheme");
}

}

}
}
3 changes: 3 additions & 0 deletions src/GitVersionCore/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class Config
[YamlMember(Alias = "assembly-versioning-scheme")]
public AssemblyVersioningScheme? AssemblyVersioningScheme { get; set; }

[YamlMember(Alias = "assembly-informational-format")]
public string AssemblyInformationalFormat { get; set; }

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

Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore/Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static void ApplyDefaultsTo(Config config)
MigrateBranches(config);

config.AssemblyVersioningScheme = config.AssemblyVersioningScheme ?? AssemblyVersioningScheme.MajorMinorPatch;
config.AssemblyInformationalFormat = config.AssemblyInformationalFormat;
config.TagPrefix = config.TagPrefix ?? DefaultTagPrefix;
config.VersioningMode = config.VersioningMode ?? VersioningMode.ContinuousDelivery;
config.ContinuousDeploymentFallbackTag = config.ContinuousDeploymentFallbackTag ?? "ci";
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersionCore/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class EffectiveConfiguration
{
public EffectiveConfiguration(
AssemblyVersioningScheme assemblyVersioningScheme,
string assemblyInformationalFormat,
VersioningMode versioningMode, string gitTagPrefix,
string tag, string nextVersion, IncrementStrategy increment,
string branchPrefixToTrim,
Expand All @@ -23,6 +24,7 @@ int buildMetaDataPadding
)
{
AssemblyVersioningScheme = assemblyVersioningScheme;
AssemblyInformationalFormat = assemblyInformationalFormat;
VersioningMode = versioningMode;
GitTagPrefix = gitTagPrefix;
Tag = tag;
Expand All @@ -44,6 +46,7 @@ int buildMetaDataPadding
public VersioningMode VersioningMode { get; private set; }

public AssemblyVersioningScheme AssemblyVersioningScheme { get; private set; }
public string AssemblyInformationalFormat { get; private set; }

/// <summary>
/// Git tag prefix
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void CalculateEffectiveConfiguration()

var nextVersion = configuration.NextVersion;
var assemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value;
var assemblyInformationalFormat = configuration.AssemblyInformationalFormat;
var gitTagPrefix = configuration.TagPrefix;
var majorMessage = configuration.MajorVersionBumpMessage;
var minorMessage = configuration.MinorVersionBumpMessage;
Expand All @@ -111,7 +112,7 @@ void CalculateEffectiveConfiguration()
var commitMessageVersionBump = currentBranchConfig.Value.CommitMessageIncrementing ?? configuration.CommitMessageIncrementing.Value;

Configuration = new EffectiveConfiguration(
assemblyVersioningScheme, versioningMode, gitTagPrefix,
assemblyVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix,
tag, nextVersion, incrementStrategy, currentBranchConfig.Key,
preventIncrementForMergedBranchVersion,
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is needed from this reference. Am worried this could cause issues porting to .net core/mono which is happening in v4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will verify and report back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringFormatWith.cs needs them for the DataBinder class which can throw HttpException. This is using the code you recommended to do the formatting, which definitely works well. Another option might be to use C#6 string format inlining e.g. $"{foo} {bar}" instead of string.Format("{0} {1}", foo, bar). But i wasn't sure you wanted to require C#6 semantics for the code to compile.

try
{
if (string.IsNullOrEmpty(format))
{
return (DataBinder.Eval(source, expression) ?? "").ToString();
}
return DataBinder.Eval(source, expression, "{0:" + format + "}");
}
catch (HttpException)
{
throw new FormatException("Failed to format '" + expression + "'.");
}

<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -117,6 +118,8 @@
<Compile Include="OutputVariables\VersionVariables.cs" />
<Compile Include="Extensions\ExtensionMethods.git.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="SemanticVersionFormatValues.cs" />
<Compile Include="StringFormatWith.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculator.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\BaseVersion.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\ConfigNextVersionBaseVersionStrategy.cs" />
Expand Down
60 changes: 42 additions & 18 deletions src/GitVersionCore/OutputVariables/VariableProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace GitVersion
{
using System;

public static class VariableProvider
{
public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration config, bool isCurrentCommitTagged)
Expand All @@ -18,25 +20,47 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion,
semanticVersion.BuildMetaData.CommitsSinceTag = null;
}

var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config);

string informationalVersion;

if (string.IsNullOrEmpty(config.AssemblyInformationalFormat))
{
informationalVersion = semverFormatValues.DefaultInformationalVersion;
}
else
{
try
{
informationalVersion = config.AssemblyInformationalFormat.FormatWith(semverFormatValues);
}
catch (FormatException formex)
{
throw new WarningException(string.Format("Unable to format AssemblyInformationalVersion. Check your format string: {0}", formex.Message));
}
}

var variables = new VersionVariables(
major: semanticVersion.Major.ToString(),
minor: semanticVersion.Minor.ToString(),
patch: semanticVersion.Patch.ToString(),
preReleaseTag: semanticVersion.PreReleaseTag,
preReleaseTagWithDash: semanticVersion.PreReleaseTag.HasTag() ? "-" + semanticVersion.PreReleaseTag : null,
buildMetaData: semanticVersion.BuildMetaData,
buildMetaDataPadded: semanticVersion.BuildMetaData.ToString("p" + config.BuildMetaDataPadding),
fullBuildMetaData: semanticVersion.BuildMetaData.ToString("f"),
majorMinorPatch: string.Format("{0}.{1}.{2}", semanticVersion.Major, semanticVersion.Minor, semanticVersion.Patch),
semVer: semanticVersion.ToString(),
legacySemVer: semanticVersion.ToString("l"),
legacySemVerPadded: semanticVersion.ToString("lp" + config.LegacySemVerPadding),
assemblySemVer: semanticVersion.GetAssemblyVersion(config.AssemblyVersioningScheme),
fullSemVer: semanticVersion.ToString("f"),
informationalVersion: semanticVersion.ToString("i"),
branchName: semanticVersion.BuildMetaData.Branch,
sha: semanticVersion.BuildMetaData.Sha,
commitDate: semanticVersion.BuildMetaData.CommitDate.UtcDateTime.ToString("yyyy-MM-dd"));
semverFormatValues.Major,
semverFormatValues.Minor,
semverFormatValues.Patch,
semverFormatValues.BuildMetaData,
semverFormatValues.BuildMetaDataPadded,
semverFormatValues.FullBuildMetaData,
semverFormatValues.BranchName,
semverFormatValues.Sha,
semverFormatValues.MajorMinorPatch,
semverFormatValues.SemVer,
semverFormatValues.LegacySemVer,
semverFormatValues.LegacySemVerPadded,
semverFormatValues.FullSemVer,
semverFormatValues.AssemblySemVer,
semverFormatValues.PreReleaseTag,
semverFormatValues.PreReleaseTagWithDash,
informationalVersion,
semverFormatValues.CommitDate,
semverFormatValues.NuGetVersion,
semverFormatValues.NuGetVersionV2);

return variables;
}
Expand Down
11 changes: 5 additions & 6 deletions src/GitVersionCore/OutputVariables/VersionVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class VersionVariables : IEnumerable<KeyValuePair<string, string>>
{
public VersionVariables(string major, string minor, string patch, string buildMetaData, string buildMetaDataPadded, string fullBuildMetaData, string branchName, string sha, string majorMinorPatch, string semVer, string legacySemVer, string legacySemVerPadded, string fullSemVer, string assemblySemVer, string preReleaseTag, string preReleaseTagWithDash, string informationalVersion,
string commitDate)
string commitDate, string nugetVersion, string nugetVersionV2)
{
Major = major;
Minor = minor;
Expand All @@ -27,6 +27,8 @@ public VersionVariables(string major, string minor, string patch, string buildMe
PreReleaseTagWithDash = preReleaseTagWithDash;
InformationalVersion = informationalVersion;
CommitDate = commitDate;
NuGetVersion = nugetVersion;
NuGetVersionV2 = nugetVersionV2;
}

public string Major { get; private set; }
Expand All @@ -46,11 +48,8 @@ public VersionVariables(string major, string minor, string patch, string buildMe
public string InformationalVersion { get; private set; }
public string BranchName { get; private set; }
public string Sha { get; private set; }

// Synonyms
// TODO When NuGet 3 is released: public string NuGetVersionV3 { get { return ??; } }
public string NuGetVersionV2 { get { return LegacySemVerPadded.ToLower(); } }
public string NuGetVersion { get { return NuGetVersionV2; } }
public string NuGetVersionV2 { get; private set; }
public string NuGetVersion { get; private set; }

public static IEnumerable<string> AvailableVariables
{
Expand Down
116 changes: 116 additions & 0 deletions src/GitVersionCore/SemanticVersionFormatValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
namespace GitVersion
{
public class SemanticVersionFormatValues
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we use the VersionVariables class for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this class in order to generate the VersionVariables class since the types we're using int he formatting need to come from a constructed object. And if we exposed the VersionVariables object as what's valid for formatting, then someone could do {InformationalVersion} and get themselves into an infinite loop. So this gives us type safety of what's allowed to be formatted as well as getting the formatting done to construct the VersionVariables type. I agree it's redundant on most fields (since VersionVariables now just becomes a passthrough/holder for most of the values calculated in the SemanticVersionFormatValues class. Open to suggestions to minimize here if you have them.

{
readonly SemanticVersion _semver;
readonly EffectiveConfiguration _config;

public SemanticVersionFormatValues(SemanticVersion semver, EffectiveConfiguration config)
{
_semver = semver;
_config = config;
}

public string Major
{
get { return _semver.Major.ToString(); }
}

public string Minor
{
get { return _semver.Minor.ToString(); }
}

public string Patch
{
get { return _semver.Patch.ToString(); }
}

public string PreReleaseTag
{
get { return _semver.PreReleaseTag; }
}

public string PreReleaseTagWithDash
{
get { return _semver.PreReleaseTag.HasTag() ? "-" + _semver.PreReleaseTag : null; }
}

public string BuildMetaData
{
get { return _semver.BuildMetaData; }
}

public string BuildMetaDataPadded
{
get { return _semver.BuildMetaData.ToString("p" + _config.BuildMetaDataPadding); }
}

public string FullBuildMetaData
{
get { return _semver.BuildMetaData.ToString("f"); }
}

public string MajorMinorPatch
{
get { return string.Format("{0}.{1}.{2}", _semver.Major, _semver.Minor, _semver.Patch); }
}

public string SemVer
{
get { return _semver.ToString(); }
}

public string LegacySemVer
{
get { return _semver.ToString("l"); }
}

public string LegacySemVerPadded
{
get { return _semver.ToString("lp" + _config.LegacySemVerPadding); }
}

public string AssemblySemVer
{
get { return _semver.GetAssemblyVersion(_config.AssemblyVersioningScheme); }
}

public string FullSemVer
{
get { return _semver.ToString("f"); }
}

public string BranchName
{
get { return _semver.BuildMetaData.Branch; }
}

public string Sha
{
get { return _semver.BuildMetaData.Sha; }
}

public string CommitDate
{
get { return _semver.BuildMetaData.CommitDate.UtcDateTime.ToString("yyyy-MM-dd"); }
}

// TODO When NuGet 3 is released: public string NuGetVersionV3 { get { return ??; } }

public string NuGetVersionV2
{
get { return LegacySemVerPadded.ToLower(); }
}

public string NuGetVersion
{
get { return NuGetVersionV2; }
}

public string DefaultInformationalVersion
{
get { return _semver.ToString("i"); }
}
}
}
Loading