コード例 #1
0
        private static string FormatVersion(StrictSemanticVersion version)
        {
            var nuGetVersion = version as SemanticVersion;
            var legacy       = nuGetVersion != null && nuGetVersion.IsLegacyVersion;

            return(String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}{3}", version.Major, version.Minor, version.Patch,
                                 legacy ? String.Format(CultureInfo.InvariantCulture, ".{0}", nuGetVersion.Version.Revision) : null));
        }
コード例 #2
0
        /// <summary>
        /// Parse a version string
        /// </summary>
        /// <returns>false if the version is not a strict semver</returns>
        public static bool TryParse(string value, out StrictSemanticVersion version)
        {
            version = null;

            if (value != null)
            {
                Version systemVersion = null;

                var sections = ParseSections(value);

                // null indicates the string did not meet the rules
                if (sections != null &&
                    Version.TryParse(sections.Item1, out systemVersion))
                {
                    // validate the version string
                    var parts = sections.Item1.Split('.');

                    if (parts.Length != 3)
                    {
                        // versions must be 3 parts
                        return(false);
                    }

                    foreach (var part in parts)
                    {
                        if (!IsValidPart(part, false))
                        {
                            // leading zeros are not allowed
                            return(false);
                        }
                    }

                    // labels
                    if (sections.Item2 != null &&
                        !sections.Item2.All(s => IsValidPart(s, false)))
                    {
                        return(false);
                    }

                    // build metadata
                    if (sections.Item3 != null &&
                        !IsValid(sections.Item3, true))
                    {
                        return(false);
                    }

                    var ver = NormalizeVersionValue(systemVersion);

                    version = new StrictSemanticVersion(version: ver,
                                                        releaseLabels: sections.Item2,
                                                        metadata: sections.Item3 ?? string.Empty);

                    return(true);
                }
            }

            return(false);
        }
コード例 #3
0
        /// <summary>
        /// Parse a version string
        /// </summary>
        /// <returns>false if the version is not a strict semver</returns>
        public static bool TryParse(string value, out StrictSemanticVersion version)
        {
            version = null;

            if (value != null)
            {
                Version systemVersion = null;

                var sections = ParseSections(value);

                // null indicates the string did not meet the rules
                if (sections != null
                    && Version.TryParse(sections.Item1, out systemVersion))
                {
                    // validate the version string
                    var parts = sections.Item1.Split('.');

                    if (parts.Length != 3)
                    {
                        // versions must be 3 parts
                        return false;
                    }

                    foreach (var part in parts)
                    {
                        if (!IsValidPart(part, false))
                        {
                            // leading zeros are not allowed
                            return false;
                        }
                    }

                    // labels
                    if (sections.Item2 != null
                        && !sections.Item2.All(s => IsValidPart(s, false)))
                    {
                        return false;
                    }

                    // build metadata
                    if (sections.Item3 != null
                        && !IsValid(sections.Item3, true))
                    {
                        return false;
                    }

                    var ver = NormalizeVersionValue(systemVersion);

                    version = new StrictSemanticVersion(version: ver,
                        releaseLabels: sections.Item2,
                        metadata: sections.Item3 ?? string.Empty);

                    return true;
                }
            }

            return false;
        }
コード例 #4
0
        /// <summary>
        /// Parses a SemVer string using strict SemVer rules.
        /// </summary>
        public static StrictSemanticVersion Parse(string value)
        {
            StrictSemanticVersion ver = null;

            if (!TryParse(value, out ver))
            {
                throw new ArgumentException($"'{value}' is not a valid version string", nameof(value));
            }

            return(ver);
        }
コード例 #5
0
        private static string Format(char c, StrictSemanticVersion version)
        {
            string s = null;

            switch (c)
            {
            case 'N':
                s = GetNormalizedString(version);
                break;

            case 'R':
                s = version.Release;
                break;

            case 'M':
                s = version.Metadata;
                break;

            case 'V':
                s = FormatVersion(version);
                break;

            case 'x':
                s = String.Format(CultureInfo.InvariantCulture, "{0}", version.Major);
                break;

            case 'y':
                s = String.Format(CultureInfo.InvariantCulture, "{0}", version.Minor);
                break;

            case 'z':
                s = String.Format(CultureInfo.InvariantCulture, "{0}", version.Patch);
                break;

            case 'r':
                var nuGetVersion = version as SemanticVersion;
                s = String.Format(CultureInfo.InvariantCulture, "{0}", nuGetVersion != null && nuGetVersion.IsLegacyVersion ? nuGetVersion.Version.Revision : 0);
                break;
            }

            return(s);
        }
コード例 #6
0
        private static string GetNormalizedString(StrictSemanticVersion version)
        {
            var sb = new StringBuilder();

            sb.Append(Format('V', version));

            if (version.IsPrerelease)
            {
                sb.Append('-');
                sb.Append(version.Release);
            }

            if (version.HasMetadata)
            {
                sb.Append('+');
                sb.Append(version.Metadata);
            }

            return sb.ToString();
        }
