Exemple #1
0
 /// <summary>
 /// Clones a version range and applies a new float range.
 /// </summary>
 public VersionRange(VersionRange range, FloatRange floatRange)
     : this(range.MinVersion, range.IsMinInclusive, range.MaxVersion, range.IsMaxInclusive, floatRange)
 {
 }
Exemple #2
0
        /// <summary>
        /// Parses a VersionRange from its string representation.
        /// </summary>
        public static bool TryParse(string value, bool allowFloating, out VersionRange versionRange)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            versionRange = null;

            var trimmedValue = value.Trim();

            var charArray = trimmedValue.ToCharArray();

            // * is the only range below 3 chars
            if (allowFloating &&
                charArray.Length == 1 &&
                charArray[0] == '*')
            {
                versionRange = new VersionRange(null, true, null, true, new FloatRange(NuGetVersionFloatBehavior.Major), originalString: value);
                return(true);
            }

            // Fail early if the string is too short to be valid
            if (charArray.Length < 3)
            {
                return(false);
            }

            string       minVersionString = null;
            string       maxVersionString = null;
            var          isMinInclusive   = false;
            var          isMaxInclusive   = false;
            NuGetVersion minVersion       = null;
            NuGetVersion maxVersion       = null;
            FloatRange   floatRange       = null;

            if (charArray[0] == '(' ||
                charArray[0] == '[')
            {
                // The first character must be [ to (
                switch (charArray[0])
                {
                case '[':
                    isMinInclusive = true;
                    break;

                case '(':
                    isMinInclusive = false;
                    break;

                default:
                    return(false);
                }

                // The last character must be ] ot )
                switch (charArray[charArray.Length - 1])
                {
                case ']':
                    isMaxInclusive = true;
                    break;

                case ')':
                    isMaxInclusive = false;
                    break;

                default:
                    return(false);
                }

                // Get rid of the two brackets
                trimmedValue = trimmedValue.Substring(1, trimmedValue.Length - 2);

                // Split by comma, and make sure we don't get more than two pieces
                var parts = trimmedValue.Split(',');
                if (parts.Length > 2)
                {
                    return(false);
                }
                else
                {
                    var allEmpty = true;

                    for (int i = 0; i < parts.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(parts[i]))
                        {
                            allEmpty = false;
                            break;
                        }
                    }

                    // If all parts are empty, then neither of upper or lower bounds were specified. Version spec is of the format (,]
                    if (allEmpty)
                    {
                        return(false);
                    }
                }

                // If there is only one piece, we use it for both min and max
                minVersionString = parts[0];
                maxVersionString = (parts.Length == 2) ? parts[1] : parts[0];
            }
            else
            {
                // default to min inclusive when there are no braces
                isMinInclusive = true;

                // use the entire value as the version
                minVersionString = trimmedValue;
            }

            if (!String.IsNullOrWhiteSpace(minVersionString))
            {
                // parse the min version string
                if (allowFloating && minVersionString.Contains("*"))
                {
                    // single floating version
                    if (FloatRange.TryParse(minVersionString, out floatRange) &&
                        floatRange.HasMinVersion)
                    {
                        minVersion = floatRange.MinVersion;
                    }
                    else
                    {
                        // invalid float
                        return(false);
                    }
                }
                else
                {
                    // single non-floating version
                    if (!NuGetVersion.TryParse(minVersionString, out minVersion))
                    {
                        // invalid version
                        return(false);
                    }
                }
            }

            // parse the max version string, the max cannot float
            if (!String.IsNullOrWhiteSpace(maxVersionString))
            {
                if (!NuGetVersion.TryParse(maxVersionString, out maxVersion))
                {
                    // invalid version
                    return(false);
                }
            }

            // Successful parse!
            versionRange = new VersionRange(
                minVersion: minVersion,
                includeMinVersion: isMinInclusive,
                maxVersion: maxVersion,
                includeMaxVersion: isMaxInclusive,
                floatRange: floatRange,
                originalString: value);

            return(true);
        }
Exemple #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>
 /// <param name="floatRange">The floating range subset used to find the best version match.</param>
 /// <param name="originalString">The original string being parsed to this object.</param>
 public VersionRange(NuGetVersion minVersion = null, bool includeMinVersion  = true, NuGetVersion maxVersion = null,
                     bool includeMaxVersion  = false, bool?includePrerelease = null, FloatRange floatRange   = null, string originalString = null)
     : base(minVersion, includeMinVersion, maxVersion, includeMaxVersion, includePrerelease)
 {
     _floatRange     = floatRange;
     _originalString = originalString;
 }
