Esempio n. 1
0
        /// <summary>
        /// Determines if a given version is better suited to the range than a current version.
        /// </summary>
        public bool IsBetter(NuGetVersion current, NuGetVersion considering)
        {
            if (ReferenceEquals(current, considering))
            {
                return(false);
            }

            // null checks
            if (ReferenceEquals(considering, null))
            {
                return(false);
            }

            // If the range contains only stable versions disallow prerelease versions
            if (!HasPrereleaseBounds &&
                considering.IsPrerelease &&
                _floatRange?.FloatBehavior != NuGetVersionFloatBehavior.Prerelease &&
                _floatRange?.FloatBehavior != NuGetVersionFloatBehavior.AbsoluteLatest)
            {
                return(false);
            }

            if (!Satisfies(considering))
            {
                // keep null over a value outside of the range
                return(false);
            }

            if (ReferenceEquals(current, null))
            {
                return(true);
            }

            if (IsFloating)
            {
                // check if either version is in the floating range
                var curInRange = _floatRange.Satisfies(current);
                var conInRange = _floatRange.Satisfies(considering);

                if (curInRange && !conInRange)
                {
                    // take the version in the range
                    return(false);
                }
                else if (conInRange && !curInRange)
                {
                    // take the version in the range
                    return(true);
                }
                else if (curInRange && conInRange)
                {
                    // prefer the highest one if both are in the range
                    return(current < considering);
                }
                else
                {
                    // neither are in range
                    var curToLower = current < _floatRange.MinVersion;
                    var conToLower = considering < _floatRange.MinVersion;

                    if (curToLower && !conToLower)
                    {
                        // favor the version above the range
                        return(true);
                    }
                    else if (!curToLower && conToLower)
                    {
                        // favor the version above the range
                        return(false);
                    }
                    else if (!curToLower &&
                             !conToLower)
                    {
                        // favor the lower version if we are above the range
                        return(current > considering);
                    }
                    else if (curToLower && conToLower)
                    {
                        // favor the higher version if we are below the range
                        return(current < considering);
                    }
                }
            }

            // Favor lower versions
            return(current > considering);
        }
Esempio n. 2
0
        /// <summary>
        /// Determines if a given version is better suited to the range than a current version.
        /// </summary>
        public bool IsBetter(NuGetVersion current, NuGetVersion considering)
        {
            if (ReferenceEquals(current, considering))
            {
                return(false);
            }

            // null checks
            if (ReferenceEquals(considering, null))
            {
                return(false);
            }

            if (!Satisfies(considering))
            {
                // keep null over a value outside of the range
                return(false);
            }

            if (ReferenceEquals(current, null))
            {
                return(true);
            }

            if (IsFloating)
            {
                // check if either version is in the floating range
                var curInRange = _floatRange.Satisfies(current);
                var conInRange = _floatRange.Satisfies(considering);

                if (curInRange && !conInRange)
                {
                    // take the version in the range
                    return(false);
                }
                else if (conInRange && !curInRange)
                {
                    // take the version in the range
                    return(true);
                }
                else if (curInRange && conInRange)
                {
                    // prefer the highest one if both are in the range
                    return(current < considering);
                }
                else
                {
                    // neither are in range
                    var curToLower = current < _floatRange.MinVersion;
                    var conToLower = considering < _floatRange.MinVersion;

                    if (curToLower && !conToLower)
                    {
                        // favor the version above the range
                        return(true);
                    }
                    else if (!curToLower && conToLower)
                    {
                        // favor the version above the range
                        return(false);
                    }
                    else if (!curToLower &&
                             !conToLower)
                    {
                        // favor the lower version if we are above the range
                        return(current > considering);
                    }
                    else if (curToLower && conToLower)
                    {
                        // favor the higher version if we are below the range
                        return(current < considering);
                    }
                }
            }

            // Favor lower versions
            return(current > considering);
        }