Skip to content

Commit

Permalink
fix: use reflection to find already imported libs #395
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-moss committed Mar 3, 2022
1 parent b6ef820 commit e278707
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
46 changes: 18 additions & 28 deletions Assets/NuGet/Editor/NugetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,43 +561,33 @@ private static bool IsAlreadyImportedInEngine(NugetPackageIdentifier package)
private static HashSet<string> alreadyImportedLibs = null;
private static HashSet<string> GetAlreadyImportedLibs()
{

if (alreadyImportedLibs == null)
{
string[] lookupPaths = GetAllLookupPaths();
IEnumerable<string> libNames = lookupPaths
.SelectMany(directory => Directory.EnumerateFiles(directory, "*.dll", SearchOption.AllDirectories))
.Select(Path.GetFileName)
.Select(p => Path.ChangeExtension(p, null));
// Find all the dll's already installed by NuGetForUnity
IList<string> nugetLibNames = new List<string>();

if (Directory.Exists(NugetConfigFile.RepositoryPath))
{
nugetLibNames = Directory.EnumerateFiles(NugetConfigFile.RepositoryPath, "*.dll",
SearchOption.AllDirectories)
.Select(Path.GetFileName)
.Select(p => Path.ChangeExtension(p, null))
.ToList();
}

// Get a list of all assemblies loaded into Unity and filter out those installed by NuGetForUnity
List<string> libNames = AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.ManifestModule.Name)
.Select(p => Path.ChangeExtension(p, null))
.Where(p => !nugetLibNames.Contains(p)).ToList();
alreadyImportedLibs = new HashSet<string>(libNames);
LogVerbose("Already imported libs: {0}", string.Join(", ", alreadyImportedLibs));
}

return alreadyImportedLibs;
}

private static string[] GetAllLookupPaths()
{
var executablePath = EditorApplication.applicationPath;
var roots = new[] {
// MacOS directory layout
Path.Combine(executablePath, "Contents"),
// Windows directory layout
Path.Combine(Directory.GetParent(executablePath).FullName, "Data")
};
var relativePaths = new[] {
Path.Combine("NetStandard", "compat"),
Path.Combine("MonoBleedingEdge", "lib", "mono")
};
var allPossiblePaths = roots
.SelectMany(root => relativePaths
.Select(relativePath => Path.Combine(root, relativePath)));
var existingPaths = allPossiblePaths
.Where(Directory.Exists)
.ToArray();
LogVerbose("All existing path to dependency lookup are: {0}", string.Join(", ", existingPaths));
return existingPaths;
}

public static NugetFrameworkGroup GetBestDependencyFrameworkGroupForCurrentSettings(NugetPackage package)
{
var targetFrameworks = package.Dependencies
Expand Down
32 changes: 28 additions & 4 deletions Assets/Tests/Editor/NuGetTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using NUnit.Framework;
using System.IO;
using System.Reflection;
using NugetForUnity;
using System.IO;
using System.Linq;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;

public class NuGetTests
{
Expand Down Expand Up @@ -222,4 +221,29 @@ public void PackageSourceCredentialsTest(string name)
Assert.That(parsedSource.UserName, Is.EqualTo(username));
Assert.That(parsedSource.SavedPassword, Is.EqualTo(password));
}

[Test]
public void TestUpgrading()
{
NugetHelper.LoadNugetConfigFile();

var componentModelAnnotation47 = new NugetPackageIdentifier("System.ComponentModel.Annotations", "4.7.0");
var componentModelAnnotation5 = new NugetPackageIdentifier("System.ComponentModel.Annotations", "5.0.0");

NugetHelper.InstallIdentifier(componentModelAnnotation47);
Assert.IsTrue(NugetHelper.IsInstalled(componentModelAnnotation47), "The package was NOT installed: {0} {1}", componentModelAnnotation47.Id, componentModelAnnotation47.Version);

// Force NuGetHelper to reload the "alreadyImportedLibs"
var field = typeof(NugetHelper).GetField("alreadyImportedLibs", BindingFlags.Static | BindingFlags.NonPublic);
Assert.IsNotNull(field, "Failed to find the field 'alreadyImportedLibs' in NugetHelper");
field.SetValue(null, null);

NugetHelper.InstallIdentifier(componentModelAnnotation5);
Assert.IsTrue(NugetHelper.IsInstalled(componentModelAnnotation5), "The package was NOT installed: {0} {1}", componentModelAnnotation5.Id, componentModelAnnotation5.Version);
Assert.IsFalse(NugetHelper.IsInstalled(componentModelAnnotation47), "The package is STILL installed: {0} {1}", componentModelAnnotation47.Id, componentModelAnnotation47.Version);

NugetHelper.UninstallAll();

Assert.IsFalse(NugetHelper.IsInstalled(componentModelAnnotation5), "The package is STILL installed: {0} {1}", componentModelAnnotation5.Id, componentModelAnnotation5.Version);
}
}

0 comments on commit e278707

Please sign in to comment.