Skip to content

Commit

Permalink
Merge pull request #303 from ap0llo/master
Browse files Browse the repository at this point in the history
Improved commandline options for prepare-release command
  • Loading branch information
AArnott committed Feb 22, 2019
2 parents d69d232 + 6675320 commit 4674c27
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 63 deletions.
22 changes: 17 additions & 5 deletions doc/nbgv-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,28 @@ nbgv prepare-release rc
no new branch will be created. Instead the tool will just update the version
in the current branch by replacing or removing the prerelease tag.

### Explicitly setting the next version
### Customizing the next version

If you want to explicitly set the next version of the main branch instead of
automatically determining it by incrementing the current version, you
can set the version as commandline parameter:
By default, the next version of the main branch is determined from the current
version and the `versionIncrement` setting in `version.json`.
To customize this behaviour, you can either explicitly set the next version
or override the version increment setting.

To explicitly set the next version, run:

```ps1
nbgv prepare-release --nextVersion 2.0
```

To override the `versionIncrement` setting from `version.json`, run:

```ps1
nbgv prepare-release --nextVersion 2.0-beta
nbgv prepare-release --versionIncrement Major
```

**Note:** The parameters `nextVersion` and `versionIncrement` cannot
be combined.

### Customizing the behaviour of `prepare-release`

The behaviour of the `prepare-release` command can be customized in
Expand Down
69 changes: 37 additions & 32 deletions src/NerdBank.GitVersioning.Tests/ReleaseManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using ReleasePreparationError = Nerdbank.GitVersioning.ReleaseManager.ReleasePreparationError;
using ReleasePreparationException = Nerdbank.GitVersioning.ReleaseManager.ReleasePreparationException;
using ReleaseVersionIncrement = Nerdbank.GitVersioning.VersionOptions.ReleaseVersionIncrement;
using Version = System.Version;

