Skip to content

Commit b9f895b

Browse files
authored
fix analyzer DLLs configuration (#646)
* fix asset postprocessor to work with newer unity version * also fix case where analyzer setting was not applied * wrapp asset importing in StartAssetEditing / StopAssetEditing
1 parent 630d3db commit b9f895b

2 files changed

Lines changed: 57 additions & 43 deletions

File tree

src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Reflection;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89
using NugetForUnity;
910
using NugetForUnity.Configuration;
@@ -14,6 +15,7 @@
1415
using NUnit.Framework;
1516
using UnityEditor;
1617
using UnityEngine;
18+
using UnityEngine.TestTools;
1719

1820
public class NuGetTests
1921
{
@@ -942,6 +944,8 @@ public void TestSerializeNugetPackageIdentifier(string version)
942944
[TestCase("jQuery", "3.7.0")]
943945
public void TestPostprocessInstall(string packageId, string packageVersion)
944946
{
947+
IgnorePackagesConfigImportError();
948+
945949
var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true };
946950

947951
var filepath = ConfigurationManager.NugetConfigFile.PackagesConfigFilePath;
@@ -960,6 +964,8 @@ public void TestPostprocessInstall(string packageId, string packageVersion)
960964
[TestCase("jQuery", "3.7.0")]
961965
public void TestPostprocessUninstall(string packageId, string packageVersion)
962966
{
967+
IgnorePackagesConfigImportError();
968+
963969
var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true };
964970

965971
var filepath = ConfigurationManager.NugetConfigFile.PackagesConfigFilePath;
@@ -980,6 +986,8 @@ public void TestPostprocessUninstall(string packageId, string packageVersion)
980986
[TestCase("jQuery", "3.7.0", "3.6.4")]
981987
public void TestPostprocessDifferentVersion(string packageId, string packageVersionOld, string packageVersionNew)
982988
{
989+
IgnorePackagesConfigImportError();
990+
983991
var packageOld = new NugetPackageIdentifier(packageId, packageVersionOld) { IsManuallyInstalled = true };
984992
var packageNew = new NugetPackageIdentifier(packageId, packageVersionNew) { IsManuallyInstalled = true };
985993

@@ -1076,6 +1084,11 @@ public void TestSourceCodePackageInstall(string packageId, string packageVersion
10761084
Assert.IsFalse(InstalledPackagesManager.IsInstalled(package, false), "The package is STILL installed: {0} {1}", package.Id, package.Version);
10771085
}
10781086

1087+
private static void IgnorePackagesConfigImportError()
1088+
{
1089+
LogAssert.Expect(LogType.Error, new Regex("NuGetForUnity: failed to process: .*packages.config' \\(ConfigurationFile\\)"));
1090+
}
1091+
10791092
private static void ConfigureNugetConfig(InstallMode installMode)
10801093
{
10811094
var nugetConfigFile = ConfigurationManager.NugetConfigFile;

src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,26 @@ internal static void OnPostprocessAllAssets(
8787
}
8888

8989
var packagesConfigFilePath = ConfigurationManager.NugetConfigFile.PackagesConfigFilePath;
90-
var foundPackagesConfigAsset = importedAssets.Any(
91-
importedAsset => Path.GetFullPath(importedAsset).Equals(packagesConfigFilePath, StringComparison.Ordinal));
92-
93-
if (!foundPackagesConfigAsset)
90+
if (importedAssets.Any(
91+
path => path.EndsWith(PackagesConfigFile.FileName) &&
92+
Path.GetFullPath(path).Equals(packagesConfigFilePath, StringComparison.Ordinal)))
9493
{
95-
return;
94+
InstalledPackagesManager.ReloadPackagesConfig();
95+
PackageRestorer.Restore(ConfigurationManager.NugetConfigFile.SlimRestore);
9696
}
9797

98-
InstalledPackagesManager.ReloadPackagesConfig();
99-
PackageRestorer.Restore(ConfigurationManager.NugetConfigFile.SlimRestore);
98+
var absoluteRepositoryPath = GetNuGetRepositoryPath();
99+
100+
AssetDatabase.StartAssetEditing();
101+
102+
try
103+
{
104+
LogResults(importedAssets.SelectMany(assetPath => HandleAsset(assetPath, absoluteRepositoryPath, true)));
105+
}
106+
finally
107+
{
108+
AssetDatabase.StopAssetEditing();
109+
}
100110
}
101111

102112
[NotNull]
@@ -128,7 +138,7 @@ internal static void OnPostprocessAllAssets(
128138
var assetPathComponents = GetPathComponents(assetPathRelativeToRepository);
129139
var packageNameParts = assetPathComponents.Length > 0 ? assetPathComponents[0].Split('.') : Array.Empty<string>();
130140
var packageName = string.Join(".", packageNameParts.TakeWhile(part => !part.All(char.IsDigit)));
131-
var packageConfig = InstalledPackagesManager.PackagesConfigFile.GetPackageConfigurationById(packageName);
141+
var configurationOfPackage = InstalledPackagesManager.PackagesConfigFile.GetPackageConfigurationById(packageName);
132142

133143
if (!GetPluginImporter(projectRelativeAssetPath, out var plugin))
134144
{
@@ -144,26 +154,37 @@ internal static void OnPostprocessAllAssets(
144154
yield break;
145155
}
146156

147-
if (packageConfig != null)
157+
var assetLablesToSet = new List<string>();
158+
if (configurationOfPackage != null)
148159
{
149-
ModifyImportSettingsOfGeneralPlugin(packageConfig, plugin, reimport);
160+
assetLablesToSet.AddRange(ModifyImportSettingsOfGeneralPlugin(configurationOfPackage, plugin));
150161
yield return ("GeneralSetting", projectRelativeAssetPath, ResultStatus.Success);
151162
}
152163

153164
if (assetPathComponents.Length > 1 && assetPathComponents[1].Equals(AnalyzersFolderName, StringComparison.OrdinalIgnoreCase))
154165
{
155-
ModifyImportSettingsOfRoslynAnalyzer(plugin, reimport);
166+
assetLablesToSet.AddRange(ModifyImportSettingsOfRoslynAnalyzer(plugin));
156167
yield return ("RoslynAnalyzer", projectRelativeAssetPath, ResultStatus.Success);
168+
}
169+
else if (assetPathComponents.Length > 0 &&
170+
UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries()
171+
.Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1])))
172+
{
173+
assetLablesToSet.AddRange(ModifyImportSettingsOfPlayerOnly(plugin));
174+
yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success);
175+
}
157176

177+
if (assetLablesToSet.Count == 0)
178+
{
158179
yield break;
159180
}
160181

161-
if (assetPathComponents.Length > 0 &&
162-
UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries()
163-
.Contains(Path.GetFileNameWithoutExtension(assetPathComponents[assetPathComponents.Length - 1])))
182+
AssetDatabase.SetLabels(plugin, assetLablesToSet.Distinct().ToArray());
183+
184+
if (reimport)
164185
{
165-
ModifyImportSettingsOfPlayerOnly(plugin, reimport);
166-
yield return ("PlayerOnly", projectRelativeAssetPath, ResultStatus.Success);
186+
// Persist and reload the change to the meta file
187+
plugin.SaveAndReimport();
167188
}
168189
}
169190

