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

Implemented support for selecting multiple packages for installation #611

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
{
"name": "NuGetForUnity.Editor.Tests",
"rootNamespace": "NugetForUnity.Tests",
"references": [
"NuGetForUnity",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
"NuGetForUnity"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
}
10 changes: 9 additions & 1 deletion src/NuGetForUnity/Editor/NugetPackageUninstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageU
}

/// <summary>
/// Uninstalls all of the currently installed packages.
/// Uninstalls all given installed packages.
/// </summary>
/// <param name="packagesToUninstall">The list of packages to uninstall.</param>
public static void UninstallAll([NotNull] [ItemNotNull] List<INugetPackage> packagesToUninstall)
Expand All @@ -87,5 +87,13 @@ public static void UninstallAll([NotNull] [ItemNotNull] List<INugetPackage> pack

AssetDatabase.Refresh();
}

/// <summary>
/// Uninstalls all of the currently installed packages.
/// </summary>
public static void UninstallAll()
{
UninstallAll(InstalledPackagesManager.InstalledPackages.ToList());
}
}
}
126 changes: 119 additions & 7 deletions src/NuGetForUnity/Editor/Ui/NugetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class NugetWindow : EditorWindow, ISerializationCallbackReceiver
/// </summary>
private readonly HashSet<INugetPackage> selectedPackageDowngrades = new HashSet<INugetPackage>(new NugetPackageIdEqualityComparer());

/// <summary>
/// Used to keep track of which packages are selected for installing.
/// </summary>
private readonly HashSet<INugetPackage> selectedPackageInstalls = new HashSet<INugetPackage>(new NugetPackageIdEqualityComparer());

/// <summary>
/// Used to keep track of which packages are selected for uninstalling.
/// </summary>
Expand Down Expand Up @@ -125,6 +130,10 @@ public class NugetWindow : EditorWindow, ISerializationCallbackReceiver
[SerializeField]
private List<SerializableNugetPackage> serializableAvailablePackages;

[CanBeNull]
[SerializeField]
private List<SerializableNugetPackage> serializableToInstallPackages;

[CanBeNull]
[SerializeField]
private List<SerializableNugetPackage> serializableUpdatePackages;
Expand All @@ -141,11 +150,21 @@ public class NugetWindow : EditorWindow, ISerializationCallbackReceiver
/// </summary>
private bool showOnlinePrerelease;

/// <summary>
/// True if packages selected for install should be displayed on Online tab, false if availablePackages should be displayed.
/// </summary>
private bool showPackagesToInstall;

/// <summary>
/// True to show beta and alpha package versions. False to only show stable versions.
/// </summary>
private bool showPrereleaseUpdates;

/// <summary>
/// The current position of the scroll bar for packages selected for installation.
/// </summary>
private Vector2 toInstallScrollPosition;

/// <summary>
/// The list of package updates available, based on the already installed packages.
/// </summary>
Expand Down Expand Up @@ -235,19 +254,25 @@ private HashSet<INugetPackage> SelectedPackages
{
get
{
if (currentTab == NugetWindowTab.UpdatesTab)
switch (currentTab)
{
return showDowngrades ? selectedPackageDowngrades : selectedPackageUpdates;
case NugetWindowTab.UpdatesTab:
return showDowngrades ? selectedPackageDowngrades : selectedPackageUpdates;
case NugetWindowTab.InstalledTab:
return selectedPackageUninstalls;
case NugetWindowTab.OnlineTab:
return selectedPackageInstalls;
default:
throw new ArgumentOutOfRangeException();
}

return selectedPackageUninstalls;
}
}

/// <inheritdoc />
public void OnBeforeSerialize()
{
serializableAvailablePackages = availablePackages.ConvertAll(package => new SerializableNugetPackage(package));
serializableToInstallPackages = selectedPackageInstalls.Select(package => new SerializableNugetPackage(package)).ToList();
serializableUpdatePackages = updatePackages.ConvertAll(package => new SerializableNugetPackage(package));
}

Expand All @@ -260,6 +285,13 @@ public void OnAfterDeserialize()
serializableAvailablePackages = null;
}

if (serializableToInstallPackages != null)
{
selectedPackageInstalls.Clear();
selectedPackageInstalls.UnionWith(serializableToInstallPackages.Select(package => package.Interfaced));
serializableToInstallPackages = null;
}

