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

New feature - selection #510

Merged
merged 13 commits into from
May 3, 2023
99 changes: 99 additions & 0 deletions src/NuGetForUnity/Editor/NugetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public static class NugetHelper
/// </summary>
private static readonly Dictionary<string, NugetPackage> installedPackages = new Dictionary<string, NugetPackage>();

/// <summary>
/// The dictionary of currently selected installed <see cref="NugetPackage" />s keyed off of their ID string.
/// </summary>
private static readonly Dictionary<string, NugetPackage> selectedInstalledPackages = new Dictionary<string, NugetPackage>();
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
JoC0de marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The dictionary of cached credentials retrieved by credential providers, keyed by feed URI.
/// </summary>
Expand Down Expand Up @@ -137,6 +142,14 @@ public static PackagesConfigFile PackagesConfigFile
/// <returns>A dictionary of installed <see cref="NugetPackage" />s.</returns>
public static IEnumerable<NugetPackage> InstalledPackages => installedPackages.Values;

/// <summary>
/// Gets the dictionary of packages that are actually installed in the project and selected, keyed off of the ID.
/// </summary>
/// <returns>A dictionary of installed and selected <see cref="NugetPackage" />s.</returns>
public static IEnumerable<NugetPackage> SelectedInstalledPackages => selectedInstalledPackages.Values;

public static int SelectedPackagesCount;
JoC0de marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Loads the NuGet.config file.
/// </summary>
Expand Down Expand Up @@ -589,6 +602,39 @@ public static void Push(NuspecFile nuspec, string nuspecFilePath, string apiKey
RunNugetProcess(arguments);
}

/// <summary>
/// Selects the given package to be uninstalled or updated.
/// </summary>
/// <param name="package">Package to be selected.</param>
public static void Select(NugetPackage package)
{
selectedInstalledPackages.Add(package.Id, package);
SelectedPackagesCount++;
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Unselects the given package to be uninstalled or updated.
/// </summary>
/// <param name="package">Package to be unselected.</param>
public static void Unselect(NugetPackage package)
{
selectedInstalledPackages.Remove(package.Id);
SelectedPackagesCount--;
}

/// <summary>
/// Unselects all selected packages.
/// </summary>
public static void UnselectAll()
{
foreach (var package in SelectedInstalledPackages)
{
package.IsSelected = false;
}
selectedInstalledPackages.Clear();
SelectedPackagesCount = 0;
}

/// <summary>
/// Recursively copies all files and sub-directories from one directory to another.
/// </summary>
Expand Down Expand Up @@ -709,6 +755,19 @@ internal static void UninstallAll()
{
Uninstall(package);
}
UnselectAll();
}

/// <summary>
/// Uninstalls all of the currently installed and selected packages.
/// </summary>
internal static void UninstallAllSelected()
{
foreach (var package in selectedInstalledPackages.Values.ToList())
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
{
Unselect(package);
Uninstall(package);
}
}

/// <summary>
Expand Down Expand Up @@ -786,6 +845,46 @@ public static void UpdateAll(IEnumerable<NugetPackage> updates, IEnumerable<Nuge

AssetDatabase.Refresh();

EditorUtility.ClearProgressBar();
UnselectAll();
}

/// <summary>
/// Installs all of the given selected updates, and uninstalls the corresponding package that is already installed.
/// </summary>
/// <param name="updates">The list of all updates to install.</param>
/// <param name="packagesToUpdate">The list of selected packages currently installed.</param>
public static void UpdateAllSelected(IEnumerable<NugetPackage> updates)
{
var progressStep = 1.0f / Math.Min(updates.Count(), selectedInstalledPackages.Count());
float currentProgress = 0;

foreach (var update in updates)
{
if (SelectedInstalledPackages.Contains(update))
{
EditorUtility.DisplayProgressBar(
string.Format("Updating to {0} {1}", update.Id, update.Version),
"Installing All Selected Updates",
currentProgress);

var installedPackage = SelectedInstalledPackages.FirstOrDefault(p => p.Id == update.Id);
if (installedPackage != null)
{
Unselect(installedPackage);
Update(installedPackage, update, false);
}
else
{
Debug.LogErrorFormat("Trying to update {0} to {1}, but no version is installed!", update.Id, update.Version);
}

currentProgress += progressStep;
}
}

AssetDatabase.Refresh();

EditorUtility.ClearProgressBar();
}

Expand Down
6 changes: 6 additions & 0 deletions src/NuGetForUnity/Editor/NugetPackage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

Expand Down Expand Up @@ -93,6 +94,11 @@ public class NugetPackage : NugetPackageIdentifier, IEquatable<NugetPackage>, IE
/// </summary>
public string Title;

