Skip to content

Commit

Permalink
(GH-995) Ensure beforeModify runs as first action
Browse files Browse the repository at this point in the history
For upgrades and uninstalls, there was an attempt to run other
operations ahead of the beforeModify script, which included removing a
rollback directory that may have been locking files for a service.
Instead ensure that before modify is the first physcial operation
attempted prior to upgrades and uninstall actions.

For uninstalls, ensure that the before modify action is passed through
as part of the actions to take prior to uninstall instead of waiting
for the regular means to come back to handle_package_uninstall.
  • Loading branch information
ferventcoder committed Oct 4, 2016
1 parent 0b91c27 commit c372fa8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -745,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 @@ -862,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
23 changes: 13 additions & 10 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 @@ -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

0 comments on commit c372fa8

Please sign in to comment.