Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
**/.idea

src/NuGetForUnity/Editor/NuGetForUnity.csproj*
Builds/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Download the `*.unitypackage` file. Right-click on it in File Explorer and choos

To launch, select **NuGet → Manage NuGet Packages**

The menu root is selected at compile time. Without a scripting define symbol it appears under **NuGet**. Define `NUGETFORUNITY_MENU_TOOLS` to use **Tools → NuGet**, or `NUGETFORUNITY_MENU_WINDOW_PACKAGE_MANAGER` to use **Window → Package Management → NuGet**. Define only one menu location symbol.

<img alt="Menu Items" src="docs/screenshots/menu_item.png" height="170px" />

After several seconds (it can take some time to query the server for packages), you should see a window like this:
Expand Down
40 changes: 40 additions & 0 deletions src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,46 @@ public void PackageSourceCredentialsTest(string name)
Assert.That(parsedSource.SavedPassword, Is.EqualTo(password));
}

[Test]
public void MenuRootMatchesScriptDefineSymbolsTest()
{
#if NUGETFORUNITY_MENU_TOOLS
Assert.That(NugetMenu.MenuRoot, Is.EqualTo("Tools/NuGet"));
#elif NUGETFORUNITY_MENU_WINDOW_PACKAGE_MANAGER
Assert.That(NugetMenu.MenuRoot, Is.EqualTo("Window/Package Management/NuGet"));
#else
Assert.That(NugetMenu.MenuRoot, Is.EqualTo(NugetConfigFile.DefaultMenuRoot));
#endif
}

[Test]
public void MenuRootConfigIsNotSavedTest()
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}_{NugetConfigFile.FileName}");

try
{
NugetConfigFile.CreateDefaultFile(path);
File.WriteAllText(
path,
File.ReadAllText(path).Replace(
" </config>",
" <add key=\"MenuRoot\" value=\"Tools/NuGet\" />\n </config>"));

var file = NugetConfigFile.Load(path);
file.Save(path);

Assert.That(File.ReadAllText(path), Does.Not.Contain("MenuRoot"));
}
finally
{
if (File.Exists(path))
{
File.Delete(path);
}
}
}

[Test]
[TestCase("2018.4.30f1", false, false, false)]
[TestCase("2018.4.30f1", true, false, false)]
Expand Down
5 changes: 5 additions & 0 deletions src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class NugetConfigFile
/// </summary>
public const string FileName = "NuGet.config";

/// <summary>
/// The default root menu path where NuGet menu items are created.
/// </summary>
public const string DefaultMenuRoot = "NuGet";

