Skip to content
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
17 changes: 13 additions & 4 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ static void Main(string[] args)
"Additional build metadata to add to a `premajor`, `preminor` or `prepatch` version bump",
CommandOptionType.SingleValue);

var prefixOption = commandLineApplication.Option(
"-p | --prefix <the-prerelease-prefix>",
"Override the default next prefix/label for a `premajor`, `preminor` or `prepatch` version bump",
CommandOptionType.SingleValue);

commandLineApplication.OnExecute(() =>
{
try
Expand Down Expand Up @@ -88,7 +93,8 @@ static void Main(string[] args)
doVcs,
dryRunEnabled,
csProjectFileOption.Value(),
buildMetaOption.Value()
buildMetaOption.Value(),
prefixOption.Value()
);
_cli.Execute(cliArgs);

Expand Down Expand Up @@ -119,12 +125,14 @@ static void Main(string[] args)
commandLineApplication.Execute(args);
}

internal static VersionCliArgs GetVersionBumpFromRemainingArgs(List<string> remainingArguments,
internal static VersionCliArgs GetVersionBumpFromRemainingArgs(
List<string> remainingArguments,
OutputFormat outputFormat,
bool doVcs,
bool dryRunEnabled,
string userSpecifiedCsProjFilePath,
string userSpecifiedBuildMeta
string userSpecifiedCsProjFilePath,
string userSpecifiedBuildMeta,
string preReleasePrefix
)
{
if (remainingArguments == null || !remainingArguments.Any())
Expand All @@ -141,6 +149,7 @@ string userSpecifiedBuildMeta
DoVcs = doVcs,
DryRun = dryRunEnabled,
BuildMeta = userSpecifiedBuildMeta,
PreReleasePrefix = preReleasePrefix,
};
var bump = VersionBump.Patch;

Expand Down
3 changes: 2 additions & 1 deletion src/VersionCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public VersionInfo Execute(VersionCliArgs args)
SemVer.FromString(_fileParser.PackageVersion),
args.VersionBump,
args.SpecificVersionToApply,
args.BuildMeta
args.BuildMeta,
args.PreReleasePrefix
);
var newSimpleVersion = semVer.ToSimpleVersionString();
var newSemVer = semVer.ToSemVerVersionString();
Expand Down
5 changes: 5 additions & 0 deletions src/VersionCliArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public class VersionCliArgs
/// Build meta for a pre-release tag passed via CLI arguments
/// </summary>
public string BuildMeta { get; set; }

/// <summary>
/// Override for the default `next` pre-release prefix/label
/// </summary>
public string PreReleasePrefix { get; set; }
}
}
45 changes: 33 additions & 12 deletions src/Versioning/SemVerBumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ public class SemVerBumper
/// <summary>
/// Bump the currently parsed version information with the specified <paramref name="bump"/>
/// </summary>
/// <param name="currentVersion"></param>
/// <param name="bump">The bump to apply to the version</param>
/// <param name="specificVersionToApply">The specific version to apply if bump is Specific</param>
/// <param name="buildMeta"></param>
public SemVer Bump(SemVer currentVersion, VersionBump bump, string specificVersionToApply = "",
string buildMeta = "")
/// <param name="buildMeta">Additional build metadata to add to the final version string</param>
/// <param name="preReleasePrefix">Override of default `next` pre-release prefix/label</param>
public SemVer Bump(
SemVer currentVersion,
VersionBump bump,
string specificVersionToApply = "",
string buildMeta = "",
string preReleasePrefix = ""
)
{
var newVersion = SemVer.FromString(currentVersion.ToSemVerVersionString());
newVersion.BuildMeta = buildMeta;
Expand All @@ -25,7 +32,7 @@ public SemVer Bump(SemVer currentVersion, VersionBump bump, string specificVersi
}
case VersionBump.PreMajor:
{
HandlePreMajorBump(newVersion);
HandlePreMajorBump(newVersion, preReleasePrefix);
break;
}
case VersionBump.Minor:
Expand All @@ -35,7 +42,7 @@ public SemVer Bump(SemVer currentVersion, VersionBump bump, string specificVersi
}
case VersionBump.PreMinor:
{
HandlePreMinorBump(newVersion);
HandlePreMinorBump(newVersion, preReleasePrefix);
break;
}
case VersionBump.Patch:
Expand All @@ -45,7 +52,7 @@ public SemVer Bump(SemVer currentVersion, VersionBump bump, string specificVersi
}
case VersionBump.PrePatch:
{
HandlePrePatchBump(newVersion);
HandlePrePatchBump(newVersion, preReleasePrefix);
break;
}
case VersionBump.PreRelease:
Expand Down Expand Up @@ -116,10 +123,14 @@ private static void HandlePreReleaseBump(SemVer newVersion)
newVersion.PreRelease = $"{preReleaseLabel}.{preReleaseNumber}";
}

private static void HandlePrePatchBump(SemVer newVersion)
private static void HandlePrePatchBump(SemVer newVersion, string preReleasePrefix)
{
if (string.IsNullOrWhiteSpace(preReleasePrefix))
{
preReleasePrefix = "next";
}
newVersion.Patch += 1;
newVersion.PreRelease = "next.0";
newVersion.PreRelease = $"{preReleasePrefix}.0";
}

private void HandlePatchBump(SemVer newVersion)
Expand All @@ -135,11 +146,16 @@ private void HandlePatchBump(SemVer newVersion)
}
}

