Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (chocolateyGH-1000) Allow `$LASTEXITCODE` check by feature
  (chocolateyGH-1000) Don't check $LASTEXITCODE by default
  (chocolateyGH-995) Ensure beforeModify runs as first action
  (maint) specs fixup
  (chocolateyGH-996) ISourceRunner.uninstall beforeModify action
  (chocolateyGH-996) beforemodify check before run
  (chocolateyGH-995) rename before_package_upgrade
  • Loading branch information
ferventcoder committed Oct 5, 2016
2 parents 70932d0 + 6a81892 commit 078cd32
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 38 deletions.
16 changes: 14 additions & 2 deletions src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,22 @@ $checksumExe = Join-Path $chocoTools 'checksum.exe'

Write-Debug "Running `'$packageScript`'";
& "$packageScript"

$scriptSuccess = $?
$lastExecutableExitCode = $LASTEXITCODE

if ($lastExecutableExitCode -ne $null -and $lastExecutableExitCode -ne '') {
Write-Debug "The last executable that ran had an exit code of '$lastExecutableExitCode'."
}

if (-not $scriptSuccess) {
Write-Debug "The script exited with a failure."
}

$exitCode = 0
if ($env:ChocolateyCheckLastExitCode -ne $null -and $env:ChocolateyCheckLastExitCode -eq 'true' -and $lastExecutableExitCode -ne $null -and $lastExecutableExitCode -ne '') {
$exitCode = $lastExecutableExitCode
}

$exitCode = $LASTEXITCODE
if ($exitCode -eq 0 -and -not $scriptSuccess) {
$exitCode = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,6 @@ public void should_have_a_version_of_one_dot_zero()
}
}


[Concern(typeof(ChocolateyInstallCommand))]
public class when_installing_a_package_that_has_nonterminating_errors_with_fail_on_stderr : ScenariosBase
{
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static class Environment
public static readonly string ChocolateyIgnoreChecksums = "ChocolateyIgnoreChecksums";
public static readonly string ChocolateyAllowEmptyChecksums = "ChocolateyAllowEmptyChecksums";
public static readonly string ChocolateyAllowEmptyChecksumsSecure = "ChocolateyAllowEmptyChecksumsSecure";
public static readonly string ChocolateyCheckLastExitCode = "ChocolateyCheckLastExitCode";
public static readonly string ChocolateyPowerShellHost = "ChocolateyPowerShellHost";
public static readonly string ChocolateyForce = "ChocolateyForce";
}
Expand Down Expand Up @@ -154,6 +155,7 @@ public static class Features
public static readonly string IgnoreInvalidOptionsSwitches = "ignoreInvalidOptionsSwitches";
public static readonly string UsePackageExitCodes = "usePackageExitCodes";
public static readonly string UseFipsCompliantChecksums = "useFipsCompliantChecksums";
public static readonly string ScriptsCheckLastExitCode = "scriptsCheckLastExitCode";
}

public static class Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile
config.Features.IgnoreInvalidOptionsSwitches = set_feature_flag(ApplicationParameters.Features.IgnoreInvalidOptionsSwitches, configFileSettings, defaultEnabled: true, description: "Ignore Invalid Options/Switches - If a switch or option is passed that is not recognized, should choco fail? Available in 0.9.10+.");
config.Features.UsePackageExitCodes = set_feature_flag(ApplicationParameters.Features.UsePackageExitCodes, configFileSettings, defaultEnabled: true, description: "Use Package Exit Codes - Package scripts can provide exit codes. With this on, package exit codes will be what choco uses for exit when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. With this feature off, choco will exit with a 0 or a 1 (matching previous behavior). Available in 0.9.10+.");
config.Features.UseFipsCompliantChecksums = set_feature_flag(ApplicationParameters.Features.UseFipsCompliantChecksums, configFileSettings, defaultEnabled: false, description: "Use FIPS Compliant Checksums - Ensure checksumming done by choco uses FIPS compliant algorithms. Not recommended unless required by FIPS Mode. Enabling on an existing installation could have unintended consequences related to upgrades/uninstalls. Available in 0.9.10+.");
config.Features.ScriptsCheckLastExitCode = set_feature_flag(ApplicationParameters.Features.ScriptsCheckLastExitCode, configFileSettings, defaultEnabled: false, description: "Scripts Check $LastExitCode (external commands) - Leave this off unless you absolutely need it while you fix your package scripts to use `throw 'error message'` or `Set-PowerShellExitCode #` instead of `exit #`. This behavior started in 0.9.10 and produced hard to find bugs. If the last external process exits successfully but with an exit code of not zero, this could cause hard to detect package failures. Available in 0.10.3+. Will be removed in 0.11.0.");
config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ public sealed class FeaturesConfiguration
public bool IgnoreInvalidOptionsSwitches { get; set; }
public bool UsePackageExitCodes { get; set; }
public bool UseFipsCompliantChecksums { get; set; }
//todo remove in 0.11.0
public bool ScriptsCheckLastExitCode { get; set; }
}

