Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Package"/> class.
        /// </summary>
        /// <param name="name">The package name.</param>
        /// <param name="version">Normalized version.</param>
        /// <param name="versionPretty">The package non-normalized version(Human readable).</param>
        public Package(string name, string version, string versionPretty)
            : base(name)
        {
            this.version       = version;
            this.versionPretty = versionPretty;

            stability = VersionParser.ParseStability(version);
        }
Exemplo n.º 2
0
        public static IPackage MockPackage(
            string name, string version, Link[] provides = null,
            Link[] replaces = null, Link[] requires = null, Link[] conflict = null,
            string type     = null)
        {
            // todo: optimization MockPackage, use GetPackage<T>().
            provides = provides ?? Array.Empty <Link>();
            replaces = replaces ?? Array.Empty <Link>();
            requires = requires ?? Array.Empty <Link>();
            conflict = conflict ?? Array.Empty <Link>();

            var         names      = new[] { name.ToLower() };
            var         package    = new Mock <IPackage>();
            IRepository repository = null;

            names = names.Concat(provides.Select((link) => link.GetTarget())).ToArray();
            names = names.Concat(replaces.Select((link) => link.GetTarget())).ToArray();
            package.Setup((o) => o.GetName()).Returns(() => name.ToLower());
            package.Setup((o) => o.GetNamePretty()).Returns(() => name);
            package.Setup((o) => o.GetPackageType()).Returns(() => type);
            package.Setup((o) => o.GetVersion()).Returns(() => GetVersionParser().Normalize(version));
            package.Setup((o) => o.GetVersionPretty()).Returns(() => version);
            package.Setup((o) => o.GetNames()).Returns(() => names);
            package.Setup((o) => o.GetStability()).Returns(() => VersionParser.ParseStability(version));
            package.Setup((o) => o.GetProvides()).Returns(() => provides);
            package.Setup((o) => o.GetReplaces()).Returns(() => replaces);
            package.Setup((o) => o.GetConflicts()).Returns(conflict);
            package.Setup((o) => o.GetRequires()).Returns(() => requires);
            package.Setup((o) => o.ToString()).Returns(() => package.Object.GetNameUnique());
            package.Setup((o) => o.GetPrettyString()).Returns(package.Object.GetNamePretty() + Str.Space + package.Object.GetVersionPretty());
            package.Setup((o) => o.GetNameUnique()).Returns(() => package.Object.GetName() + "-" + package.Object.GetVersion());
            package.Setup((o) => o.Equals(It.IsAny <IPackage>())).Returns((IPackage other) =>
            {
                IPackage self = package.Object;
                if (self is PackageAlias packageAlias)
                {
                    self = packageAlias.GetAliasOf();
                }

                if (other is PackageAlias objPackageAlias)
                {
                    other = objPackageAlias.GetAliasOf();
                }

                return(other.GetNameUnique() == self.GetNameUnique());
            });

            package.Setup((o) => o.SetRepository(It.IsAny <IRepository>())).Callback((IRepository repo) =>
            {
                repository = repo;
            });

            package.Setup((o) => o.GetRepository()).Returns(() => repository);

            package.SetupProperty((o) => o.Id);

            return(package.Object);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PackageAlias"/> class.
        /// </summary>
        /// <param name="aliasOf">The package this package is an alias of.</param>
        /// <param name="version">The version the alias must report.</param>
        /// <param name="versionPretty">The alias's non-normalized version.</param>
        public PackageAlias(IPackage aliasOf, string version, string versionPretty)
            : base(aliasOf.GetName())
        {
            this.aliasOf       = aliasOf;
            this.version       = version;
            this.versionPretty = versionPretty;
            stability          = VersionParser.ParseStability(version);

            requires    = ReplaceSelfVersion(aliasOf.GetRequires(), nameof(requires), true);
            requiresDev = ReplaceSelfVersion(aliasOf.GetRequiresDev(), nameof(requiresDev), true);
            conflicts   = ReplaceSelfVersion(aliasOf.GetConflicts(), nameof(conflicts));
            provides    = ReplaceSelfVersion(aliasOf.GetProvides(), nameof(provides));
            replaces    = ReplaceSelfVersion(aliasOf.GetReplaces(), nameof(replaces));
        }
Exemplo n.º 4
0
        private void ExtractReferences(IDictionary <string, string> requires, IDictionary <string, string> collection)
        {
            foreach (var item in requires)
            {
                var require        = item.Key;
                var requireVersion = item.Value;
                requireVersion = Regex.Replace(requireVersion, @"^([^,\s@]+) as .+$", "${1}");

                // Only use the reference in developer mode.
                var match = Regex.Match(requireVersion, @"^[^,\s@]+?#(?<reference>[a-f0-9]+)$");
                if (!match.Success || VersionParser.ParseStability(requireVersion) != Stabilities.Dev)
                {
                    continue;
                }

                collection[require.ToLower()] = match.Groups["reference"].Value;
            }
        }
Exemplo n.º 5
0
        private static bool IsVersionAcceptable(PredicatePackageAcceptable predicatePackageAcceptable, IConstraint constraint, string name, ConfigPackageBucket config)
        {
            // todo: to load aliase package.
            var versions = new[] { config.VersionNormalized };

            foreach (var version in versions)
            {
                if (predicatePackageAcceptable != null &&
                    !predicatePackageAcceptable(VersionParser.ParseStability(version), name))
                {
                    continue;
                }

                if (constraint != null && !constraint.Matches(new Constraint("==", version)))
                {
                    continue;
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        private void ExtractStabilityFlags(
            IDictionary <string, string> requires,
            Stabilities?minimumStability,
            IDictionary <string, Stabilities> collection)
        {
            foreach (var item in requires)
            {
                var require        = item.Key;
                var requireVersion = item.Value;
                var orSplit        = Regex.Split(requireVersion.Trim(), @"\s*\|\|?\s*");
                var constraints    = new LinkedList <string>();

                // extract all sub-constraints in case it is an OR/AND multi-constraint
                foreach (var orConstraint in orSplit)
                {
                    var andSplit = Regex.Split(orConstraint, @"(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)");
                    foreach (var andConstraint in andSplit)
                    {
                        constraints.AddLast(andConstraint);
                    }
                }

                var matchStabilityFlags = false;
                foreach (var constraint in constraints)
                {
                    // todo: stable|RC|beta|alpha|dev need replace to variable.
                    var match = Regex.Match(constraint, "^[^@]*?@(?<stability>stable|RC|beta|alpha|dev)", RegexOptions.IgnoreCase);
                    if (!match.Success || !stabilities.TryGetValue(match.Groups["stability"].Value.ToLower(), out Stabilities stability))
                    {
                        continue;
                    }

                    // Choose the most unstable version.
                    var requireLower = require.ToLower();
                    if (collection.TryGetValue(requireLower, out Stabilities prevStability) && stability < prevStability)
                    {
                        continue;
                    }

                    collection[requireLower] = stability;
                    matchStabilityFlags      = true;
                }

                if (matchStabilityFlags)
                {
                    continue;
                }

                foreach (var constraint in constraints)
                {
                    // Used to handle non-standard stability markers
                    // for example: ~1.2.0-stable2
                    requireVersion = Regex.Replace(constraint, @"^([^,\s@]+) as .+$", "${1}");
                    if (!Regex.IsMatch(requireVersion, @"^[^,\s@]+$"))
                    {
                        continue;
                    }

                    // Version speculation only for non-stable versions
                    // Because stable is the default.
                    var stability = VersionParser.ParseStability(requireVersion);
                    if (stability == Stabilities.Stable)
                    {
                        continue;
                    }

                    // Record only stability markers that are less than
                    // the minimum stability and choose the most unstable
                    // version.
                    var requireLower = require.ToLower();
                    if ((collection.TryGetValue(requireLower, out Stabilities prevStability) && stability < prevStability) ||
                        stability < minimumStability)
                    {
                        continue;
                    }

                    collection[requireLower] = stability;
                }
            }
        }