From 51f5be1e740f8897339afbbd5916fd627c081db0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 8 Oct 2017 00:09:16 -0400 Subject: move semver implementation into shared project for reuse --- src/SMAPI/SemanticVersion.cs | 129 +++++++------------------------------------ 1 file changed, 21 insertions(+), 108 deletions(-) (limited to 'src/SMAPI') diff --git a/src/SMAPI/SemanticVersion.cs b/src/SMAPI/SemanticVersion.cs index 1b99dae6..ce86dceb 100644 --- a/src/SMAPI/SemanticVersion.cs +++ b/src/SMAPI/SemanticVersion.cs @@ -1,6 +1,6 @@ using System; -using System.Text.RegularExpressions; using Newtonsoft.Json; +using StardewModdingAPI.Common; namespace StardewModdingAPI { @@ -10,31 +10,24 @@ namespace StardewModdingAPI /********* ** Properties *********/ - /// A regular expression matching a semantic version string. - /// - /// This pattern is derived from the BNF documentation in the semver repo, - /// with three important deviations intended to support Stardew Valley mod conventions: - /// - allows short-form "x.y" versions; - /// - allows hyphens in prerelease tags as synonyms for dots (like "-unofficial-update.3"); - /// - doesn't allow '+build' suffixes. - /// - private static readonly Regex Regex = new Regex(@"^(?>(?0|[1-9]\d*))\.(?>(?0|[1-9]\d*))(?>(?:\.(?0|[1-9]\d*))?)(?:-(?(?>[a-z0-9]+[\-\.]?)+))?$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture); + /// The underlying semantic version implementation. + private readonly SemanticVersionImpl Version; /********* ** Accessors *********/ /// The major version incremented for major API changes. - public int MajorVersion { get; } + public int MajorVersion => this.Version.Major; /// The minor version incremented for backwards-compatible changes. - public int MinorVersion { get; } + public int MinorVersion => this.Version.Minor; /// The patch version for backwards-compatible bug fixes. - public int PatchVersion { get; } + public int PatchVersion => this.Version.Patch; /// An optional build tag. - public string Build { get; } + public string Build => this.Version.Tag; /********* @@ -47,32 +40,14 @@ namespace StardewModdingAPI /// An optional build tag. [JsonConstructor] public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, string build = null) - { - this.MajorVersion = majorVersion; - this.MinorVersion = minorVersion; - this.PatchVersion = patchVersion; - this.Build = this.GetNormalisedTag(build); - } + : this(new SemanticVersionImpl(majorVersion, minorVersion, patchVersion, build)) { } /// Construct an instance. /// The semantic version string. /// The is null. /// The is not a valid semantic version. public SemanticVersion(string version) - { - // parse - if (version == null) - throw new ArgumentNullException(nameof(version), "The input version string can't be null."); - var match = SemanticVersion.Regex.Match(version.Trim()); - if (!match.Success) - throw new FormatException($"The input '{version}' isn't a valid semantic version."); - - // initialise - this.MajorVersion = int.Parse(match.Groups["major"].Value); - this.MinorVersion = match.Groups["minor"].Success ? int.Parse(match.Groups["minor"].Value) : 0; - this.PatchVersion = match.Groups["patch"].Success ? int.Parse(match.Groups["patch"].Value) : 0; - this.Build = match.Groups["prerelease"].Success ? this.GetNormalisedTag(match.Groups["prerelease"].Value) : null; - } + : this(new SemanticVersionImpl(version)) { } /// Get an integer indicating whether this version precedes (less than 0), supercedes (more than 0), or is equivalent to (0) the specified version. /// The version to compare with this instance. @@ -80,56 +55,7 @@ namespace StardewModdingAPI /// The implementation is defined by Semantic Version 2.0 (http://semver.org/). public int CompareTo(ISemanticVersion other) { - if (other == null) - throw new ArgumentNullException(nameof(other)); - - const int same = 0; - const int curNewer = 1; - const int curOlder = -1; - - // compare stable versions - if (this.MajorVersion != other.MajorVersion) - return this.MajorVersion.CompareTo(other.MajorVersion); - if (this.MinorVersion != other.MinorVersion) - return this.MinorVersion.CompareTo(other.MinorVersion); - if (this.PatchVersion != other.PatchVersion) - return this.PatchVersion.CompareTo(other.PatchVersion); - if (this.Build == other.Build) - return same; - - // stable supercedes pre-release - bool curIsStable = string.IsNullOrWhiteSpace(this.Build); - bool otherIsStable = string.IsNullOrWhiteSpace(other.Build); - if (curIsStable) - return curNewer; - if (otherIsStable) - return curOlder; - - // compare two pre-release tag values - string[] curParts = this.Build.Split('.', '-'); - string[] otherParts = other.Build.Split('.', '-'); - for (int i = 0; i < curParts.Length; i++) - { - // longer prerelease tag supercedes if otherwise equal - if (otherParts.Length <= i) - return curNewer; - - // compare if different - if (curParts[i] != otherParts[i]) - { - // compare numerically if possible - { - if (int.TryParse(curParts[i], out int curNum) && int.TryParse(otherParts[i], out int otherNum)) - return curNum.CompareTo(otherNum); - } - - // else compare lexically - return string.Compare(curParts[i], otherParts[i], StringComparison.OrdinalIgnoreCase); - } - } - - // fallback (this should never happen) - return string.Compare(this.ToString(), other.ToString(), StringComparison.InvariantCultureIgnoreCase); + return this.Version.CompareTo(other.MajorVersion, other.MinorVersion, other.PatchVersion, other.Build); } /// Get whether this version is older than the specified version. @@ -190,16 +116,7 @@ namespace StardewModdingAPI /// Get a string representation of the version. public override string ToString() { - // version - string result = this.PatchVersion != 0 - ? $"{this.MajorVersion}.{this.MinorVersion}.{this.PatchVersion}" - : $"{this.MajorVersion}.{this.MinorVersion}"; - - // tag - string tag = this.Build; - if (tag != null) - result += $"-{tag}"; - return result; + return this.Version.ToString(); } /// Parse a version string without throwing an exception if it fails. @@ -208,30 +125,26 @@ namespace StardewModdingAPI /// Returns whether parsing the version succeeded. internal static bool TryParse(string version, out ISemanticVersion parsed) { - try + if (SemanticVersionImpl.TryParse(version, out SemanticVersionImpl versionImpl)) { - parsed = new SemanticVersion(version); + parsed = new SemanticVersion(versionImpl); return true; } - catch - { - parsed = null; - return false; - } + + parsed = null; + return false; } /********* ** Private methods *********/ - /// Get a normalised build tag. - /// The tag to normalise. - private string GetNormalisedTag(string tag) + /// Construct an instance. + /// The underlying semantic version implementation. + private SemanticVersion(SemanticVersionImpl version) { - tag = tag?.Trim(); - if (string.IsNullOrWhiteSpace(tag) || tag == "0") // '0' from incorrect examples in old SMAPI documentation - return null; - return tag; + this.Version = version; } + } } -- cgit