88using JetBrains . Annotations ;
99using NugetForUnity . Configuration ;
1010using NugetForUnity . Helper ;
11- using NugetForUnity . Models ;
1211using UnityEditor ;
1312using UnityEngine ;
1413using Object = UnityEngine . Object ;
@@ -51,16 +50,6 @@ public class NugetAssetPostprocessor : AssetPostprocessor
5150 /// </summary>
5251 private const string RoslynAnalyzerLabel = "RoslynAnalyzer" ;
5352
54- /// <summary>
55- /// Name of the root folder containing dotnet analyzers.
56- /// </summary>
57- private static readonly string AnalyzersRoslynVersionsFolderName = Path . Combine ( AnalyzersFolderName , "dotnet" ) ;
58-
59- /// <summary>
60- /// Prefix for the path of dll's of roslyn analyzers.
61- /// </summary>
62- private static readonly string AnalyzersRoslynVersionSubFolderPrefix = Path . Combine ( AnalyzersRoslynVersionsFolderName , "roslyn" ) ;
63-
6453 private static readonly List < BuildTarget > NonObsoleteBuildTargets = typeof ( BuildTarget ) . GetFields ( BindingFlags . Public | BindingFlags . Static )
6554 . Where ( fieldInfo => fieldInfo . GetCustomAttribute ( typeof ( ObsoleteAttribute ) ) == null )
6655 . Select ( fieldInfo => ( BuildTarget ) fieldInfo . GetValue ( null ) )
@@ -280,44 +269,6 @@ private static string GetNuGetRepositoryPath()
280269 return ConfigurationManager . NugetConfigFile . RepositoryPath + Path . DirectorySeparatorChar ;
281270 }
282271
283- [ CanBeNull ]
284- private static NugetPackageVersion GetMaxSupportedRoslynVersion ( )
285- {
286- var unityVersion = UnityVersion . Current ;
287- if ( unityVersion >= new UnityVersion ( 2022 , 3 , 12 , 'f' , 1 ) )
288- {
289- return new NugetPackageVersion ( "4.3.0" ) ;
290- }
291-
292- if ( unityVersion >= new UnityVersion ( 2022 , 2 , 1 , 'f' , 1 ) )
293- {
294- return new NugetPackageVersion ( "4.1.0" ) ;
295- }
296-
297- if ( unityVersion >= new UnityVersion ( 2021 , 2 , 1 , 'f' , 1 ) )
298- {
299- return new NugetPackageVersion ( "3.8.0" ) ;
300- }
301-
302- return null ;
303- }
304-
305- [ CanBeNull ]
306- private static NugetPackageVersion GetRoslynVersionNumberFromAnalyzerPath ( string analyzerAssetPath )
307- {
308- var versionPrefixStartIndex = analyzerAssetPath . IndexOf ( AnalyzersRoslynVersionSubFolderPrefix , StringComparison . Ordinal ) ;
309- if ( versionPrefixStartIndex < 0 )
310- {
311- return null ;
312- }
313-
314- var versionStartIndex = versionPrefixStartIndex + AnalyzersRoslynVersionSubFolderPrefix . Length ;
315- var separatorIndex = analyzerAssetPath . IndexOf ( Path . DirectorySeparatorChar , versionStartIndex ) ;
316- var versionLength = separatorIndex >= 0 ? separatorIndex - versionStartIndex : analyzerAssetPath . Length - versionStartIndex ;
317- var versionString = analyzerAssetPath . Substring ( versionStartIndex , versionLength ) ;
318- return string . IsNullOrEmpty ( versionString ) ? null : new NugetPackageVersion ( versionString ) ;
319- }
320-
321272 private static string [ ] ModifyImportSettingsOfRoslynAnalyzer ( [ NotNull ] PluginImporter plugin )
322273 {
323274 plugin . SetCompatibleWithAnyPlatform ( false ) ;
@@ -327,42 +278,14 @@ private static string[] ModifyImportSettingsOfRoslynAnalyzer([NotNull] PluginImp
327278 plugin . SetExcludeFromAnyPlatform ( platform , false ) ;
328279 }
329280
330- var enableRoslynAnalyzer = true ;
331-
332- // The nuget package can contain analyzers for multiple Roslyn versions.
333- // In that case, for the same package, the most recent version must be chosen out of those available for the current Unity version.
334- var assetPath = Path . GetFullPath ( plugin . assetPath ) ;
335- var assetRoslynVersion = GetRoslynVersionNumberFromAnalyzerPath ( assetPath ) ;
336- if ( assetRoslynVersion != null )
281+ var enableRoslynAnalyzer = AnalyzerHelper . ShouldEnableRoslynAnalyzer ( plugin . assetPath ) ;
282+ if ( enableRoslynAnalyzer )
337283 {
338- var maxSupportedRoslynVersion = GetMaxSupportedRoslynVersion ( ) ;
339- if ( maxSupportedRoslynVersion == null )
340- {
341- // the current unity version doesn't support roslyn analyzers
342- enableRoslynAnalyzer = false ;
343- }
344- else
345- {
346- var versionPrefixIndex = assetPath . IndexOf ( AnalyzersRoslynVersionsFolderName , StringComparison . Ordinal ) ;
347- var analyzerVersionsRootDirectoryPath = Path . Combine (
348- assetPath . Substring ( 0 , versionPrefixIndex ) ,
349- AnalyzersRoslynVersionsFolderName ) ;
350- var analyzersFolders = Directory . EnumerateDirectories ( analyzerVersionsRootDirectoryPath ) ;
351- var allEnabledRoslynVersions = analyzersFolders . Select ( GetRoslynVersionNumberFromAnalyzerPath )
352- . Where ( version => version != null && version . CompareTo ( maxSupportedRoslynVersion ) <= 0 )
353- . ToArray ( ) ;
354-
355- // If most recent valid analyzers exist elsewhere, don't add label `RoslynAnalyzer`
356- var maxMatchingVersion = allEnabledRoslynVersions . Max ( ) ;
357- if ( ! allEnabledRoslynVersions . Contains ( assetRoslynVersion ) || assetRoslynVersion < maxMatchingVersion )
358- {
359- enableRoslynAnalyzer = false ;
360- }
361- }
284+ NugetLogger . LogVerbose ( "Configured asset '{0}' as a Roslyn-Analyzer." , plugin . assetPath ) ;
285+ return new [ ] { RoslynAnalyzerLabel , ProcessedLabel } ;
362286 }
363287
364- NugetLogger . LogVerbose ( "Configured asset '{0}' as a Roslyn-Analyzer." , plugin . assetPath ) ;
365- return enableRoslynAnalyzer ? new [ ] { RoslynAnalyzerLabel , ProcessedLabel } : new [ ] { ProcessedLabel } ;
288+ return new [ ] { ProcessedLabel } ;
366289 }
367290
368291 private static string [ ] ModifyImportSettingsOfGeneralPlugin ( [ NotNull ] PackageConfig packageConfig , [ NotNull ] PluginImporter plugin )
0 commit comments