Skip to content

Commit

Permalink
(chocolatey#1185) Still run hooks even without a package automation s…
Browse files Browse the repository at this point in the history
…cript

This allows hooks to run even without a package automation script being
present.
  • Loading branch information
TheCakeIsNaOH committed Jan 17, 2022
1 parent 68a2416 commit b8ad1e0
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit b8ad1e0

Please sign in to comment.