Skip to content

Commit

Permalink
include build metadata in informational version only
Browse files Browse the repository at this point in the history
  • Loading branch information
adamralph committed Sep 14, 2023
1 parent 29a44a8 commit f9121b5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 22 deletions.
7 changes: 5 additions & 2 deletions MinVer/build/MinVer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@
<MinVerBuildMetadata Condition="$(MinVerVersion.Contains(`+`))">$(MinVerVersion.Split(`+`, 2)[1])</MinVerBuildMetadata>
<AssemblyVersion>$(MinVerMajor).0.0.0</AssemblyVersion>
<FileVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch).0</FileVersion>
<PackageVersion>$(MinVerVersion)</PackageVersion>
<Version>$(MinVerVersion)</Version>
<InformationalVersion>$(MinVerVersion)</InformationalVersion>
<PackageVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch)</PackageVersion>
<PackageVersion Condition="'$(MinVerPreRelease)' != ''">$(PackageVersion)-$(MinVerPreRelease)</PackageVersion>
<Version>$(PackageVersion)</Version>
</PropertyGroup>
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] MinVerVersion=$(MinVerVersion)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] MinVerMajor=$(MinVerMajor)" />
Expand All @@ -73,6 +75,7 @@
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] MinVerBuildMetadata=$(MinVerBuildMetadata)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] AssemblyVersion=$(AssemblyVersion)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] FileVersion=$(FileVersion)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] InformationalVersion=$(InformationalVersion)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] PackageVersion=$(PackageVersion)" />
<Message Importance="$(MinVerDetailed)" Text="MinVer: [output] Version=$(Version)" />
</Target>
Expand Down
7 changes: 4 additions & 3 deletions MinVerTests.Infra/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace MinVerTests.Infra
{
public record Package(string Version, AssemblyVersion AssemblyVersion, FileVersion FileVersion)
public record Package(string Version, AssemblyVersion AssemblyVersion, FileVersion FileVersion, string InformationalVersion)
{
public static Package WithVersion(int major, int minor, int patch, IEnumerable<string>? preReleaseIdentifiers = null, int height = 0, string buildMetadata = "")
{
var preReleaseToken = preReleaseIdentifiers == null ? "" : GetPreReleaseToken(preReleaseIdentifiers.ToList());
var heightToken = height == 0 ? "" : $".{height}";
var buildMetadataToken = string.IsNullOrEmpty(buildMetadata) ? "" : $"+{buildMetadata}";

var version = $"{major}.{minor}.{patch}{preReleaseToken}{heightToken}{buildMetadataToken}";
var version = $"{major}.{minor}.{patch}{preReleaseToken}{heightToken}";
var informationalVersion = $"{major}.{minor}.{patch}{preReleaseToken}{heightToken}{buildMetadataToken}";

return new Package(version, new AssemblyVersion(major, 0, 0, 0), new FileVersion(major, minor, patch, 0, version));
return new Package(version, new AssemblyVersion(major, 0, 0, 0), new FileVersion(major, minor, patch, 0, informationalVersion), informationalVersion);
}

private static string GetPreReleaseToken(IReadOnlyList<string> preReleaseIdentifiers) => preReleaseIdentifiers.Any() ? $"-{string.Join(".", preReleaseIdentifiers)}" : "";
Expand Down
12 changes: 8 additions & 4 deletions MinVerTests.Infra/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;
using Microsoft.Extensions.FileSystemGlobbing;
Expand Down Expand Up @@ -171,23 +172,26 @@ private static async Task<Package> GetPackage(string fileName)

var assemblyFileName = Directory.EnumerateFiles(extractedDirectoryName, "*.dll", new EnumerationOptions { RecurseSubdirectories = true, }).First();

var systemAssemblyVersion = GetAssemblyVersion(assemblyFileName);
var (systemAssemblyVersion, informationalVersion) = GetAssemblyVersions(assemblyFileName);
var assemblyVersion = new AssemblyVersion(systemAssemblyVersion.Major, systemAssemblyVersion.Minor, systemAssemblyVersion.Build, systemAssemblyVersion.Revision);

var fileVersionInfo = FileVersionInfo.GetVersionInfo(assemblyFileName);
var fileVersion = new FileVersion(fileVersionInfo.FileMajorPart, fileVersionInfo.FileMinorPart, fileVersionInfo.FileBuildPart, fileVersionInfo.FilePrivatePart, fileVersionInfo.ProductVersion ?? "");

return new Package(nuspecVersion, assemblyVersion, fileVersion);
return new Package(nuspecVersion, assemblyVersion, fileVersion, informationalVersion);
}

private static Version GetAssemblyVersion(string assemblyFileName)
private static (Version Version, string InformationalVersion) GetAssemblyVersions(string assemblyFileName)
{
var assemblyLoadContext = new AssemblyLoadContext(default, true);
var assembly = assemblyLoadContext.LoadFromAssemblyPath(assemblyFileName);

try
{
return assembly.GetName().Version ?? throw new InvalidOperationException("The assembly version is null.");
return (
assembly.GetName().Version ?? throw new InvalidOperationException("The assembly version is null."),
assembly.GetCustomAttributes().OfType<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion ??
throw new InvalidOperationException("The assembly has no informational version."));
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion MinVerTests.Packages/AnnotatedTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public static async Task HasTagVersion()

// assert
Assert.Equal(expected, actual);
Assert.Equal(expected.Version, cliStandardOutput.Trim());
Assert.Equal(expected.InformationalVersion, cliStandardOutput.Trim());
}
}
2 changes: 1 addition & 1 deletion MinVerTests.Packages/BuildMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static async Task HasBuildMetadata()

// assert
Assert.Equal(expected, actual);
Assert.Equal(expected.Version, cliStandardOutput.Trim());
Assert.Equal(expected.InformationalVersion, cliStandardOutput.Trim());
}
}
5 changes: 3 additions & 2 deletions MinVerTests.Packages/OutputVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static async Task AreSet()
Assert.Contains("MinVer: [output] MinVerBuildMetadata=build.6", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] AssemblyVersion=2.0.0.0", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] FileVersion=2.3.4.0", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] PackageVersion=2.3.4-alpha-x.5+build.6", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] Version=2.3.4-alpha-x.5+build.6", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] InformationalVersion=2.3.4-alpha-x.5+build.6", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] PackageVersion=2.3.4-alpha-x.5", standardOutput, StringComparison.Ordinal);
Assert.Contains("MinVer: [output] Version=2.3.4-alpha-x.5", standardOutput, StringComparison.Ordinal);
}
}
2 changes: 1 addition & 1 deletion MinVerTests.Packages/TagWithBuildMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public static async Task HasTagVersion()