private void HandlePreMinorBump(SemVer newVersion)
private void HandlePreMinorBump(SemVer newVersion, string preReleasePrefix)
{
if (string.IsNullOrWhiteSpace(preReleasePrefix))
{
preReleasePrefix = "next";
}

newVersion.Minor += 1;
newVersion.Patch = 0;
newVersion.PreRelease = "next.0";
newVersion.PreRelease = $"{preReleasePrefix}.0";
}

private void HandleMinorBump(SemVer newVersion)
Expand All @@ -156,12 +172,17 @@ private void HandleMinorBump(SemVer newVersion)
}
}

private void HandlePreMajorBump(SemVer newVersion)
private void HandlePreMajorBump(SemVer newVersion, string preReleasePrefix)
{
if (string.IsNullOrWhiteSpace(preReleasePrefix))
{
preReleasePrefix = "next";
}

newVersion.Major += 1;
newVersion.Minor = 0;
newVersion.Patch = 0;
newVersion.PreRelease = "next.0";
newVersion.PreRelease = $"{preReleasePrefix}.0";
}

private void HandleMajorBump(SemVer newVersion)
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-version.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Description>A dotnet core global tool for changing your csproj version and automatically comitting and tagging - npm version style.</Description>
<PackageTags>core;version;npm version;version patch;</PackageTags>
<PackageProjectUrl>https://github.com/skarpdev/dotnet-version-cli/</PackageProjectUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/skarpdev/dotnet-version-cli/master/LICENSE</PackageLicenseUrl>
<PackageLicense>https://raw.githubusercontent.com/skarpdev/dotnet-version-cli/master/LICENSE</PackageLicense>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/skarpdev/dotnet-version-cli/</RepositoryUrl>
Expand Down
3 changes: 3 additions & 0 deletions test/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void GetVersionBumpFromRemaingArgsWork(string strVersionBump, VersionBump
true,
true,
string.Empty,
string.Empty,
string.Empty
);
Assert.Equal(expectedBump, args.VersionBump);
Expand All @@ -48,6 +49,7 @@ public void Get_version_bump_throws_on_missing_value()
true,
true,
string.Empty,
string.Empty,
string.Empty
)
);
Expand All @@ -69,6 +71,7 @@ public void Get_version_bump_throws_on_invalid_value()
true,
true,
string.Empty,
string.Empty,
string.Empty
)
);
Expand Down
46 changes: 46 additions & 0 deletions test/VersionCliTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,52 @@ public void VersionCli_can_bump_pre_release_versions()
A<string>.That.Matches(tag => tag == "v2.0.0-next.0")))
.MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
public void VersionCli_can_bump_pre_release_with_custom_prefix()
{
// Configure
A.CallTo(() => _vcsTool.IsRepositoryClean()).Returns(true);
A.CallTo(() => _vcsTool.IsVcsToolPresent()).Returns(true);
A.CallTo(() => _vcsTool.Commit(A<string>._, A<string>._)).DoesNothing();
A.CallTo(() => _vcsTool.Tag(A<string>._)).DoesNothing();

A.CallTo(() => _fileDetector.FindAndLoadCsProj(A<string>._)).Returns("<Project/>");
const string csProjFilePath = "/unit-test/test.csproj";
A.CallTo(() => _fileDetector.ResolvedCsProjFile).Returns(csProjFilePath);

A.CallTo(() => _fileParser.Load(A<string>._)).DoesNothing();
A.CallTo(() => _fileParser.Version).Returns("1.2.1");
A.CallTo(() => _fileParser.PackageVersion).Returns("1.2.1");

// Act
_cli.Execute(new VersionCliArgs{VersionBump = VersionBump.PreMajor, DoVcs = true, DryRun = false, PreReleasePrefix = "beta"});

// Verify
A.CallTo(() => _filePatcher.PatchVersionField(
A<string>._,
A<string>._
))
.MustNotHaveHappened();

A.CallTo(() => _filePatcher.PatchPackageVersionField(
A<string>.That.Matches(ver => ver == "1.2.1"),
"2.0.0-beta.0"
))
.MustHaveHappened(Repeated.Exactly.Once);

A.CallTo(() => _filePatcher.Flush(
csProjFilePath))
.MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => _vcsTool.Commit(
csProjFilePath,
"v2.0.0-beta.0"))
.MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => _vcsTool.Tag(
"v2.0.0-beta.0"))
.MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
public void VersionCli_can_bump_pre_release_with_build_meta_versions()
{
Expand Down
16 changes: 16 additions & 0 deletions test/Versioning/SemVerBumperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public SemVerBumperTests()
{
_bumper = new SemVerBumper();
}

[Theory]
[InlineData("1.1.0", VersionBump.Major, 2, 0, 0, "", "")]
[InlineData("1.1.0", VersionBump.PreMajor, 2, 0, 0, "next.0", "")]
Expand Down Expand Up @@ -59,5 +60,20 @@ public void CanBumpAndSerializeStringVersion(string version, VersionBump bump, s
var semver = _bumper.Bump(SemVer.FromString(version), bump);
Assert.Equal(expectedVersion, semver.ToSemVerVersionString());
}

[Theory]
[InlineData("1.0.0", VersionBump.PreMajor, "2.0.0-alpha.0", "alpha")]
[InlineData("1.0.0", VersionBump.PreMinor, "1.1.0-beta.0", "beta")]
[InlineData("1.0.0", VersionBump.PrePatch, "1.0.1-pre.0", "pre")]
public void Respects_custom_pre_release_prefix(
string version,
VersionBump bump,
string expectedVersion,
string prefix
)
{
var semver = _bumper.Bump(SemVer.FromString(version), bump, preReleasePrefix: prefix);
Assert.Equal(expectedVersion, semver.ToSemVerVersionString());
}
}
}