Skip to content

Commit

Permalink
Fixed PreprocessAsset and resolved AccessToDisposedClosure warning
Browse files Browse the repository at this point in the history
  • Loading branch information
igor84 committed Jul 15, 2024
1 parent 2c9fcd7 commit 81a4884
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,23 @@ internal static void OnPostprocessAllAssets(

var absoluteRepositoryPath = GetNuGetRepositoryPath();

LogResults(ProcessAssets(importedAssets, absoluteRepositoryPath));
}

[NotNull]
private static IEnumerable<(string AssetType, string AssetPath, ResultStatus Status)> ProcessAssets(
[NotNull]string[] importedAssets,
[NotNull] string absoluteRepositoryPath)
{
using (var delayedAssetEditor = new DelayedAssetEditor())
{
// ReSharper disable once AccessToDisposedClosure
LogResults(importedAssets.SelectMany(assetPath => HandleAsset(assetPath, absoluteRepositoryPath, true, delayedAssetEditor)));
foreach (var assetPath in importedAssets)
{
foreach (var result in HandleAsset(assetPath, absoluteRepositoryPath, true, delayedAssetEditor))
{
yield return result;
}
}
}
}

Expand All @@ -109,7 +122,7 @@ internal static void OnPostprocessAllAssets(
[NotNull] string projectRelativeAssetPath,
[NotNull] string absoluteRepositoryPath,
bool reimport,
DelayedAssetEditor delayedAssetEditor)
DelayedAssetEditor delayedAssetEditor = null)
{
var assetFileName = Path.GetFileName(projectRelativeAssetPath);
if (assetFileName.Equals(NugetConfigFile.FileName, StringComparison.OrdinalIgnoreCase) ||
Expand Down Expand Up @@ -153,22 +166,22 @@ internal static void OnPostprocessAllAssets(
var assetLablesToSet = new List<string>();
if (configurationOfPackage != null)
{
delayedAssetEditor.Start();
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();
delayedAssetEditor?.Start();
assetLablesToSet.AddRange(ModifyImportSettingsOfRoslynAnalyzer(plugin));
yield return ("RoslynAnalyzer", projectRelativeAssetPath, ResultStatus.Success);
}
else if (assetPathComponents.Length > 0 &&
UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries()
.Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1])))
{
delayedAssetEditor.Start();
delayedAssetEditor?.Start();
assetLablesToSet.AddRange(ModifyImportSettingsOfPlayerOnly(plugin));
yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success);
}
Expand Down Expand Up @@ -339,7 +352,7 @@ private static ResultStatus ModifyImportSettingsOfConfigurationFile(
return ResultStatus.AlreadyProcessed;
}

delayedAssetEditor.Start();
delayedAssetEditor?.Start();
plugin.SetCompatibleWithPlatform(BuildTarget.WSAPlayer, false);
AssetDatabase.SetLabels(plugin, new[] { ProcessedLabel });

Expand Down Expand Up @@ -439,11 +452,7 @@ private static bool AlreadyProcessed([NotNull] Object asset)
private void OnPreprocessAsset()
{
var absoluteRepositoryPath = GetNuGetRepositoryPath();
using (var delayedAssetEditor = new DelayedAssetEditor())
{
var results = HandleAsset(assetPath, absoluteRepositoryPath, false, delayedAssetEditor);
LogResults(results);
}
LogResults(HandleAsset(assetPath, absoluteRepositoryPath, false));
}

[SuppressMessage(
Expand Down

0 comments on commit 81a4884

Please sign in to comment.