Skip to content

Commit 00a9b3c

Browse files
committed
Emit a warning when SemVer 2.0 format version is used with explicit SemVer 1.0
Should the user explicitly set `nugetPackageVersion.semVer` to `1` but use a SemVer 2 prerelease in their `version` (such as: `1.0.0-preview.{height}) a warning will now be displayed telling the user they are probably not getting what they expected. Fixes #318
1 parent 7551e58 commit 00a9b3c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,22 @@ public IDictionary<string, string> CloudBuildVersionVars
385385
/// </summary>
386386
public int SemVer1NumericIdentifierPadding => this.VersionOptions?.SemVer1NumericIdentifierPaddingOrDefault ?? 4;
387387

388+
/// <summary>
389+
/// Gets the SemVer 1 format without padding or the build metadata.
390+
/// </summary>
391+
public string SemVer1WithoutPaddingOrBuildMetadata =>
392+
$"{this.Version.ToStringSafe(3)}{MakePrereleaseSemVer1Compliant(this.PrereleaseVersion, 0)}";
393+
394+
/// <summary>
395+
/// Determines if it was likely the user misconfigured their the prerelease version options and
396+
/// their semver setting.
397+
/// </summary>
398+
/// <returns>False when NuGet SemVer 1 explicitly set but the prerelease does not match.</returns>
399+
public bool MisconfiguredPrereleaseAndSemVer1()
400+
{
401+
return this.VersionOptions != null && this.VersionOptions.NuGetPackageVersion != null && this.VersionOptions.NuGetPackageVersion.SemVer == 1 && this.PrereleaseVersion != MakePrereleaseSemVer1Compliant(this.PrereleaseVersion, 0);
402+
}
403+
388404
/// <summary>
389405
/// Gets the build metadata, compliant to the NuGet-compatible subset of SemVer 1.0.
390406
/// </summary>

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ protected override bool ExecuteInner()
202202
oracle.BuildMetadata.AddRange(this.BuildMetadata);
203203
}
204204

205+
if (oracle.MisconfiguredPrereleaseAndSemVer1())
206+
{
207+
this.Log.LogWarning("The 'nugetPackageVersion' is explicitly set to 'semVer': 1 but the prerelease version '{0}' is not SemVer1 compliant. Change the 'nugetPackageVersion'.'semVer' value to 2 or chagne the 'version' member to follow SemVer1 (like: '{1}').", oracle.PrereleaseVersion, oracle.SemVer1WithoutPaddingOrBuildMetadata);
208+
}
209+
205210
this.PublicRelease = oracle.PublicRelease;
206211
this.Version = oracle.Version.ToString();
207212
this.AssemblyVersion = oracle.AssemblyVersion.ToString();

0 commit comments

Comments
 (0)