From b8ad1e059c4ca64b61f1fe1be284abf5dfcaa337 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Mon, 17 Jan 2022 17:51:32 -0600 Subject: [PATCH] (#1185) Still run hooks even without a package automation script This allows hooks to run even without a package automation script being present. --- .../services/PowershellService.cs | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index 9466819721..e3c6f08f9a 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -237,62 +237,70 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack var hookPreScriptPathList = get_hook_scripts(configuration, packageResult, command, true); var hookPostScriptPathList = get_hook_scripts(configuration, packageResult, command, false); - if (!string.IsNullOrEmpty(chocoPowerShellScript)) + if (!string.IsNullOrEmpty(chocoPowerShellScript) || hookPreScriptPathList.Any() || hookPostScriptPathList.Any()) { var failure = false; var package = packageResult.Package; prepare_powershell_environment(package, configuration, packageDirectory); - - this.Log().Debug(ChocolateyLoggers.Important, "Contents of '{0}':".format_with(chocoPowerShellScript)); - string chocoPowerShellScriptContents = _fileSystem.read_file(chocoPowerShellScript); - // leave this way, doesn't take it through formatting. - this.Log().Debug(() => chocoPowerShellScriptContents.escape_curly_braces()); - bool shouldRun = !configuration.PromptForConfirmation; - if (!shouldRun) + if (!string.IsNullOrEmpty(chocoPowerShellScript)) { - this.Log().Info(ChocolateyLoggers.Important, () => "The package {0} wants to run '{1}'.".format_with(package.Id, _fileSystem.get_file_name(chocoPowerShellScript))); - this.Log().Info(ChocolateyLoggers.Important, () => "Note: If you don't run this script, the installation will fail."); - this.Log().Info(ChocolateyLoggers.Important, () => @"Note: To confirm automatically next time, use '-y' or consider:"); - this.Log().Info(ChocolateyLoggers.Important, () => @"choco feature enable -n allowGlobalConfirmation"); - - var selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run the script?", - new[] { "yes", "all - yes to all", "no", "print" }, - defaultChoice: null, - requireAnswer: true, - allowShortAnswer: true, - shortPrompt: true - ); + this.Log().Debug(ChocolateyLoggers.Important, "Contents of '{0}':".format_with(chocoPowerShellScript)); + string chocoPowerShellScriptContents = _fileSystem.read_file(chocoPowerShellScript); + // leave this way, doesn't take it through formatting. + this.Log().Debug(() => chocoPowerShellScriptContents.escape_curly_braces()); - if (selection.is_equal_to("print")) + if (!shouldRun) { - this.Log().Info(ChocolateyLoggers.Important, "------ BEGIN SCRIPT ------"); - this.Log().Info(() => "{0}{1}{0}".format_with(Environment.NewLine, chocoPowerShellScriptContents.escape_curly_braces())); - this.Log().Info(ChocolateyLoggers.Important, "------- END SCRIPT -------"); - selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run this script?", - new[] { "yes", "no" }, + this.Log().Info(ChocolateyLoggers.Important, () => "The package {0} wants to run '{1}'.".format_with(package.Id, _fileSystem.get_file_name(chocoPowerShellScript))); + this.Log().Info(ChocolateyLoggers.Important, () => "Note: If you don't run this script, the installation will fail."); + this.Log().Info(ChocolateyLoggers.Important, () => @"Note: To confirm automatically next time, use '-y' or consider:"); + this.Log().Info(ChocolateyLoggers.Important, () => @"choco feature enable -n allowGlobalConfirmation"); + + var selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run the script?", + new[] { "yes", "all - yes to all", "no", "print" }, defaultChoice: null, requireAnswer: true, allowShortAnswer: true, shortPrompt: true + ); + + if (selection.is_equal_to("print")) + { + this.Log().Info(ChocolateyLoggers.Important, "------ BEGIN SCRIPT ------"); + this.Log().Info(() => "{0}{1}{0}".format_with(Environment.NewLine, chocoPowerShellScriptContents.escape_curly_braces())); + this.Log().Info(ChocolateyLoggers.Important, "------- END SCRIPT -------"); + selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run this script?", + new[] { "yes", "no" }, + defaultChoice: null, + requireAnswer: true, + allowShortAnswer: true, + shortPrompt: true ); - } + } - if (selection.is_equal_to("yes")) shouldRun = true; - if (selection.is_equal_to("all - yes to all")) - { - configuration.PromptForConfirmation = false; - shouldRun = true; - } - if (selection.is_equal_to("no")) - { - //MSI ERROR_INSTALL_USEREXIT - 1602 - https://support.microsoft.com/en-us/kb/304888 / https://msdn.microsoft.com/en-us/library/aa376931.aspx - //ERROR_INSTALL_CANCEL - 15608 - https://msdn.microsoft.com/en-us/library/windows/desktop/ms681384.aspx - Environment.ExitCode = 15608; - packageResult.Messages.Add(new ResultMessage(ResultType.Error, "User canceled powershell portion of installation for '{0}'.{1} Specify -n to skip automated script actions.".format_with(chocoPowerShellScript, Environment.NewLine))); + if (selection.is_equal_to("yes")) shouldRun = true; + if (selection.is_equal_to("all - yes to all")) + { + configuration.PromptForConfirmation = false; + shouldRun = true; + } + + if (selection.is_equal_to("no")) + { + //MSI ERROR_INSTALL_USEREXIT - 1602 - https://support.microsoft.com/en-us/kb/304888 / https://msdn.microsoft.com/en-us/library/aa376931.aspx + //ERROR_INSTALL_CANCEL - 15608 - https://msdn.microsoft.com/en-us/library/windows/desktop/ms681384.aspx + Environment.ExitCode = 15608; + packageResult.Messages.Add(new ResultMessage(ResultType.Error, "User canceled powershell portion of installation for '{0}'.{1} Specify -n to skip automated script actions.".format_with(chocoPowerShellScript, Environment.NewLine))); + } } } + else + { + shouldRun = true; + this.Log().Warn("No package automation script, running only hooks"); + } if (shouldRun) {