diff --git a/src/Program.cs b/src/Program.cs index 2aa8b18..5ece3a3 100755 --- a/src/Program.cs +++ b/src/Program.cs @@ -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 ", + "Override the default next prefix/label for a `premajor`, `preminor` or `prepatch` version bump", + CommandOptionType.SingleValue); + commandLineApplication.OnExecute(() => { try @@ -88,7 +93,8 @@ static void Main(string[] args) doVcs, dryRunEnabled, csProjectFileOption.Value(), - buildMetaOption.Value() + buildMetaOption.Value(), + prefixOption.Value() ); _cli.Execute(cliArgs); @@ -119,12 +125,14 @@ static void Main(string[] args) commandLineApplication.Execute(args); } - internal static VersionCliArgs GetVersionBumpFromRemainingArgs(List remainingArguments, + internal static VersionCliArgs GetVersionBumpFromRemainingArgs( + List remainingArguments, OutputFormat outputFormat, bool doVcs, bool dryRunEnabled, - string userSpecifiedCsProjFilePath, - string userSpecifiedBuildMeta + string userSpecifiedCsProjFilePath, + string userSpecifiedBuildMeta, + string preReleasePrefix ) { if (remainingArguments == null || !remainingArguments.Any()) @@ -141,6 +149,7 @@ string userSpecifiedBuildMeta DoVcs = doVcs, DryRun = dryRunEnabled, BuildMeta = userSpecifiedBuildMeta, + PreReleasePrefix = preReleasePrefix, }; var bump = VersionBump.Patch; diff --git a/src/VersionCli.cs b/src/VersionCli.cs index 52872e4..8a73220 100644 --- a/src/VersionCli.cs +++ b/src/VersionCli.cs @@ -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(); diff --git a/src/VersionCliArgs.cs b/src/VersionCliArgs.cs index c13e66d..aa09477 100644 --- a/src/VersionCliArgs.cs +++ b/src/VersionCliArgs.cs @@ -25,5 +25,10 @@ public class VersionCliArgs /// Build meta for a pre-release tag passed via CLI arguments /// public string BuildMeta { get; set; } + + /// + /// Override for the default `next` pre-release prefix/label + /// + public string PreReleasePrefix { get; set; } } } \ No newline at end of file diff --git a/src/Versioning/SemVerBumper.cs b/src/Versioning/SemVerBumper.cs index bdd947a..994dcb4 100644 --- a/src/Versioning/SemVerBumper.cs +++ b/src/Versioning/SemVerBumper.cs @@ -7,11 +7,18 @@ public class SemVerBumper /// /// Bump the currently parsed version information with the specified /// + /// /// The bump to apply to the version /// The specific version to apply if bump is Specific - /// - public SemVer Bump(SemVer currentVersion, VersionBump bump, string specificVersionToApply = "", - string buildMeta = "") + /// Additional build metadata to add to the final version string + /// Override of default `next` pre-release prefix/label + public SemVer Bump( + SemVer currentVersion, + VersionBump bump, + string specificVersionToApply = "", + string buildMeta = "", + string preReleasePrefix = "" + ) { var newVersion = SemVer.FromString(currentVersion.ToSemVerVersionString()); newVersion.BuildMeta = buildMeta; @@ -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: @@ -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: @@ -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: @@ -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) @@ -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) @@ -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) diff --git a/src/dotnet-version.csproj b/src/dotnet-version.csproj index 6d43000..ae4e470 100755 --- a/src/dotnet-version.csproj +++ b/src/dotnet-version.csproj @@ -12,7 +12,7 @@ A dotnet core global tool for changing your csproj version and automatically comitting and tagging - npm version style. core;version;npm version;version patch; https://github.com/skarpdev/dotnet-version-cli/ - https://raw.githubusercontent.com/skarpdev/dotnet-version-cli/master/LICENSE + https://raw.githubusercontent.com/skarpdev/dotnet-version-cli/master/LICENSE false git https://github.com/skarpdev/dotnet-version-cli/ diff --git a/test/ProgramTest.cs b/test/ProgramTest.cs index 1336728..83d680f 100644 --- a/test/ProgramTest.cs +++ b/test/ProgramTest.cs @@ -29,6 +29,7 @@ public void GetVersionBumpFromRemaingArgsWork(string strVersionBump, VersionBump true, true, string.Empty, + string.Empty, string.Empty ); Assert.Equal(expectedBump, args.VersionBump); @@ -48,6 +49,7 @@ public void Get_version_bump_throws_on_missing_value() true, true, string.Empty, + string.Empty, string.Empty ) ); @@ -69,6 +71,7 @@ public void Get_version_bump_throws_on_invalid_value() true, true, string.Empty, + string.Empty, string.Empty ) ); diff --git a/test/VersionCliTest.cs b/test/VersionCliTest.cs index 4e2648c..2b417b9 100644 --- a/test/VersionCliTest.cs +++ b/test/VersionCliTest.cs @@ -171,6 +171,52 @@ public void VersionCli_can_bump_pre_release_versions() A.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._, A._)).DoesNothing(); + A.CallTo(() => _vcsTool.Tag(A._)).DoesNothing(); + + A.CallTo(() => _fileDetector.FindAndLoadCsProj(A._)).Returns(""); + const string csProjFilePath = "/unit-test/test.csproj"; + A.CallTo(() => _fileDetector.ResolvedCsProjFile).Returns(csProjFilePath); + + A.CallTo(() => _fileParser.Load(A._)).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._, + A._ + )) + .MustNotHaveHappened(); + + A.CallTo(() => _filePatcher.PatchPackageVersionField( + A.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() { diff --git a/test/Versioning/SemVerBumperTests.cs b/test/Versioning/SemVerBumperTests.cs index d56c776..5da0def 100644 --- a/test/Versioning/SemVerBumperTests.cs +++ b/test/Versioning/SemVerBumperTests.cs @@ -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", "")] @@ -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()); + } } } \ No newline at end of file