diff --git a/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs b/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs index 488a15fa..28f98476 100644 --- a/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs +++ b/src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; using System.Threading.Tasks; using NugetForUnity; using NugetForUnity.Configuration; @@ -14,6 +15,7 @@ using NUnit.Framework; using UnityEditor; using UnityEngine; +using UnityEngine.TestTools; public class NuGetTests { @@ -882,6 +884,8 @@ public void TestSerializeNugetPackageIdentifier(string version) [TestCase("jQuery", "3.7.0")] public void TestPostprocessInstall(string packageId, string packageVersion) { + IgnorePackagesConfigImportError(); + var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true }; var filepath = ConfigurationManager.NugetConfigFile.PackagesConfigFilePath; @@ -900,6 +904,8 @@ public void TestPostprocessInstall(string packageId, string packageVersion) [TestCase("jQuery", "3.7.0")] public void TestPostprocessUninstall(string packageId, string packageVersion) { + IgnorePackagesConfigImportError(); + var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true }; var filepath = ConfigurationManager.NugetConfigFile.PackagesConfigFilePath; @@ -920,6 +926,8 @@ public void TestPostprocessUninstall(string packageId, string packageVersion) [TestCase("jQuery", "3.7.0", "3.6.4")] public void TestPostprocessDifferentVersion(string packageId, string packageVersionOld, string packageVersionNew) { + IgnorePackagesConfigImportError(); + var packageOld = new NugetPackageIdentifier(packageId, packageVersionOld) { IsManuallyInstalled = true }; var packageNew = new NugetPackageIdentifier(packageId, packageVersionNew) { IsManuallyInstalled = true }; @@ -1016,6 +1024,11 @@ public void TestSourceCodePackageInstall(string packageId, string packageVersion Assert.IsFalse(InstalledPackagesManager.IsInstalled(package, false), "The package is STILL installed: {0} {1}", package.Id, package.Version); } + private static void IgnorePackagesConfigImportError() + { + LogAssert.Expect(LogType.Error, new Regex("NuGetForUnity: failed to process: .*packages.config' \\(ConfigurationFile\\)")); + } + private static void ConfigureNugetConfig(InstallMode installMode) { var nugetConfigFile = ConfigurationManager.NugetConfigFile; diff --git a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs index a84ea25e..f89562b6 100644 --- a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs +++ b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs @@ -87,16 +87,26 @@ internal static void OnPostprocessAllAssets( } var packagesConfigFilePath = ConfigurationManager.NugetConfigFile.PackagesConfigFilePath; - var foundPackagesConfigAsset = importedAssets.Any( - importedAsset => Path.GetFullPath(importedAsset).Equals(packagesConfigFilePath, StringComparison.Ordinal)); - - if (!foundPackagesConfigAsset) + if (importedAssets.Any( + path => path.EndsWith(PackagesConfigFile.FileName) && + Path.GetFullPath(path).Equals(packagesConfigFilePath, StringComparison.Ordinal))) { - return; + InstalledPackagesManager.ReloadPackagesConfig(); + PackageRestorer.Restore(ConfigurationManager.NugetConfigFile.SlimRestore); } - InstalledPackagesManager.ReloadPackagesConfig(); - PackageRestorer.Restore(ConfigurationManager.NugetConfigFile.SlimRestore); + var absoluteRepositoryPath = GetNuGetRepositoryPath(); + + AssetDatabase.StartAssetEditing(); + + try + { + LogResults(importedAssets.SelectMany(assetPath => HandleAsset(assetPath, absoluteRepositoryPath, true))); + } + finally + { + AssetDatabase.StopAssetEditing(); + } } [NotNull] @@ -128,7 +138,7 @@ internal static void OnPostprocessAllAssets( var assetPathComponents = GetPathComponents(assetPathRelativeToRepository); var packageNameParts = assetPathComponents.Length > 0 ? assetPathComponents[0].Split('.') : Array.Empty(); var packageName = string.Join(".", packageNameParts.TakeWhile(part => !part.All(char.IsDigit))); - var packageConfig = InstalledPackagesManager.PackagesConfigFile.GetPackageConfigurationById(packageName); + var configurationOfPackage = InstalledPackagesManager.PackagesConfigFile.GetPackageConfigurationById(packageName); if (!GetPluginImporter(projectRelativeAssetPath, out var plugin)) { @@ -144,26 +154,37 @@ internal static void OnPostprocessAllAssets( yield break; } - if (packageConfig != null) + var assetLablesToSet = new List(); + if (configurationOfPackage != null) { - ModifyImportSettingsOfGeneralPlugin(packageConfig, plugin, reimport); + assetLablesToSet.AddRange(ModifyImportSettingsOfGeneralPlugin(configurationOfPackage, plugin)); yield return ("GeneralSetting", projectRelativeAssetPath, ResultStatus.Success); } if (assetPathComponents.Length > 1 && assetPathComponents[1].Equals(AnalyzersFolderName, StringComparison.OrdinalIgnoreCase)) { - ModifyImportSettingsOfRoslynAnalyzer(plugin, reimport); + assetLablesToSet.AddRange(ModifyImportSettingsOfRoslynAnalyzer(plugin)); yield return ("RoslynAnalyzer", projectRelativeAssetPath, ResultStatus.Success); + } + else if (assetPathComponents.Length > 0 && + UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries() + .Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1]))) + { + assetLablesToSet.AddRange(ModifyImportSettingsOfPlayerOnly(plugin)); + yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success); + } + if (assetLablesToSet.Count == 0) + { yield break; } - if (assetPathComponents.Length > 0 && - UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries() - .Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1]))) + AssetDatabase.SetLabels(plugin, assetLablesToSet.Distinct().ToArray()); + + if (reimport) { - ModifyImportSettingsOfPlayerOnly(plugin, reimport); - yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success); + // Persist and reload the change to the meta file + plugin.SaveAndReimport(); } } @@ -228,7 +249,7 @@ private static NugetPackageVersion GetRoslynVersionNumberFromAnalyzerPath(string return string.IsNullOrEmpty(versionString) ? null : new NugetPackageVersion(versionString); } - private static void ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporter plugin, bool reimport) + private static string[] ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporter plugin) { plugin.SetCompatibleWithAnyPlatform(false); plugin.SetCompatibleWithEditor(false); @@ -271,28 +292,15 @@ private static void ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporte } } - AssetDatabase.SetLabels(plugin, enableRoslynAnalyzer ? new[] { RoslynAnalyzerLabel, ProcessedLabel } : new[] { ProcessedLabel }); - - if (reimport) - { - // Persist and reload the change to the meta file - plugin.SaveAndReimport(); - } - NugetLogger.LogVerbose("Configured asset '{0}' as a Roslyn-Analyzer.", plugin.assetPath); + return enableRoslynAnalyzer ? new[] { RoslynAnalyzerLabel, ProcessedLabel } : new[] { ProcessedLabel }; } - private static void ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig packageConfig, [NotNull] PluginImporter plugin, bool reimport) + private static string[] ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig packageConfig, [NotNull] PluginImporter plugin) { PluginImporterIsExplicitlyReferencedProperty.SetValue(plugin, !packageConfig.AutoReferenced); - AssetDatabase.SetLabels(plugin, new[] { ProcessedLabel }); - - if (reimport) - { - // Persist and reload the change to the meta file - plugin.SaveAndReimport(); - } + return new[] { ProcessedLabel }; } /// @@ -301,21 +309,14 @@ private static void ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig /// . /// /// The asset to edit. - /// Whether or not to save and re-import the file. - private static void ModifyImportSettingsOfPlayerOnly([NotNull] PluginImporter plugin, bool reimport) + private static string[] ModifyImportSettingsOfPlayerOnly([NotNull] PluginImporter plugin) { plugin.SetCompatibleWithAnyPlatform(true); plugin.SetExcludeEditorFromAnyPlatform(true); - AssetDatabase.SetLabels(plugin, new[] { ProcessedLabel }); - - if (reimport) - { - // Persist and reload the change to the meta file - plugin.SaveAndReimport(); - } - NugetLogger.LogVerbose("Configured asset '{0}' as a Player Only.", plugin.assetPath); + + return new[] { ProcessedLabel }; } ///