@@ -228,7 +249,7 @@ private static NugetPackageVersion GetRoslynVersionNumberFromAnalyzerPath(string
228249
return string.IsNullOrEmpty(versionString) ? null : new NugetPackageVersion(versionString);
229250
}
230251

231-
private static void ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporter plugin, bool reimport)
252+
private static string[] ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporter plugin)
232253
{
233254
plugin.SetCompatibleWithAnyPlatform(false);
234255
plugin.SetCompatibleWithEditor(false);
@@ -271,28 +292,15 @@ private static void ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporte
271292
}
272293
}
273294

274-
AssetDatabase.SetLabels(plugin, enableRoslynAnalyzer ? new[] { RoslynAnalyzerLabel, ProcessedLabel } : new[] { ProcessedLabel });
275-
276-
if (reimport)
277-
{
278-
// Persist and reload the change to the meta file
279-
plugin.SaveAndReimport();
280-
}
281-
282295
NugetLogger.LogVerbose("Configured asset '{0}' as a Roslyn-Analyzer.", plugin.assetPath);
296+
return enableRoslynAnalyzer ? new[] { RoslynAnalyzerLabel, ProcessedLabel } : new[] { ProcessedLabel };
283297
}
284298

285-
private static void ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig packageConfig, [NotNull] PluginImporter plugin, bool reimport)
299+
private static string[] ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig packageConfig, [NotNull] PluginImporter plugin)
286300
{
287301
PluginImporterIsExplicitlyReferencedProperty.SetValue(plugin, !packageConfig.AutoReferenced);
288302

289-
AssetDatabase.SetLabels(plugin, new[] { ProcessedLabel });
290-
291-
if (reimport)
292-
{
293-
// Persist and reload the change to the meta file
294-
plugin.SaveAndReimport();
295-
}
303+
return new[] { ProcessedLabel };
296304
}
297305

298306
/// <summary>
@@ -301,21 +309,14 @@ private static void ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig
301309
/// <seealso cref="UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries" />.
302310
/// </summary>
303311
/// <param name="plugin">The asset to edit.</param>
304-
/// <param name="reimport">Whether or not to save and re-import the file.</param>
305-
private static void ModifyImportSettingsOfPlayerOnly([NotNull] PluginImporter plugin, bool reimport)
312+
private static string[] ModifyImportSettingsOfPlayerOnly([NotNull] PluginImporter plugin)
306313
{
307314
plugin.SetCompatibleWithAnyPlatform(true);
308315
plugin.SetExcludeEditorFromAnyPlatform(true);
309316

310-
AssetDatabase.SetLabels(plugin, new[] { ProcessedLabel });
311-
312-
if (reimport)
313-
{
314-
// Persist and reload the change to the meta file
315-
plugin.SaveAndReimport();
316-
}
317-
318317
NugetLogger.LogVerbose("Configured asset '{0}' as a Player Only.", plugin.assetPath);
318+
319+
return new[] { ProcessedLabel };
319320
}
320321

321322
/// <summary>

0 commit comments

Comments
 (0)