/// <summary>
/// Gets or sets if the package is selected to be batch uninstalled or updated.
/// </summary>
public bool IsSelected;

/// <summary>
/// Gets the icon for the package as a task returning a <see cref="Texture2D" />.
/// </summary>
Expand Down
64 changes: 55 additions & 9 deletions src/NuGetForUnity/Editor/NugetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ protected void OnGUI()

private void OnTabChanged()
{
NugetHelper.UnselectAll();
openCloneWindows.Clear();
}

Expand Down Expand Up @@ -460,7 +461,7 @@ private void DrawUpdates()

if (filteredUpdatePackages != null && filteredUpdatePackages.Count > 0)
{
DrawPackages(filteredUpdatePackages);
DrawPackages(filteredUpdatePackages, true);
}
else
{
Expand Down Expand Up @@ -489,7 +490,7 @@ private void DrawInstalled()
var filteredInstalledPackages = FilteredInstalledPackages.ToList();
if (filteredInstalledPackages != null && filteredInstalledPackages.Count > 0)
{
DrawPackages(filteredInstalledPackages);
DrawPackages(filteredInstalledPackages, true);
}
else
{
Expand Down Expand Up @@ -551,15 +552,15 @@ private void DrawOnline()
EditorGUILayout.EndScrollView();
}

private void DrawPackages(List<NugetPackage> packages)
private void DrawPackages(List<NugetPackage> packages, bool canBeSelected = false)
{
var backgroundStyle = GetBackgroundStyle();
var contrastStyle = GetContrastStyle();

for (var i = 0; i < packages.Count; i++)
{
EditorGUILayout.BeginVertical(backgroundStyle);
DrawPackage(packages[i], backgroundStyle, contrastStyle);
DrawPackage(packages[i], backgroundStyle, contrastStyle, canBeSelected);
EditorGUILayout.EndVertical();

// swap styles
Expand Down Expand Up @@ -670,6 +671,27 @@ private void DrawInstalledHeader()
SettingsService.OpenUserPreferences("Preferences/NuGet For Unity");
GetWindow<NugetWindow>().Close();
}

GUILayout.FlexibleSpace();
if (NugetHelper.InstalledPackages.Count() > 0)
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
{
if (GUILayout.Button("Uninstall All", EditorStyles.miniButtonRight, GUILayout.Width(150)))
{
NugetHelper.UninstallAll();
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}

if (NugetHelper.SelectedPackagesCount > 0)
{
if (GUILayout.Button("Uninstall All Selected", EditorStyles.miniButtonRight, GUILayout.Width(150)))
{
NugetHelper.UninstallAllSelected();
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}
}
EditorGUILayout.EndHorizontal();

Expand Down Expand Up @@ -724,11 +746,24 @@ private void DrawUpdatesHeader()
UpdateUpdatePackages();
}

if (GUILayout.Button("Install All Updates", GUILayout.Width(150)))
if (updatePackages.Count > 0)
{
NugetHelper.UpdateAll(updatePackages, NugetHelper.InstalledPackages);
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
if (GUILayout.Button("Install All Updates", GUILayout.Width(150)))
{
NugetHelper.UpdateAll(updatePackages, NugetHelper.InstalledPackages);
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}

if (NugetHelper.SelectedPackagesCount > 0)
{
if (GUILayout.Button("Install All Selected Updates", GUILayout.Width(200)))
{
NugetHelper.UpdateAllSelected(updatePackages);
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}

if (GUILayout.Button("Refresh", GUILayout.Width(60)))
Expand Down Expand Up @@ -787,11 +822,22 @@ private void DrawUpdatesHeader()
/// Draws the given <see cref="NugetPackage" />.
/// </summary>
/// <param name="package">The <see cref="NugetPackage" /> to draw.</param>
private void DrawPackage(NugetPackage package, GUIStyle packageStyle, GUIStyle contrastStyle)
private void DrawPackage(NugetPackage package, GUIStyle packageStyle, GUIStyle contrastStyle, bool canBeSelected = false)
{
var installedPackages = NugetHelper.InstalledPackages;
var installed = installedPackages.FirstOrDefault(p => p.Id == package.Id);

if (canBeSelected)
{
var isSelectedTemp = EditorGUILayout.Toggle(package.IsSelected);
if (isSelectedTemp != package.IsSelected)
{
package.IsSelected = isSelectedTemp;
if (package.IsSelected) NugetHelper.Select(package);
else NugetHelper.Unselect(package);
}
}

EditorGUILayout.BeginHorizontal();
{
// The Unity GUI system (in the Editor) is terrible. This probably requires some explanation.
Expand Down