コード例 #7
0
        private static string GetNormalizedString(StrictSemanticVersion version)
        {
            var sb = new StringBuilder();

            sb.Append(Format('V', version));

            if (version.IsPrerelease)
            {
                sb.Append('-');
                sb.Append(version.Release);
            }

            if (version.HasMetadata)
            {
                sb.Append('+');
                sb.Append(version.Metadata);
            }

            return(sb.ToString());
        }
コード例 #8
0
        /// <summary>
        /// Gives a hash code based on the normalized version string.
        /// </summary>
        public int GetHashCode(StrictSemanticVersion version)
        {
            if (ReferenceEquals(version, null))
            {
                return(0);
            }

            var combiner = new HashCodeCombiner();

            combiner.AddObject(version.Major);
            combiner.AddObject(version.Minor);
            combiner.AddObject(version.Patch);

            var nuGetVersion = version as SemanticVersion;

            if (nuGetVersion != null &&
                nuGetVersion.Revision > 0)
            {
                combiner.AddObject(nuGetVersion.Revision);
            }

            if (_mode == VersionComparison.Default ||
                _mode == VersionComparison.VersionRelease ||
                _mode == VersionComparison.VersionReleaseMetadata)
            {
                if (version.IsPrerelease)
                {
                    combiner.AddObject(version.Release.ToUpperInvariant());
                }
            }

            if (_mode == VersionComparison.Default || _mode == VersionComparison.VersionReleaseMetadata)
            {
                if (version.HasMetadata)
                {
                    combiner.AddObject(version.Metadata);
                }
            }

            return(combiner.CombinedHash);
        }
コード例 #9
0
        /// <summary>
        /// Gives a hash code based on the normalized version string.
        /// </summary>
        public int GetHashCode(StrictSemanticVersion version)
        {
            if (ReferenceEquals(version, null))
            {
                return 0;
            }

            var combiner = new HashCodeCombiner();

            combiner.AddObject(version.Major);
            combiner.AddObject(version.Minor);
            combiner.AddObject(version.Patch);

            var nuGetVersion = version as SemanticVersion;
            if (nuGetVersion != null
                && nuGetVersion.Revision > 0)
            {
                combiner.AddObject(nuGetVersion.Revision);
            }

            if (_mode == VersionComparison.Default
                || _mode == VersionComparison.VersionRelease
                || _mode == VersionComparison.VersionReleaseMetadata)
            {
                if (version.IsPrerelease)
                {
                    combiner.AddObject(version.Release.ToUpperInvariant());
                }
            }

            if (_mode == VersionComparison.Default ||_mode == VersionComparison.VersionReleaseMetadata)
            {
                if (version.HasMetadata)
                {
                    combiner.AddObject(version.Metadata);
                }
            }

            return combiner.CombinedHash;
        }
コード例 #10
0
        /// <summary>
        /// Compare versions.
        /// </summary>
        public int Compare(StrictSemanticVersion x, StrictSemanticVersion y)
        {
            if (ReferenceEquals(x, y))
            {
                return 0;
            }

            if (ReferenceEquals(y, null))
            {
                return 1;
            }

            if (ReferenceEquals(x, null))
            {
                return -1;
            }

            // compare version
            var result = x.Major.CompareTo(y.Major);
            if (result != 0)
            {
                return result;
            }

            result = x.Minor.CompareTo(y.Minor);
            if (result != 0)
            {
                return result;
            }

            result = x.Patch.CompareTo(y.Patch);
            if (result != 0)
            {
                return result;
            }

            var legacyX = x as SemanticVersion;
            var legacyY = y as SemanticVersion;

            result = CompareLegacyVersion(legacyX, legacyY);
            if (result != 0)
            {
                return result;
            }

            if (_mode != VersionComparison.Version)
            {
                // compare release labels
                if (x.IsPrerelease
                    && !y.IsPrerelease)
                {
                    return -1;
                }

                if (!x.IsPrerelease
                    && y.IsPrerelease)
                {
                    return 1;
                }

                if (x.IsPrerelease
                    && y.IsPrerelease)
                {
                    result = CompareReleaseLabels(x.ReleaseLabels, y.ReleaseLabels);
                    if (result != 0)
                    {
                        return result;
                    }
                }

                // compare the metadata
                if (_mode == VersionComparison.Default || _mode == VersionComparison.VersionReleaseMetadata)
                {
                    result = StringComparer.OrdinalIgnoreCase.Compare(x.Metadata ?? string.Empty, y.Metadata ?? string.Empty);
                    if (result != 0)
                    {
                        return result;
                    }
                }
            }

            return 0;
        }
コード例 #11
0
 /// <summary>
 /// Compares the given versions using the VersionComparison mode.
 /// </summary>
 public static int Compare(StrictSemanticVersion version1, StrictSemanticVersion version2, VersionComparison versionComparison)
 {
     IVersionComparer comparer = new VersionComparer(versionComparison);
     return comparer.Compare(version1, version2);
 }