//todo: retrofit other command configs this way
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static void set_environment_variables(ChocolateyConfiguration config)
if (!config.Features.ChecksumFiles) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyIgnoreChecksums, "true");
if (config.Features.AllowEmptyChecksums) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyAllowEmptyChecksums, "true");
if (config.Features.AllowEmptyChecksumsSecure) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyAllowEmptyChecksumsSecure, "true");
if (config.Features.ScriptsCheckLastExitCode) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyCheckLastExitCode, "true");
Environment.SetEnvironmentVariable("chocolateyRequestTimeout", config.WebRequestTimeoutSeconds.to_string() + "000");
Environment.SetEnvironmentVariable("chocolateyResponseTimeout", config.CommandExecutionTimeoutSeconds.to_string() + "000");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu

get_environment_before(config, allowLogging: true);

var beforeUpgradeAction = new Action<PackageResult>(packageResult => before_package_upgrade(packageResult, config));

var beforeUpgradeAction = new Action<PackageResult>(packageResult => before_package_modify(packageResult, config));
var packageUpgrades = perform_source_runner_function(config, r => r.upgrade_run(config, action, beforeUpgradeAction));

var upgradeFailures = report_action_summary(packageUpgrades, "upgraded");
Expand All @@ -705,9 +704,12 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
return packageUpgrades;
}

private void before_package_upgrade(PackageResult packageResult, ChocolateyConfiguration config)
private void before_package_modify(PackageResult packageResult, ChocolateyConfiguration config)
{
_powershellService.before_modify(config, packageResult);
if (!config.SkipPackageInstallProvider)
{
_powershellService.before_modify(config, packageResult);
}
}

public void uninstall_noop(ChocolateyConfiguration config)
Expand Down Expand Up @@ -743,8 +745,8 @@ public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfi
}

var environmentBefore = get_environment_before(config);

var packageUninstalls = perform_source_runner_function(config, r => r.uninstall_run(config, action));
var beforeUninstallAction = new Action<PackageResult>(packageResult => before_package_modify(packageResult, config));
var packageUninstalls = perform_source_runner_function(config, r => r.uninstall_run(config, action, beforeUninstallAction));

// not handled in the uninstall handler
IEnumerable<GenericRegistryValue> environmentChanges;
Expand Down Expand Up @@ -860,11 +862,6 @@ public void handle_package_uninstall(PackageResult packageResult, ChocolateyConf
packageResult.InstallLocation += ".{0}".format_with(packageResult.Package.Version.to_string());
}

if (!config.SkipPackageInstallProvider)
{
_powershellService.before_modify(config, packageResult);
}

_shimgenService.uninstall(config, packageResult);

if (!config.SkipPackageInstallProvider)
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/services/CygwinService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfig
return new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);
}

public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction)
public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction = null)
{
throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME));
}
Expand All @@ -273,7 +273,7 @@ public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult>
this.Log().Warn(ChocolateyLoggers.Important, "{0} does not implement uninstall".format_with(APP_NAME));
}

