Skip to content

Commit 5296101

Browse files
committed
Allow AssemblyVersion to be set explicitly without being altered by the rest of the computed build version
**Breaking change**: The `assemblyVersion.precision` property in the `version.json` file is now ignored if `assemblyVersion.version` is set to a non-null value. When set, `assemblyVersion.version` is taken literally. Specifying `assemblyVersion.precision` alone will still set the assembly version based on the ordinary computed `version` as it did before. The schema has been updated to reflect that specifying `assemblyVersion.version` *and* `assemblyVersion.precision` is an error. Fixes #703
1 parent d486cf6 commit 5296101

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,13 +1009,19 @@ private static Version GetExpectedAssemblyVersion(VersionOptions versionOptions,
10091009
{
10101010
// Function should be very similar to VersionOracle.GetAssemblyVersion()
10111011
var assemblyVersion = (versionOptions?.AssemblyVersion?.Version ?? versionOptions.Version.Version).EnsureNonNegativeComponents();
1012-
var precision = versionOptions?.AssemblyVersion?.Precision ?? VersionOptions.DefaultVersionPrecision;
10131012

1014-
assemblyVersion = new System.Version(
1015-
assemblyVersion.Major,
1016-
precision >= VersionOptions.VersionPrecision.Minor ? assemblyVersion.Minor : 0,
1017-
precision >= VersionOptions.VersionPrecision.Build ? version.Build : 0,
1018-
precision >= VersionOptions.VersionPrecision.Revision ? version.Revision : 0);
1013+
if (versionOptions?.AssemblyVersion?.Version is null)
1014+
{
1015+
VersionOptions.VersionPrecision precision = versionOptions?.AssemblyVersion?.Precision ?? VersionOptions.DefaultVersionPrecision;
1016+
assemblyVersion = version;
1017+
1018+
assemblyVersion = new Version(
1019+
assemblyVersion.Major,
1020+
precision >= VersionOptions.VersionPrecision.Minor ? assemblyVersion.Minor : 0,
1021+
precision >= VersionOptions.VersionPrecision.Build ? assemblyVersion.Build : 0,
1022+
precision >= VersionOptions.VersionPrecision.Revision ? assemblyVersion.Revision : 0);
1023+
}
1024+
10191025
return assemblyVersion;
10201026
}
10211027

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,32 @@ public void SemVerStableNonPublicVersionShortened()
259259
Assert.Matches(@"^2.3.1-g[a-f0-9]{7}$", oracle.ChocolateyPackageVersion);
260260
}
261261

262+
[Theory]
263+
[InlineData("1.2.0.0", null, null)]
264+
[InlineData("1.0.0.0", null, VersionOptions.VersionPrecision.Major)]
265+
[InlineData("1.2.0.0", null, VersionOptions.VersionPrecision.Minor)]
266+
[InlineData("1.2.1.0", null, VersionOptions.VersionPrecision.Build)]
267+
[InlineData("2.3.4.0", "2.3.4", null)]
268+
[InlineData("2.3.4.0", "2.3.4", VersionOptions.VersionPrecision.Minor)]
269+
[InlineData("2.3.4.0", "2.3.4", VersionOptions.VersionPrecision.Build)]
270+
[InlineData("2.3.4.0", "2.3.4.0", VersionOptions.VersionPrecision.Revision)]
271+
public void CustomAssemblyVersion(string expectedAssemblyVersion, string prescribedAssemblyVersion, VersionOptions.VersionPrecision? precision)
272+
{
273+
this.InitializeSourceControl(withInitialCommit: false);
274+
this.WriteVersionFile(new VersionOptions
275+
{
276+
Version = new SemanticVersion("1.2"),
277+
AssemblyVersion = new VersionOptions.AssemblyVersionOptions
278+
{
279+
Version = prescribedAssemblyVersion is object ? new Version(prescribedAssemblyVersion) : null,
280+
Precision = precision,
281+
},
282+
});
283+
284+
VersionOracle oracle = this.GetVersionOracle();
285+
Assert.Equal(expectedAssemblyVersion, oracle.AssemblyVersion.ToString());
286+
}
287+
262288
[Fact]
263289
public void DefaultNuGetPackageVersionIsSemVer1PublicRelease()
264290
{

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,25 @@ private static Version GetAssemblyVersion(Version version, VersionOptions? versi
444444
// If there is no repo, "version" could have uninitialized components (-1).
445445
version = version.EnsureNonNegativeComponents();
446446

447-
var assemblyVersion = versionOptions?.AssemblyVersionOrDefault.Version ?? new System.Version(version.Major, version.Minor);
448-
var precision = versionOptions?.AssemblyVersionOrDefault.PrecisionOrDefault;
449-
450-
assemblyVersion = new System.Version(
451-
assemblyVersion.Major,
452-
precision >= VersionOptions.VersionPrecision.Minor ? assemblyVersion.Minor : 0,
453-
precision >= VersionOptions.VersionPrecision.Build ? version.Build : 0,
454-
precision >= VersionOptions.VersionPrecision.Revision ? version.Revision : 0);
447+
Version assemblyVersion;
448+
449+
if (versionOptions?.AssemblyVersion?.Version is not null)
450+
{
451+
// When specified explicitly, use the assembly version as the user defines it.
452+
assemblyVersion = versionOptions.AssemblyVersion.Version;
453+
}
454+
else
455+
{
456+
// Otherwise consider precision to base the assembly version off of the main computed version.
457+
VersionOptions.VersionPrecision precision = versionOptions?.AssemblyVersion?.Precision ?? VersionOptions.DefaultVersionPrecision;
458+
459+
assemblyVersion = new Version(
460+
version.Major,
461+
precision >= VersionOptions.VersionPrecision.Minor ? version.Minor : 0,
462+
precision >= VersionOptions.VersionPrecision.Build ? version.Build : 0,
463+
precision >= VersionOptions.VersionPrecision.Revision ? version.Revision : 0);
464+
}
465+
455466
return assemblyVersion.EnsureNonNegativeComponents(4);
456467
}
457468

src/NerdBank.GitVersioning/version.schema.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@
4444
{ "$ref": "#/definitions/twoToFourComponentVersion" },
4545
{
4646
"type": "object",
47+
"additionalProperties": false,
48+
"properties": {
49+
"version": { "$ref": "#/definitions/twoToFourComponentVersion" }
50+
}
51+
},
52+
{
53+
"type": "object",
54+
"additionalProperties": false,
4755
"properties": {
48-
"version": { "$ref": "#/definitions/twoToFourComponentVersion" },
4956
"precision": {
5057
"type": "string",
5158
"description": "Identifies the last component to be explicitly set in the version.",

0 commit comments

Comments
 (0)