Пример #1
0
        /// <summary>
        /// Gives a hash code based on the normalized version string.
        /// </summary>
        public int GetHashCode(SimpleVersion obj)
        {
            if (Object.ReferenceEquals(obj, null))
            {
                return(0);
            }

            string verString = string.Empty;

            VersionFormatter formatter = new VersionFormatter();

            if (_mode == VersionComparison.Default || _mode == VersionComparison.VersionRelease)
            {
                verString = obj.ToString("V-R", formatter).ToUpperInvariant();
            }
            else if (_mode == VersionComparison.Version)
            {
                verString = obj.ToString("V", formatter);
            }
            else if (_mode == VersionComparison.VersionReleaseMetadata)
            {
                verString = String.Format(CultureInfo.InvariantCulture, "{0}+{1}",
                                          obj.ToString("V-R", formatter).ToUpperInvariant(),
                                          obj.ToString("M", formatter));
            }

            if (String.IsNullOrEmpty(verString))
            {
                verString = obj.ToNormalizedString().ToUpperInvariant();
            }

            return(verString.GetHashCode());
        }
Пример #2
0
        private static bool?IsPrerelease(SimpleVersion version)
        {
            bool?b = null;

            SemanticVersion semVer = version as SemanticVersion;

            if (semVer != null)
            {
                b = semVer.IsPrerelease;
            }

            return(b);
        }
Пример #3
0
        /// <summary>
        /// Creates a VersionRange with the given min and max.
        /// </summary>
        /// <param name="minVersion">Lower bound of the version range.</param>
        /// <param name="includeMinVersion">True if minVersion satisfies the condition.</param>
        /// <param name="maxVersion">Upper bound of the version range.</param>
        /// <param name="includeMaxVersion">True if maxVersion satisfies the condition.</param>
        /// <param name="includePrerelease">True if prerelease versions should satisfy the condition.</param>
        public VersionRange(SimpleVersion minVersion = null, bool includeMinVersion  = true, SimpleVersion maxVersion = null,
                            bool includeMaxVersion   = false, bool?includePrerelease = null)
        {
            _minVersion        = minVersion;
            _maxVersion        = maxVersion;
            _includeMinVersion = includeMinVersion;
            _includeMaxVersion = includeMaxVersion;

            if (includePrerelease == null)
            {
                _includePrerelease = (_maxVersion != null && IsPrerelease(_maxVersion) == true) ||
                                     (_minVersion != null && IsPrerelease(_minVersion) == true);
            }
            else
            {
                _includePrerelease = includePrerelease == true;
            }
        }
Пример #4
0
        /// <summary>
        /// Determines if an NuGetVersion meets the requirements using the version comparer.
        /// </summary>
        /// <param name="version">SemVer to compare.</param>
        /// <param name="comparer">Version comparer used to determine if the version criteria is met.</param>
        /// <returns>True if the given version meets the version requirements.</returns>
        public bool Satisfies(SimpleVersion version, IVersionComparer comparer)
        {
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }

            // Determine if version is in the given range using the comparer.
            bool condition = true;

            if (HasLowerBound)
            {
                if (IsMinInclusive)
                {
                    condition &= comparer.Compare(MinVersion, version) <= 0;
                }
                else
                {
                    condition &= comparer.Compare(MinVersion, version) < 0;
                }
            }

            if (HasUpperBound)
            {
                if (IsMaxInclusive)
                {
                    condition &= comparer.Compare(MaxVersion, version) >= 0;
                }
                else
                {
                    condition &= comparer.Compare(MaxVersion, version) > 0;
                }
            }

            if (!IncludePrerelease)
            {
                condition &= IsPrerelease(version) != true;
            }

            return(condition);
        }
Пример #5
0
 /// <summary>
 /// Determines if an NuGetVersion meets the requirements using the given mode.
 /// </summary>
 /// <param name="version">SemVer to compare</param>
 /// <param name="versionComparison">VersionComparison mode used to determine the version range.</param>
 /// <returns>True if the given version meets the version requirements.</returns>
 public bool Satisfies(SimpleVersion version, VersionComparison versionComparison)
 {
     return(Satisfies(version, new VersionComparer(versionComparison)));
 }
Пример #6
0
 /// <summary>
 /// Determines if an NuGetVersion meets the requirements.
 /// </summary>
 /// <param name="version">SemVer to compare</param>
 /// <returns>True if the given version meets the version requirements.</returns>
 public bool Satisfies(SimpleVersion version)
 {
     // ignore metadata by default when finding a range.
     return(Satisfies(version, VersionComparer.VersionRelease));
 }
Пример #7
0
        /// <summary>
        /// Compare versions.
        /// </summary>
        public int Compare(SimpleVersion x, SimpleVersion y)
        {
            // null checks
            if (Object.ReferenceEquals(x, null) && Object.ReferenceEquals(y, null))
            {
                return(0);
            }

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

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

            SemanticVersion semX = x as SemanticVersion;
            SemanticVersion semY = y as SemanticVersion;

            if (semX != null && semY != null)
            {
                // compare version
                int result = semX.Major.CompareTo(semY.Major);
                if (result != 0)
                {
                    return(result);
                }

                result = semX.Minor.CompareTo(semY.Minor);
                if (result != 0)
                {
                    return(result);
                }

                result = semX.Patch.CompareTo(semY.Patch);
                if (result != 0)
                {
                    return(result);
                }

                NuGetVersion legacyX = x as NuGetVersion;
                NuGetVersion legacyY = y as NuGetVersion;

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

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

                    if (!semX.IsPrerelease && semY.IsPrerelease)
                    {
                        return(1);
                    }

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

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

            return(0);
        }
Пример #8
0
        /// <summary>
        /// Compares the given versions using the VersionComparison mode.
        /// </summary>
        public static int Compare(SimpleVersion version1, SimpleVersion version2, VersionComparison versionComparison)
        {
            IVersionComparer comparer = new VersionComparer(versionComparison);

            return(comparer.Compare(version1, version2));
        }
Пример #9
0
 /// <summary>
 /// Determines if both versions are equal.
 /// </summary>
 public bool Equals(SimpleVersion x, SimpleVersion y)
 {
     return(Compare(x, y) == 0);
 }