public IPackage download_package(string packageId, string packageVersion, string downloadLocation, IConfigurationSettings configuration)
        {
            var version = new SemanticVersion(0, 0, 0, 0);

            if (!string.IsNullOrWhiteSpace(packageVersion))
            {
                version = new SemanticVersion(packageVersion);
            }

            var packageManager = get_package_manager(downloadLocation, configuration);

            this.Log().Debug(() => "Searching for {0} v{1} to install from {2}.".format_with(packageId, version.to_string(), packageManager.SourceRepository.Source));

            IPackage availablePackage = packageManager.SourceRepository.FindPackage(packageId, version, allowPrereleaseVersions: true, allowUnlisted: true);

            if (availablePackage == null)
            {
                //todo: do something here
            }

            this.Log().Debug(() => "Installing {0} v{1} from {2}.".format_with(packageId, version.to_string(), packageManager.SourceRepository.Source));

            packageManager.InstallPackage(availablePackage, ignoreDependencies: true, allowPrereleaseVersions: true);

            var cachePackage = _fileSystem.combine_paths(Environment.GetEnvironmentVariable("LocalAppData"), "NuGet", "Cache", "{0}.{1}.nupkg".format_with(packageId, version.to_string()));

            if (_fileSystem.file_exists(cachePackage))
            {
                _fileSystem.delete_file(cachePackage);
            }

            this.Log().Debug(() => "Returning {0} v{1} package.".format_with(packageId, version.to_string()));

            return(packageManager.LocalRepository.FindPackage(packageId, version, allowPrereleaseVersions: true, allowUnlisted: true));
        }
Exemple #2
0
        public void pack_run(ChocolateyConfiguration config)
        {
            string nuspecFilePath  = validate_and_return_package_file(config, Constants.ManifestExtension);
            var    nuspecDirectory = _fileSystem.get_full_path(_fileSystem.get_directory_name(nuspecFilePath));

            IDictionary <string, string> properties = new Dictionary <string, string>();

            // Set the version property if the flag is set
            if (!string.IsNullOrWhiteSpace(config.Version))
            {
                properties["version"] = config.Version;
            }

            // Initialize the property provider based on what was passed in using the properties flag
            var propertyProvider = new DictionaryPropertyProvider(properties);

            var basePath = _fileSystem.get_current_directory();

            if (config.Information.PlatformType != PlatformType.Windows)
            {
                //bug with nuspec and tools/** folder location on Windows.
                basePath = "./";
            }

            var builder = new PackageBuilder(nuspecFilePath, basePath, propertyProvider, includeEmptyDirectories: true);

            if (!string.IsNullOrWhiteSpace(config.Version))
            {
                builder.Version = new SemanticVersion(config.Version);
            }

            string outputFile = builder.Id + "." + builder.Version + Constants.PackageExtension;
            string outputPath = _fileSystem.combine_paths(nuspecDirectory, outputFile);

            this.Log().Info(() => "Attempting to build package from '{0}'.".format_with(_fileSystem.get_file_name(nuspecFilePath)));

            //IPackage package =
            NugetPack.BuildPackage(builder, _fileSystem, outputPath);
            //todo: v1 analyze package
            //if (package != null)
            //{
            //    AnalyzePackage(package);
            //}

            this.Log().Info(ChocolateyLoggers.Important, () => "Successfully created package '{0}'".format_with(outputFile));
        }
Exemple #3
0
        public string wrap_script_with_module(string script, ChocolateyConfiguration config)
        {
            var installerModule = _fileSystem.combine_paths(get_helpers_folder(), "chocolateyInstaller.psm1");
            var scriptRunner    = _fileSystem.combine_paths(get_helpers_folder(), "chocolateyScriptRunner.ps1");

            // removed setting all errors to terminating. Will cause too
            // many issues in existing packages, including upgrading
            // Chocolatey from older POSH client due to log errors
            //$ErrorActionPreference = 'Stop';
            return("[System.Threading.Thread]::CurrentThread.CurrentCulture = '';[System.Threading.Thread]::CurrentThread.CurrentUICulture = ''; & import-module -name '{0}';{2} & '{1}' {3}"
                   .format_with(
                       installerModule,
                       scriptRunner,
                       string.IsNullOrWhiteSpace(_customImports) ? string.Empty : "& {0}".format_with(_customImports.EndsWith(";") ? _customImports : _customImports + ";"),
                       get_script_arguments(script, config)
                       ));
        }
