Beispiel #1
0
        public static Tuple <int, Comparator[]> HyphenRange(string spec)
        {
            string pattern = String.Format(@"^\s*({0}+)\s+\-\s+({0}+)\s*", versionChars);

            var regex = new Regex(pattern);
            var match = regex.Match(spec);

            if (!match.Success)
            {
                return(null);
            }

            PartialVersion minPartialVersion = null;
            PartialVersion maxPartialVersion = null;

            // Parse versions from lower and upper ranges, which might
            // be partial versions.
            try
            {
                minPartialVersion = new PartialVersion(match.Groups[1].Value);
                maxPartialVersion = new PartialVersion(match.Groups[2].Value);
            }
            catch (ArgumentException)
            {
                return(null);
            }

            // Lower range has any non-supplied values replaced with zero
            var minVersion = minPartialVersion.ToZeroVersion();

            Comparator.Operator maxOperator = maxPartialVersion.IsFull()
                                                  ? Comparator.Operator.LessThanOrEqual : Comparator.Operator.LessThan;

            Version maxVersion = null;

            // Partial upper range means supplied version values can't change
            if (!maxPartialVersion.Major.HasValue)
            {
                // eg. upper range = "*", then maxVersion remains null
                // and there's only a minimum
            }
            else if (!maxPartialVersion.Minor.HasValue)
            {
                maxVersion = new Version(maxPartialVersion.Major.Value + 1, 0, 0);
            }
            else if (!maxPartialVersion.Patch.HasValue)
            {
                maxVersion = new Version(maxPartialVersion.Major.Value, maxPartialVersion.Minor.Value + 1, 0);
            }
            else
            {
                // Fully specified max version
                maxVersion = maxPartialVersion.ToZeroVersion();
            }
            return(Tuple.Create(
                       match.Length,
                       minMaxComparators(minVersion, maxVersion, maxOperator)));
        }
Beispiel #2
0
        // A version that might not have a minor or patch
        // number, for use in ranges like "^1.2" or "2.x"
        public static Tuple <int, Comparator[]> StarRange(string spec)
        {
            // Also match with an equals sign, eg. "=0.7.x"
            string pattern = $@"^\s*=?\s*({versionChars}+)\s*";

            var regex = new Regex(pattern);
            var match = regex.Match(spec);

            if (!match.Success)
            {
                return(null);
            }

            PartialVersion version = null;

            try
            {
                version = new PartialVersion(match.Groups[1].Value);
            }
            catch (ArgumentException)
            {
                return(null);
            }

            // If partial version match is actually a full version,
            // then this isn't a star range, so return null.
            if (version.IsFull())
            {
                return(null);
            }

            Version minVersion = null;
            Version maxVersion = null;

            if (!version.Major.HasValue)
            {
                minVersion = version.ToZeroVersion();
                // no max version
            }
            else if (!version.Minor.HasValue)
            {
                minVersion = version.ToZeroVersion();
                maxVersion = new Version(version.Major.Value + 1, 0, 0);
            }
            else
            {
                minVersion = version.ToZeroVersion();
                maxVersion = new Version(version.Major.Value, version.Minor.Value + 1, 0);
            }

            return(Tuple.Create(
                       match.Length,
                       minMaxComparators(minVersion, maxVersion)));
        }
Beispiel #3
0
        public Comparator(string input)
        {
            var regex = new Regex($"^{pattern}$",
                                  RegexOptions.IgnorePatternWhitespace);
            var match = regex.Match(input);

            if (!match.Success)
            {
                throw new ArgumentException($"Invalid comparator string: {input}");
            }

            ComparatorType = ParseComparatorType(match.Groups[1].Value);
            var partialVersion = new PartialVersion(match.Groups[2].Value);

            if (!partialVersion.IsFull())
            {
                // For Operator.Equal, partial versions are handled by the StarRange
                // desugarer, and desugar to multiple comparators.

                switch (ComparatorType)
                {
                // For <= with a partial version, eg. <=1.2.x, this
                // means the same as < 1.3.0, and <=1.x means <2.0
                case Operator.LessThanOrEqual:
                    ComparatorType = Operator.LessThan;
                    if (!partialVersion.Major.HasValue)
                    {
                        // <=* means >=0.0.0
                        ComparatorType = Operator.GreaterThanOrEqual;
                        Version        = new Version(0, 0, 0);
                    }
                    else if (!partialVersion.Minor.HasValue)
                    {
                        Version = new Version(partialVersion.Major.Value + 1, 0, 0);
                    }
                    else
                    {
                        Version = new Version(partialVersion.Major.Value, partialVersion.Minor.Value + 1, 0);
                    }
                    break;

                case Operator.GreaterThan:
                    ComparatorType = Operator.GreaterThanOrEqual;
                    if (!partialVersion.Major.HasValue)
                    {
                        // >* is unsatisfiable, so use <0.0.0
                        ComparatorType = Operator.LessThan;
                        Version        = new Version(0, 0, 0);
                    }
                    else if (!partialVersion.Minor.HasValue)
                    {
                        // eg. >1.x -> >=2.0
                        Version = new Version(partialVersion.Major.Value + 1, 0, 0);
                    }
                    else
                    {
                        // eg. >1.2.x -> >=1.3
                        Version = new Version(partialVersion.Major.Value, partialVersion.Minor.Value + 1, 0);
                    }
                    break;

                default:
                    // <1.2.x means <1.2.0
                    // >=1.2.x means >=1.2.0
                    Version = partialVersion.ToZeroVersion();
                    break;
                }
            }
            else
            {
                Version = partialVersion.ToZeroVersion();
            }
        }