diff --git a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs index f89562b6..17b9364b 100644 --- a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs +++ b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs @@ -97,15 +97,23 @@ internal static void OnPostprocessAllAssets( var absoluteRepositoryPath = GetNuGetRepositoryPath(); - AssetDatabase.StartAssetEditing(); + LogResults(ProcessAssets(importedAssets, absoluteRepositoryPath)); + } - try - { - LogResults(importedAssets.SelectMany(assetPath => HandleAsset(assetPath, absoluteRepositoryPath, true))); - } - finally + [NotNull] + private static IEnumerable<(string AssetType, string AssetPath, ResultStatus Status)> ProcessAssets( + [NotNull]string[] importedAssets, + [NotNull] string absoluteRepositoryPath) + { + using (var delayedAssetEditor = new DelayedAssetEditor()) { - AssetDatabase.StopAssetEditing(); + foreach (var assetPath in importedAssets) + { + foreach (var result in HandleAsset(assetPath, absoluteRepositoryPath, true, delayedAssetEditor)) + { + yield return result; + } + } } } @@ -113,14 +121,15 @@ internal static void OnPostprocessAllAssets( private static IEnumerable<(string AssetType, string AssetPath, ResultStatus Status)> HandleAsset( [NotNull] string projectRelativeAssetPath, [NotNull] string absoluteRepositoryPath, - bool reimport) + bool reimport, + DelayedAssetEditor delayedAssetEditor = null) { var assetFileName = Path.GetFileName(projectRelativeAssetPath); if (assetFileName.Equals(NugetConfigFile.FileName, StringComparison.OrdinalIgnoreCase) || assetFileName.Equals(PackagesConfigFile.FileName, StringComparison.OrdinalIgnoreCase)) { // Not sure why but for .config files we need to re-import always. I think this is because they are treated as native plug-ins. - var result = ModifyImportSettingsOfConfigurationFile(projectRelativeAssetPath, true); + var result = ModifyImportSettingsOfConfigurationFile(projectRelativeAssetPath, true, delayedAssetEditor); yield return ("ConfigurationFile", projectRelativeAssetPath, result); yield break; @@ -157,12 +166,14 @@ internal static void OnPostprocessAllAssets( var assetLablesToSet = new List(); if (configurationOfPackage != null) { + delayedAssetEditor?.Start(); assetLablesToSet.AddRange(ModifyImportSettingsOfGeneralPlugin(configurationOfPackage, plugin)); yield return ("GeneralSetting", projectRelativeAssetPath, ResultStatus.Success); } if (assetPathComponents.Length > 1 && assetPathComponents[1].Equals(AnalyzersFolderName, StringComparison.OrdinalIgnoreCase)) { + delayedAssetEditor?.Start(); assetLablesToSet.AddRange(ModifyImportSettingsOfRoslynAnalyzer(plugin)); yield return ("RoslynAnalyzer", projectRelativeAssetPath, ResultStatus.Success); } @@ -170,6 +181,7 @@ internal static void OnPostprocessAllAssets( UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries() .Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1]))) { + delayedAssetEditor?.Start(); assetLablesToSet.AddRange(ModifyImportSettingsOfPlayerOnly(plugin)); yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success); } @@ -324,7 +336,11 @@ private static string[] ModifyImportSettingsOfPlayerOnly([NotNull] PluginImporte /// /// The path to the .config file. /// Whether or not to save and re-import the file. - private static ResultStatus ModifyImportSettingsOfConfigurationFile([NotNull] string analyzerAssetPath, bool reimport) + /// + private static ResultStatus ModifyImportSettingsOfConfigurationFile( + [NotNull] string analyzerAssetPath, + bool reimport, + DelayedAssetEditor delayedAssetEditor) { if (!GetPluginImporter(analyzerAssetPath, out var plugin)) { @@ -336,6 +352,7 @@ private static ResultStatus ModifyImportSettingsOfConfigurationFile([NotNull] st return ResultStatus.AlreadyProcessed; } + delayedAssetEditor?.Start(); plugin.SetCompatibleWithPlatform(BuildTarget.WSAPlayer, false); AssetDatabase.SetLabels(plugin, new[] { ProcessedLabel }); @@ -435,14 +452,13 @@ private static bool AlreadyProcessed([NotNull] Object asset) private void OnPreprocessAsset() { var absoluteRepositoryPath = GetNuGetRepositoryPath(); - var results = HandleAsset(assetPath, absoluteRepositoryPath, false); - LogResults(results); + LogResults(HandleAsset(assetPath, absoluteRepositoryPath, false)); } [SuppressMessage( "StyleCop.CSharp.OrderingRules", "SA1201:Elements should appear in the correct order", - Justification = "We like private enums at the botom of the file.")] + Justification = "We like private enums at the bottom of the file.")] private enum ResultStatus { Success, @@ -451,5 +467,31 @@ private enum ResultStatus AlreadyProcessed, } + + [SuppressMessage( + "StyleCop.CSharp.OrderingRules", + "SA1201:Elements should appear in the correct order", + Justification = "We like private classes at the bottom of the file.")] + private class DelayedAssetEditor : IDisposable + { + private bool editingStarted; + + public void Start() + { + if (!editingStarted) + { + editingStarted = true; + AssetDatabase.StartAssetEditing(); + } + } + + public void Dispose() + { + if (editingStarted) + { + AssetDatabase.StopAssetEditing(); + } + } + } } }