From 21e7bf54936ea422170b39b69cffcf8c27bb8dfc Mon Sep 17 00:00:00 2001 From: Igor Stojkovic Date: Thu, 14 Dec 2023 10:17:30 +0100 Subject: [PATCH] Fixed path handling for plugins --- .github/workflows/main.yml | 2 +- src/NuGetForUnity.Cli/Fakes/AssemblyLoader.cs | 6 ++-- .../NuGetForUnity.Cli.csproj | 2 +- .../IFoundInstalledPackageHandler.cs | 16 +++++++++ .../INugetPluginRegistry.cs | 6 ++++ .../Models/INugetPackageIdentifier.cs | 4 +-- .../Models/INugetPluginService.cs | 5 +++ .../Models/INuspecFile.cs | 5 +++ .../Editor/Configuration/NugetConfigFile.cs | 2 +- .../Configuration/NugetForUnityPluginId.cs | 3 +- .../Editor/InstalledPackagesManager.cs | 2 ++ .../Editor/NugetPackageUninstaller.cs | 2 +- .../PluginAPI/NuGetForUnity.PluginAPI.dll | Bin 8704 -> 9216 bytes .../PluginAPI/NuGetForUnity.PluginAPI.xml | 29 +++++++++++++++- .../PluginSupport/NugetPluginService.cs | 4 +++ .../Editor/PluginSupport/PluginRegistry.cs | 31 +++++++++++++++++- 16 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 src/NuGetForUnity.PluginAPI/ExtensionPoints/IFoundInstalledPackageHandler.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 89ed5869..48a714e6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -168,7 +168,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v3 with: - dotnet-version: "7.0.x" + dotnet-version: "8.0.x" - name: Build and pack .NET Core Global Tool run: >- diff --git a/src/NuGetForUnity.Cli/Fakes/AssemblyLoader.cs b/src/NuGetForUnity.Cli/Fakes/AssemblyLoader.cs index e09f33db..7b45d74c 100644 --- a/src/NuGetForUnity.Cli/Fakes/AssemblyLoader.cs +++ b/src/NuGetForUnity.Cli/Fakes/AssemblyLoader.cs @@ -1,4 +1,5 @@ #nullable enable +using System.IO; using System.Reflection; using NuGetForUnity.Cli; using NugetForUnity.Configuration; @@ -17,8 +18,9 @@ internal static class AssemblyLoader /// Assembly of the loaded plugin. internal static Assembly Load(NugetForUnityPluginId pluginId) { - var loadContext = new NugetAssemblyLoadContext(pluginId.Path); - var assembly = loadContext.LoadFromAssemblyPath(pluginId.Path); + var path = Path.GetFullPath(Path.Combine(UnityPathHelper.AbsoluteProjectPath, pluginId.Path)); + var loadContext = new NugetAssemblyLoadContext(path); + var assembly = loadContext.LoadFromAssemblyPath(path); return assembly; } } diff --git a/src/NuGetForUnity.Cli/NuGetForUnity.Cli.csproj b/src/NuGetForUnity.Cli/NuGetForUnity.Cli.csproj index 8a3c9548..164a8c70 100644 --- a/src/NuGetForUnity.Cli/NuGetForUnity.Cli.csproj +++ b/src/NuGetForUnity.Cli/NuGetForUnity.Cli.csproj @@ -3,7 +3,7 @@ Exe NuGetForUnity.Cli NuGetForUnity.Cli - net7.0 + net8.0 AnyCPU true nugetforunity diff --git a/src/NuGetForUnity.PluginAPI/ExtensionPoints/IFoundInstalledPackageHandler.cs b/src/NuGetForUnity.PluginAPI/ExtensionPoints/IFoundInstalledPackageHandler.cs new file mode 100644 index 00000000..4783366f --- /dev/null +++ b/src/NuGetForUnity.PluginAPI/ExtensionPoints/IFoundInstalledPackageHandler.cs @@ -0,0 +1,16 @@ +using NugetForUnity.PluginAPI.Models; + +namespace NugetForUnity.PluginAPI.ExtensionPoints +{ + /// + /// Implement this interface to add additional handling for each found installed package. + /// + public interface IFoundInstalledPackageHandler + { + /// + /// This will be called for each found installed package in the project. + /// + /// The installedPackage created from found nuspec file. + void ProcessInstalledPackage(INugetPackage installedPackage); + } +} diff --git a/src/NuGetForUnity.PluginAPI/INugetPluginRegistry.cs b/src/NuGetForUnity.PluginAPI/INugetPluginRegistry.cs index 42782d0d..772115bb 100644 --- a/src/NuGetForUnity.PluginAPI/INugetPluginRegistry.cs +++ b/src/NuGetForUnity.PluginAPI/INugetPluginRegistry.cs @@ -36,5 +36,11 @@ public interface INugetPluginRegistry /// /// The package uninstall handler to register. void RegisterPackageUninstallHandler(IPackageUninstallHandler packageUninstallHandler); + + /// + /// Register a class that will be called when installed package is found. + /// + /// The found installed package handler to register. + void RegisterFoundInstalledPackageHandler(IFoundInstalledPackageHandler foundInstalledPackageHandler); } } diff --git a/src/NuGetForUnity.PluginAPI/Models/INugetPackageIdentifier.cs b/src/NuGetForUnity.PluginAPI/Models/INugetPackageIdentifier.cs index 0bd9d1df..87315f66 100644 --- a/src/NuGetForUnity.PluginAPI/Models/INugetPackageIdentifier.cs +++ b/src/NuGetForUnity.PluginAPI/Models/INugetPackageIdentifier.cs @@ -11,10 +11,10 @@ public interface INugetPackageIdentifier string Id { get; } /// - /// Gets the normalized version number of the NuGet package. + /// Gets or sets the normalized version number of the NuGet package. /// This is the normalized version number without build-metadata e.g. 1.0.0+b3a8 is normalized to 1.0.0. /// - string Version { get; } + string Version { get; set; } /// /// Returns the folder path where this package is or will be installed. diff --git a/src/NuGetForUnity.PluginAPI/Models/INugetPluginService.cs b/src/NuGetForUnity.PluginAPI/Models/INugetPluginService.cs index 7423b279..a85d86b2 100644 --- a/src/NuGetForUnity.PluginAPI/Models/INugetPluginService.cs +++ b/src/NuGetForUnity.PluginAPI/Models/INugetPluginService.cs @@ -12,6 +12,11 @@ public interface INugetPluginService /// string ProjectAssetsDir { get; } + /// + /// Gets the absolute path to the directory where packages are installed. + /// + string PackageInstallDir { get; } + /// /// Allows plugin to register a function that will modify the contents of default new nuspec file. /// diff --git a/src/NuGetForUnity.PluginAPI/Models/INuspecFile.cs b/src/NuGetForUnity.PluginAPI/Models/INuspecFile.cs index 207c070d..3c79a87a 100644 --- a/src/NuGetForUnity.PluginAPI/Models/INuspecFile.cs +++ b/src/NuGetForUnity.PluginAPI/Models/INuspecFile.cs @@ -5,6 +5,11 @@ /// public interface INuspecFile : INugetPackageIdentifier { + /// + /// Gets or sets the Id of the package. + /// + new string Id { get; set; } + /// /// Gets or sets the source control branch the package is from. /// diff --git a/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs b/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs index 3486b4a6..e0f451a3 100644 --- a/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs +++ b/src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs @@ -84,7 +84,7 @@ public class NugetConfigFile public INugetPackageSource ActivePackageSource { get; private set; } /// - /// Gets the local path where packages are to be installed. It can be a full path or a relative path. + /// Gets the absolute path where packages are to be installed. /// [NotNull] public string RepositoryPath { get; private set; } = Path.GetFullPath(Path.Combine(Application.dataPath, "Packages")); diff --git a/src/NuGetForUnity/Editor/Configuration/NugetForUnityPluginId.cs b/src/NuGetForUnity/Editor/Configuration/NugetForUnityPluginId.cs index c2d5c160..2144b969 100644 --- a/src/NuGetForUnity/Editor/Configuration/NugetForUnityPluginId.cs +++ b/src/NuGetForUnity/Editor/Configuration/NugetForUnityPluginId.cs @@ -1,6 +1,7 @@ using System; using System.Reflection; using JetBrains.Annotations; +using NugetForUnity.Helper; namespace NugetForUnity.Configuration { @@ -17,7 +18,7 @@ internal sealed class NugetForUnityPluginId : IEquatable internal NugetForUnityPluginId([NotNull] string name, [NotNull] string path) { Name = name; - Path = path; + Path = System.IO.Path.IsPathRooted(path) ? PathHelper.GetRelativePath(UnityPathHelper.AbsoluteProjectPath, path) : path; } /// diff --git a/src/NuGetForUnity/Editor/InstalledPackagesManager.cs b/src/NuGetForUnity/Editor/InstalledPackagesManager.cs index 176ed8d2..ac54dca9 100644 --- a/src/NuGetForUnity/Editor/InstalledPackagesManager.cs +++ b/src/NuGetForUnity/Editor/InstalledPackagesManager.cs @@ -8,6 +8,7 @@ using NugetForUnity.Configuration; using NugetForUnity.Models; using NugetForUnity.PackageSource; +using NugetForUnity.PluginSupport; using Debug = UnityEngine.Debug; namespace NugetForUnity @@ -347,6 +348,7 @@ private static void AddPackageToInstalledInternal([NotNull] INugetPackage packag var packages = InstalledPackagesDictionary; if (!packages.ContainsKey(package.Id)) { + PluginRegistry.Instance.ProcessInstalledPackage(package); package.IsManuallyInstalled = GetPackageConfigurationById(package.Id)?.IsManuallyInstalled ?? false; if (package.IsManuallyInstalled) { diff --git a/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs b/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs index 44c04ed7..4b6a6585 100644 --- a/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageUninstaller.cs @@ -76,7 +76,7 @@ public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageU /// Uninstalls all of the currently installed packages. /// /// The list of packages to uninstall. - internal static void UninstallAll([NotNull] [ItemNotNull] List packagesToUninstall) + public static void UninstallAll([NotNull] [ItemNotNull] List packagesToUninstall) { foreach (var package in packagesToUninstall) { diff --git a/src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.dll b/src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.dll index 607e3f53d30e0429e4b5b82284fc6e8b4d40f8a3..a37ea353df789a0164e72f681f30873d8ac1c221 100644 GIT binary patch delta 3321 zcma)94QyN06+ZVqKmR0tiIX%xc3Rho+lDS}5~p_JkWiX7V~HSH+N_i+43Ri-)YeMZ zq(enf`@)iq5}~Qt6Rbrl^k;%nO&VYg5J+n^Iy12`{@JRjTLnWxLr{>y)Jf~M?;bmT z-lR?3=zi~f=ezgZ>wC|=?`5`Uw)fc4`mr^`4^ysOrTS2@Xdw!~;m$hCI~LF1S#mK> z^iAd1LbT*qtgGozzDJCD$oHU^I=}uad3Y}~}!}b8@VI@q|!Ke8H&NDA2 zt(YWJiFKHLH;98yW0gL4bfTk%6;SM?C(AG(BF*TX)DO+enqTx|``tNqM$fWR*3M9t zRWNJMwG1$ONq>R+ZKl&EhWMp3OK)W{kYihz9muh6T1R2`X5cYxD>E!`=+~Uzwwdv=C&5{v)QoYA zCTyl(ccuzh%sGX%S&p*cJjD6O{q#TRM>waL8Qa7>XFJwk1wG@)QD(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U*EhYvcYx=`|)`-RrqFs z4;jDllCT5JiuxexSX7Rhj;f-LkA2pkQwwUozepl0|S`qjiyP=Jse1y{| zldn@W3Ml!iMr*jhYZCh;1|-hm`C$kbB)P{#qm2>|F%fQ&@|i^=)h6&0Ch)vPU<$Qh z07k;jk!|M6Hdk%G!}9_>VrpX2V#=5(bJ8x>Z>@~8n}{zMr?3z?W?u*OHs6!^#^;VLs5h?Q4(iFk2Se;3%4nc8pWr^yG5H!KW z29JtsJE0}mQfRn0V*S=byU)T={Sw5?xcCwFBEgPPYZ5z;1Wjo5n5ekOM2YIpV4?}u zf?X50qbO0m1G|=BtI#jolqee3H=tvj>Or+Dc5+3>3e}m!-b2uw`yKTU5j4SWB4}=0 zyN#G}rVd*VQQmz`wX4U-6K7%Fa*BL$`V;P~LNG?@$QDNRxRM#2$7RT9jOKJQ8bD4l z+9&;k(uNbBI@p|gaUcBCUW((v=wC7X-}3)ZZ5W=@bxc~JfSk1HxrGTS^tGI#>1k7K zxZ+b$?KG@miNLUi%^%iMs2J#^Qi;I3G`acOUn zXhY6Vz#$w(0h}6p5vOZ4=VzDZ2}u)`;$^N`(CvQ|6=Je z<7|@l6^Un+u#mr^L13$Fx?Bd@EbS^)sHddoFL*d6ssOFHV>za41*T!r99Li}(19<< zwKf8njz5k=o`F?n0Xb4)GE&DVyE zpWO!xNA66$Nqb16!YG$Z6GiJb7%G9#EX*N(@nc((2e MMbf>$vc918Z{gXR=l}o! diff --git a/src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.xml b/src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.xml index 3c662983..566bd17f 100644 --- a/src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.xml +++ b/src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.xml @@ -4,6 +4,17 @@ NuGetForUnity.PluginAPI + + + Implement this interface to add additional handling for each found installed package. + + + + + This will be called for each found installed package in the project. + + The installedPackage created from found nuspec file. + Implement this interface to add additional buttons for each package in NugetForUnity window. @@ -97,6 +108,12 @@ The package uninstall handler to register. + + + Register a class that will be called when installed package is found. + + The found installed package handler to register. + Represents a NuGet package. @@ -130,7 +147,7 @@ - Gets the normalized version number of the NuGet package. + Gets or sets the normalized version number of the NuGet package. This is the normalized version number without build-metadata e.g. 1.0.0+b3a8 is normalized to 1.0.0. @@ -157,6 +174,11 @@ Gets the absolute path to the projects Assets directory. + + + Gets the absolute path to the directory where packages are installed. + + Allows plugin to register a function that will modify the contents of default new nuspec file. @@ -194,6 +216,11 @@ Represents a .nuspec file used to store metadata for a NuGet package. + + + Gets or sets the Id of the package. + + Gets or sets the source control branch the package is from. diff --git a/src/NuGetForUnity/Editor/PluginSupport/NugetPluginService.cs b/src/NuGetForUnity/Editor/PluginSupport/NugetPluginService.cs index d23cbd5b..24b5b00b 100644 --- a/src/NuGetForUnity/Editor/PluginSupport/NugetPluginService.cs +++ b/src/NuGetForUnity/Editor/PluginSupport/NugetPluginService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using NugetForUnity.Configuration; using NugetForUnity.Helper; using NugetForUnity.PluginAPI.Models; using NugetForUnity.Ui; @@ -16,6 +17,9 @@ public sealed class NugetPluginService : INugetPluginService, IDisposable /// public string ProjectAssetsDir => UnityPathHelper.AbsoluteAssetsPath; + /// + public string PackageInstallDir => ConfigurationManager.NugetConfigFile.RepositoryPath; + /// public void RegisterNuspecCustomizer(Action customizer) { diff --git a/src/NuGetForUnity/Editor/PluginSupport/PluginRegistry.cs b/src/NuGetForUnity/Editor/PluginSupport/PluginRegistry.cs index 2c1ca22a..e0b3324e 100644 --- a/src/NuGetForUnity/Editor/PluginSupport/PluginRegistry.cs +++ b/src/NuGetForUnity/Editor/PluginSupport/PluginRegistry.cs @@ -14,7 +14,12 @@ namespace NugetForUnity.PluginSupport /// /// Plugin Registry loads the plugins and provides methods for calling them. /// - internal class PluginRegistry : INugetPluginRegistry, IPackageButtonsHandler, IPackageInstallFileHandler, IPackageUninstallHandler + internal class PluginRegistry : + INugetPluginRegistry, + IPackageButtonsHandler, + IPackageInstallFileHandler, + IPackageUninstallHandler, + IFoundInstalledPackageHandler { private readonly List packageButtonsHandlers = new List(); @@ -22,6 +27,8 @@ internal class PluginRegistry : INugetPluginRegistry, IPackageButtonsHandler, IP private readonly List packageUninstallHandlers = new List(); + private readonly List foundInstalledPackageHandlers = new List(); + /// /// Gets the static instance of PluginRegistry. /// @@ -120,6 +127,12 @@ public void RegisterPackageUninstallHandler(IPackageUninstallHandler packageUnin packageUninstallHandlers.Add(packageUninstallHandler); } + /// + public void RegisterFoundInstalledPackageHandler(IFoundInstalledPackageHandler foundInstalledPackageHandler) + { + foundInstalledPackageHandlers.Add(foundInstalledPackageHandler); + } + /// public void DrawButtons(INugetPackage package, INugetPackage installedPackage, bool existsInUnity) { @@ -188,5 +201,21 @@ public void HandleUninstalledAll() } } } + + /// + public void ProcessInstalledPackage(INugetPackage installedPackage) + { + foreach (var foundInstalledPackageHandler in foundInstalledPackageHandlers) + { + try + { + foundInstalledPackageHandler.ProcessInstalledPackage(installedPackage); + } + catch (Exception e) + { + Debug.LogError($"Exception while executing ProcessInstalledPackage plugin handler {e}"); + } + } + } } }