Exemple #4
0
        /// <summary>
        /// Parses a VersionRange from its string representation.
        /// </summary>
        public static bool TryParse(string value, bool allowFloating, out VersionRange versionRange)
        {
            versionRange = null;

            var trimmedValue = value?.Trim();

            if (string.IsNullOrEmpty(trimmedValue))
            {
                return(false);
            }

            var charArray = trimmedValue.ToCharArray();

            // * is the only 1 char range
            if (allowFloating &&
                charArray.Length == 1 &&
                charArray[0] == '*')
            {
                versionRange = new VersionRange(new NuGetVersion(0, 0, 0), true, null, true, FloatRange.Parse(trimmedValue), originalString: value);
                return(true);
            }

            string       minVersionString = null;
            string       maxVersionString = null;
            var          isMinInclusive   = false;
            var          isMaxInclusive   = false;
            NuGetVersion minVersion       = null;
            NuGetVersion maxVersion       = null;
            FloatRange   floatRange       = null;

            if (charArray[0] == '(' ||
                charArray[0] == '[')
            {
                // The first character must be [ to (
                switch (charArray[0])
                {
                case '[':
                    isMinInclusive = true;
                    break;

                case '(':
                    isMinInclusive = false;
                    break;

                default:
                    return(false);
                }

                // The last character must be ] ot )
                switch (charArray[charArray.Length - 1])
                {
                case ']':
                    isMaxInclusive = true;
                    break;

                case ')':
                    isMaxInclusive = false;
                    break;

                default:
                    return(false);
                }

                // Get rid of the two brackets
                trimmedValue = trimmedValue.Substring(1, trimmedValue.Length - 2);

                // Split by comma, and make sure we don't get more than two pieces
                var parts = trimmedValue.Split(',');

                if (parts.Length > 2)
                {
                    return(false);
                }
                else
                {
                    var allEmpty = true;

                    for (int i = 0; i < parts.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(parts[i]))
                        {
                            allEmpty = false;
                            break;
                        }
                    }

                    // If all parts are empty, then neither of upper or lower bounds were specified. Version spec is of the format (,]
                    if (allEmpty)
                    {
                        return(false);
                    }
                }

                // (1.0.0] and [1.0.0),(1.0.0) are invalid.
                if (parts.Length == 1 &&
                    !(isMinInclusive && isMaxInclusive))
                {
                    return(false);
                }

                // If there is only one piece, we use it for both min and max
                minVersionString = parts[0];
                maxVersionString = (parts.Length == 2) ? parts[1] : parts[0];
            }
            else
            {
                // default to min inclusive when there are no braces
                isMinInclusive = true;

                // use the entire value as the version
                minVersionString = trimmedValue;
            }

            if (!string.IsNullOrWhiteSpace(minVersionString))
            {
                // parse the min version string
                if (allowFloating && minVersionString.Contains("*"))
                {
                    // single floating version
                    if (FloatRange.TryParse(minVersionString, out floatRange) &&
                        floatRange.HasMinVersion)
                    {
                        minVersion = floatRange.MinVersion;
                    }
                    else
                    {
                        // invalid float
                        return(false);
                    }
                }
                else
                {
                    // single non-floating version
                    if (!NuGetVersion.TryParse(minVersionString, out minVersion))
                    {
                        // invalid version
                        return(false);
                    }
                }
            }

            // parse the max version string, the max cannot float
            if (!string.IsNullOrWhiteSpace(maxVersionString))
            {
                if (!NuGetVersion.TryParse(maxVersionString, out maxVersion))
                {
                    // invalid version
                    return(false);
                }
            }

            if (minVersion != null && maxVersion != null)
            {
                int result = minVersion.CompareTo(maxVersion);

                // minVersion > maxVersion
                if (result > 0)
                {
                    return(false);
                }

                // minVersion is equal to maxVersion (1.0.0, 1.0.0], [1.0.0, 1.0.0)
                if (result == 0 &&
                    (isMinInclusive ^ isMaxInclusive))
                {
                    return(false);
                }
            }

            // Successful parse!
            versionRange = new VersionRange(
                minVersion: minVersion,
                includeMinVersion: isMinInclusive,
                maxVersion: maxVersion,
                includeMaxVersion: isMaxInclusive,
                floatRange: floatRange,
                originalString: value);

            return(true);
        }
Exemple #5
0
 /// <summary>
 /// Creates a range that is greater than or equal to the minVersion with the given float behavior.
 /// </summary>
 /// <param name="minVersion">Lower bound of the version range.</param>
 public VersionRange(NuGetVersion minVersion, FloatRange floatRange)
     : this(minVersion, true, null, false, null, floatRange)
 {
 }