using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using NugetForUnity.Models; using NugetForUnity.PluginAPI; using NugetForUnity.PluginSupport; using UnityEditor; using UnityEngine; namespace NugetForUnity { /// /// Uninstalls NuGet packages from the Unity project. Opposite of . /// public static class NugetPackageUninstaller { /// /// "Uninstalls" the given package by simply deleting its folder. /// /// 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) { // 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)) { Debug.LogWarning($"Uninstalling {package} makes no sense because it is a package that is 'pre-imported' by Unity."); } NugetLogger.LogVerbose("Uninstalling: {0} {1}", package.Id, package.Version); var foundPackage = package as INugetPackage ?? PackageCacheManager.GetPackageFromCacheOrSource(package); if (foundPackage is null) { return; } PluginRegistry.Instance.HandleUninstall(foundPackage, uninstallReason); InstalledPackagesManager.RemovePackage(foundPackage); PackageContentManager.DeletePackageContentPackage(foundPackage); // Since uninstall all will remove all packages we don't have to handle dependencies here. if (uninstallReason != PackageUninstallReason.UninstallAll) { // uninstall all non manually installed dependencies that are not a dependency of another installed package var frameworkDependencies = foundPackage.CurrentFrameworkDependencies; foreach (var dependency in frameworkDependencies) { if (InstalledPackagesManager.GetManuallyInstalledFlagFromConfiguration(dependency.Id)) { continue; } var hasMoreParents = InstalledPackagesManager.InstalledPackages.SelectMany( installedPackage => installedPackage.CurrentFrameworkDependencies) .Any(dep => dep.Id == dependency.Id); if (!hasMoreParents) { Uninstall(dependency, uninstallReason, false); } } } if (refreshAssets) { AssetDatabase.Refresh(); } } /// /// Uninstalls all given installed packages. /// /// The list of packages to uninstall. public static void UninstallAll([NotNull] [ItemNotNull] List packagesToUninstall) { foreach (var package in packagesToUninstall) { Uninstall(package, PackageUninstallReason.UninstallAll, false); } PluginRegistry.Instance.HandleUninstalledAll(); AssetDatabase.Refresh(); } /// /// Uninstalls all of the currently installed packages. /// public static void UninstallAll() { UninstallAll(InstalledPackagesManager.InstalledPackages.ToList()); } } }