Skip to content

Commit

Permalink
(chocolatey#1092) Update NugetCommon.GetPackageManager()
Browse files Browse the repository at this point in the history
This update is necessary for easy access to the PackageInstalling
and PackageUninstalling event handlers which we need to utilise to
ensure we're properly handling backing up and beforeModify scripts of
package dependencies as well as the main packages we're modifying.
  • Loading branch information
vexx32 committed Feb 27, 2023
1 parent 2eacdd6 commit 2e7ceca
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/chocolatey/infrastructure.app/nuget/NugetCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con
// keep this here for the licensed edition for now
public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, Action<PackageOperationEventArgs> installSuccessAction, Action<PackageOperationEventArgs> uninstallSuccessAction, bool addUninstallHandler)
{
return GetPackageManager(configuration, nugetLogger, new PackageDownloader(), installSuccessAction, uninstallSuccessAction, addUninstallHandler);
return GetPackageManager(configuration, nugetLogger, new PackageDownloader(), installSuccessAction, uninstallSuccessAction, beforeModifyAction: null, addUninstallHandler);
}

public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader, Action<PackageOperationEventArgs> installSuccessAction, Action<PackageOperationEventArgs> uninstallSuccessAction, bool addUninstallHandler)
public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader, Action<PackageOperationEventArgs> installSuccessAction, Action<PackageOperationEventArgs> uninstallSuccessAction, Action<PackageOperationEventArgs> beforeModifyAction, bool addUninstallHandler)
{
IFileSystem nugetPackagesFileSystem = GetNuGetFileSystem(configuration, nugetLogger);
IPackagePathResolver pathResolver = GetPathResolver(configuration, nugetPackagesFileSystem);
Expand Down Expand Up @@ -205,6 +205,21 @@ public static IPackageManager GetPackageManager(ChocolateyConfiguration configur
if (installSuccessAction != null) installSuccessAction.Invoke(e);
};

if (beforeModifyAction != null)
{
// These event handlers MUST NOT call back into their corresponding packageManager methods -- InstallPackage() / UninstallPackage()
// Doing so will cause them to be liable to loop indefinitely, and we don't want that.
packageManager.PackageInstalling += (sender, eventArgs) =>
{
beforeModifyAction(eventArgs);
};

packageManager.PackageUninstalling += (sender, eventArgs) =>
{
beforeModifyAction(eventArgs);
};
}

if (addUninstallHandler)
{
// NOTE DO NOT EVER use this method, or endless loop - packageManager.PackageUninstalling += (s, e) =>
Expand Down

0 comments on commit 2e7ceca

Please sign in to comment.