using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using NugetForUnity.Models; using NugetForUnity.PluginAPI; using UnityEditor; using UnityEngine; namespace NugetForUnity { /// /// Handles the updating of NuGet packages. /// public static class NugetPackageUpdater { /// /// Updates a package by uninstalling the currently installed version and installing the "new" version. /// /// 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. /// The reason uninstall is being called. /// True if the package was successfully updated. False otherwise. public static bool Update( [NotNull] INugetPackageIdentifier currentVersion, [NotNull] INugetPackage newVersion, bool refreshAssets = true, PackageUninstallReason uninstallReason = PackageUninstallReason.IndividualUpdate) { NugetLogger.LogVerbose("Updating {0} {1} to {2}", currentVersion.Id, currentVersion.Version, newVersion.Version); NugetPackageUninstaller.Uninstall(currentVersion, uninstallReason, false); newVersion.IsManuallyInstalled = newVersion.IsManuallyInstalled || currentVersion.IsManuallyInstalled; return NugetPackageInstaller.InstallIdentifier(newVersion, refreshAssets); } /// /// Installs all of the given updates, and Uninstalls the corresponding package that is already installed. /// /// The list of all updates to install. /// The list of all packages currently installed. public static void UpdateAll( [NotNull] [ItemNotNull] IEnumerable updates, [NotNull] [ItemNotNull] IEnumerable packagesToUpdate) { var updatesCollection = updates as IReadOnlyCollection ?? updates.ToList(); var packagesToUpdateCollection = packagesToUpdate as IReadOnlyCollection ?? packagesToUpdate.ToList(); var progressStep = 1.0f / updatesCollection.Count; float currentProgress = 0; foreach (var update in updatesCollection) { EditorUtility.DisplayProgressBar($"Updating to {update.Id} {update.Version}", "Installing All Updates", currentProgress); var installedPackage = packagesToUpdateCollection.FirstOrDefault(p => p.Id == update.Id); if (installedPackage != null) { Update(installedPackage, update, false, PackageUninstallReason.UpdateAll); } else { Debug.LogErrorFormat("Trying to update {0} to {1}, but no version is installed!", update.Id, update.Version); } currentProgress += progressStep; } AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); } } }