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

Only execute Start/StopAssetEditing if something will change #661

Merged
merged 2 commits into from
Jul 16, 2024
Merged
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
68 changes: 55 additions & 13 deletions src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,39 @@ 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;
}
}
}
}

[NotNull]
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;
Expand Down Expand Up @@ -157,19 +166,22 @@ internal static void OnPostprocessAllAssets(
var assetLablesToSet = new List<string>();
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);
}
else if (assetPathComponents.Length > 0 &&
UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries()
.Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1])))
{
delayedAssetEditor?.Start();
assetLablesToSet.AddRange(ModifyImportSettingsOfPlayerOnly(plugin));
yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success);
}
Expand Down Expand Up @@ -324,7 +336,11 @@ private static string[] ModifyImportSettingsOfPlayerOnly([NotNull] PluginImporte
/// </summary>
/// <param name="analyzerAssetPath">The path to the .config file.</param>
/// <param name="reimport">Whether or not to save and re-import the file.</param>
private static ResultStatus ModifyImportSettingsOfConfigurationFile([NotNull] string analyzerAssetPath, bool reimport)
/// <param name="delayedAssetEditor"></param>
private static ResultStatus ModifyImportSettingsOfConfigurationFile(
[NotNull] string analyzerAssetPath,
bool reimport,
DelayedAssetEditor delayedAssetEditor)
{
if (!GetPluginImporter(analyzerAssetPath, out var plugin))
{
Expand All @@ -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 });

Expand Down Expand Up @@ -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,
Expand All @@ -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();
}
}
}
}
}
Loading