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

simplify inputs #82

Merged
merged 1 commit into from
Nov 4, 2018
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
24 changes: 12 additions & 12 deletions MinVer.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ static void Main(string[] args)
app.HelpOption();

var buildMetadata = app.Option("-b|--build-metadata <BUILD_METADATA>", "The build metadata to append to the version.", CommandOptionType.SingleValue);
var minimumMajorMinor = app.Option("-m|--minimum-major-minor <MINIMUM_MAJOR_MINOR>", "The minimum major and minor version range. E.g. '2.0'.", CommandOptionType.SingleValue);
var majorMinor = app.Option("-m|--major-minor <MAJOR.MINOR>", "The MAJOR.MINOR version range. E.g. '2.0'.", CommandOptionType.SingleValue);
var path = app.Option("-p|--path <PATH>", "The path of the repository.", CommandOptionType.SingleValue);
var tagPrefix = app.Option("-t|--tag-prefix <TAG_PREFIX>", "The tag prefix.", CommandOptionType.SingleValue);
var verbose = app.Option("-v|--verbose", "Enable verbose logging.", CommandOptionType.NoValue);

app.OnExecute(() =>
{
var minimumMajor = 0;
var minimumMinor = 0;
var major = 0;
var minor = 0;

var minimumMajorMinorValue = minimumMajorMinor.Value();
var majorMinorValue = majorMinor.Value();

if (!string.IsNullOrEmpty(minimumMajorMinorValue))
if (!string.IsNullOrEmpty(majorMinorValue))
{
var numbers = minimumMajorMinorValue.Split('.');
var numbers = majorMinorValue.Split('.');

if (numbers.Length > 2)
{
throw new Exception($"More than one dot in minimum major and minor version range '{minimumMajorMinorValue}'.");
throw new Exception($"More than one dot in MAJOR.MINOR range '{majorMinorValue}'.");
}

if (!int.TryParse(numbers[0], out minimumMajor))
if (!int.TryParse(numbers[0], out major))
{
throw new Exception($"Invalid major version '{numbers[0]}' in minimum major and minor version range '{minimumMajorMinorValue}'.");
throw new Exception($"Invalid MAJOR '{numbers[0]}' in MAJOR.MINOR range '{majorMinorValue}'.");
}

if (numbers.Length > 1 && !int.TryParse(numbers[1], out minimumMinor))
if (numbers.Length > 1 && !int.TryParse(numbers[1], out minor))
{
throw new Exception($"Invalid minor version '{numbers[1]}' in minimum major and minor version range '{minimumMajorMinorValue}'.");
throw new Exception($"Invalid MINOR '{numbers[1]}' in MAJOR.MINOR range '{majorMinorValue}'.");
}
}

Console.Out.WriteLine(Versioner.GetVersion(path.Value() ?? ".", verbose.HasValue(), tagPrefix.Value(), minimumMajor, minimumMinor, buildMetadata.Value()));
Console.Out.WriteLine(Versioner.GetVersion(path.Value() ?? ".", verbose.HasValue(), tagPrefix.Value(), major, minor, buildMetadata.Value()));
});

app.Execute(args);
Expand Down
12 changes: 6 additions & 6 deletions MinVer/Versioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MinVer