/// <summary>
/// The name of the attribute that is used to configure <see cref="NugetPackageSourceV3.PackageDownloadUrlTemplateOverwrite" /> of
/// <see cref="NugetPackageSourceV3" />.
Expand Down
3 changes: 1 addition & 2 deletions src/NuGetForUnity/Editor/Ui/DependencyTreeViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public class DependencyTreeViewer : EditorWindow
/// <summary>
/// Opens the NuGet Package Manager Window.
/// </summary>
[MenuItem("NuGet/Show Dependency Tree", false, 5)]
protected static void DisplayDependencyTree()
internal static void DisplayDependencyTree()
{
GetWindow<DependencyTreeViewer>();
}
Expand Down
2 changes: 0 additions & 2 deletions src/NuGetForUnity/Editor/Ui/NuGetForUnityUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ internal static class NuGetForUnityUpdater
/// <summary>
/// Opens release notes for the current version.
/// </summary>
[MenuItem("NuGet/Version " + NugetPreferences.NuGetForUnityVersion + " \uD83D\uDD17", false, 10)]
public static void DisplayVersion()
{
Application.OpenURL($"{GitHubReleasesPageUrl}/tag/v{NugetPreferences.NuGetForUnityVersion}");
Expand All @@ -39,7 +38,6 @@ public static void DisplayVersion()
/// <summary>
/// Checks/launches the Releases page to update NuGetForUnity with a new version.
/// </summary>
[MenuItem("NuGet/Check for Updates...", false, 11)]
public static void CheckForUpdates()
{
var request = UnityWebRequest.Get(GitHubReleasesApiUrl);
Expand Down
71 changes: 71 additions & 0 deletions src/NuGetForUnity/Editor/Ui/NugetMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using NugetForUnity.Configuration;
using UnityEditor;

namespace NugetForUnity.Ui
{
#if NUGETFORUNITY_MENU_TOOLS && NUGETFORUNITY_MENU_WINDOW_PACKAGE_MANAGER
#error Define only one NuGetForUnity menu location symbol.
#endif

/// <summary>
/// Registers NuGet menu items at the compile-time selected root path.
/// </summary>
internal static class NugetMenu
{
#if NUGETFORUNITY_MENU_TOOLS
internal const string MenuRoot = "Tools/NuGet";
#elif NUGETFORUNITY_MENU_WINDOW_PACKAGE_MANAGER
internal const string MenuRoot = "Window/Package Management/NuGet";
#else
internal const string MenuRoot = NugetConfigFile.DefaultMenuRoot;
#endif

internal const string ManagePackagesMenuItem = MenuRoot + "/Manage NuGet Packages";

internal const string RestorePackagesMenuItem = MenuRoot + "/Restore Packages";

internal const string DependencyTreeMenuItem = MenuRoot + "/Show Dependency Tree";

internal const string PreferencesMenuItem = MenuRoot + "/Preferences";

internal const string VersionMenuItem = MenuRoot + "/Version " + NugetPreferences.NuGetForUnityVersion + " \uD83D\uDD17";

internal const string CheckForUpdatesMenuItem = MenuRoot + "/Check for Updates...";

[MenuItem(ManagePackagesMenuItem, false, 0)]
private static void DisplayNugetWindow()
{
NugetWindow.DisplayNugetWindow();
}

[MenuItem(RestorePackagesMenuItem, false, 1)]
private static void RestorePackages()
{
NugetWindow.RestorePackages();
}

[MenuItem(DependencyTreeMenuItem, false, 5)]
private static void DisplayDependencyTree()
{
DependencyTreeViewer.DisplayDependencyTree();
}

[MenuItem(PreferencesMenuItem, false, 9)]
private static void DisplayPreferences()
{
NugetWindow.DisplayPreferences();
}

[MenuItem(VersionMenuItem, false, 10)]
private static void DisplayVersion()
{
NuGetForUnityUpdater.DisplayVersion();
}

[MenuItem(CheckForUpdatesMenuItem, false, 11)]
private static void CheckForUpdates()
{
NuGetForUnityUpdater.CheckForUpdates();
}
}
}
2 changes: 2 additions & 0 deletions src/NuGetForUnity/Editor/Ui/NugetMenu.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 87 additions & 1 deletion src/NuGetForUnity/Editor/Ui/NugetPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ public class NugetPreferences : SettingsProvider

private const float LabelPading = 5;

private static readonly string[] SearchKeywords = { "NuGet", "Packages" };
private const string ToolsMenuDefineSymbol = "NUGETFORUNITY_MENU_TOOLS";

private const string WindowPackageManagementMenuDefineSymbol = "NUGETFORUNITY_MENU_WINDOW_PACKAGE_MANAGER";

private static readonly string[] SearchKeywords = { "NuGet", "Packages", "Menu" };

private static readonly string[] MenuLocationLabels =
{
"NuGet",
"Tools - NuGet",
"Window Package Management - NuGet",
};

private readonly GUIContent deleteX = new GUIContent("\u2716");

Expand Down Expand Up @@ -127,6 +138,17 @@ public override void OnGUI([CanBeNull] string searchContext)
var biggestLabelSize = EditorStyles.label.CalcSize(new GUIContent("Prefer .NET Standard dependencies over .NET Framework")).x;
EditorGUIUtility.labelWidth = biggestLabelSize;
EditorGUILayout.LabelField($"Version: {NuGetForUnityVersion}");
var menuLocation = GetMenuLocationFromScriptingDefineSymbols();
var selectedMenuLocation = (NugetMenuLocation)EditorGUILayout.Popup(
new GUIContent(
"Menu Location",
"Changes the scripting define symbol used to compile the NuGet menu path."),
(int)menuLocation,
MenuLocationLabels);
if (selectedMenuLocation != menuLocation)
{
SetMenuLocationScriptingDefineSymbols(selectedMenuLocation);
}

var installFromCache = EditorGUILayout.Toggle("Install From the Cache", ConfigurationManager.NugetConfigFile.InstallFromCache);
if (installFromCache != ConfigurationManager.NugetConfigFile.InstallFromCache)
Expand Down Expand Up @@ -630,6 +652,70 @@ public override void OnGUI([CanBeNull] string searchContext)
}
}

