示例#1
0
        /// <inheritdoc />
        protected override int Execute(IInput input, IOutput output)
        {
            var bucket = GetBucket(true, input.GetOption("no-plugins"));

            string[] packages = input.GetArgument("packages") ?? Array.Empty <string>();

            packages = ProcessRootRequires(bucket.GetPackage(), input, packages);

            // todo: set no-progress.
            // todo: add gui interactive select the packages.
            var commandEvent = new CommandEventArgs(PluginEvents.Command, "update", input, output);

            bucket.GetEventDispatcher().Dispatch(this, commandEvent);

            var io        = GetIO();
            var installer = new BucketInstaller(io, bucket);

            var config = bucket.GetConfig();

            var(preferSource, preferDist) = GetPreferredInstallOptions(config, input);

            ISet <string> GetUpdateWhitlist()
            {
                if (input.GetOption("lock"))
                {
                    return(new HashSet <string>(new[] { "lock" }));
                }

                return(packages.Empty() ? null : new HashSet <string>(packages));
            }

            installer.SetDryRun(input.GetOption("dry-run"))
            .SetVerbose(input.GetOption("verbose"))
            .SetPreferSource(preferSource)
            .SetPreferDist(preferDist)
            .SetDevMode(!input.GetOption("no-dev"))
            .SetRunScripts(!input.GetOption("no-scripts"))
            .SetSkipSuggest(input.GetOption("no-suggest"))
            .SetIgnorePlatformRequirements(input.GetOption("ignore-platform-reqs"))
            .SetUpdate(true)
            .SetUpdateWhitelist(GetUpdateWhitlist())
            .SetWhitelistAllDependencies(input.GetOption("with-all-dependencies"))
            .SetWhitelistTransitiveDependencies(input.GetOption("with-dependencies"))
            .SetPreferStable(input.GetOption("prefer-stable"))
            .SetPreferLowest(input.GetOption("prefer-lowest"));

            if (input.GetOption("no-plugins"))
            {
                installer.DisablePlugins();
            }

            return(installer.Run());
        }
示例#2
0
        /// <inheritdoc />
        protected override int Execute(IInput input, IOutput output)
        {
            var io = GetIO();

            string[] args = input.GetArgument("packages");
            if (!args.Empty())
            {
                io.WriteError($"<error>Invalid argument {string.Join(Str.Space, args)}. Use \"bucket require {string.Join(Str.Space, args)}\" instead to add packages to your bucket.json.</error>");
                return(ExitCodes.GeneralException);
            }

            var bucket = GetBucket(true, input.GetOption("no-plugins"));

            // todo: set no-progress.
            var commandEvent = new CommandEventArgs(PluginEvents.Command, "install", input, output);

            bucket.GetEventDispatcher().Dispatch(this, commandEvent);

            var installer = new BucketInstaller(io, bucket);

            var config = bucket.GetConfig();

            var(preferSource, preferDist) = GetPreferredInstallOptions(config, input);

            installer.SetDryRun(input.GetOption("dry-run"))
            .SetVerbose(input.GetOption("verbose"))
            .SetPreferSource(preferSource)
            .SetPreferDist(preferDist)
            .SetDevMode(!input.GetOption("no-dev"))
            .SetRunScripts(!input.GetOption("no-scripts"))
            .SetSkipSuggest(input.GetOption("no-suggest"))
            .SetIgnorePlatformRequirements(input.GetOption("ignore-platform-reqs"));

            if (input.GetOption("no-plugins"))
            {
                installer.DisablePlugins();
            }

            return(installer.Run());
        }
