Skip to content

Commit

Permalink
fix analzyer DLLs configuration
Browse files Browse the repository at this point in the history
fix asset postprocesor to work with newer unity version + also fix case where analyzer setting was not applied
  • Loading branch information
JoC0de committed Jun 1, 2024
1 parent bd685f3 commit eea04c4
Showing 1 changed file with 35 additions and 44 deletions.
79 changes: 35 additions & 44 deletions src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ 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();
LogResults(importedAssets.SelectMany(assetPath => HandleAsset(assetPath, absoluteRepositoryPath, true)));
}

[NotNull]
Expand Down Expand Up @@ -128,7 +128,7 @@ internal static void OnPostprocessAllAssets(
var assetPathComponents = GetPathComponents(assetPathRelativeToRepository);
var packageNameParts = assetPathComponents.Length > 0 ? assetPathComponents[0].Split('.') : Array.Empty<string>();
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))
{
Expand All @@ -144,26 +144,37 @@ internal static void OnPostprocessAllAssets(
yield break;
}

if (packageConfig != null)
var assetLablesToSet = new List<string>();
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();
}
}

Expand Down Expand Up @@ -228,7 +239,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);
Expand All @@ -237,7 +248,7 @@ private static void ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImporte
plugin.SetExcludeFromAnyPlatform(platform, false);
}

var enableRoslynAnalyzer = true;
var enableRoslynAnalyzer = /*false;/*/true;

// The nuget package can contain analyzers for multiple Roslyn versions.
// In that case, for the same package, the most recent version must be chosen out of those available for the current Unity version.
Expand Down Expand Up @@ -271,28 +282,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 };
}

/// <summary>
Expand All @@ -301,21 +299,14 @@ private static void ModifyImportSettingsOfGeneralPlugin([NotNull] PackageConfig
/// <seealso cref="UnityPreImportedLibraryResolver.GetAlreadyImportedEditorOnlyLibraries" />.
/// </summary>
/// <param name="plugin">The asset to edit.</param>
/// <param name="reimport">Whether or not to save and re-import the file.</param>
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 };
}

/// <summary>
Expand Down

0 comments on commit eea04c4

Please sign in to comment.