// assert
Assert.Equal(expected, actual);
Assert.Equal(expected.Version, cliStandardOutput.Trim());
Assert.Equal(expected.InformationalVersion, cliStandardOutput.Trim());
}
}
2 changes: 1 addition & 1 deletion MinVerTests.Packages/VersionOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static async Task HasVersionOverride()

// assert
Assert.Equal(expected, actual);
Assert.Equal(expected.Version, cliStandardOutput.Trim());
Assert.Equal(expected.InformationalVersion, cliStandardOutput.Trim());
}
}
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ MinVer sets the following custom properties:

Those properties are used to set the following .NET SDK properties, satisfying the official [open-source library guidance for version numbers](https://docs.microsoft.com/en-ca/dotnet/standard/library-guidance/versioning#version-numbers):

| Property | Value |
| ----------------- | --------------------------------------------- |
| `AssemblyVersion` | `{MinVerMajor}.0.0.0` |
| `FileVersion` | `{MinVerMajor}.{MinVerMinor}.{MinVerPatch}.0` |
| `PackageVersion` | `{MinVerVersion}` |
| `Version` | `{MinVerVersion}` |
| Property | Value |
|------------------------|-----------------------------------------------------------------------------------------------------------------|
| `AssemblyVersion` | `{MinVerMajor}.0.0.0` |
| `FileVersion` | `{MinVerMajor}.{MinVerMinor}.{MinVerPatch}.0` |
| `InformationalVersion` | `{MinVerVersion}` |
| `PackageVersion` | `{MinVerMajor}.{MinVerMinor}.{MinVerPatch}` (or `{MinVerMajor}.{MinVerMinor}.{MinVerPatch}-{MinVerPreRelease}`) |
| `Version` | `{MinVerMajor}.{MinVerMinor}.{MinVerPatch}` (or `{MinVerMajor}.{MinVerMinor}.{MinVerPatch}-{MinVerPreRelease}`) |

This behaviour can be [customised](#can-i-use-the-version-calculated-by-minver-for-other-purposes).

Expand Down Expand Up @@ -249,6 +250,17 @@ environment:
You can also specify build metadata in a version tag. If the tag is on the current commit, its build metadata will be used. If the tag is on an older commit, its build metadata will be ignored. Build metadata in `MinVerBuildMetadata` will be appended to build metadata in the tag.

Build metadata is only included in the [assembly informational version](https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/versioning#assembly-informational-version). You can include it elsewhere using a custom target. E.g.:

```xml
<Target Name="MyTarget" AfterTargets="MinVer" Condition="'$(MinVerBuildMetadata)' != ''" >
<PropertyGroup>
<PackageVersion>$(PackageVersion)+$(MinVerBuildMetadata)</PackageVersion>
<Version>$(PackageVersion)</Version>
</PropertyGroup>
</Target>
```

### Can I auto-increment the minor or major version after an RTM tag instead of the patch version?

Yes! Specify which part of the version to auto-increment with `MinVerAutoIncrement`. By default, [MinVer will auto-increment the patch version](#how-it-works), but you can specify `minor` or `major` to increment the minor or major version instead.
Expand Down Expand Up @@ -277,7 +289,6 @@ For example, for pull requests, you may want to inject the pull request number a
<Target Name="MyTarget" AfterTargets="MinVer" Condition="'$(APPVEYOR_PULL_REQUEST_NUMBER)' != ''" >
<PropertyGroup>
<PackageVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch)-pr.$(APPVEYOR_PULL_REQUEST_NUMBER).build-id.$(APPVEYOR_BUILD_ID).$(MinVerPreRelease)</PackageVersion>
<PackageVersion Condition="'$(MinVerBuildMetadata)' != ''">$(PackageVersion)+$(MinVerBuildMetadata)</PackageVersion>
<Version>$(PackageVersion)</Version>
</PropertyGroup>
</Target>
Expand Down

0 comments on commit f9121b5

Please sign in to comment.