public class ReleaseManagerTests : RepoTestBase
{
Expand Down Expand Up @@ -200,42 +201,45 @@ public void PrepeareRelease_ReleaseBranchWithVersionDecrement(string initialVers

[Theory]
// base test cases
[InlineData("1.2-beta", null, null, null, null, null, "v1.2", "1.2", "1.3-alpha")]
[InlineData("1.2-beta", null, null, null, "rc", null, "v1.2", "1.2-rc", "1.3-alpha")]
[InlineData("1.2-beta.{height}", null, null, null, null, null, "v1.2", "1.2", "1.3-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, null, null, "rc", null, "v1.2", "1.2-rc.{height}", "1.3-alpha.{height}")]
[InlineData("1.2-beta", null, null, null, null, null, null, "v1.2", "1.2", "1.3-alpha")]
[InlineData("1.2-beta", null, null, null, "rc", null, null, "v1.2", "1.2-rc", "1.3-alpha")]
[InlineData("1.2-beta.{height}", null, null, null, null, null, null, "v1.2", "1.2", "1.3-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, null, null, "rc", null, null, "v1.2", "1.2-rc.{height}", "1.3-alpha.{height}")]
// modify release.branchName
[InlineData("1.2-beta", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", null, null, "v1.2release", "1.2", "1.3-alpha")]
[InlineData("1.2-beta", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", "rc", null, "v1.2release", "1.2-rc", "1.3-alpha")]
[InlineData("1.2-beta.{height}", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", null, null, "v1.2release", "1.2", "1.3-alpha.{height}")]
[InlineData("1.2-beta.{height}", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", "rc", null, "v1.2release", "1.2-rc.{height}", "1.3-alpha.{height}")]
[InlineData("1.2-beta", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2release", "1.2", "1.3-alpha")]
[InlineData("1.2-beta", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2release", "1.2-rc", "1.3-alpha")]
[InlineData("1.2-beta.{height}", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2release", "1.2", "1.3-alpha.{height}")]
[InlineData("1.2-beta.{height}", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2release", "1.2-rc.{height}", "1.3-alpha.{height}")]
// modify release.versionIncrement: "Major"
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Major, "alpha", null, null, "v1.2", "1.2", "2.0-alpha")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Major, "alpha", "rc", null, "v1.2", "1.2-rc", "2.0-alpha")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Major, "alpha", null, null, "v1.2", "1.2", "2.0-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Major, "alpha", "rc", null, "v1.2", "1.2-rc.{height}", "2.0-alpha.{height}")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Major, "alpha", "rc", null, null, "v1.2", "1.2-rc", "2.0-alpha")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Major, "alpha", "rc", null, null, "v1.2", "1.2-rc.{height}", "2.0-alpha.{height}")]
// modify release.versionIncrement: "Build"
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Build, "alpha", null, null, "v1.2.3", "1.2.3", "1.2.4-alpha")]
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Build, "alpha", "rc", null, "v1.2.3", "1.2.3-rc", "1.2.4-alpha")]
[InlineData("1.2.3-beta.{height}", null, ReleaseVersionIncrement.Build, "alpha", null, null, "v1.2.3", "1.2.3", "1.2.4-alpha.{height}")]
[InlineData("1.2.3-beta.{height}", null, ReleaseVersionIncrement.Build, "alpha", "rc", null, "v1.2.3", "1.2.3-rc.{height}", "1.2.4-alpha.{height}")]
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Build, "alpha", null, null, null, "v1.2.3", "1.2.3", "1.2.4-alpha")]
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Build, "alpha", "rc", null, null, "v1.2.3", "1.2.3-rc", "1.2.4-alpha")]
[InlineData("1.2.3-beta.{height}", null, ReleaseVersionIncrement.Build, "alpha", null, null, null, "v1.2.3", "1.2.3", "1.2.4-alpha.{height}")]
[InlineData("1.2.3-beta.{height}", null, ReleaseVersionIncrement.Build, "alpha", "rc", null, null, "v1.2.3", "1.2.3-rc.{height}", "1.2.4-alpha.{height}")]
// modify release.firstUnstableTag
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, "preview", null, null, "v1.2", "1.2", "1.3-preview")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, "preview", "rc", null, "v1.2", "1.2-rc", "1.3-preview")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Minor, "preview", null, null, "v1.2", "1.2", "1.3-preview.{height}")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Minor, "preview", "rc", null, "v1.2", "1.2-rc.{height}", "1.3-preview.{height}")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, "preview", null, null, null, "v1.2", "1.2", "1.3-preview")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, "preview", "rc", null, null, "v1.2", "1.2-rc", "1.3-preview")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Minor, "preview", null, null, null, "v1.2", "1.2", "1.3-preview.{height}")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Minor, "preview", "rc", null, null, "v1.2", "1.2-rc.{height}", "1.3-preview.{height}")]
// include build metadata in version
[InlineData("1.2-beta+metadata", null, ReleaseVersionIncrement.Minor, "alpha", null, null, "v1.2", "1.2+metadata", "1.3-alpha+metadata")]
[InlineData("1.2-beta+metadata", null, ReleaseVersionIncrement.Minor, "alpha", "rc", null, "v1.2", "1.2-rc+metadata", "1.3-alpha+metadata")]
[InlineData("1.2-beta.{height}+metadata", null, ReleaseVersionIncrement.Minor, "alpha", null, null, "v1.2", "1.2+metadata", "1.3-alpha.{height}+metadata")]
[InlineData("1.2-beta.{height}+metadata", null, ReleaseVersionIncrement.Minor, "alpha", "rc", null, "v1.2", "1.2-rc.{height}+metadata", "1.3-alpha.{height}+metadata")]
[InlineData("1.2-beta+metadata", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2+metadata", "1.3-alpha+metadata")]
[InlineData("1.2-beta+metadata", null, ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2", "1.2-rc+metadata", "1.3-alpha+metadata")]
[InlineData("1.2-beta.{height}+metadata", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2+metadata", "1.3-alpha.{height}+metadata")]
[InlineData("1.2-beta.{height}+metadata", null, ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2", "1.2-rc.{height}+metadata", "1.3-alpha.{height}+metadata")]
// versions without prerelease tags
[InlineData("1.2", null, ReleaseVersionIncrement.Minor, "alpha", null, null, "v1.2", "1.2", "1.3-alpha")]
[InlineData("1.2", null, ReleaseVersionIncrement.Major, "alpha", null, null, "v1.2", "1.2", "2.0-alpha")]
// explicitly set next version (firstUnstableTag setting will be ignored)
[InlineData("1.2-beta", null, null, null, null, "4.5", "v1.2", "1.2", "4.5")]
[InlineData("1.2-beta", null, null, null, null, "4.5-pre", "v1.2", "1.2", "4.5-pre")]
[InlineData("1.2-beta.{height}", null, null, null, null, "4.5-pre.{height}", "v1.2", "1.2", "4.5-pre.{height}")]
[InlineData("1.2", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2", "1.3-alpha")]
[InlineData("1.2", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha")]
// explicitly set next version
[InlineData("1.2-beta", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5-alpha")]
[InlineData("1.2-beta.{height}", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, null, "pre", null, "4.5.6", null, "v1.2", "1.2", "4.5.6-pre.{height}")]
// explicitly set version increment overriding the setting from ReleaseOptions
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, null, null, null, ReleaseVersionIncrement.Major, "v1.2", "1.2", "2.0-alpha")]
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Minor, null, null, null, ReleaseVersionIncrement.Build, "v1.2.3", "1.2.3", "1.2.4-alpha")]
public void PrepareRelease_Master(
// data for initial setup (version and release options configured in version.json)
string initialVersion,
Expand All @@ -245,6 +249,7 @@ public void PrepeareRelease_ReleaseBranchWithVersionDecrement(string initialVers
// arguments passed to PrepareRelease()
string releaseUnstableTag,
string nextVersion,
ReleaseVersionIncrement? parameterVersionIncrement,
// expected versions and branch name after running PrepareRelease()
string expectedBranchName,
string resultingReleaseVersion,
Expand Down Expand Up @@ -295,7 +300,7 @@ public void PrepeareRelease_ReleaseBranchWithVersionDecrement(string initialVers

// prepare release
var releaseManager = new ReleaseManager();
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : SemanticVersion.Parse(nextVersion)));
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : Version.Parse(nextVersion)), parameterVersionIncrement);

// check if a branch was created
Assert.Contains(this.Repo.Branches, branch => branch.FriendlyName == expectedBranchName);
Expand Down Expand Up @@ -371,7 +376,7 @@ public void PrepareRelease_MasterWithVersionDecrement(string initialVersion, str
// running PrepareRelease should result in an error
// because we're trying to add a prerelease tag to a version without prerelease tag
this.AssertError(
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : SemanticVersion.Parse(nextVersion))),
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : Version.Parse(nextVersion))),
ReleasePreparationError.VersionDecrement);
}

Expand Down
55 changes: 35 additions & 20 deletions src/NerdBank.GitVersioning/ReleaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp;
using Validation;
using Version = System.Version;

/// <summary>
/// Methods for creating releases
Expand Down Expand Up @@ -110,7 +110,12 @@ public ReleaseManager(TextWriter outputWriter = null, TextWriter errorWriter = n
/// version based on the current version and the <c>versionIncrement</c> setting in <c>version.json</c>.
/// Parameter will be ignored if the current branch is a release branch.
/// </param>
public void PrepareRelease(string projectDirectory, string releaseUnstableTag = null, SemanticVersion nextVersion = null)
/// <param name="versionIncrement">
/// The increment to apply in order to determine the next version on the current branch.
/// If specified, value will be used instead of the increment specified in <c>version.json</c>.
/// Parameter will be ignored if the current branch is a release branch.
/// </param>
public void PrepareRelease(string projectDirectory, string releaseUnstableTag = null, Version nextVersion = null, VersionOptions.ReleaseVersionIncrement? versionIncrement = null)
{
Requires.NotNull(projectDirectory, nameof(projectDirectory));

Expand All @@ -136,7 +141,6 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
var releaseVersion = string.IsNullOrEmpty(releaseUnstableTag)
? versionOptions.Version.WithoutPrepreleaseTags()
: versionOptions.Version.SetFirstPrereleaseTag(releaseUnstableTag);
var nextDevVersion = this.GetNextDevVersion(versionOptions, nextVersion);

// check if the current branch is the release branch
if (string.Equals(originalBranchName, releaseBranchName, StringComparison.OrdinalIgnoreCase))
Expand All @@ -146,6 +150,8 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
return;
}

var nextDevVersion = this.GetNextDevVersion(versionOptions, nextVersion, versionIncrement);

// check if the release branch already exists
if (repository.Branches[releaseBranchName] != null)
{
Expand Down Expand Up @@ -273,29 +279,38 @@ private static bool IsVersionDecrement(SemanticVersion oldVersion, SemanticVersi
}
}

private SemanticVersion GetNextDevVersion(VersionOptions versionOptions, SemanticVersion nextVersion)
private SemanticVersion GetNextDevVersion(VersionOptions versionOptions, Version nextVersionOverride, VersionOptions.ReleaseVersionIncrement? versionIncrementOverride)
{
if (nextVersion != null)
return nextVersion;
var currentVersion = versionOptions.Version;

var releaseOptions = versionOptions.ReleaseOrDefault;
SemanticVersion nextDevVersion;
if(nextVersionOverride != null)
{
nextDevVersion = new SemanticVersion(nextVersionOverride, currentVersion.Prerelease, currentVersion.BuildMetadata);
}
else
{
// Determine the increment to use:
// Use parameter versionIncrementOverride if it has a value, otherwise use setting from version.json.
var versionIncrement = versionIncrementOverride ?? versionOptions.ReleaseOrDefault.VersionIncrementOrDefault;

// the increment is only valid if the current version has the required precision
// increment settings "Major" and "Minor" are always valid
// increment setting "Build" is only valid if the version has at lease three segments
var isValidIncrement = releaseOptions.VersionIncrementOrDefault != VersionOptions.ReleaseVersionIncrement.Build ||
versionOptions.Version.Version.Build >= 0;
// The increment is only valid if the current version has the required precision:
// - increment settings "Major" and "Minor" are always valid.
// - increment setting "Build" is only valid if the version has at lease three segments.
var isValidIncrement = versionIncrement != VersionOptions.ReleaseVersionIncrement.Build ||
versionOptions.Version.Version.Build >= 0;

// increment is ignored when the next version was specified explicitly
if (!isValidIncrement)
{
this.stderr.WriteLine($"Cannot apply version increment 'build' to version '{versionOptions.Version}' because it only has major and minor segments");
throw new ReleasePreparationException(ReleasePreparationError.InvalidVersionIncrementSetting);
if (!isValidIncrement)
{
this.stderr.WriteLine($"Cannot apply version increment 'build' to version '{versionOptions.Version}' because it only has major and minor segments.");
throw new ReleasePreparationException(ReleasePreparationError.InvalidVersionIncrementSetting);
}

nextDevVersion = currentVersion.Increment(versionIncrement);
}

return versionOptions.Version
.Increment(releaseOptions.VersionIncrementOrDefault)
.SetFirstPrereleaseTag(releaseOptions.FirstUnstableTagOrDefault);
// return next version with prerelease tag specified in version.json
return nextDevVersion.SetFirstPrereleaseTag(versionOptions.ReleaseOrDefault.FirstUnstableTagOrDefault);
}
}
}
Loading

0 comments on commit 4674c27

Please sign in to comment.