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

add MinVerVersionOverride #181

Merged
merged 1 commit into from
Jan 1, 2019
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
2 changes: 2 additions & 0 deletions MinVer.Lib/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public Version AddBuildMetadata(string buildMetadata)
return new Version(this.Major, this.Minor, this.Patch, this.preReleaseIdentifiers, this.height, $"{this.buildMetadata}{separator}{buildMetadata}");
}

public static bool TryParse(string text, out Version version) => (version = ParseOrDefault(text, default)) != default;

public static Version ParseOrDefault(string text, string prefix) =>
text == default || !text.StartsWith(prefix ?? "") ? default : ParseOrDefault(text.Substring(prefix?.Length ?? 0));

Expand Down
5 changes: 5 additions & 0 deletions MinVer/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static void ErrorInvalidMinMajorMinor(string minMajorMinor) =>
public static void ErrorInvalidVerbosity(string verbosity) =>
Error(1004, $"Invalid verbosity '{verbosity}'. The value must be {VerbosityMap.ValidValue}.");

#if MINVER
public static void ErrorInvalidVersionOverride(string versionOverride) =>
Error(1005, $"Invalid version override '{versionOverride}'");
#endif

private static void Error(int code, string message) => Message($"error MINVER{code:D4} : {message}");

private static void Message(string message) => Console.Error.WriteLine($"MinVer: {message}");
Expand Down
1 change: 1 addition & 0 deletions MinVer/MinVer.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<DefineConstants>$(DefineConstants);MINVER</DefineConstants>
<Description>Minimalistic versioning for .NET SDK-style projects using Git tags.</Description>
<DevelopmentDependency>true</DevelopmentDependency>
<IncludeBuildOutput>false</IncludeBuildOutput>
Expand Down
2 changes: 2 additions & 0 deletions MinVer/build/MinVer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MSBuildProjectDirectory=$(MSBuildProjectDirectory)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerTagPrefix=$(MinVerTagPrefix)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerVerbosity=$(MinVerVerbosity)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [input] MinVerVersionOverride=$(MinVerVersionOverride)" />
<ItemGroup>
<MinVerInputs Include="--build-metadata &quot;$(MinVerBuildMetadata)&quot;" />
<MinVerInputs Include="--minimum-major-minor &quot;$(MinVerMinimumMajorMinor)&quot;" />
<MinVerInputs Include="--repo &quot;$(MSBuildProjectDirectory)&quot;" />
<MinVerInputs Include="--tag-prefix &quot;$(MinVerTagPrefix)&quot;" />
<MinVerInputs Include="--verbosity &quot;$(MinVerVerbosity)&quot;" />
<MinVerInputs Include="--version-override &quot;$(MinVerVersionOverride)&quot;" />
</ItemGroup>
<Exec Command="dotnet &quot;$(MSBuildThisFileDirectory)../minver/MinVer.dll&quot; @(MinVerInputs->'%(Identity)', ' ')" ConsoleToMSBuild="true" StandardOutputImportance="Low" >
<Output TaskParameter="ConsoleOutput" ItemName="MinVerConsoleOutput" />
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Options can be specified as either MSBuild properties or environment variables.
- [`MinVerMinimumMajorMinor`](#can-i-bump-the-major-or-minor-version) (this is still named `MinVerMajorMinor` in 1.0.0-beta.1, the new name will be used in 1.0.0-beta.2)
- [`MinVerTagPrefix`](#can-i-prefix-my-tag-names)
- [`MinVerVerbosity`](#can-I-get-log-output-to-see-how-minver-calculates-the-version)
- [`MinVerVersionOverride`](#can-i-use-minver-to-version-software-which-is-not-built-using-a-net-sdk-style-project)

Note that the option names are case-insensitive.

Expand Down Expand Up @@ -205,6 +206,8 @@ In a future version of MinVer, the verbosity level may be inherited from MSBuild

Yes! MinVer is also available as a [command line tool](https://www.nuget.org/packages/minver-cli). Run `minver --help` for usage. The calculated version is printed to standard output (stdout).

Sometimes you may want to version both .NET projects and other outputs, such as non-.NET projects, or a container image, in the same build. In those scenarios, you should use both the command line tool _and_ the regular MinVer package. Before building any .NET projects, your build script should run the command line tool and set the [`MINVERVERSIONOVERRIDE`](#options) environment variable to the calculated version. The MinVer package will then use that value rather than calculating the version a second time. This ensures that the command line tool and the MinVer package produce the same version.

### What if the history diverges, and more than one tag is found?

The tag with the higher version is used.
Expand Down
21 changes: 21 additions & 0 deletions minver-cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ private static int Main(string[] args)
var repoOrWorkDirOption = app.Option("-r|--repo <REPO>", "Repository or working directory.", CommandOptionType.SingleValue);
var tagPrefixOption = app.Option("-t|--tag-prefix <TAG_PREFIX>", "", CommandOptionType.SingleValue);
var verbosityOption = app.Option("-v|--verbosity <VERBOSITY>", VerbosityMap.ValidValue, CommandOptionType.SingleValue);
#if MINVER
var versionOverrideOption = app.Option("-o|--version-override <VERSION>", "", CommandOptionType.SingleValue);
#endif

app.OnExecute(() =>
{
Expand All @@ -43,7 +46,25 @@ private static int Main(string[] args)
log.Debug($"MinVer {informationalVersion}.");
}

#if MINVER
Lib.Version version;
if (!string.IsNullOrEmpty(versionOverrideOption.Value()))
{
if (!Lib.Version.TryParse(versionOverrideOption.Value(), out version))
{
Logger.ErrorInvalidVersionOverride(versionOverrideOption.Value());
return 2;
}

log.Info($"Using version override {version}.");
}
else
{
version = Versioner.GetVersion(repoOrWorkDir, tagPrefixOption.Value(), minMajorMinor, buildMetaOption.Value(), log);
}
#else
var version = Versioner.GetVersion(repoOrWorkDir, tagPrefixOption.Value(), minMajorMinor, buildMetaOption.Value(), log);
#endif

Console.Out.WriteLine(version);

Expand Down
22 changes: 21 additions & 1 deletion targets/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,27 @@ public static async Task Main(string[] args)
}
});

Target("test-package", DependsOn("test-package-minimum-major-minor"));
Target(
"test-package-version-override",
DependsOn("test-package-minimum-major-minor"),
async () =>
{
using (var repo = new Repository(testRepo))
{
// arrange
Environment.SetEnvironmentVariable("MinVerVersionOverride", "3.2.1-rc.4+build.5", EnvironmentVariableTarget.Process);

var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-version-override");

// act
await CleanAndPack(testRepo, output, "diagnostic");

// assert
AssertPackageFileNameContains("3.2.1-rc.4.nupkg", output);
}
});

Target("test-package", DependsOn("test-package-version-override"));

await RunTargetsAndExitAsync(args);
}
Expand Down