Skip to content

Commit fea1a21

Browse files
committed
Add "precision" settting to the "nugetPackageVersion" section in the version.json file
1 parent e2795b2 commit fea1a21

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

doc/versionJson.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The content of the version.json file is a JSON serialized object with these prop
3939
"gitCommitIdShortAutoMinimum": 0, // optional. Set to use the short commit ID abbreviation provided by the git repository.
4040
"nugetPackageVersion": {
4141
"semVer": 1 // optional. Set to either 1 or 2 to control how the NuGet package version string is generated. Default is 1.
42+
"precision": "build" // optional. Use when you want to a more or less precise package version than the default major.minor.build.
4243
},
4344
"pathFilters": [
4445
// optional list of paths to consider when calculating version height.

src/NerdBank.GitVersioning.Tests/VersionFileTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,38 @@ public void GetVersion_ProducesAbsolutePath()
608608
Assert.True(Path.IsPathRooted(actualDirectory));
609609
}
610610

611+
612+
[Theory]
613+
[InlineData(1)]
614+
[InlineData(2)]
615+
public void GetVersion_ReadNuGetPackageVersionSettings_SemVer(int semVer)
616+
{
617+
var json = $@"{{ ""version"" : ""1.0"", ""nugetPackageVersion"" : {{ ""semVer"" : {semVer} }} }}";
618+
var path = Path.Combine(this.RepoPath, "version.json");
619+
File.WriteAllText(path, json);
620+
621+
var versionOptions = this.Context.VersionFile.GetVersion();
622+
623+
Assert.NotNull(versionOptions.NuGetPackageVersion);
624+
Assert.NotNull(versionOptions.NuGetPackageVersion.SemVer);
625+
Assert.Equal(semVer, versionOptions.NuGetPackageVersion.SemVer);
626+
}
627+
628+
[Theory]
629+
[CombinatorialData]
630+
public void GetVersion_ReadNuGetPackageVersionSettings_Precision(VersionOptions.VersionPrecision precision)
631+
{
632+
var json = $@"{{ ""version"" : ""1.0"", ""nugetPackageVersion"" : {{ ""precision"" : ""{precision}"" }} }}";
633+
var path = Path.Combine(this.RepoPath, "version.json");
634+
File.WriteAllText(path, json);
635+
636+
var versionOptions = this.Context.VersionFile.GetVersion();
637+
638+
Assert.NotNull(versionOptions.NuGetPackageVersion);
639+
Assert.NotNull(versionOptions.NuGetPackageVersion.Precision);
640+
Assert.Equal(precision, versionOptions.NuGetPackageVersion.Precision);
641+
}
642+
611643
private void AssertPathHasVersion(string committish, string absolutePath, VersionOptions expected)
612644
{
613645
var actual = this.GetVersionOptions(absolutePath, committish);

src/NerdBank.GitVersioning.Tests/VersionOptionsTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public void CannotWriteToDefaultInstances()
178178
Assert.Throws<InvalidOperationException>(() => options.CloudBuildOrDefault.BuildNumberOrDefault.IncludeCommitIdOrDefault.Where = VersionOptions.CloudBuildNumberCommitWhere.BuildMetadata);
179179
Assert.Throws<InvalidOperationException>(() => options.CloudBuildOrDefault.SetVersionVariables = true);
180180
Assert.Throws<InvalidOperationException>(() => options.NuGetPackageVersionOrDefault.SemVer = 2);
181+
Assert.Throws<InvalidOperationException>(() => options.NuGetPackageVersionOrDefault.Precision = VersionOptions.VersionPrecision.Revision);
181182
Assert.Throws<InvalidOperationException>(() => options.ReleaseOrDefault.BranchName = "BranchName");
182183
Assert.Throws<InvalidOperationException>(() => options.ReleaseOrDefault.VersionIncrement = VersionOptions.ReleaseVersionIncrement.Major);
183184
Assert.Throws<InvalidOperationException>(() => options.ReleaseOrDefault.FirstUnstableTag = "-tag");
@@ -226,4 +227,30 @@ public void ReleaseOptions_Equality()
226227
Assert.NotEqual(ro3, ro5);
227228
Assert.NotEqual(ro5, ro6);
228229
}
230+
231+
[Fact]
232+
public void NuGetPackageVersionOptions_Equality()
233+
{
234+
var npvo1a = new VersionOptions.NuGetPackageVersionOptions { };
235+
var npvo1b = new VersionOptions.NuGetPackageVersionOptions { };
236+
Assert.Equal(npvo1a, npvo1b);
237+
238+
var npvo2a = new VersionOptions.NuGetPackageVersionOptions
239+
{
240+
SemVer = 2
241+
};
242+
Assert.NotEqual(npvo2a, npvo1a);
243+
244+
var npvo3a = new VersionOptions.NuGetPackageVersionOptions
245+
{
246+
Precision = VersionOptions.VersionPrecision.Revision
247+
};
248+
Assert.NotEqual(npvo3a, npvo1a);
249+
250+
var npvo4a = new VersionOptions.NuGetPackageVersionOptions
251+
{
252+
Precision = VersionOptions.VersionPrecision.Build
253+
};
254+
Assert.Equal(npvo4a, npvo1a); // Equal because we haven't changed defaults.
255+
}
229256
}

