11using System . Security . Cryptography ;
2+ using System . Threading ;
23using Buildalyzer ;
3- using Microsoft . Build . Construction ;
4+ using Microsoft . VisualStudio . SolutionPersistence ;
5+ using Microsoft . VisualStudio . SolutionPersistence . Serializer ;
46
57namespace Covenant . Analysis . Dotnet ;
68
@@ -16,9 +18,10 @@ internal class DotnetAnalyzer : Analyzer
1618 private readonly DotnetAssetFileReader _assetFileReader ;
1719 private readonly AnalyzerManager _analyzerManager ;
1820 private bool _enabled = true ;
21+ private static readonly string [ ] _solutionExtensions = new [ ] { ".sln" , ".slnx" } ;
1922
2023 public override bool Enabled => _enabled ;
21- public override string [ ] Patterns { get ; } = new [ ] { "**/*.sln" } ;
24+ public override string [ ] Patterns { get ; } = _solutionExtensions . Select ( e => $ "**/*{ e } " ) . ToArray ( ) ;
2225
2326 public DotnetAnalyzer (
2427 IFileSystem fileSystem , IEnvironment environment , IGlobber globber )
@@ -53,9 +56,10 @@ public override bool ShouldTraverse(DirectoryPath path)
5356
5457 public override bool CanHandle ( AnalysisContext context , FilePath path )
5558 {
56- var isSolution = path . GetExtension ( ) ? . Equals ( ".sln" , StringComparison . OrdinalIgnoreCase ) ?? false ;
57- var isCsProject = path . GetExtension ( ) ? . Equals ( ".csproj" , StringComparison . OrdinalIgnoreCase ) ?? false ;
58- var isFsProject = path . GetExtension ( ) ? . Equals ( ".fsproj" , StringComparison . OrdinalIgnoreCase ) ?? false ;
59+ var extension = path . GetExtension ( ) ?? string . Empty ;
60+ var isSolution = _solutionExtensions . Contains ( extension , StringComparer . OrdinalIgnoreCase ) ;
61+ var isCsProject = extension . Equals ( ".csproj" , StringComparison . OrdinalIgnoreCase ) ;
62+ var isFsProject = extension . Equals ( ".fsproj" , StringComparison . OrdinalIgnoreCase ) ;
5963 return isSolution || isCsProject || isFsProject ;
6064 }
6165
@@ -64,22 +68,23 @@ public override void Analyze(AnalysisContext context, FilePath path)
6468 path = path . MakeAbsolute ( _environment ) ;
6569 var extension = path . GetExtension ( ) ?? string . Empty ;
6670
67- if ( extension . Equals ( ".sln" , StringComparison . OrdinalIgnoreCase ) )
71+ if ( _solutionExtensions . Contains ( extension , StringComparer . OrdinalIgnoreCase ) )
6872 {
6973 // Analyze solution
70- var solution = SolutionFile . Parse ( path . FullPath ) ;
74+ ISolutionSerializer ? serializer = SolutionSerializers . GetSerializerByMoniker ( path . FullPath ) ;
75+ if ( serializer is null )
76+ {
77+ return ;
78+ }
7179
72- // Collect all projects
73- var projects = solution . ProjectsInOrder . Where (
74- csproj => csproj . ProjectType != SolutionProjectType . SolutionFolder
75- && csproj . ProjectType != SolutionProjectType . Unknown ) ;
80+ var solution = serializer . OpenAsync ( path . FullPath , CancellationToken . None ) . Result ;
7681
7782 // Add all components in all projects
7883 var assetFiles = new List < AssetFile > ( ) ;
79- foreach ( var csproj in projects )
84+ foreach ( var csproj in solution . SolutionProjects )
8085 {
81- var ( version , copyright , analyzerResult ) = PerformDesignTimeBuild ( context , csproj . AbsolutePath ) ;
82- var assetsFile = ReadAssetFile ( context , csproj . AbsolutePath , analyzerResult ) ;
86+ var ( version , copyright , analyzerResult ) = PerformDesignTimeBuild ( context , csproj . FilePath ) ;
87+ var assetsFile = ReadAssetFile ( context , csproj . FilePath , analyzerResult ) ;
8388 if ( assetsFile != null )
8489 {
8590 assetFiles . Add ( assetsFile ) ;
0 commit comments