Ejemplo n.º 1
0
        private static MyNugetPackage find_dependency_pacakge_using_http_request(MyNugetPackageDependency dependencyPkg, ChocolateyConfiguration configuration)
        {
            bool allowPreRelease = configuration.Prerelease;
            List <MyNugetPackage> totalFoundBestPkgs = new List <MyNugetPackage>();

            foreach (string sourceUri in configuration.Sources.Split(';'))
            {
                bool isUriOk = Uri.IsWellFormedUriString(sourceUri.Trim(), UriKind.Absolute);
                if (!isUriOk)
                {
                    "chocolatey".Log().Debug("Skipping search in unsupported source: '{0}' - Only WEB sources are supported for this command".format_with(sourceUri)); continue;
                }
                ChocolateyConfiguration tempConf = configuration.deep_copy();
                tempConf.Sources = sourceUri.Trim();
                string responseStr = query_for_package_using_http_request(dependencyPkg, tempConf);
                if (responseStr == null)
                {
                    continue;
                }
                List <MyNugetPackage> foundPackagesList = parseHttpQueryPackageDetailsRequestResponse(dependencyPkg, responseStr);
                if (foundPackagesList.Count() == 0)
                {
                    "chocolatey".Log().Debug("Failed to find any 'entry' child nodes of pkg: '{0}' in source: '{1}'".format_with(dependencyPkg.Id, sourceUri)); continue;
                }

                // Sort results based on their version so that higher is first
                foundPackagesList.Sort(delegate(MyNugetPackage x, MyNugetPackage y) {
                    return(y.Version.CompareTo(x.Version));
                });

                MyNugetPackage bestMatchingPkg = foundPackagesList[0]; //Latest version found
                if (dependencyPkg.VersionSpec != null)                 // If want a specific version
                // Attempt to get the best matching package
                {
                    bestMatchingPkg = getBestMatchingDependencyPkgFromList(foundPackagesList, dependencyPkg);
                }

                if (bestMatchingPkg == null)
                {
                    "chocolatey".Log().Debug("Failed to determine best package from found 'entry' child nodes of pkg: '{0}' in source: '{1}'".format_with(dependencyPkg.Id, sourceUri)); continue;
                }
                totalFoundBestPkgs.Add(bestMatchingPkg);
            }

            if (totalFoundBestPkgs.Count() == 0)
            {
                "chocolatey".Log().Warn("Failed to find a dependency package '{0}' that satisfies version spec: {1}".format_with(dependencyPkg.Id, dependencyPkg.VersionSpec.ToString()));
                return(null);
            }

            // Sort results based on their version so that higher is first
            totalFoundBestPkgs.Sort(delegate(MyNugetPackage x, MyNugetPackage y) {
                return(y.Version.CompareTo(x.Version));
            });
            return(totalFoundBestPkgs[0]);
        }
Ejemplo n.º 2
0
        public static void install_package(ChocolateyConfiguration config, string packageId, string version)
        {
            if (_service == null)
            {
                _service = NUnitSetup.Container.GetInstance <IChocolateyPackageService>();
            }
            var installConfig = config.deep_copy();

            installConfig.PackageNames = packageId;
            installConfig.Version      = version;
            _service.install_run(installConfig);

            NUnitSetup.MockLogger.Messages.Clear();
        }
        private IEnumerable <ChocolateyConfiguration> get_packages_from_config(string packageConfigFile, ChocolateyConfiguration config, ConcurrentDictionary <string, PackageResult> packageInstalls)
        {
            IList <ChocolateyConfiguration> packageConfigs = new List <ChocolateyConfiguration>();

            if (!_fileSystem.file_exists(_fileSystem.get_full_path(packageConfigFile)))
            {
                var logMessage = "Could not find '{0}' in the location specified.".format_with(packageConfigFile);
                this.Log().Error(ChocolateyLoggers.Important, logMessage);
                var results = packageInstalls.GetOrAdd(packageConfigFile, new PackageResult(packageConfigFile, null, null));
                results.Messages.Add(new ResultMessage(ResultType.Error, logMessage));

                return(packageConfigs);
            }

            var settings = _xmlService.deserialize <PackagesConfigFileSettings>(_fileSystem.get_full_path(packageConfigFile));

            foreach (var pkgSettings in settings.Packages.or_empty_list_if_null())
            {
                if (!pkgSettings.Disabled)
                {
                    var packageConfig = config.deep_copy();
                    packageConfig.PackageNames      = pkgSettings.Id;
                    packageConfig.Sources           = string.IsNullOrWhiteSpace(pkgSettings.Source) ? packageConfig.Sources : pkgSettings.Source;
                    packageConfig.Version           = pkgSettings.Version;
                    packageConfig.InstallArguments  = string.IsNullOrWhiteSpace(pkgSettings.InstallArguments) ? packageConfig.InstallArguments : pkgSettings.InstallArguments;
                    packageConfig.PackageParameters = string.IsNullOrWhiteSpace(pkgSettings.PackageParameters) ? packageConfig.PackageParameters : pkgSettings.PackageParameters;
                    if (pkgSettings.ForceX86)
                    {
                        packageConfig.ForceX86 = true;
                    }
                    if (pkgSettings.AllowMultipleVersions)
                    {
                        packageConfig.AllowMultipleVersions = true;
                    }
                    if (pkgSettings.IgnoreDependencies)
                    {
                        packageConfig.IgnoreDependencies = true;
                    }

                    packageConfigs.Add(packageConfig);
                }
            }

            return(packageConfigs);
        }
