Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-1689) Remove packaging script before upgrade #1751

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ public virtual ConcurrentDictionary<string, PackageResult> get_outdated(Chocolat
var pkgInfo = _packageInfoService.get_package_information(installedPackage);
bool isPinned = pkgInfo.IsPinned;

// if the package is pinned and we are skipping pinned,
// if the package is pinned and we are skipping pinned,
// move on quickly
if (isPinned && config.OutdatedCommand.IgnorePinned)
{
Expand All @@ -918,7 +918,7 @@ public virtual ConcurrentDictionary<string, PackageResult> get_outdated(Chocolat
}

var latestPackage = find_package(packageName, null, config, repository);

if (latestPackage == null)
{
if (config.Features.IgnoreUnfoundPackagesOnUpgradeOutdated) continue;
Expand All @@ -933,7 +933,7 @@ public virtual ConcurrentDictionary<string, PackageResult> get_outdated(Chocolat
}

if (latestPackage.Version <= installedPackage.Version) continue;

var packageResult = outdatedPackages.GetOrAdd(packageName, new PackageResult(latestPackage, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, latestPackage.Id)));

string logMessage = "You have {0} v{1} installed. Version {2} is available based on your source(s).{3} Source(s): \"{4}\"".format_with(installedPackage.Id, installedPackage.Version, latestPackage.Version, Environment.NewLine, config.Sources);
Expand Down Expand Up @@ -1146,6 +1146,8 @@ public virtual void backup_existing_version(ChocolateyConfiguration config, IPac
try
{
_fileSystem.copy_directory(backupLocation, pkgInstallPath, overwriteExisting: true);

remove_packaging_files_prior_to_upgrade(pkgInstallPath, config.CommandName);
}
catch (Exception ex)
{
Expand All @@ -1167,6 +1169,29 @@ process locking the folder or files. Please make sure nothing is
}
}

public virtual void remove_packaging_files_prior_to_upgrade(string directoryPath, string commandName)
{
if (commandName.to_lower() == "upgrade")
{
// Due to the way that Package Reducer works, there is a potential that a Chocolatey Packaging
// script could be incorrectly left in place during an upgrade operation. To guard against this,
// remove any Chocolatey Packaging scripts, which will then be restored by the new package, if
// they are still required
var filesToDelete = new List<string> {"chocolateyinstall", "chocolateyuninstall", "chocolateybeforemodify"};
var packagingScripts = _fileSystem.get_files(directoryPath, "*.ps1", SearchOption.AllDirectories)
.Where(p => filesToDelete.Contains(_fileSystem.get_file_name_without_extension(p).to_lower()));

foreach (var packagingScript in packagingScripts)
{
if (_fileSystem.file_exists(packagingScript))
{
this.Log().Debug("Deleting file {0}".format_with(packagingScript));
_fileSystem.delete_file(packagingScript);
}
}
}
}

public virtual void backup_changed_files(string packageInstallPath, ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
{
if (packageInfo == null || packageInfo.Package == null) return;
Expand Down