private static NugetMenuLocation GetMenuLocationFromScriptingDefineSymbols()
{
var defineSymbols = GetCurrentBuildTargetDefineSymbols();
var symbols = defineSymbols.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(symbol => symbol.Trim())
.ToList();

if (symbols.Contains(WindowPackageManagementMenuDefineSymbol))
{
return NugetMenuLocation.WindowPackageManagementNuGet;
}

return symbols.Contains(ToolsMenuDefineSymbol) ? NugetMenuLocation.ToolsNuGet : NugetMenuLocation.NuGet;
}

private static void SetMenuLocationScriptingDefineSymbols(NugetMenuLocation menuLocation)
{
var defineSymbols = GetCurrentBuildTargetDefineSymbols();
var symbols = defineSymbols.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(symbol => symbol.Trim())
.Where(symbol => !string.IsNullOrEmpty(symbol))
.ToList();

symbols.RemoveAll(symbol => symbol == ToolsMenuDefineSymbol || symbol == WindowPackageManagementMenuDefineSymbol);

if (menuLocation == NugetMenuLocation.ToolsNuGet)
{
symbols.Add(ToolsMenuDefineSymbol);
}
else if (menuLocation == NugetMenuLocation.WindowPackageManagementNuGet)
{
symbols.Add(WindowPackageManagementMenuDefineSymbol);
}

SetCurrentBuildTargetDefineSymbols(string.Join(";", symbols.ToArray()));
}

private static string GetCurrentBuildTargetDefineSymbols()
{
#if UNITY_2021_2_OR_NEWER
return PlayerSettings.GetScriptingDefineSymbols(UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup));
#else
return PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
#endif
}

private static void SetCurrentBuildTargetDefineSymbols(string defineSymbols)
{
#if UNITY_2021_2_OR_NEWER
PlayerSettings.SetScriptingDefineSymbols(UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup), defineSymbols);
#else
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defineSymbols);
#endif
}

private enum NugetMenuLocation
{
NuGet,

ToolsNuGet,

WindowPackageManagementNuGet,
}

private sealed class NugetPlugin
{
public NugetPlugin([NotNull] Assembly assembly, bool enabled)
Expand Down
9 changes: 3 additions & 6 deletions src/NuGetForUnity/Editor/Ui/NugetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,15 @@ public void OnAfterDeserialize()
/// <summary>
/// Opens the NuGet Package Manager Window.
/// </summary>
[MenuItem("NuGet/Manage NuGet Packages", false, 0)]
protected static void DisplayNugetWindow()
internal static void DisplayNugetWindow()
{
GetWindow<NugetWindow>();
}

/// <summary>
/// Restores all packages defined in packages.config.
/// </summary>
[MenuItem("NuGet/Restore Packages", false, 1)]
protected static void RestorePackages()
internal static void RestorePackages()
{
PackageRestorer.Restore(false);
foreach (var nugetWindow in Resources.FindObjectsOfTypeAll<NugetWindow>())
Expand All @@ -312,8 +310,7 @@ protected static void RestorePackages()
/// <summary>
/// Opens the preferences window.
/// </summary>
[MenuItem("NuGet/Preferences", false, 9)]
protected static void DisplayPreferences()
internal static void DisplayPreferences()
{
SettingsService.OpenProjectSettings(NugetPreferences.MenuItemLocation);
}
Expand Down
Binary file added src/NuGetForUnity/NuGetForUnityIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading