Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance finding already imported Assemblies #502

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions src/NuGetForUnity/Editor/UnityPreImportedLibraryResolver.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Compilation;

namespace NugetForUnity
{
Expand All @@ -19,34 +19,52 @@ internal static class UnityPreImportedLibraryResolver
/// <returns>A set of all names of libraries that are already imported by unity.</returns>
internal static HashSet<string> GetAlreadyImportedLibs()
{
if (alreadyImportedLibs == null)
if (alreadyImportedLibs != null)
{
// Find all the dll's already installed by NuGetForUnity
var alreadyInstalledDllFileNames = new HashSet<string>();

if (NugetHelper.NugetConfigFile != null && Directory.Exists(NugetHelper.NugetConfigFile.RepositoryPath))
{
alreadyInstalledDllFileNames = new HashSet<string>(
Directory.EnumerateFiles(NugetHelper.NugetConfigFile.RepositoryPath, "*.dll", SearchOption.AllDirectories)
.Select(Path.GetFileNameWithoutExtension));
}

// Get all assemblies loaded into Unity and filter out those installed by NuGetForUnity
alreadyImportedLibs = new HashSet<string>(
AppDomain.CurrentDomain.GetAssemblies()
.Select(assembly => Path.GetFileNameWithoutExtension(assembly.ManifestModule.Name))
.Where(p => !alreadyInstalledDllFileNames.Contains(p)));

if (PlayerSettings.GetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup) ==
ApiCompatibilityLevel.NET_Standard_2_0)
{
alreadyImportedLibs.Add("NETStandard.Library");
alreadyImportedLibs.Add("Microsoft.NETCore.Platforms");
}

NugetHelper.LogVerbose("Already imported libs: {0}", string.Join(", ", alreadyImportedLibs));
return alreadyImportedLibs;
}

// Find all the assemblies already installed by NuGetForUnity
var alreadyInstalledDllFileNames = new HashSet<string>();

if (NugetHelper.NugetConfigFile != null && Directory.Exists(NugetHelper.NugetConfigFile.RepositoryPath))
{
alreadyInstalledDllFileNames = new HashSet<string>(
Directory.EnumerateFiles(NugetHelper.NugetConfigFile.RepositoryPath, "*.dll", SearchOption.AllDirectories)
.Select(Path.GetFileNameWithoutExtension));
}

// Search the all project assemblies that are not from a package or a Unity assembly.
// We only use player assemblies as we don't need to collect UnityEditor assemblies, we don't support installing NuGet packages with reference to UnityEditor.
#if UNITY_2019_3_OR_NEWER
const AssembliesType assemblieType = AssembliesType.PlayerWithoutTestAssemblies;
#else
const AssembliesType assemblieType = AssembliesType.Player;
#endif
var projectAssemblies = CompilationPipeline.GetAssemblies(assemblieType)
.Where(
playerAssembly => playerAssembly.sourceFiles.Length == 0 ||
playerAssembly.sourceFiles.Any(
sourceFilePath => sourceFilePath.StartsWith("Assets/") || sourceFilePath.StartsWith("Assets\\")));

// Collect all referenced assemblies but exclude all assemblies installed by NuGetForUnity.
var porojectReferences = projectAssemblies.SelectMany(playerAssembly => playerAssembly.allReferences);
alreadyImportedLibs = new HashSet<string>(
porojectReferences.Select(compiledAssemblyReference => Path.GetFileNameWithoutExtension(compiledAssemblyReference))
.Where(assemblyName => !alreadyInstalledDllFileNames.Contains(assemblyName)));

if (PlayerSettings.GetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup) == ApiCompatibilityLevel.NET_Standard_2_0)
{
// mark NuGet packages that contain the .net standard references as already imported
alreadyImportedLibs.Add("NETStandard.Library");
alreadyImportedLibs.Add("Microsoft.NETCore.Platforms");
}

// the compiler / language is available by default
alreadyImportedLibs.Add("Microsoft.CSharp");

NugetHelper.LogVerbose("Already imported libs: {0}", string.Join(", ", alreadyImportedLibs));

return alreadyImportedLibs;
}
}
Expand Down

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

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

12 changes: 12 additions & 0 deletions tools/format-staged.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ErrorActionPreference = 'Stop'

$repositoryRoot = Resolve-Path $PSScriptRoot\..
$currentLocation = Get-Location

try {
Set-Location $repositoryRoot
. pre-commit run --verbose
}
finally {
Set-Location $currentLocation
}