示例#3
0
        /// <inheritdoc />
        protected override int Execute(IInput input, IOutput output)
        {
            string[] packages = input.GetArgument("packages");
            packages = Arr.Map(packages, (package) => package.ToLower());

            var file     = Factory.GetBucketFile();
            var filePath = Path.Combine(Environment.CurrentDirectory, file);

            var jsonFile     = new JsonFile(filePath);
            var configBucket = jsonFile.Read <ConfigBucket>();
            var backup       = File.ReadAllText(filePath);

            var source = new JsonConfigSource(jsonFile);
            var io     = GetIO();

            var requireKey            = input.GetOption("dev") ? LinkType.RequireDev : LinkType.Require;
            var alternativeRequireKey = input.GetOption("dev") ? LinkType.Require : LinkType.RequireDev;

            var requireMapping            = new Dictionary <string, string>();
            var alternativeRequireMapping = new Dictionary <string, string>();

            void EstablishCaseMapping(IDictionary <string, string> mapping, LinkType type)
            {
                var collection = type == LinkType.Require ? configBucket.Requires : configBucket.RequiresDev;

                if (collection == null)
                {
                    return;
                }

                foreach (var item in collection)
                {
                    mapping[item.Key.ToLower()] = item.Key;
                }
            }

            EstablishCaseMapping(requireMapping, requireKey);
            EstablishCaseMapping(alternativeRequireMapping, alternativeRequireKey);

            bool TryGetMatchPackages(IDictionary <string, string> mapping, string package, out IEnumerable <string> matches)
            {
                var result       = new List <string>();
                var regexPackage = BasePackage.PackageNameToRegexPattern(package);

                foreach (var item in mapping)
                {
                    if (Regex.IsMatch(item.Key, regexPackage, RegexOptions.IgnoreCase))
                    {
                        result.Add(item.Value);
                    }
                }

                matches = result;
                return(result.Count > 0);
            }

            foreach (var package in packages)
            {
                if (requireMapping.TryGetValue(package, out string originalName))
                {
                    source.RemoveLink(requireKey, originalName);
                }
                else if (alternativeRequireMapping.TryGetValue(package, out originalName))
                {
                    io.WriteError($"<warning>{originalName} could not be found in {Str.LowerDashes(requireKey.ToString())} but it is present in {Str.LowerDashes(alternativeRequireKey.ToString())}</warning>");
                    if (io.IsInteractive && io.AskConfirmation($"Do you want to remove it from {Str.LowerDashes(alternativeRequireKey.ToString())} [<comment>yes</comment>]? ", true))
                    {
                        source.RemoveLink(alternativeRequireKey, originalName);
                    }
                }
                else if (TryGetMatchPackages(requireMapping, package, out IEnumerable <string> matches))
                {
                    foreach (var match in matches)
                    {
                        source.RemoveLink(requireKey, match);
                    }
                }
                else if (TryGetMatchPackages(alternativeRequireMapping, package, out matches))
                {
                    foreach (var match in matches)
                    {
                        io.WriteError($"<warning>{match} could not be found in {Str.LowerDashes(requireKey.ToString())} but it is present in {Str.LowerDashes(alternativeRequireKey.ToString())}</warning>");
                        if (io.IsInteractive && io.AskConfirmation($"Do you want to remove it from {Str.LowerDashes(alternativeRequireKey.ToString())} [<comment>yes</comment>]? ", true))
                        {
                            source.RemoveLink(alternativeRequireKey, match);
                        }
                    }
                }
                else
                {
                    io.WriteError($"<warning>{package} is not required in your bucket.json and has not been removed</warning>");
                }
            }

            if (input.GetOption("no-update"))
            {
                return(ExitCodes.Normal);
            }

            // todo: implement installer uninstall.
            ResetBucket();

            var bucket = GetBucket(true, input.GetOption("no-plugins"));

            // todo: set no-progress.
            var commandEvent = new CommandEventArgs(PluginEvents.Command, "remove", input, output);

            bucket.GetEventDispatcher().Dispatch(this, commandEvent);

            ISet <string> GetRemoveWhitlist()
            {
                Guard.Requires <UnexpectedException>(packages.Length > 0);
                return(new HashSet <string>(packages));
            }

            var installer = new BucketInstaller(io, bucket);
            var status    = installer
                            .SetVerbose(input.GetOption("verbose"))
                            .SetDevMode(!input.GetOption("update-no-dev"))
                            .SetRunScripts(!input.GetOption("no-scripts"))
                            .SetUpdate(true)
                            .SetUpdateWhitelist(GetRemoveWhitlist())
                            .SetWhitelistTransitiveDependencies(!input.GetOption("no-update-with-dependencies"))
                            .SetIgnorePlatformRequirements(input.GetOption("ignore-platform-reqs"))
                            .Run();

            if (status != 0)
            {
                io.WriteError($"{Environment.NewLine}<error>Removal failed, reverting {file} to its original content.</error>");
                File.WriteAllText(filePath, backup);
            }

            return(status);
        }