From 91f4b5cc628c1c4e52cd43c5295185bf535df9d4 Mon Sep 17 00:00:00 2001 From: "robin.moss" Date: Tue, 21 Dec 2021 16:41:15 +0000 Subject: [PATCH] fix: use reflection to find already imported libs #395 --- Assets/NuGet/Editor/NugetHelper.cs | 34 ++++----------------- Assets/Tests/Editor/NuGetTests.cs | 49 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/Assets/NuGet/Editor/NugetHelper.cs b/Assets/NuGet/Editor/NugetHelper.cs index 630b27ef..ee1336d1 100644 --- a/Assets/NuGet/Editor/NugetHelper.cs +++ b/Assets/NuGet/Editor/NugetHelper.cs @@ -561,13 +561,14 @@ private static bool IsAlreadyImportedInEngine(NugetPackageIdentifier package) private static HashSet alreadyImportedLibs = null; private static HashSet GetAlreadyImportedLibs() { + if (alreadyImportedLibs == null) { - string[] lookupPaths = GetAllLookupPaths(); - IEnumerable libNames = lookupPaths - .SelectMany(directory => Directory.EnumerateFiles(directory, "*.dll", SearchOption.AllDirectories)) - .Select(Path.GetFileName) - .Select(p => Path.ChangeExtension(p, null)); + List packageIds = PackagesConfigFile.Packages.Select(p => p.Id).ToList(); + IEnumerable libNames = AppDomain.CurrentDomain.GetAssemblies() + .Select(a => a.ManifestModule.Name) + .Select(p => Path.ChangeExtension(p, null)) + .Where(p => !packageIds.Contains(p)); alreadyImportedLibs = new HashSet(libNames); LogVerbose("Already imported libs: {0}", string.Join(", ", alreadyImportedLibs)); } @@ -575,29 +576,6 @@ private static HashSet GetAlreadyImportedLibs() 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 diff --git a/Assets/Tests/Editor/NuGetTests.cs b/Assets/Tests/Editor/NuGetTests.cs index 53e26596..00183084 100644 --- a/Assets/Tests/Editor/NuGetTests.cs +++ b/Assets/Tests/Editor/NuGetTests.cs @@ -1,6 +1,7 @@ using NUnit.Framework; using NugetForUnity; using System.IO; +using System.Reflection; using System.Linq; using UnityEditor; using UnityEngine; @@ -222,4 +223,52 @@ public void PackageSourceCredentialsTest(string name) Assert.That(parsedSource.UserName, Is.EqualTo(username)); Assert.That(parsedSource.SavedPassword, Is.EqualTo(password)); } + + /// + /// Dependencies under MonoBleedingEdge may not be part of Unity Editor/Runtime, lets test the + /// by checking the status of MonoBleedingEdge only DLLs and + /// those that are also part of Unity Runtime. + /// + [Test] + [TestCase("System.ComponentModel.Annotations", "5.0.0", false)] + [TestCase("Mono.Posix", "7.1.0-final.1.21458.1", true)] + public void MonoBleedingEdgeTest(string id, string version, bool alreadyInstalled) + { + var identifier = new NugetPackageIdentifier(id, version); + if (alreadyInstalled) + { + Assert.IsTrue(NugetHelper.IsInstalled(identifier), "The package should be installed: {0} {1}", identifier.Id, + identifier.Version); + } + else + { + Assert.IsFalse(NugetHelper.IsInstalled(identifier), "The package should not be installed: {0} {1}", identifier.Id, + identifier.Version); + } + } + + [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); + } }