Ejemplo n.º 4
0
        public virtual void set_pin(ChocolateyConfiguration config)
        {
            var             addingAPin         = config.PinCommand.Command == PinCommandType.add;
            var             versionUnspecified = string.IsNullOrWhiteSpace(config.Version);
            List <IPackage> packages           = new List <IPackage>();

            // package exists in registry
            var confList = config.deep_copy();

            confList.Input       = confList.PinCommand.Name;
            confList.QuietOutput = true;
            packages             = _packageService.list_run(confList).Select(x => x.Package).ToList();

            if (packages.Count == 0)
            {
                throw new ApplicationException("Unable to find package named '{0}'{1} to pin. Please check to ensure it is installed.".format_with(config.PinCommand.Name, versionUnspecified ? "" : " (version '{0}')".format_with(config.Version)));
            }

            foreach (var package in packages)
            {
                var  pkgInfo       = _packageInfoService.get_package_information(package);
                bool changeMessage = pkgInfo.IsPinned != addingAPin;

                if (package is RegistryPackage regp)
                {
                    regp.RegistryKey.IsPinned = addingAPin;
                    _registryService.set_key_values(regp.RegistryKey, nameof(RegistryApplicationKey.IsPinned));
                }
                else
                {
                    pkgInfo.IsPinned = addingAPin;
                    _packageInfoService.save_package_information(pkgInfo);
                }

                if (changeMessage)
                {
                    this.Log().Warn("Successfully {0} a pin for {1} v{2}.".format_with(addingAPin ? "added" : "removed", pkgInfo.Package.Id, pkgInfo.Package.Version.to_string()));
                }
                else
                {
                    this.Log().Warn(NO_CHANGE_MESSAGE);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parses command line. Instantiates command if not already instantiated.
        /// Cannot change command type without creating new instance of CommandContext
        /// </summary>
        /// <param name="cmdLine">command line to process</param>
        /// <param name="diffAtStart">true if configuration comparison starts from start of command line processing, false if after main
        /// command is executed</param>
        public IEnumerable <Option> ParseCommandLine2(string cmdLine, bool diffAtStart = false, bool logCalls = false)
        {
            var console = LogService.console;

            console.Info($"> {cmdLine}");
            string[] args = ChocolateyOptionSet.SplitArgs(cmdLine);
            ChocolateyConfiguration config = new ChocolateyConfiguration();

            parser = new ChocolateyOptionSet();

            if (diffAtStart)
            {
                original = config.deep_copy();
            }
            if (parser.Parse(args, new ChocolateyStartupCommand(), config, () =>
            {
                console.Info("- logInit called.");
            }
                             ))
            {
                console.Info("chocoArgsParse: return on startup");
                return(parser);
            }

            parser = new ChocolateyOptionSet();

            if (parser.Parse(args, new ChocolateyMainCommand(), config))
            {
                console.Info("chocoArgsParse: return on main");
                return(parser);
            }

            genericArgumentsCount = parser.Count;

            if (args.Length == 0)
            {
                "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "Please run 'choco -?' or 'choco <command> -?' for help menu.");
            }

            InstantiateCommand(config.CommandName, logCalls);
            if (command == null)
            {
                return(parser.Skip(genericArgumentsCount));
            }

            LogService.Instance.adjustLogLevels(config.Debug, config.Verbose, config.Trace);

            if (!diffAtStart)
            {
                original = config.deep_copy();
            }

            string phase = "parse";

            try
            {
                if (!parser.Parse(args.Skip(1), command, config))
                {
                    phase = "validation";
                    command.handle_validation(config);

                    console.Info("config: " + config.CompareWith(original));

                    // GenericRunner.run logic, see after line:
                    // var command = find_command(config, container, isConsole, parseArgs);
                    if (config.Noop)
                    {
                        if (config.RegularOutput)
                        {
                            this.Log().Info("_ {0}:{1} - Noop Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
                        }

                        command.noop(config);
                    }
                    else
                    {
                        this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
                        command.run(config);
                    }
                }
            }
            catch (ApplicationException ex)
            {
                console.Info($"{phase} failure: " + ex.Message);
            }

            return(parser.Skip(genericArgumentsCount));
        }