From 6e1cbd365e8330e2b2a215c5e3be08b3d5304697 Mon Sep 17 00:00:00 2001 From: Tyler Szabo Date: Fri, 19 Apr 2019 21:20:30 -0700 Subject: [PATCH] (GH-1806) Support upgrading Cygwin packages --- .../services/CygwinService.cs | 102 +++++++++++++++++- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/CygwinService.cs b/src/chocolatey/infrastructure.app/services/CygwinService.cs index a2458c6ed0..4ea0910439 100644 --- a/src/chocolatey/infrastructure.app/services/CygwinService.cs +++ b/src/chocolatey/infrastructure.app/services/CygwinService.cs @@ -52,6 +52,7 @@ public sealed class CygwinService : ISourceRunner public static readonly Regex PackageNameRegex = new Regex(@"/(?<{0}>[^/]*).tar.".format_with(PACKAGE_NAME_GROUP), RegexOptions.Compiled); private readonly IDictionary _installArguments = new Dictionary(StringComparer.InvariantCultureIgnoreCase); + private readonly IDictionary _upgradeArguments = new Dictionary(StringComparer.InvariantCultureIgnoreCase); public CygwinService(ICommandExecutor commandExecutor, INugetService nugetService, IFileSystem fileSystem, IRegistryService registryService) { @@ -68,6 +69,7 @@ public CygwinService(ICommandExecutor commandExecutor, INugetService nugetServic private void set_cmd_args_dictionaries() { set_install_dictionary(_installArguments); + set_upgrade_dictionary(_upgradeArguments); } /// @@ -119,6 +121,49 @@ private void set_install_dictionary(IDictionary }); } + /// + /// Sets upgrade dictionary + /// + private void set_upgrade_dictionary(IDictionary args) + { + //args.Add("_cmd_c_", new ExternalCommandArgument { ArgumentOption = "/c", Required = true }); + //args.Add("_app_", new ExternalCommandArgument + //{ + // ArgumentOption = "", + // ArgumentValue = _fileSystem.combine_paths(INSTALL_ROOT_TOKEN, "cygwinsetup.exe"), + // QuoteValue = false, + // UseValueOnly = true, + // Required = true + //}); + args.Add("_quiet_", new ExternalCommandArgument {ArgumentOption = "--quiet-mode", Required = true}); + args.Add("_no_desktop_", new ExternalCommandArgument {ArgumentOption = "--no-desktop", Required = true}); + args.Add("_no_startmenu_", new ExternalCommandArgument {ArgumentOption = "--no-startmenu", Required = true}); + args.Add("_root_", new ExternalCommandArgument + { + ArgumentOption = "--root ", + ArgumentValue = INSTALL_ROOT_TOKEN, + QuoteValue = false, + Required = true + }); + args.Add("_local_pkgs_dir_", new ExternalCommandArgument + { + ArgumentOption = "--local-package-dir ", + ArgumentValue = "{0}\\packages".format_with(INSTALL_ROOT_TOKEN), + QuoteValue = false, + Required = true + }); + + args.Add("_site_", new ExternalCommandArgument + { + ArgumentOption = "--site ", + ArgumentValue = "http://mirrors.kernel.org/sourceware/cygwin/", + QuoteValue = false, + Required = true + }); + + args.Add("_upgrade_", new ExternalCommandArgument {ArgumentOption = "--upgrade-also", Required = true}); + } + public SourceType SourceType { get { return SourceType.cygwin; } @@ -260,13 +305,64 @@ public ConcurrentDictionary install_run(ChocolateyConfigu public ConcurrentDictionary upgrade_noop(ChocolateyConfiguration config, Action continueAction) { - this.Log().Warn(ChocolateyLoggers.Important, "{0} does not implement upgrade".format_with(APP_NAME)); + if (!config.PackageNames.Equals("all")) + { + throw new ApplicationException("{0} does not support upgrading individual packages. Either use install or upgrade all".format_with(APP_NAME)); + } + + var args = build_args(config, _upgradeArguments); + this.Log().Info("Would have run '{0} {1}'".format_with(get_exe(_rootDirectory).escape_curly_braces(), args.escape_curly_braces())); return new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); } public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) { - throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME)); + if (!config.PackageNames.Equals("all")) + { + throw new ApplicationException("{0} does not support upgrading individual packages. Either use install or upgrade all".format_with(APP_NAME)); + } + + var packageResults = new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); + var args = build_args(config, _upgradeArguments); + + var exitCode = _commandExecutor.execute( + get_exe(_rootDirectory), + args, + config.CommandExecutionTimeoutSeconds, + _fileSystem.get_current_directory(), + (s, e) => + { + var logMessage = e.Data; + if (string.IsNullOrWhiteSpace(logMessage)) return; + this.Log().Info(() => " [{0}] {1}".format_with(APP_NAME, logMessage.escape_curly_braces())); + + if (InstalledRegex.IsMatch(logMessage)) + { + var packageName = get_value_from_output(logMessage, PackageNameRegex, PACKAGE_NAME_GROUP); + var results = packageResults.GetOrAdd(packageName, new PackageResult(packageName, null, null)); + results.Messages.Add(new ResultMessage(ResultType.Note, packageName)); + if (!string.IsNullOrWhiteSpace(packageName)) + { + this.Log().Info(ChocolateyLoggers.Important, " {0} has been installed successfully.".format_with(packageName)); + } + } + }, + (s, e) => + { + var logMessage = e.Data; + if (string.IsNullOrWhiteSpace(logMessage)) return; + this.Log().Error("[{0}] {1}".format_with(APP_NAME, logMessage.escape_curly_braces())); + }, + updateProcessPath: false, + allowUseWindow: true + ); + + if (exitCode != 0) + { + Environment.ExitCode = exitCode; + } + + return packageResults; } public void uninstall_noop(ChocolateyConfiguration config, Action continueAction) @@ -276,7 +372,7 @@ public void uninstall_noop(ChocolateyConfiguration config, Action public ConcurrentDictionary uninstall_run(ChocolateyConfiguration config, Action continueAction, Action beforeUninstallAction = null) { - throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME)); + throw new NotImplementedException("{0} does not implement uninstall".format_with(APP_NAME)); } ///