Exemplo n.º 1
0
        // Allows patch-level changes if a minor version is specified
        // on the comparator. Allows minor-level changes if not.
        public static Tuple <int, SemverComparator[]> TildeRange(string spec)
        {
            string pattern = String.Format(@"^\s*~\s*({0}+)\s*", versionChars);

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

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

            SemanticVersion minVersion = null;
            SemanticVersion maxVersion = null;

            var version = new PartialVersion(match.Groups[1].Value);

            if (version.Minor.HasValue)
            {
                // Doesn't matter whether patch version is null or not,
                // the logic is the same, min patch version will be zero if null.
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(version.Major.Value, version.Minor.Value + 1, 0);
            }
            else
            {
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(version.Major.Value + 1, 0, 0);
            }

            return(Tuple.Create(
                       match.Length,
                       minMaxComparators(minVersion, maxVersion)));
        }
Exemplo n.º 2
0
        public static Tuple <int, SemverComparator[]> 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();

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

            SemanticVersion 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 SemanticVersion(maxPartialVersion.Major.Value + 1, 0, 0);
            }
            else if (!maxPartialVersion.Patch.HasValue)
            {
                maxVersion = new SemanticVersion(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)));
        }
Exemplo n.º 3
0
        public static Tuple <int, SemverComparator[]> StarRange(string spec)
        {
            // Also match with an equals sign, eg. "=0.7.x"
            string pattern = String.Format(@"^\s*=?\s*({0}+)\s*", versionChars);

            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);
            }

            SemanticVersion minVersion = null;
            SemanticVersion maxVersion = null;

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

            return(Tuple.Create(
                       match.Length,
                       minMaxComparators(minVersion, maxVersion)));
        }
Exemplo n.º 4
0
        // Allows changes that do not modify the left-most non-zero digit
        // in the [major, minor, patch] tuple.
        public static Tuple <int, SemverComparator[]> CaretRange(string spec)
        {
            string pattern = String.Format(@"^\s*\^\s*({0}+)\s*", versionChars);

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

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

            SemanticVersion minVersion = null;
            SemanticVersion maxVersion = null;

            var version = new PartialVersion(match.Groups[1].Value);

            if (version.Major.Value > 0)
            {
                // Don't allow major version change
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(version.Major.Value + 1, 0, 0);
            }
            else if (!version.Minor.HasValue)
            {
                // Don't allow major version change, even if it's zero
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(version.Major.Value + 1, 0, 0);
            }
            else if (!version.Patch.HasValue)
            {
                // Don't allow minor version change, even if it's zero
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(0, version.Minor.Value + 1, 0);
            }
            else if (version.Minor > 0)
            {
                // Don't allow minor version change
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(0, version.Minor.Value + 1, 0);
            }
            else
            {
                // Only patch non-zero, don't allow patch change
                minVersion = version.ToZeroVersion();
                maxVersion = new SemanticVersion(0, 0, version.Patch.Value + 1);
            }

            return(Tuple.Create(
                       match.Length,
                       minMaxComparators(minVersion, maxVersion)));
        }
Exemplo n.º 5
0
        public SemverComparator(string input)
        {
            var regex = new Regex(String.Format("^{0}$", pattern),
                                  RegexOptions.IgnorePatternWhitespace);
            var match = regex.Match(input);

            if (!match.Success)
            {
                throw new ArgumentException(String.Format("Invalid comparator string: {0}", 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 SemanticVersion(0, 0, 0);
                    }
                    else if (!partialVersion.Minor.HasValue)
                    {
                        Version = new SemanticVersion(partialVersion.Major.Value + 1, 0, 0);
                    }
                    else
                    {
                        Version = new SemanticVersion(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 SemanticVersion(0, 0, 0);
                    }
                    else if (!partialVersion.Minor.HasValue)
                    {
                        // eg. >1.x -> >=2.0
                        Version = new SemanticVersion(partialVersion.Major.Value + 1, 0, 0);
                    }
                    else
                    {
                        // eg. >1.2.x -> >=1.3
                        Version = new SemanticVersion(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();
            }
        }