public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUninstallAction = null)
{
throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME));
}
Expand Down
3 changes: 2 additions & 1 deletion src/chocolatey/infrastructure.app/services/ISourceRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public interface ISourceRunner
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="continueAction">The action to continue with when upgrade is successful.</param>
/// <param name="beforeUninstallAction">The action (if any) to run on any currently installed package before triggering the uninstall.</param>
/// <returns>results of installs</returns>
ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction);
ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUninstallAction = null);
}
}
29 changes: 16 additions & 13 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,6 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
foreach (string packageName in packageNames.or_empty_list_if_null())
{
//todo: get smarter about realizing multiple versions have been installed before and allowing that

remove_rollback_directory_if_exists(packageName);

IPackage installedPackage = packageManager.LocalRepository.FindPackage(packageName);

if (installedPackage != null && (version == null || version == installedPackage.Version) && !config.Force)
Expand Down Expand Up @@ -433,7 +430,8 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
{
var forcedResult = packageInstalls.GetOrAdd(packageName, new PackageResult(availablePackage, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, availablePackage.Id)));
forcedResult.Messages.Add(new ResultMessage(ResultType.Note, "Backing up and removing old version"));


remove_rollback_directory_if_exists(packageName);
backup_existing_version(config, installedPackage, _packageInfoService.get_package_information(installedPackage));

try
Expand Down Expand Up @@ -543,8 +541,6 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu

foreach (string packageName in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null())
{
remove_rollback_directory_if_exists(packageName);

IPackage installedPackage = packageManager.LocalRepository.FindPackage(packageName);

if (installedPackage == null)
Expand Down Expand Up @@ -717,14 +713,15 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
packageName,
version == null ? null : version.ToString()))
{
ensure_package_files_have_compatible_attributes(config, installedPackage, pkgInfo);
rename_legacy_package_version(config, installedPackage, pkgInfo);
if (beforeUpgradeAction != null)
{
var currentPackageResult = new PackageResult(installedPackage, get_install_directory(config, installedPackage));
beforeUpgradeAction(currentPackageResult);
}

remove_rollback_directory_if_exists(packageName);
ensure_package_files_have_compatible_attributes(config, installedPackage, pkgInfo);
rename_legacy_package_version(config, installedPackage, pkgInfo);
backup_existing_version(config, installedPackage, pkgInfo);
remove_shim_directors(config, installedPackage, pkgInfo);
if (config.Force && (installedPackage.Version == availablePackage.Version))
Expand Down Expand Up @@ -965,12 +962,12 @@ public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult>
}
}

public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUninstallAction = null)
{
return uninstall_run(config, continueAction, performAction: true);
return uninstall_run(config, continueAction, performAction: true, beforeUninstallAction: beforeUninstallAction);
}

public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, bool performAction)
public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, bool performAction, Action<PackageResult> beforeUninstallAction = null)
{
var packageUninstalls = new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);

Expand Down Expand Up @@ -1089,8 +1086,6 @@ public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfi

foreach (string packageName in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null())
{
remove_rollback_directory_if_exists(packageName);

IList<IPackage> installedPackageVersions = new List<IPackage>();
if (string.IsNullOrWhiteSpace(config.Version))
{
Expand Down Expand Up @@ -1177,8 +1172,16 @@ public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfi
packageVersion.Id, packageVersion.Version.to_string())
)
{
if (beforeUninstallAction != null)
{
// guessing this is not added so that it doesn't fail the action if an error is recorded?
//var currentPackageResult = packageUninstalls.GetOrAdd(packageName, new PackageResult(packageVersion, get_install_directory(config, packageVersion)));
var currentPackageResult = new PackageResult(packageVersion, get_install_directory(config, packageVersion));
beforeUninstallAction(currentPackageResult);
}
ensure_package_files_have_compatible_attributes(config, packageVersion, pkgInfo);
rename_legacy_package_version(config, packageVersion, pkgInfo);
remove_rollback_directory_if_exists(packageName);
backup_existing_version(config, packageVersion, pkgInfo);
packageManager.UninstallPackage(packageVersion.Id, forceRemove: config.Force, removeDependencies: config.ForceDependencies, version: packageVersion.Version);
ensure_nupkg_is_removed(packageVersion, pkgInfo);
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/services/PythonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfig
return new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);
}

public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction)
public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction = null)
{
set_executable_path_if_not_set();
var args = build_args(config, _upgradeArguments);
Expand Down Expand Up @@ -465,7 +465,7 @@ public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult>
this.Log().Info("Would have run '{0} {1}'".format_with(_exePath.escape_curly_braces(), args.escape_curly_braces()));
}

public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUninstallAction = null)
{
set_executable_path_if_not_set();
var args = build_args(config, _uninstallArguments);
Expand Down
Loading

0 comments on commit 078cd32

Please sign in to comment.