public static class Versioner
{
public static Version GetVersion(string path, bool verbose, string tagPrefix, int minimumMajor, int minimumMinor, string buildMetadata)
public static Version GetVersion(string path, bool verbose, string tagPrefix, int major, int minor, string buildMetadata)
{
if (verbose)
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public static Version GetVersion(string path, bool verbose, string tagPrefix, in
{
try
{
return GetVersion(repo, verbose, tagPrefix, minimumMajor, minimumMinor, buildMetadata);
return GetVersion(repo, verbose, tagPrefix, major, minor, buildMetadata);
}
finally
{
Expand All @@ -55,7 +55,7 @@ public static Version GetVersion(string path, bool verbose, string tagPrefix, in
return new Version();
}

private static Version GetVersion(Repository repo, bool verbose, string tagPrefix, int minimumMajor, int minimumMinor, string buildMetadata)
private static Version GetVersion(Repository repo, bool verbose, string tagPrefix, int major, int minor, string buildMetadata)
{
var commit = repo.Commits.FirstOrDefault();

Expand Down Expand Up @@ -132,13 +132,13 @@ private static Version GetVersion(Repository repo, bool verbose, string tagPrefi
var selectedCandidate = orderedCandidates.Last();
Log($"Using{(verbose && orderedCandidates.Count > 1 ? " " : " ")}{selectedCandidate.ToString(tagWidth, versionWidth, heightWidth)}.");

var baseVersion = selectedCandidate.Version.IsBefore(minimumMajor, minimumMinor) ?
new Version(minimumMajor, minimumMinor)
var baseVersion = selectedCandidate.Version.IsBefore(major, minor) ?
new Version(major, minor)
: selectedCandidate.Version;

if (baseVersion != selectedCandidate.Version)
{
Log($"Bumping version to {baseVersion} to satisify minimum major minor {minimumMajor}.{minimumMinor}.");
Log($"Bumping version to {baseVersion} to satisfy {major}.{minor} range.");
}

var calculatedVersion = baseVersion.WithHeight(selectedCandidate.Height).WithBuildMetadata(buildMetadata);
Expand Down
6 changes: 2 additions & 4 deletions MinVer/build/MinVer.targets
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<MinVerBuildMetadata Condition=" '$(MinVerBuildMetadata)' == '' ">$(MINVER_BUILD_METADATA)</MinVerBuildMetadata>
<MinVerMinimumMajorMinor Condition=" '$(MinVerMinimumMajorMinor)' == '' ">$(MINVER_MINIMUM_MAJOR_MINOR)</MinVerMinimumMajorMinor>
<MinVerMinimumMajorMinor Condition=" '$(MinVerMinimumMajorMinor)' == '' ">0.0</MinVerMinimumMajorMinor>
<MinVerMajorMinor Condition=" '$(MinVerMajorMinor)' == '' ">0.0</MinVerMajorMinor>
<MinVerTagPrefix Condition=" '$(MinVerTagPrefix)' == '' ">$(MINVER_TAG_PREFIX)</MinVerTagPrefix>
<MinVerVerbose Condition=" '$(MinVerVerbose)' == '' ">$(MINVER_VERBOSE)</MinVerVerbose>
<MinVerVerbose Condition=" '$(MinVerVerbose)' == '' ">false</MinVerVerbose>
Expand All @@ -20,7 +18,7 @@
<PropertyGroup>
<MinVerVerboseOption Condition="'$(MinVerVerbose)' == 'true'"> --verbose</MinVerVerboseOption>
</PropertyGroup>
<Exec Command="dotnet &quot;$(MSBuildThisFileDirectory)../MinVer/MinVer.Cli.dll&quot; --build-metadata &quot;$(MinVerBuildMetadata)&quot; --path &quot;$(MSBuildProjectDirectory)&quot; --minimum-major-minor &quot;$(MinVerMinimumMajorMinor)&quot; --tag-prefix &quot;$(MinVerTagPrefix)&quot;$(MinVerVerboseOption)" ConsoleToMSBuild="true">
<Exec Command="dotnet &quot;$(MSBuildThisFileDirectory)../MinVer/MinVer.Cli.dll&quot; --build-metadata &quot;$(MINVER_BUILD_METADATA)&quot; --path &quot;$(MSBuildProjectDirectory)&quot; --major-minor &quot;$(MinVerMajorMinor)&quot; --tag-prefix &quot;$(MinVerTagPrefix)&quot;$(MinVerVerboseOption)" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" ItemName="MinVerConsoleOutputItems" />
</Exec>
<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Your project will be versioned according to the latest tag found in the commit h

- The last tag on HEAD or it's ancestors which represents a [SemVer](https://semver.org) version number._\*_
- By how many commits HEAD is ahead of the tag (known as "height"). Or if no tag is found, by how commits HEAD is ahead of the first root commit found._\*\*_
- An optional `MINVER_MINIMUM_MAJOR_MINOR` or `MinVerMinimumMajorMinor` environment variable or MSBuild property.
- An optional `MinVerMajorMinor` MSBuild property.

\* _Each time the history diverges, the last tag is found on each path and the tag with the latest version number is used._

Expand All @@ -42,8 +42,8 @@ Your project will be versioned according to the latest tag found in the commit h

#### HEAD version (final)

- If a `MINVER_MINIMUM_MAJOR_MINOR` or `MinVerMinimumMajorMinor` environment variable or MSBuild property is not set, then the HEAD version matches the candidate version.
- If a `MINVER_MINIMUM_MAJOR_MINOR` or `MinVerMinimumMajorMinor` environment variable or MSBuild property is set to `MAJOR.MINOR` or `MAJOR` (`MINOR` defaults to `0`), then:
- If a `MinVerMajorMinor` MSBuild property is not set, then the HEAD version matches the candidate version.
- If a `MinVerMajorMinor` MSBuild property is set to `MAJOR.MINOR` or `MAJOR` (`MINOR` defaults to `0`), then:
- If the candidate version is in the `MAJOR.MINOR` version range or later, then the HEAD version matches the candidate version.
- If the candidate version is in an earlier version range than `MAJOR.MINOR`, then the HEAD version is `MAJOR.MINOR.0-alpha.0.{height}`.

Expand All @@ -69,7 +69,7 @@ That means MinVer is compatible with [Git Flow](https://nvie.com/posts/a-success

### Can I include build metadata in the version?

Yes! You can specify [build metadata](https://semver.org/#spec-item-10) in an environment variable or MSBuild property named `MINVER_BUILD_METADATA` or `MinVerBuildMetadata`.
Yes! You can specify [build metadata](https://semver.org/#spec-item-10) in an environment variable named `MINVER_BUILD_METADATA`.

### Can I use the version calculated by MinVer for other purposes?

Expand Down
4 changes: 3 additions & 1 deletion targets/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public static Task Main(string[] args)
}

Environment.SetEnvironmentVariable("MINVER_BUILD_METADATA", "build.42", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("MINVER_MINIMUM_MAJOR_MINOR", "2.0", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("MINVER_VERBOSE", "true", EnvironmentVariableTarget.Process);

// normally set via an MSBuild property
Environment.SetEnvironmentVariable("MinVerMajorMinor", "2.0", EnvironmentVariableTarget.Process);

DeletePackages();

await RunAsync("dotnet", "build --no-restore", path);
Expand Down