Exemple #4
0
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);

            if (package == null)
            {
                this.Log().Debug("No package information as package is null.");
                return(packageInformation);
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));

            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return(packageInformation);
            }

            var deserializationErrorMessage = @"
A corrupt .registry file exists at {0}.
 Open this file in a text editor, and remove/escape any characters that
 are regarded as illegal within XML strings not surrounded by CData. 
 These are typically the characters &, `<`, and `>`. Again, this
 is an XML document, so you will see many < and > characters, so just
 focus exclusively in the string values not surrounded by CData. Once 
 these have been corrected, rename the .registry.bad file to .registry.
 Once saved, try running the same Chocolatey command that was just 
 executed, so verify problem is fixed.
 NOTE: It will not be possible to rename the file in Windows Explorer.
 Instead, you can use the following PowerShell command:
 Move-Item .\.registry.bad .\.registry
".format_with(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE));

            try
            {
                if (_fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE)))
                {
                    this.Log().Warn(deserializationErrorMessage);
                }
                else
                {
                    packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
                }
            }
            catch (Exception)
            {
                FaultTolerance.try_catch_with_logging_exception(
                    () =>
                {
                    this.Log().Warn(deserializationErrorMessage);

                    // rename the bad registry file so that it isn't processed again
                    _fileSystem.move_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE), _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE));
                },
                    "Unable to read registry snapshot file for {0} (located at {1})".format_with(package.Id, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE)),
                    throwError: false,
                    logWarningInsteadOfError: true,
                    isSilent: true
                    );
            }

            FaultTolerance.try_catch_with_logging_exception(
                () =>
            {
                packageInformation.FilesSnapshot = _filesService.read_from_file(_fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
            },
                "Unable to read files snapshot file",
                throwError: false,
                logWarningInsteadOfError: true,
                isSilent: true
                );

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide       = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned           = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);

            if (_fileSystem.file_exists(argsFile))
            {
                packageInformation.Arguments = _fileSystem.read_file(argsFile);
            }
            var extraInfoFile = _fileSystem.combine_paths(pkgStorePath, EXTRA_FILE);

            if (_fileSystem.file_exists(extraInfoFile))
            {
                packageInformation.ExtraInformation = _fileSystem.read_file(extraInfoFile);
            }

            var versionOverrideFile = _fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE);

            if (_fileSystem.file_exists(versionOverrideFile))
            {
                FaultTolerance.try_catch_with_logging_exception(
                    () =>
                {
                    packageInformation.VersionOverride = new SemanticVersion(_fileSystem.read_file(versionOverrideFile).trim_safe());
                },
                    "Unable to read version override file",
                    throwError: false,
                    logWarningInsteadOfError: true
                    );
            }

            return(packageInformation);
        }
 private string get_helpers_folder()
 {
     return(_fileSystem.combine_paths(ApplicationParameters.InstallLocation, "helpers"));
 }
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));

            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return(packageInformation);
            }

            string registrySnapshotFile = _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE);

            if (_fileSystem.file_exists(registrySnapshotFile))
            {
                packageInformation.RegistrySnapshot = _registryService.read_from_file(registrySnapshotFile);
            }

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide       = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned           = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));

            return(packageInformation);
        }