コード例 #12
0
 /// <summary>
 /// Determines if both versions are equal.
 /// </summary>
 public bool Equals(StrictSemanticVersion x, StrictSemanticVersion y)
 {
     return Compare(x, y) == 0;
 }
コード例 #13
0
 /// <summary>
 /// Creates a SemanticVersion from an existing SemanticVersion
 /// </summary>
 public StrictSemanticVersion(StrictSemanticVersion version)
     : this(version.Major, version.Minor, version.Patch, version.ReleaseLabels, version.Metadata)
 {
 }
コード例 #14
0
 /// <summary>
 /// Creates a SemanticVersion from an existing SemanticVersion
 /// </summary>
 public StrictSemanticVersion(StrictSemanticVersion version)
     : this(version.Major, version.Minor, version.Patch, version.ReleaseLabels, version.Metadata)
 {
 }
コード例 #15
0
        private static string Format(char c, StrictSemanticVersion version)
        {
            string s = null;

            switch (c)
            {
                case 'N':
                    s = GetNormalizedString(version);
                    break;
                case 'R':
                    s = version.Release;
                    break;
                case 'M':
                    s = version.Metadata;
                    break;
                case 'V':
                    s = FormatVersion(version);
                    break;
                case 'x':
                    s = String.Format(CultureInfo.InvariantCulture, "{0}", version.Major);
                    break;
                case 'y':
                    s = String.Format(CultureInfo.InvariantCulture, "{0}", version.Minor);
                    break;
                case 'z':
                    s = String.Format(CultureInfo.InvariantCulture, "{0}", version.Patch);
                    break;
                case 'r':
                    var nuGetVersion = version as SemanticVersion;
                    s = String.Format(CultureInfo.InvariantCulture, "{0}", nuGetVersion != null && nuGetVersion.IsLegacyVersion ? nuGetVersion.Version.Revision : 0);
                    break;
            }

            return s;
        }
コード例 #16
0
 /// <summary>
 /// Determines if both versions are equal.
 /// </summary>
 public bool Equals(StrictSemanticVersion x, StrictSemanticVersion y)
 {
     return(Compare(x, y) == 0);
 }
コード例 #17
0
        /// <summary>
        /// Compares the given versions using the VersionComparison mode.
        /// </summary>
        public static int Compare(StrictSemanticVersion version1, StrictSemanticVersion version2, VersionComparison versionComparison)
        {
            IVersionComparer comparer = new VersionComparer(versionComparison);

            return(comparer.Compare(version1, version2));
        }
コード例 #18
0
        private static string FormatVersion(StrictSemanticVersion version)
        {
            var nuGetVersion = version as SemanticVersion;
            var legacy = nuGetVersion != null && nuGetVersion.IsLegacyVersion;

            return String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}{3}", version.Major, version.Minor, version.Patch,
                legacy ? String.Format(CultureInfo.InvariantCulture, ".{0}", nuGetVersion.Version.Revision) : null);
        }
コード例 #19
0
        /// <summary>
        /// Compare versions.
        /// </summary>
        public int Compare(StrictSemanticVersion x, StrictSemanticVersion y)
        {
            if (ReferenceEquals(x, y))
            {
                return(0);
            }

            if (ReferenceEquals(y, null))
            {
                return(1);
            }

            if (ReferenceEquals(x, null))
            {
                return(-1);
            }

            // compare version
            var result = x.Major.CompareTo(y.Major);

            if (result != 0)
            {
                return(result);
            }

            result = x.Minor.CompareTo(y.Minor);
            if (result != 0)
            {
                return(result);
            }

            result = x.Patch.CompareTo(y.Patch);
            if (result != 0)
            {
                return(result);
            }

            var legacyX = x as SemanticVersion;
            var legacyY = y as SemanticVersion;

            result = CompareLegacyVersion(legacyX, legacyY);
            if (result != 0)
            {
                return(result);
            }

            if (_mode != VersionComparison.Version)
            {
                // compare release labels
                if (x.IsPrerelease &&
                    !y.IsPrerelease)
                {
                    return(-1);
                }

                if (!x.IsPrerelease &&
                    y.IsPrerelease)
                {
                    return(1);
                }

                if (x.IsPrerelease &&
                    y.IsPrerelease)
                {
                    result = CompareReleaseLabels(x.ReleaseLabels, y.ReleaseLabels);
                    if (result != 0)
                    {
                        return(result);
                    }
                }

                // compare the metadata
                if (_mode == VersionComparison.Default || _mode == VersionComparison.VersionReleaseMetadata)
                {
                    result = StringComparer.OrdinalIgnoreCase.Compare(x.Metadata ?? string.Empty, y.Metadata ?? string.Empty);
                    if (result != 0)
                    {
                        return(result);
                    }
                }
            }

            return(0);
        }