Skip to content

Commit

Permalink
replace default pre-release phase with default pre-release identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
adamralph committed Nov 23, 2022
1 parent e381cde commit 8276d73
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 96 deletions.
2 changes: 1 addition & 1 deletion MinVer.Lib/MajorMinor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace MinVer.Lib;

public class MajorMinor
{
public static MajorMinor Zero { get; } = new(0, 0);
public static MajorMinor Default { get; } = new(0, 0);

public MajorMinor(int major, int minor)
{
Expand Down
8 changes: 8 additions & 0 deletions MinVer.Lib/PreReleaseIdentifiers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;

namespace MinVer.Lib;

public static class PreReleaseIdentifiers
{
public static readonly IReadOnlyCollection<string> Default = new List<string> { "alpha", "0", };
}
14 changes: 7 additions & 7 deletions MinVer.Lib/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Version : SemanticVersion
private readonly List<string> preReleaseIdentifiers;
private readonly int height;

public Version(string defaultPreReleasePhase) : this(0, 0, 0, new List<string> { defaultPreReleasePhase, "0", }, 0, "") { }
public Version(IEnumerable<string> defaultPreReleaseIdentifiers) : this(0, 0, 0, defaultPreReleaseIdentifiers.ToList(), 0, "") { }

private Version(int major, int minor, int patch, List<string> preReleaseIdentifiers, int height, string buildMetadata) :
base(
Expand All @@ -31,22 +31,22 @@ private Version(int major, int minor, int patch, List<string> preReleaseIdentifi
public override string ToString(string format, IFormatProvider formatProvider) =>
$"{this.Major}.{this.Minor}.{this.Patch}{(string.IsNullOrEmpty(this.Release) ? "" : $"-{this.Release}")}{(string.IsNullOrEmpty(this.Metadata) ? "" : $"+{this.Metadata}")}";

public Version Satisfying(MajorMinor minMajorMinor, string defaultPreReleasePhase)
public Version Satisfying(MajorMinor minMajorMinor, IEnumerable<string> defaultPreReleaseIdentifiers)
{
minMajorMinor = minMajorMinor ?? throw new ArgumentNullException(nameof(minMajorMinor));

return minMajorMinor.Major < this.Major || (minMajorMinor.Major == this.Major && minMajorMinor.Minor <= this.Minor)
? this
: new Version(minMajorMinor.Major, minMajorMinor.Minor, 0, new List<string> { defaultPreReleasePhase, "0", }, this.height, this.Metadata);
: new Version(minMajorMinor.Major, minMajorMinor.Minor, 0, defaultPreReleaseIdentifiers.ToList(), this.height, this.Metadata);
}

public Version WithHeight(int newHeight, VersionPart autoIncrement, string defaultPreReleasePhase) =>
public Version WithHeight(int newHeight, VersionPart autoIncrement, IEnumerable<string> defaultPreReleaseIdentifiers) =>
this.preReleaseIdentifiers.Count == 0 && newHeight > 0
? autoIncrement switch
{
VersionPart.Major => new Version(this.Major + 1, 0, 0, new List<string> { defaultPreReleasePhase, "0", }, newHeight, ""),
VersionPart.Minor => new Version(this.Major, this.Minor + 1, 0, new List<string> { defaultPreReleasePhase, "0", }, newHeight, ""),
VersionPart.Patch => new Version(this.Major, this.Minor, this.Patch + 1, new List<string> { defaultPreReleasePhase, "0", }, newHeight, ""),
VersionPart.Major => new Version(this.Major + 1, 0, 0, defaultPreReleaseIdentifiers.ToList(), newHeight, ""),
VersionPart.Minor => new Version(this.Major, this.Minor + 1, 0, defaultPreReleaseIdentifiers.ToList(), newHeight, ""),
VersionPart.Patch => new Version(this.Major, this.Minor, this.Patch + 1, defaultPreReleaseIdentifiers.ToList(), newHeight, ""),
_ => throw new ArgumentOutOfRangeException(nameof(autoIncrement)),
}
: new Version(this.Major, this.Minor, this.Patch, this.preReleaseIdentifiers, newHeight, newHeight == 0 ? this.Metadata : "");
Expand Down
24 changes: 11 additions & 13 deletions MinVer.Lib/Versioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ namespace MinVer.Lib;

public static class Versioner
{
public static Version GetVersion(string workDir, string tagPrefix, MajorMinor minMajorMinor, string buildMeta, VersionPart autoIncrement, string defaultPreReleasePhase, bool ignoreHeight, ILogger log)
public static Version GetVersion(string workDir, string tagPrefix, MajorMinor minMajorMinor, string buildMeta, VersionPart autoIncrement, IEnumerable<string> defaultPreReleaseIdentifiers, bool ignoreHeight, ILogger log)
{
log = log ?? throw new ArgumentNullException(nameof(log));

defaultPreReleasePhase = string.IsNullOrEmpty(defaultPreReleasePhase)
? "alpha"
: defaultPreReleasePhase;
var defaultPreReleaseIdentifiersList = defaultPreReleaseIdentifiers.ToList();

var (version, height) = GetVersion(workDir, tagPrefix, defaultPreReleasePhase, log);
var (version, height) = GetVersion(workDir, tagPrefix, defaultPreReleaseIdentifiersList, log);

_ = height.HasValue && ignoreHeight && log.IsDebugEnabled && log.Debug("Ignoring height.");
version = !height.HasValue || ignoreHeight ? version : version.WithHeight(height.Value, autoIncrement, defaultPreReleasePhase);
version = !height.HasValue || ignoreHeight ? version : version.WithHeight(height.Value, autoIncrement, defaultPreReleaseIdentifiersList);

version = version.AddBuildMetadata(buildMeta);

var calculatedVersion = version.Satisfying(minMajorMinor, defaultPreReleasePhase);
var calculatedVersion = version.Satisfying(minMajorMinor, defaultPreReleaseIdentifiersList);

_ = calculatedVersion != version
? log.IsInfoEnabled && log.Info($"Bumping version to {calculatedVersion} to satisfy minimum major minor {minMajorMinor}.")
Expand All @@ -33,11 +31,11 @@ public static Version GetVersion(string workDir, string tagPrefix, MajorMinor mi
return calculatedVersion;
}

private static (Version Version, int? Height) GetVersion(string workDir, string tagPrefix, string defaultPreReleasePhase, ILogger log)
private static (Version Version, int? Height) GetVersion(string workDir, string tagPrefix, List<string> defaultPreReleaseIdentifiers, ILogger log)
{
if (!Git.IsWorkingDirectory(workDir, log))
{
var version = new Version(defaultPreReleasePhase);
var version = new Version(defaultPreReleaseIdentifiers);

_ = log.IsWarnEnabled && log.Warn(1001, $"'{workDir}' is not a valid Git working directory. Using default version {version}.");

Expand All @@ -46,7 +44,7 @@ private static (Version Version, int? Height) GetVersion(string workDir, string

if (!Git.TryGetHead(workDir, out var head, log))
{
var version = new Version(defaultPreReleasePhase);
var version = new Version(defaultPreReleaseIdentifiers);

_ = log.IsInfoEnabled && log.Info($"No commits found. Using default version {version}.");

Expand All @@ -55,7 +53,7 @@ private static (Version Version, int? Height) GetVersion(string workDir, string

var tags = Git.GetTags(workDir, log);

var orderedCandidates = GetCandidates(head, tags, tagPrefix, defaultPreReleasePhase, log)
var orderedCandidates = GetCandidates(head, tags, tagPrefix, defaultPreReleaseIdentifiers, log)
.OrderBy(candidate => candidate.Version)
.ThenByDescending(candidate => candidate.Index).ToList();

Expand All @@ -79,7 +77,7 @@ private static (Version Version, int? Height) GetVersion(string workDir, string
return (selectedCandidate.Version, selectedCandidate.Height);
}

private static List<Candidate> GetCandidates(Commit head, IEnumerable<(string Name, string Sha)> tags, string tagPrefix, string defaultPreReleasePhase, ILogger log)
private static List<Candidate> GetCandidates(Commit head, IEnumerable<(string Name, string Sha)> tags, string tagPrefix, List<string> defaultPreReleaseIdentifiers, ILogger log)
{
var tagsAndVersions = new List<(string Name, string Sha, Version Version)>();

Expand Down Expand Up @@ -135,7 +133,7 @@ private static List<Candidate> GetCandidates(Commit head, IEnumerable<(string Na

if (!item.Commit.Parents.Any())
{
candidates.Add(new Candidate(item.Commit, item.Height, "", new Version(defaultPreReleasePhase), candidates.Count));
candidates.Add(new Candidate(item.Commit, item.Height, "", new Version(defaultPreReleaseIdentifiers), candidates.Count));
_ = log.IsTraceEnabled && log.Trace($"Found root commit {candidates.Last()}.");
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions MinVer/build/MinVer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MSBuildProjectDirectory=$(MSBuildProjectDirectory)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerAutoIncrement=$(MinVerAutoIncrement)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerBuildMetadata=$(MinVerBuildMetadata)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerDefaultPreReleaseIdentifiers=$(MinVerDefaultPreReleaseIdentifiers)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerDefaultPreReleasePhase=$(MinVerDefaultPreReleasePhase)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerIgnoreHeight=$(MinVerIgnoreHeight)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerMinimumMajorMinor=$(MinVerMinimumMajorMinor)" />
Expand All @@ -37,6 +38,7 @@
<MinVerInputs Include="&quot;$(MSBuildProjectDirectory)&quot;" />
<MinVerInputs Include="--auto-increment &quot;$(MinVerAutoIncrement)&quot;" />
<MinVerInputs Include="--build-metadata &quot;$(MinVerBuildMetadata)&quot;" />
<MinVerInputs Include="--default-pre-release-identifiers &quot;$(MinVerDefaultPreReleaseIdentifiers)&quot;" />
<MinVerInputs Include="--default-pre-release-phase &quot;$(MinVerDefaultPreReleasePhase)&quot;" />
<MinVerInputs Include="--ignore-height" Condition="'$(MinVerIgnoreHeight)' == 'true'" />
<MinVerInputs Include="--minimum-major-minor &quot;$(MinVerMinimumMajorMinor)&quot;" />
Expand Down
2 changes: 1 addition & 1 deletion MinVerTests.Lib/AutoIncrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static async Task RtmVersionIncrement(string tag, VersionPart autoIncreme
await Commit(path);

// act
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Zero, "", autoIncrement, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Default, "", autoIncrement, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand Down
8 changes: 4 additions & 4 deletions MinVerTests.Lib/BuildMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static async Task NoCommits(string buildMetadata, string expectedVersion)
await EnsureEmptyRepository(path);

// act
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Zero, buildMetadata, default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Default, buildMetadata, default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand All @@ -36,7 +36,7 @@ public static async Task NoTag(string buildMetadata, string expectedVersion)
await EnsureEmptyRepositoryAndCommit(path);

// act
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Zero, buildMetadata, default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Default, buildMetadata, default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand All @@ -57,7 +57,7 @@ public static async Task CurrentTag(string tag, string buildMetadata, string exp
await Tag(path, tag);

// act
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Zero, buildMetadata, default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Default, buildMetadata, default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand All @@ -79,7 +79,7 @@ public static async Task PreviousTag(string tag, string buildMetadata, string ex
await Commit(path);

// act
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Zero, buildMetadata, default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Default, buildMetadata, default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

namespace MinVerTests.Lib
{
public static class DefaultPreReleasePhases
public static class DefaultPreReleaseIdentifiers
{
[Theory]
[InlineData("", "0.0.0-alpha.0")]
[InlineData("preview", "0.0.0-preview.0")]
public static async Task DefaultPreReleasePhase(string phase, string expectedVersion)
[InlineData("alpha.0", "0.0.0-alpha.0")]
[InlineData("preview.x", "0.0.0-preview.x")]
public static async Task Various(string identifiers, string expectedVersion)
{
// arrange
var path = MethodBase.GetCurrentMethod().GetTestDirectory(phase);
var path = MethodBase.GetCurrentMethod().GetTestDirectory(identifiers);
await EnsureEmptyRepositoryAndCommit(path);
#pragma warning disable CA1062 // Validate arguments of public methods
var identifierList = identifiers.Split('.');
#pragma warning restore CA1062 // Validate arguments of public methods

// act
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Zero, "", default, phase, false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", MajorMinor.Default, "", default, identifierList, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand Down
2 changes: 1 addition & 1 deletion MinVerTests.Lib/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ git checkout main
var log = new TestLogger();

// act
_ = Versioner.GetVersion(path, "", minMajorMinor, "", default, "", false, log);
_ = Versioner.GetVersion(path, "", minMajorMinor, "", default, PreReleaseIdentifiers.Default, false, log);

// assert
var logMessages = log.ToString();
Expand Down
6 changes: 3 additions & 3 deletions MinVerTests.Lib/MinMajorMinor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static async Task NoCommits()
await EnsureEmptyRepository(path);

// act
var actualVersion = Versioner.GetVersion(path, "", new MajorMinor(1, 2), "", default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", new MajorMinor(1, 2), "", default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal("1.2.0-alpha.0", actualVersion.ToString());
Expand All @@ -38,7 +38,7 @@ public static async Task Tagged(string tag, int major, int minor, string expecte
var logger = new TestLogger();

// act
var actualVersion = Versioner.GetVersion(path, "", new MajorMinor(major, minor), "", default, "", false, logger);
var actualVersion = Versioner.GetVersion(path, "", new MajorMinor(major, minor), "", default, PreReleaseIdentifiers.Default, false, logger);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand All @@ -57,7 +57,7 @@ public static async Task NotTagged()
await EnsureEmptyRepositoryAndCommit(path);

// act
var actualVersion = Versioner.GetVersion(path, "", new MajorMinor(1, 0), "", default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, "", new MajorMinor(1, 0), "", default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal("1.0.0-alpha.0", actualVersion.ToString());
Expand Down
2 changes: 1 addition & 1 deletion MinVerTests.Lib/TagPrefixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static async Task TagPrefix(string tag, string prefix, string expectedVer
await Tag(path, tag);

// act
var actualVersion = Versioner.GetVersion(path, prefix, MajorMinor.Zero, "", default, "", false, NullLogger.Instance);
var actualVersion = Versioner.GetVersion(path, prefix, MajorMinor.Default, "", default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal(expectedVersion, actualVersion.ToString());
Expand Down
6 changes: 3 additions & 3 deletions MinVerTests.Lib/Versions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ git tag 1.1.0 -a -m '.'
{
await Checkout(path, sha);

var version = Versioner.GetVersion(path, "", MajorMinor.Zero, "", default, "", false, log);
var version = Versioner.GetVersion(path, "", MajorMinor.Default, "", default, PreReleaseIdentifiers.Default, false, log);
var versionString = version.ToString();
var tagName = $"v/{versionString}";

Expand Down Expand Up @@ -114,7 +114,7 @@ public static async Task EmptyRepo()
await EnsureEmptyRepository(path);

// act
var version = Versioner.GetVersion(path, "", MajorMinor.Zero, "", default, "", false, NullLogger.Instance);
var version = Versioner.GetVersion(path, "", MajorMinor.Default, "", default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal("0.0.0-alpha.0", version.ToString());
Expand All @@ -128,7 +128,7 @@ public static void NoRepo()
EnsureEmptyDirectory(path);

// act
var version = Versioner.GetVersion(path, "", MajorMinor.Zero, "", default, "", false, NullLogger.Instance);
var version = Versioner.GetVersion(path, "", MajorMinor.Default, "", default, PreReleaseIdentifiers.Default, false, NullLogger.Instance);

// assert
Assert.Equal("0.0.0-alpha.0", version.ToString());
Expand Down
63 changes: 63 additions & 0 deletions MinVerTests.Packages/CustomDefaultPreReleaseIdentifiers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using MinVerTests.Infra;
using Xunit;

namespace MinVerTests.Packages;

public static class CustomDefaultPreReleaseIdentifiers
{
[Fact]
public static async Task HasCustomDefaultPreReleaseIdentifiers()
{
// arrange
var path = MethodBase.GetCurrentMethod().GetTestDirectory();
await Sdk.CreateProject(path);

await Git.Init(path);
await Git.Commit(path);
await Git.Tag(path, "2.3.4");
await Git.Commit(path);

var envVars = ("MinVerDefaultPreReleaseIdentifiers".ToAltCase(), "preview.0");

var expected = Package.WithVersion(2, 3, 5, new[] { "preview", "0", }, 1);

// act
var (actual, _, _) = await Sdk.BuildProject(path, envVars: envVars);
var (cliStandardOutput, _) = await MinVerCli.ReadAsync(path, envVars: envVars);

// assert
Assert.Equal(expected, actual);
Assert.Equal(expected.Version, cliStandardOutput.Trim());
}

[Fact]
public static async Task HasCustomDefaultPreReleasePhase()
{
// arrange
var path = MethodBase.GetCurrentMethod().GetTestDirectory();
await Sdk.CreateProject(path);

await Git.Init(path);
await Git.Commit(path);
await Git.Tag(path, "2.3.4");
await Git.Commit(path);

var envVars = ("MinVerDefaultPreReleasePhase".ToAltCase(), "preview");

var expected = Package.WithVersion(2, 3, 5, new[] { "preview", "0", }, 1);

// act
var (actual, sdkStandardOutput, _) = await Sdk.BuildProject(path, envVars: envVars);
var (cliStandardOutput, cliStandardError) = await MinVerCli.ReadAsync(path, envVars: envVars);

// assert
Assert.Equal(expected, actual);
Assert.Contains("MINVER1008", sdkStandardOutput, StringComparison.Ordinal);

Assert.Equal(expected.Version, cliStandardOutput.Trim());
Assert.Contains("MinVerDefaultPreReleasePhase is deprecated", cliStandardError, StringComparison.Ordinal);
}
}
Loading

0 comments on commit 8276d73

Please sign in to comment.