diff --git a/.gitignore b/.gitignore index 7b9d911a..ebcf6ee1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ **/.idea src/NuGetForUnity/Editor/NuGetForUnity.csproj* +Builds/ diff --git a/README.md b/README.md index 0e7813e9..e302ea23 100644 --- a/README.md +++ b/README.md @@ -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. + Menu Items After several seconds (it can take some time to query the server for packages), you should see a window like this: diff --git a/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs b/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs index 33680a7e..87f7a341 100644 --- a/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs +++ b/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs @@ -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( + " ", + " \n ")); + + 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)] diff --git a/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs b/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs index 7baa678f..072043d3 100644 --- a/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs +++ b/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs @@ -37,6 +37,11 @@ public class NugetConfigFile /// public const string FileName = "NuGet.config"; + /// + /// The default root menu path where NuGet menu items are created. + /// + public const string DefaultMenuRoot = "NuGet"; + /// /// The name of the attribute that is used to configure of /// . diff --git a/src/NuGetForUnity/Editor/Ui/DependencyTreeViewer.cs b/src/NuGetForUnity/Editor/Ui/DependencyTreeViewer.cs index 2423c037..1e22650d 100644 --- a/src/NuGetForUnity/Editor/Ui/DependencyTreeViewer.cs +++ b/src/NuGetForUnity/Editor/Ui/DependencyTreeViewer.cs @@ -49,8 +49,7 @@ public class DependencyTreeViewer : EditorWindow /// /// Opens the NuGet Package Manager Window. /// - [MenuItem("NuGet/Show Dependency Tree", false, 5)] - protected static void DisplayDependencyTree() + internal static void DisplayDependencyTree() { GetWindow(); } diff --git a/src/NuGetForUnity/Editor/Ui/NuGetForUnityUpdater.cs b/src/NuGetForUnity/Editor/Ui/NuGetForUnityUpdater.cs index d3ae7273..081a89dd 100644 --- a/src/NuGetForUnity/Editor/Ui/NuGetForUnityUpdater.cs +++ b/src/NuGetForUnity/Editor/Ui/NuGetForUnityUpdater.cs @@ -30,7 +30,6 @@ internal static class NuGetForUnityUpdater /// /// Opens release notes for the current version. /// - [MenuItem("NuGet/Version " + NugetPreferences.NuGetForUnityVersion + " \uD83D\uDD17", false, 10)] public static void DisplayVersion() { Application.OpenURL($"{GitHubReleasesPageUrl}/tag/v{NugetPreferences.NuGetForUnityVersion}"); @@ -39,7 +38,6 @@ public static void DisplayVersion() /// /// Checks/launches the Releases page to update NuGetForUnity with a new version. /// - [MenuItem("NuGet/Check for Updates...", false, 11)] public static void CheckForUpdates() { var request = UnityWebRequest.Get(GitHubReleasesApiUrl); diff --git a/src/NuGetForUnity/Editor/Ui/NugetMenu.cs b/src/NuGetForUnity/Editor/Ui/NugetMenu.cs new file mode 100644 index 00000000..ee10b363 --- /dev/null +++ b/src/NuGetForUnity/Editor/Ui/NugetMenu.cs @@ -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 + + /// + /// Registers NuGet menu items at the compile-time selected root path. + /// + 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(); + } + } +} diff --git a/src/NuGetForUnity/Editor/Ui/NugetMenu.cs.meta b/src/NuGetForUnity/Editor/Ui/NugetMenu.cs.meta new file mode 100644 index 00000000..c3f5216a --- /dev/null +++ b/src/NuGetForUnity/Editor/Ui/NugetMenu.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 303d73d13c81749ff81dea424bf5bd9e \ No newline at end of file diff --git a/src/NuGetForUnity/Editor/Ui/NugetPreferences.cs b/src/NuGetForUnity/Editor/Ui/NugetPreferences.cs index 6f971386..188e416c 100644 --- a/src/NuGetForUnity/Editor/Ui/NugetPreferences.cs +++ b/src/NuGetForUnity/Editor/Ui/NugetPreferences.cs @@ -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"); @@ -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) @@ -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) diff --git a/src/NuGetForUnity/Editor/Ui/NugetWindow.cs b/src/NuGetForUnity/Editor/Ui/NugetWindow.cs index d118c320..39bea7dd 100644 --- a/src/NuGetForUnity/Editor/Ui/NugetWindow.cs +++ b/src/NuGetForUnity/Editor/Ui/NugetWindow.cs @@ -290,8 +290,7 @@ public void OnAfterDeserialize() /// /// Opens the NuGet Package Manager Window. /// - [MenuItem("NuGet/Manage NuGet Packages", false, 0)] - protected static void DisplayNugetWindow() + internal static void DisplayNugetWindow() { GetWindow(); } @@ -299,8 +298,7 @@ protected static void DisplayNugetWindow() /// /// Restores all packages defined in packages.config. /// - [MenuItem("NuGet/Restore Packages", false, 1)] - protected static void RestorePackages() + internal static void RestorePackages() { PackageRestorer.Restore(false); foreach (var nugetWindow in Resources.FindObjectsOfTypeAll()) @@ -312,8 +310,7 @@ protected static void RestorePackages() /// /// Opens the preferences window. /// - [MenuItem("NuGet/Preferences", false, 9)] - protected static void DisplayPreferences() + internal static void DisplayPreferences() { SettingsService.OpenProjectSettings(NugetPreferences.MenuItemLocation); } diff --git a/src/NuGetForUnity/NuGetForUnityIcon.png b/src/NuGetForUnity/NuGetForUnityIcon.png new file mode 100644 index 00000000..eb637c34 Binary files /dev/null and b/src/NuGetForUnity/NuGetForUnityIcon.png differ diff --git a/src/NuGetForUnity/NuGetForUnityIcon.png.meta b/src/NuGetForUnity/NuGetForUnityIcon.png.meta new file mode 100644 index 00000000..0874fc0d --- /dev/null +++ b/src/NuGetForUnity/NuGetForUnityIcon.png.meta @@ -0,0 +1,169 @@ +fileFormatVersion: 2 +guid: 7acff9de9ad274c3d900d7b06367bbbe +TextureImporter: + internalIDToNameTable: + - first: + 213: 5957285066381558381 + second: NuGetForUnityIcon_0 + - first: + 213: -1823813202051018648 + second: NuGetForUnityIcon_1 + externalObjects: {} + serializedVersion: 13 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + flipGreenChannel: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + ignoreMipmapLimit: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 1 + aniso: 1 + mipBias: 0 + wrapU: 1 + wrapV: 1 + wrapW: 1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 2 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + swizzle: 50462976 + cookieLightType: 0 + platformSettings: + - serializedVersion: 4 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + ignorePlatformSupport: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 4 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + ignorePlatformSupport: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: + - serializedVersion: 2 + name: NuGetForUnityIcon_0 + rect: + serializedVersion: 2 + x: 43 + y: 459 + width: 87 + height: 87 + alignment: 0 + pivot: {x: 0, y: 0} + border: {x: 0, y: 0, z: 0, w: 0} + customData: + outline: [] + physicsShape: [] + tessellationDetail: -1 + bones: [] + spriteID: d62896f64378ca250800000000000000 + internalID: 5957285066381558381 + vertices: [] + indices: + edges: [] + weights: [] + - serializedVersion: 2 + name: NuGetForUnityIcon_1 + rect: + serializedVersion: 2 + x: 149 + y: 54 + width: 405 + height: 401 + alignment: 0 + pivot: {x: 0, y: 0} + border: {x: 0, y: 0, z: 0, w: 0} + customData: + outline: [] + physicsShape: [] + tessellationDetail: -1 + bones: [] + spriteID: 860d4134e8380b6e0800000000000000 + internalID: -1823813202051018648 + vertices: [] + indices: + edges: [] + weights: [] + outline: [] + customData: + physicsShape: [] + bones: [] + spriteID: + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spriteCustomMetadata: + entries: [] + nameFileIdTable: + NuGetForUnityIcon_0: 5957285066381558381 + NuGetForUnityIcon_1: -1823813202051018648 + mipmapLimitGroupName: + pSDRemoveMatte: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/NuGetForUnity/package.json b/src/NuGetForUnity/package.json index 9e2d7866..b2517cc7 100644 --- a/src/NuGetForUnity/package.json +++ b/src/NuGetForUnity/package.json @@ -8,8 +8,12 @@ "nuget", "unity" ], + "author": { + "name": "GlitchEnzo", + "url": "https://glitchenzo.github.io/" + }, "license": "MIT", "licensesUrl": "https://github.com/GlitchEnzo/NuGetForUnity/blob/master/LICENSE", "changelogUrl": "https://github.com/GlitchEnzo/NuGetForUnity/releases", "documentationUrl": "https://github.com/GlitchEnzo/NuGetForUnity" -} +} \ No newline at end of file