From 02d0313c3ed19c5708d7046ee64f01190c9c879d 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 | 43 ++++++++++++------------------ Assets/Tests/Editor/NuGetTests.cs | 41 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/Assets/NuGet/Editor/NugetHelper.cs b/Assets/NuGet/Editor/NugetHelper.cs index b1de5f04..7666c3c4 100644 --- a/Assets/NuGet/Editor/NugetHelper.cs +++ b/Assets/NuGet/Editor/NugetHelper.cs @@ -626,38 +626,29 @@ private static HashSet GetAlreadyImportedLibs() { if (alreadyImportedLibs == null) { - var lookupPaths = GetAllLookupPaths(); - var libNames = lookupPaths.SelectMany(directory => Directory.EnumerateFiles(directory, "*.dll", SearchOption.AllDirectories)) - .Select(Path.GetFileName) - .Select(p => Path.ChangeExtension(p, null)); - alreadyImportedLibs = new HashSet(libNames); - - // workaround not sure why but it reported as missing when not added as NuGet package - alreadyImportedLibs.Remove("System.Runtime.CompilerServices.Unsafe"); + // Find all the dll's already installed by NuGetForUnity + var alreadyInstalledDllFileNames = new HashSet(); + + if (NugetConfigFile != null && Directory.Exists(NugetConfigFile.RepositoryPath)) + { + alreadyInstalledDllFileNames = new HashSet( + Directory.EnumerateFiles(NugetConfigFile.RepositoryPath, "*.dll", SearchOption.AllDirectories) + .Select(Path.GetFileNameWithoutExtension)); + } + + // Get all assemblies loaded into Unity and filter out those installed by NuGetForUnity + alreadyImportedLibs = new HashSet( + AppDomain.CurrentDomain.GetAssemblies() + .Select(a => a.ManifestModule.Name) + .Select(p => Path.ChangeExtension(p, null)) + .Where(p => !alreadyInstalledDllFileNames.Contains(p))); + 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.Select(x => x.TargetFramework); diff --git a/Assets/Tests/Editor/NuGetTests.cs b/Assets/Tests/Editor/NuGetTests.cs index c8cd7b6d..ad8f69c1 100644 --- a/Assets/Tests/Editor/NuGetTests.cs +++ b/Assets/Tests/Editor/NuGetTests.cs @@ -359,4 +359,45 @@ public void TryGetBestTargetFrameworkForCurrentSettingsTest(string unityVersion, PlayerSettings.SetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup, oldApiCompatibilityLevel); } } + + [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" (like if the editor is re-opend) + 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); + } }