Skip to content

Commit

Permalink
Fixed path handling for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
igor84 committed Dec 14, 2023
1 parent 441f6df commit 21e7bf5
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >-
Expand Down
6 changes: 4 additions & 2 deletions src/NuGetForUnity.Cli/Fakes/AssemblyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System.IO;
using System.Reflection;
using NuGetForUnity.Cli;
using NugetForUnity.Configuration;
Expand All @@ -17,8 +18,9 @@ internal static class AssemblyLoader
/// <returns>Assembly of the loaded plugin.</returns>
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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetForUnity.Cli/NuGetForUnity.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>NuGetForUnity.Cli</RootNamespace>
<AssemblyName>NuGetForUnity.Cli</AssemblyName>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
<PackAsTool>true</PackAsTool>
<ToolCommandName>nugetforunity</ToolCommandName>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using NugetForUnity.PluginAPI.Models;

namespace NugetForUnity.PluginAPI.ExtensionPoints
{
/// <summary>
/// Implement this interface to add additional handling for each found installed package.
/// </summary>
public interface IFoundInstalledPackageHandler
{
/// <summary>
/// This will be called for each found installed package in the project.
/// </summary>
/// <param name="installedPackage">The installedPackage created from found nuspec file.</param>
void ProcessInstalledPackage(INugetPackage installedPackage);
}
}
6 changes: 6 additions & 0 deletions src/NuGetForUnity.PluginAPI/INugetPluginRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ public interface INugetPluginRegistry
/// </summary>
/// <param name="packageUninstallHandler">The package uninstall handler to register.</param>
void RegisterPackageUninstallHandler(IPackageUninstallHandler packageUninstallHandler);

/// <summary>
/// Register a class that will be called when installed package is found.
/// </summary>
/// <param name="foundInstalledPackageHandler">The found installed package handler to register.</param>
void RegisterFoundInstalledPackageHandler(IFoundInstalledPackageHandler foundInstalledPackageHandler);
}
}
4 changes: 2 additions & 2 deletions src/NuGetForUnity.PluginAPI/Models/INugetPackageIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public interface INugetPackageIdentifier
string Id { get; }

/// <summary>
/// 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. <b>1.0.0+b3a8</b> is normalized to <b>1.0.0</b>.
/// </summary>
string Version { get; }
string Version { get; set; }

/// <summary>
/// Returns the folder path where this package is or will be installed.
Expand Down
5 changes: 5 additions & 0 deletions src/NuGetForUnity.PluginAPI/Models/INugetPluginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public interface INugetPluginService
/// </summary>
string ProjectAssetsDir { get; }

/// <summary>
/// Gets the absolute path to the directory where packages are installed.
/// </summary>
string PackageInstallDir { get; }

/// <summary>
/// Allows plugin to register a function that will modify the contents of default new nuspec file.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/NuGetForUnity.PluginAPI/Models/INuspecFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
/// </summary>
public interface INuspecFile : INugetPackageIdentifier
{
/// <summary>
/// Gets or sets the Id of the package.
/// </summary>
new string Id { get; set; }

/// <summary>
/// Gets or sets the source control branch the package is from.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class NugetConfigFile
public INugetPackageSource ActivePackageSource { get; private set; }

/// <summary>
/// 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.
/// </summary>
[NotNull]
public string RepositoryPath { get; private set; } = Path.GetFullPath(Path.Combine(Application.dataPath, "Packages"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using JetBrains.Annotations;
using NugetForUnity.Helper;

namespace NugetForUnity.Configuration
{
Expand All @@ -17,7 +18,7 @@ internal sealed class NugetForUnityPluginId : IEquatable<NugetForUnityPluginId>
internal NugetForUnityPluginId([NotNull] string name, [NotNull] string path)
{
Name = name;
Path = path;
Path = System.IO.Path.IsPathRooted(path) ? PathHelper.GetRelativePath(UnityPathHelper.AbsoluteProjectPath, path) : path;
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/NuGetForUnity/Editor/InstalledPackagesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NugetForUnity.Configuration;
using NugetForUnity.Models;
using NugetForUnity.PackageSource;
using NugetForUnity.PluginSupport;
using Debug = UnityEngine.Debug;

namespace NugetForUnity
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetForUnity/Editor/NugetPackageUninstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void Uninstall([NotNull] INugetPackageIdentifier package, PackageU
/// Uninstalls all of the currently installed packages.
/// </summary>
/// <param name="packagesToUninstall">The list of packages to uninstall.</param>
internal static void UninstallAll([NotNull] [ItemNotNull] List<INugetPackage> packagesToUninstall)
public static void UninstallAll([NotNull] [ItemNotNull] List<INugetPackage> packagesToUninstall)
{
foreach (var package in packagesToUninstall)
{
Expand Down
Binary file modified src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.dll
Binary file not shown.
29 changes: 28 additions & 1 deletion src/NuGetForUnity/Editor/PluginAPI/NuGetForUnity.PluginAPI.xml

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

4 changes: 4 additions & 0 deletions src/NuGetForUnity/Editor/PluginSupport/NugetPluginService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +17,9 @@ public sealed class NugetPluginService : INugetPluginService, IDisposable
/// <inheritdoc />
public string ProjectAssetsDir => UnityPathHelper.AbsoluteAssetsPath;

/// <inheritdoc />
public string PackageInstallDir => ConfigurationManager.NugetConfigFile.RepositoryPath;

/// <inheritdoc />
public void RegisterNuspecCustomizer(Action<INuspecFile> customizer)
{
Expand Down
31 changes: 30 additions & 1 deletion src/NuGetForUnity/Editor/PluginSupport/PluginRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ namespace NugetForUnity.PluginSupport
/// <summary>
/// Plugin Registry loads the plugins and provides methods for calling them.
/// </summary>
internal class PluginRegistry : INugetPluginRegistry, IPackageButtonsHandler, IPackageInstallFileHandler, IPackageUninstallHandler
internal class PluginRegistry :
INugetPluginRegistry,
IPackageButtonsHandler,
IPackageInstallFileHandler,
IPackageUninstallHandler,
IFoundInstalledPackageHandler
{
private readonly List<IPackageButtonsHandler> packageButtonsHandlers = new List<IPackageButtonsHandler>();

private readonly List<IPackageInstallFileHandler> packageInstallFileHandlers = new List<IPackageInstallFileHandler>();

private readonly List<IPackageUninstallHandler> packageUninstallHandlers = new List<IPackageUninstallHandler>();

private readonly List<IFoundInstalledPackageHandler> foundInstalledPackageHandlers = new List<IFoundInstalledPackageHandler>();

/// <summary>
/// Gets the static instance of PluginRegistry.
/// </summary>
Expand Down Expand Up @@ -120,6 +127,12 @@ public void RegisterPackageUninstallHandler(IPackageUninstallHandler packageUnin
packageUninstallHandlers.Add(packageUninstallHandler);
}

/// <inheritdoc />
public void RegisterFoundInstalledPackageHandler(IFoundInstalledPackageHandler foundInstalledPackageHandler)
{
foundInstalledPackageHandlers.Add(foundInstalledPackageHandler);
}

/// <inheritdoc />
public void DrawButtons(INugetPackage package, INugetPackage installedPackage, bool existsInUnity)
{
Expand Down Expand Up @@ -188,5 +201,21 @@ public void HandleUninstalledAll()
}
}
}

/// <inheritdoc />
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}");
}
}
}
}
}

0 comments on commit 21e7bf5

Please sign in to comment.