if (serializableUpdatePackages != null)
{
updatePackages = serializableUpdatePackages.ConvertAll(package => package.Interfaced);
Expand Down Expand Up @@ -553,6 +585,12 @@ private void UpdateOnlinePackages()
searchTerm,
availablePackages.Count,
stopwatch.ElapsedMilliseconds);

// We need to make sure previously selected packages remain in the list
foreach (var selectedPackage in selectedPackageInstalls.Where(selectedPackage => !availablePackages.Contains(selectedPackage)))
{
availablePackages.Add(selectedPackage);
}
}

private void UpdateInstalledPackages()
Expand Down Expand Up @@ -668,18 +706,50 @@ private void DrawPackagesSplittedByManuallyInstalled(List<INugetPackage> package
private void DrawOnline()
{
DrawOnlineHeader();
var headerStyle = GetHeaderStyle();

if (selectedPackageInstalls.Count > 0)
{
DrawSelectedForInstallationHeader(headerStyle);
}

// display all of the packages
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
EditorGUILayout.BeginVertical();

DrawPackages(availablePackages);
IEnumerable<INugetPackage> packagesToShow;

if (selectedPackageInstalls.Count > 0 && showPackagesToInstall)
{
packagesToShow = availablePackages.Where(p => selectedPackageInstalls.Contains(p));
}
else
{
packagesToShow = availablePackages.Where(p => !selectedPackageInstalls.Contains(p));
}

DrawPackages(packagesToShow, true);

// If user deselected all the packages revert to showing available packages
if (selectedPackageInstalls.Count == 0)
{
showPackagesToInstall = false;
}

var showMoreStyle = GetHeaderStyle();
EditorGUILayout.BeginVertical(showMoreStyle);

if (showPackagesToInstall)
{
var arrow = !showPackagesToInstall ? "\u2227" : "\u2228";
if (GUILayout.Button($" {arrow} Online packages", headerStyle, GUILayout.Height(25f)))
{
showPackagesToInstall = !showPackagesToInstall;
}
}

// allow the user to display more results
if (GUILayout.Button("Show More", GUILayout.Width(120)))
if (!showPackagesToInstall && GUILayout.Button("Show More", GUILayout.Width(120)))
{
numberToSkip += numberToGet;
availablePackages.AddRange(
Expand All @@ -699,6 +769,48 @@ private void DrawOnline()
EditorGUILayout.EndScrollView();
}

private void DrawSelectedForInstallationHeader(GUIStyle headerStyle)
{
var rectangle = GUILayoutUtility.GetRect(GUIContent.none, headerStyle, GUILayout.Height(25f));

EditorGUI.LabelField(rectangle, string.Empty, headerStyle);

var arrow = showPackagesToInstall ? "\u2227" : "\u2228";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have used some constants for the string so we don't have "magic numbers"

rectangle.width -= 150f;
if (GUI.Button(rectangle, $" {arrow} Selected for installation: {selectedPackageInstalls.Count}", headerStyle))
{
showPackagesToInstall = !showPackagesToInstall;
}

rectangle.x += rectangle.width;
rectangle.width = 148f;
rectangle.y += 2f;
rectangle.height -= 6f;
if (GUI.Button(rectangle, "Install All Selected"))
{
foreach (var package in selectedPackageInstalls)
{
package.IsManuallyInstalled = true;
NugetPackageInstaller.InstallIdentifier(package, false);
}

selectedPackageInstalls.Clear();

AssetDatabase.Refresh();
UpdateInstalledPackages();
UpdateUpdatePackages();
}

if (!showPackagesToInstall)
{
arrow = !showPackagesToInstall ? "\u2227" : "\u2228";
if (GUILayout.Button($" {arrow} Online packages", headerStyle, GUILayout.Height(25f)))
{
showPackagesToInstall = !showPackagesToInstall;
}
}
}

private void DrawPackages(IEnumerable<INugetPackage> packages, bool canBeSelected = false)
{
var backgroundStyle = GetBackgroundStyle();
Expand Down Expand Up @@ -787,7 +899,7 @@ private void DrawInstalledHeader()
{
if (GUILayout.Button("Uninstall All", GUILayout.Width(100)))
{
NugetPackageUninstaller.UninstallAll(InstalledPackagesManager.InstalledPackages.ToList());
NugetPackageUninstaller.UninstallAll();
UpdateInstalledPackages();
UpdateUpdatePackages();
}
Expand Down
Loading