From f7161b962e856bea961e62e461489fe8e7f89dad Mon Sep 17 00:00:00 2001 From: Igor Stojkovic Date: Wed, 29 Jan 2025 13:47:58 +0100 Subject: [PATCH 1/2] Fixed restore from CLI when packages need to be updated --- src/NuGetForUnity/Editor/NugetPackageInstaller.cs | 4 ++-- src/NuGetForUnity/Editor/NugetPackageUninstaller.cs | 6 +++--- src/NuGetForUnity/Editor/NugetPackageUpdater.cs | 7 ++++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs index 25b07576..88267384 100644 --- a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs @@ -118,7 +118,7 @@ private static PackageInstallOperationResult Install( installedPackage.Version, package.Version, package.Version); - return NugetPackageUpdater.UpdateWithInformation(installedPackage, package, refreshAssets); + return NugetPackageUpdater.UpdateWithInformation(installedPackage, package, refreshAssets, isSlimRestoreInstall); } if (comparisonResult > 0) @@ -131,7 +131,7 @@ private static PackageInstallOperationResult Install( installedPackage.Id, installedPackage.Version, package.Version); - return NugetPackageUpdater.UpdateWithInformation(installedPackage, package, refreshAssets); + return NugetPackageUpdater.UpdateWithInformation(installedPackage, package, refreshAssets, isSlimRestoreInstall); } NugetLogger.LogVerbose( diff --git a/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs b/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs index 826bffb7..b4b5d721 100644 --- a/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs @@ -20,11 +20,11 @@ public static class NugetPackageUninstaller /// The NugetPackage to uninstall. /// The reason uninstall is being called. /// True to force Unity to refresh its Assets folder. False to temporarily ignore the change. Defaults to true. - public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageUninstallReason uninstallReason, bool refreshAssets = true) + public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageUninstallReason uninstallReason, bool refreshAssets = true, bool isSlimRestore = false) { // Checking for pre-imported packages also ensures that the pre-imported package list is up-to-date before we uninstall packages. // Without this the pre-imported package list can contain the package as we delete the .dll before we call 'AssetDatabase.Refresh()'. - if (UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package.Id, false)) + if (!isSlimRestore && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package.Id, false)) { Debug.LogWarning($"Uninstalling {package} makes no sense because it is a package that is 'pre-imported' by Unity."); } @@ -44,7 +44,7 @@ public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageU PackageContentManager.DeletePackageContentPackage(foundPackage); // Since uninstall all will remove all packages we don't have to handle dependencies here. - if (uninstallReason != PackageUninstallReason.UninstallAll) + if (uninstallReason != PackageUninstallReason.UninstallAll && !isSlimRestore) { // uninstall all non manually installed dependencies that are not a dependency of another installed package var frameworkDependencies = foundPackage.CurrentFrameworkDependencies; diff --git a/src/NuGetForUnity/Editor/NugetPackageUpdater.cs b/src/NuGetForUnity/Editor/NugetPackageUpdater.cs index f6ac22fa..03c91059 100644 --- a/src/NuGetForUnity/Editor/NugetPackageUpdater.cs +++ b/src/NuGetForUnity/Editor/NugetPackageUpdater.cs @@ -27,7 +27,7 @@ public static bool Update( bool refreshAssets = true, PackageUninstallReason uninstallReason = PackageUninstallReason.IndividualUpdate) { - var result = UpdateWithInformation(currentVersion, newVersion, refreshAssets, uninstallReason); + var result = UpdateWithInformation(currentVersion, newVersion, refreshAssets, false, uninstallReason); return result.Successful; } @@ -79,12 +79,13 @@ internal static PackageInstallOperationResult UpdateWithInformation( [NotNull] INugetPackageIdentifier currentVersion, [NotNull] INugetPackage newVersion, bool refreshAssets = true, + bool isSlimRestoreInstall = false, PackageUninstallReason uninstallReason = PackageUninstallReason.IndividualUpdate) { NugetLogger.LogVerbose("Updating {0} {1} to {2}", currentVersion.Id, currentVersion.Version, newVersion.Version); - NugetPackageUninstaller.Uninstall(currentVersion, uninstallReason, false); + NugetPackageUninstaller.Uninstall(currentVersion, uninstallReason, false, isSlimRestoreInstall); newVersion.IsManuallyInstalled = newVersion.IsManuallyInstalled || currentVersion.IsManuallyInstalled; - return NugetPackageInstaller.Install(newVersion, refreshAssets); + return NugetPackageInstaller.Install(newVersion, refreshAssets, isSlimRestoreInstall); } } } From 28cb364196a7a903bdf540b82520bbffc3f6f7e5 Mon Sep 17 00:00:00 2001 From: Igor Stojkovic Date: Sat, 1 Feb 2025 11:37:11 +0100 Subject: [PATCH 2/2] Added documentation for new isSlimRestore param --- src/NuGetForUnity/Editor/NugetPackageUninstaller.cs | 1 + src/NuGetForUnity/Editor/NugetPackageUpdater.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs b/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs index b4b5d721..a242b150 100644 --- a/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs @@ -20,6 +20,7 @@ public static class NugetPackageUninstaller /// The NugetPackage to uninstall. /// The reason uninstall is being called. /// True to force Unity to refresh its Assets folder. False to temporarily ignore the change. Defaults to true. + /// True if uninstall needs to be done as part of slim restore operation. public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageUninstallReason uninstallReason, bool refreshAssets = true, bool isSlimRestore = false) { // Checking for pre-imported packages also ensures that the pre-imported package list is up-to-date before we uninstall packages. diff --git a/src/NuGetForUnity/Editor/NugetPackageUpdater.cs b/src/NuGetForUnity/Editor/NugetPackageUpdater.cs index 03c91059..eeb3ae0e 100644 --- a/src/NuGetForUnity/Editor/NugetPackageUpdater.cs +++ b/src/NuGetForUnity/Editor/NugetPackageUpdater.cs @@ -73,6 +73,7 @@ public static void UpdateAll( /// The current package to uninstall. /// The package to install. /// True to refresh the assets inside Unity. False to ignore them (for now). Defaults to true. + /// True if update needs to be done as part of slim restore operation. /// The reason uninstall is being called. /// The information about how the package has been installed. internal static PackageInstallOperationResult UpdateWithInformation(