Exemple #7
0
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);

            if (package == null)
            {
                this.Log().Debug("No package information as package is null.");
                return(packageInformation);
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));

            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return(packageInformation);
            }

            FaultTolerance.try_catch_with_logging_exception(
                () =>
            {
                packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
            },
                "Unable to read registry snapshot file for {0} (located at {1})".format_with(package.Id, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE)),
                throwError: false,
                logWarningInsteadOfError: true
                );

            FaultTolerance.try_catch_with_logging_exception(
                () =>
            {
                packageInformation.FilesSnapshot = _filesService.read_from_file(_fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
            },
                "Unable to read files snapshot file",
                throwError: false,
                logWarningInsteadOfError: true
                );

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide       = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned           = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);

            if (_fileSystem.file_exists(argsFile))
            {
                packageInformation.Arguments = _fileSystem.read_file(argsFile);
            }

            var versionOverrideFile = _fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE);

            if (_fileSystem.file_exists(versionOverrideFile))
            {
                FaultTolerance.try_catch_with_logging_exception(
                    () =>
                {
                    packageInformation.VersionOverride = new SemanticVersion(_fileSystem.read_file(versionOverrideFile).trim_safe());
                },
                    "Unable to read version override file",
                    throwError: false,
                    logWarningInsteadOfError: true
                    );
            }

            return(packageInformation);
        }
        public void generate_noop(ChocolateyConfiguration configuration)
        {
            var templateLocation = _fileSystem.combine_paths(configuration.OutputDirectory ?? _fileSystem.get_current_directory(), configuration.NewCommand.Name);

            this.Log().Info(() => "Would have generated a new package specification at {0}".format_with(templateLocation));
        }
        public ConcurrentDictionary <string, PackageResult> install_run(ChocolateyConfiguration config)
        {
            this.Log().Info(@"Installing the following packages:");
            this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));
            this.Log().Info(@"By installing you accept licenses for the packages.");

            var packageInstalls = new ConcurrentDictionary <string, PackageResult>();

            foreach (var packageConfig in set_config_from_package_names_and_packages_config(config, packageInstalls).or_empty_list_if_null())
            {
                Action <PackageResult> action = null;
                if (packageConfig.SourceType == SourceType.normal)
                {
                    action = (packageResult) => handle_package_result(packageResult, packageConfig, CommandNameType.install);
                }
                var results = perform_source_runner_function(packageConfig, r => r.install_run(packageConfig, action));

                foreach (var result in results)
                {
                    packageInstalls.GetOrAdd(result.Key, result.Value);
                }
            }

            var installFailures = packageInstalls.Count(p => !p.Value.Success);
            var installWarnings = packageInstalls.Count(p => p.Value.Warning);

            this.Log().Warn(() => @"{0}{1} installed {2}/{3} package(s). {4} package(s) failed.{5}{0} See the log for details ({6}).".format_with(
                                Environment.NewLine,
                                ApplicationParameters.Name,
                                packageInstalls.Count(p => p.Value.Success && !p.Value.Inconclusive),
                                packageInstalls.Count,
                                installFailures,
                                installWarnings == 0 ? string.Empty : "{0} {1} package(s) had warnings.".format_with(Environment.NewLine, installWarnings),
                                _fileSystem.combine_paths(ApplicationParameters.LoggingLocation, ApplicationParameters.LoggingFile)
                                ));

            if (installWarnings != 0)
            {
                this.Log().Warn(ChocolateyLoggers.Important, "Warnings:");
                foreach (var warning in packageInstalls.Where(p => p.Value.Warning).or_empty_list_if_null())
                {
                    this.Log().Warn(ChocolateyLoggers.Important, " - {0}".format_with(warning.Value.Name));
                }
            }

            if (installFailures != 0)
            {
                this.Log().Error("Failures:");
                foreach (var failure in packageInstalls.Where(p => !p.Value.Success).or_empty_list_if_null())
                {
                    this.Log().Error(" - {0}".format_with(failure.Value.Name));
                }
            }

            if (installFailures != 0 && Environment.ExitCode == 0)
            {
                Environment.ExitCode = 1;
            }

            return(packageInstalls);
        }