Skip to content

Commit

Permalink
(chocolatey#1092) Pull beforeModify changes into develop
Browse files Browse the repository at this point in the history
Changes originally in 9f5d9cc, adapted
for the current develop branch and the changes therein.

Additionally, replaced and obsoleted some overloads of public (why?)
methods that have unused method arguments and made them protected.
  • Loading branch information
vexx32 committed Mar 14, 2023
1 parent 7acbd15 commit f4a943d
Showing 1 changed file with 89 additions and 30 deletions.
119 changes: 89 additions & 30 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,7 @@ Version was specified as '{0}'. It is possible that version
if (packageToUninstall != null)
{
shouldAddForcedResultMessage = true;
remove_rollback_directory_if_exists(packageRemoteMetadata.Identity.Id);
backup_existing_version(config, packageToUninstall.PackageMetadata, _packageInfoService.get_package_information(packageToUninstall.PackageMetadata));
backup_and_before_modify(packageToUninstall, config, beforeModifyAction);
packageToUninstall.InstallLocation = pathResolver.GetInstallPath(packageToUninstall.Identity);
try
{
Expand Down Expand Up @@ -1371,21 +1370,11 @@ public virtual ConcurrentDictionary<string, PackageResult> upgrade_run(Chocolate

try
{
remove_rollback_directory_if_exists(packageName);

if (packageToUninstall != null)
{
var oldPkgInfo = _packageInfoService.get_package_information(packageToUninstall.PackageMetadata);

if (beforeUpgradeAction != null && packageToUninstall.PackageMetadata != null)
{
beforeUpgradeAction(packageToUninstall, config);
}

ensure_package_files_have_compatible_attributes(config, packageToUninstall.PackageMetadata, oldPkgInfo);
rename_legacy_package_version(config, packageToUninstall.PackageMetadata, oldPkgInfo);
backup_existing_version(config, packageToUninstall.PackageMetadata, oldPkgInfo);
remove_shim_directors(config, packageToUninstall.PackageMetadata, pkgInfo);
backup_and_before_modify(packageToUninstall, oldPkgInfo, config, beforeUpgradeAction);

packageToUninstall.InstallLocation = pathResolver.GetInstallPath(packageToUninstall.Identity);
try
Expand Down Expand Up @@ -1817,15 +1806,23 @@ private string get_install_directory(ChocolateyConfiguration config, IPackageMet
return installDirectory;
}

[Obsolete("This overload is obsolete and will be removed in a future version.")]
public virtual void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackageMetadata installedPackage, ChocolateyPackageInformation pkgInfo)
=> ensure_package_files_have_compatible_attributes(config, installedPackage);

protected virtual void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackageMetadata installedPackage)
{
var installDirectory = get_install_directory(config, installedPackage);
if (!_fileSystem.directory_exists(installDirectory)) return;

_filesService.ensure_compatible_file_attributes(installDirectory, config);
}

[Obsolete("This overload is obsolete and will be removed in a future version.")]
public virtual void rename_legacy_package_version(ChocolateyConfiguration config, IPackageMetadata installedPackage, ChocolateyPackageInformation pkgInfo)
=> normalize_package_legacy_folder_name(config, installedPackage, pkgInfo);

protected virtual void normalize_package_legacy_folder_name(ChocolateyConfiguration config, IPackageMetadata installedPackage, ChocolateyPackageInformation pkgInfo)
{
if (pkgInfo != null && pkgInfo.IsSideBySide) return;

Expand All @@ -1845,15 +1842,19 @@ public virtual void rename_legacy_package_version(ChocolateyConfiguration config

}

[Obsolete("This overload is obsolete and will be removed in a future version.")]
public virtual void backup_existing_version(ChocolateyConfiguration config, IPackageMetadata installedPackage, ChocolateyPackageInformation packageInfo)
=> backup_existing_version(config, packageInfo);

protected virtual void backup_existing_version(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
{
_fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageBackupLocation);

var pkgInstallPath = get_install_directory(config, installedPackage);
var pkgInstallPath = get_install_directory(config, packageInfo.Package);

if (_fileSystem.directory_exists(pkgInstallPath))
{
this.Log().Debug("Backing up existing {0} prior to operation.".format_with(installedPackage.Id));
this.Log().Debug("Backing up existing {0} prior to operation.".format_with(packageInfo.Package.Id));

var backupLocation = pkgInstallPath.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageBackupLocation);

Expand Down Expand Up @@ -1970,7 +1971,7 @@ public virtual void backup_changed_files(string packageInstallPath, ChocolateyCo
/// <param name="config">The configuration.</param>
/// <param name="installedPackage">The installed package.</param>
/// <param name="pkgInfo">The package information.</param>
private void remove_shim_directors(ChocolateyConfiguration config, IPackageMetadata installedPackage, ChocolateyPackageInformation pkgInfo)
private void remove_shim_directors(ChocolateyConfiguration config, IPackageMetadata installedPackage)
{
var pkgInstallPath = get_install_directory(config, installedPackage);

Expand Down Expand Up @@ -2211,7 +2212,6 @@ public virtual ConcurrentDictionary<string, PackageResult> uninstall_run(Chocola
var pathResolver = NugetCommon.GetPathResolver(config, _fileSystem);
var nugetProject = new FolderNuGetProject(ApplicationParameters.PackagesLocation, pathResolver, NuGetFramework.AnyFramework);


var pkgInfo = _packageInfoService.get_package_information(installedPackage.PackageMetadata);
if (pkgInfo != null && pkgInfo.IsPinned)
{
Expand Down Expand Up @@ -2241,21 +2241,10 @@ public virtual ConcurrentDictionary<string, PackageResult> uninstall_run(Chocola

foreach (var packageToUninstall in packagesToUninstall)
{
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)));
beforeUninstallAction(packageToUninstall, config);
}

var uninstallPkgInfo = _packageInfoService.get_package_information(packageToUninstall.PackageMetadata);

try
{
ensure_package_files_have_compatible_attributes(config, packageToUninstall.PackageMetadata, uninstallPkgInfo);
rename_legacy_package_version(config, packageToUninstall.PackageMetadata, uninstallPkgInfo);
remove_rollback_directory_if_exists(packageName);
backup_existing_version(config, packageToUninstall.PackageMetadata, uninstallPkgInfo);
var uninstallPkgInfo = _packageInfoService.get_package_information(packageToUninstall.PackageMetadata);
backup_and_before_modify(packageToUninstall, uninstallPkgInfo, config, beforeUninstallAction);

var packageResult = packageResultsToReturn.GetOrAdd(packageToUninstall.Name + "." + packageToUninstall.Version.to_string(), packageToUninstall);
packageResult.InstallLocation = packageToUninstall.InstallLocation;
Expand Down Expand Up @@ -2320,6 +2309,76 @@ public virtual ConcurrentDictionary<string, PackageResult> uninstall_run(Chocola
return packageResultsToReturn;
}

/// <summary>
/// This method should be called before any modifications are made to a package.
/// Typically this should be called before doing an uninstall of an existing package
/// or package dependency during an install, upgrade, or uninstall operation.
/// </summary>
/// <param name="packageResult">The package currently being modified.</param>
/// <param name="config">The current configuration.</param>
/// <param name="beforeModifyAction">Any action to run before performing backup operations. Typically this is an invocation of the chocolateyBeforeModify script.</param>
protected void backup_and_before_modify(
PackageResult packageResult,
ChocolateyConfiguration config,
Action<PackageResult, ChocolateyConfiguration> beforeModifyAction)
{
var packageInformation = _packageInfoService.get_package_information(packageResult.PackageMetadata);
backup_and_before_modify(packageResult, packageInformation, config, beforeModifyAction);
}

/// <summary>
/// This method should be called before any modifications are made to a package.
/// Typically this should be called before doing an uninstall of an existing package
/// or package dependency during an install, upgrade, or uninstall operation.
/// </summary>
/// <param name="packageResult">The package currently being modified.</param>
/// <param name="packageInformation">The package information for the package being modified.</param>
/// <param name="config">The current configuration.</param>
/// <param name="beforeModifyAction">Any action to run before performing backup operations. Typically this is an invocation of the chocolateyBeforeModify script.</param>
protected virtual void backup_and_before_modify(
PackageResult packageResult,
ChocolateyPackageInformation packageInformation,
ChocolateyConfiguration config,
Action<PackageResult, ChocolateyConfiguration> beforeModifyAction)
{
try
{
if (packageResult.InstallLocation != null)
{
// If this is an already installed package we're modifying, ensure we run its beforeModify script and back it up properly.
if (beforeModifyAction != null)
{
"chocolatey".Log().Debug("Running beforeModify step for '{0}'", packageResult.PackageMetadata.Id);
beforeModifyAction(packageResult, config);
}

"chocolatey".Log().Debug("Backing up package files for '{0}'", packageResult.PackageMetadata.Id);

backup_existing_package_files(config, packageResult.PackageMetadata, packageInformation);
}
}
catch (Exception error)
{
"chocolatey".Log().Error("Failed to run backup or beforeModify steps for package '{0}': {1}", packageResult.PackageMetadata.Id, error.Message);
"chocolatey".Log().Trace(error.StackTrace);
}
}

/// <summary>
/// Takes a backup of the existing package files.
/// </summary>
/// <param name="config">The current configuration settings</param>
/// <param name="package">The metadata for the package to backup</param>
/// <param name="packageInformation">The package information to backup</param>
protected void backup_existing_package_files(ChocolateyConfiguration config, IPackageMetadata package, ChocolateyPackageInformation packageInformation)
{
remove_rollback_directory_if_exists(package.Id);
ensure_package_files_have_compatible_attributes(config, package);
normalize_package_legacy_folder_name(config, package, packageInformation);
backup_existing_version(config, packageInformation);
remove_shim_directors(config, package);
}

/// <summary>
/// NuGet will happily report a package has been uninstalled, even if it doesn't always remove the nupkg.
/// Ensure that the package is deleted or throw an error.
Expand Down

0 comments on commit f4a943d

Please sign in to comment.