src/NerdBank.GitVersioning/VersionOptions.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ public class NuGetPackageVersionOptions : IEquatable<NuGetPackageVersionOptions>
584584
{
585585
isFrozen = true,
586586
semVer = 1.0f,
587+
precision = VersionPrecision.Build
587588
};
588589

589590
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
@@ -592,6 +593,9 @@ public class NuGetPackageVersionOptions : IEquatable<NuGetPackageVersionOptions>
592593
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
593594
private float? semVer;
594595

596+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
597+
private VersionPrecision? precision;
598+
595599
/// <summary>
596600
/// Initializes a new instance of the <see cref="NuGetPackageVersionOptions" /> class.
597601
/// </summary>
@@ -623,6 +627,22 @@ public float? SemVer
623627
[JsonIgnore]
624628
public float? SemVerOrDefault => this.SemVer ?? DefaultInstance.SemVer;
625629

630+
/// <summary>
631+
/// Gets or sets number of version components to include when generating the package version.
632+
/// </summary>
633+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
634+
public VersionPrecision? Precision
635+
{
636+
get => this.precision;
637+
set => this.SetIfNotReadOnly(ref this.precision, value);
638+
}
639+
640+
/// <summary>
641+
/// Gets the number of version components to include when generating the package version.
642+
/// </summary>
643+
[JsonIgnore]
644+
public VersionPrecision PrecisionOrDefault => this.Precision ?? DefaultInstance.Precision!.Value;
645+
626646
/// <summary>
627647
/// Gets a value indicating whether this instance rejects all attempts to mutate it.
628648
/// </summary>
@@ -679,13 +699,22 @@ public bool Equals(NuGetPackageVersionOptions? x, NuGetPackageVersionOptions? y)
679699
return false;
680700
}
681701

682-
return x.SemVerOrDefault == y.SemVerOrDefault;
702+
return x.SemVerOrDefault == y.SemVerOrDefault &&
703+
x.PrecisionOrDefault == y.PrecisionOrDefault;
683704
}
684705

685706
/// <inheritdoc />
686707
public int GetHashCode(NuGetPackageVersionOptions? obj)
687708
{
688-
return obj?.SemVerOrDefault.GetHashCode() ?? 0;
709+
if (obj is null)
710+
return 0;
711+
712+
unchecked
713+
{
714+
var hash = obj.SemVerOrDefault.GetHashCode() * 397;
715+
hash ^= obj.PrecisionOrDefault.GetHashCode();
716+
return hash;
717+
}
689718
}
690719
}
691720
}

src/NerdBank.GitVersioning/version.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
"default": 1,
109109
"minimum": 1,
110110
"maximum": 2
111+
},
112+
"precision": {
113+
"type": "string",
114+
"description": "Specifies the number of components to include in the NuGet package version.",
115+
"enum": [ "major", "minor", "build", "revision" ],
116+
"default": "build"
111117
}
112118
}
113119
},

0 commit comments

Comments
 (0)