1- namespace StyleCop . Analyzers . Status . Generator
1+ // Copyright (c) Dennis Fischer. All Rights Reserved.
2+ // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+ namespace StyleCop . Analyzers . Status . Generator
25{
36 using System ;
4- using System . Collections . Immutable ;
57 using System . Collections . Generic ;
8+ using System . Collections . Immutable ;
69 using System . IO ;
710 using System . Linq ;
811 using System . Reflection ;
912 using System . Text . RegularExpressions ;
1013 using System . Threading . Tasks ;
1114 using Microsoft . CodeAnalysis ;
1215 using Microsoft . CodeAnalysis . CodeFixes ;
13- using Microsoft . CodeAnalysis . MSBuild ;
14- using Microsoft . CodeAnalysis . Diagnostics ;
1516 using Microsoft . CodeAnalysis . CSharp ;
1617 using Microsoft . CodeAnalysis . CSharp . Syntax ;
18+ using Microsoft . CodeAnalysis . Diagnostics ;
19+ using Microsoft . CodeAnalysis . MSBuild ;
1720
1821 /// <summary>
1922 /// A class that is used to parse the StyleCop.Analyzers solution to get an overview
@@ -65,7 +68,6 @@ private SolutionReader()
6568 /// <returns>A <see cref="Task{SolutionReader}"/> representing the asynchronous operation</returns>
6669 public static async Task < SolutionReader > CreateAsync ( string pathToSln , string analyzerProjectName = "StyleCop.Analyzers" , string codeFixProjectName = "StyleCop.Analyzers.CodeFixes" )
6770 {
68-
6971 SolutionReader reader = new SolutionReader ( ) ;
7072
7173 reader . SlnPath = pathToSln ;
@@ -78,67 +80,10 @@ public static async Task<SolutionReader> CreateAsync(string pathToSln, string an
7880 return reader ;
7981 }
8082
81- private async Task InitializeAsync ( )
82- {
83- this . solution = await this . workspace . OpenSolutionAsync ( this . SlnPath ) ;
84-
85- this . analyzerProject = this . solution . Projects . Single ( x => x . Name == this . AnalyzerProjectName ) ;
86- this . analyzerCompilation = await this . analyzerProject . GetCompilationAsync ( ) ;
87- this . analyzerCompilation = this . analyzerCompilation . WithOptions ( this . analyzerCompilation . Options . WithOutputKind ( OutputKind . DynamicallyLinkedLibrary ) ) ;
88-
89- this . codeFixProject = this . solution . Projects . Single ( x => x . Name == this . CodeFixProjectName ) ;
90- this . codeFixCompilation = await this . codeFixProject . GetCompilationAsync ( ) ;
91- this . codeFixCompilation = this . codeFixCompilation . WithOptions ( this . codeFixCompilation . Options . WithOutputKind ( OutputKind . DynamicallyLinkedLibrary ) ) ;
92-
93- this . booleanType = this . analyzerCompilation . GetSpecialType ( SpecialType . System_Boolean ) ;
94-
95- this . Compile ( ) ;
96-
97- this . noCodeFixAttributeTypeSymbol = this . analyzerCompilation . GetTypeByMetadataName ( "StyleCop.Analyzers.NoCodeFixAttribute" ) ;
98- this . diagnosticAnalyzerTypeSymbol = this . analyzerCompilation . GetTypeByMetadataName ( typeof ( DiagnosticAnalyzer ) . FullName ) ;
99-
100- this . InitializeCodeFixTypes ( ) ;
101- }
102-
103- private void InitializeCodeFixTypes ( )
104- {
105- var codeFixTypes = this . codeFixAssembly . GetTypes ( ) . Where ( x => x . FullName . EndsWith ( "CodeFixProvider" ) ) ;
106-
107- this . CodeFixProviders = ImmutableArray . Create (
108- codeFixTypes
109- . Select ( t => Activator . CreateInstance ( t , true ) )
110- . OfType < CodeFixProvider > ( )
111- . Where ( x => x != null )
112- . Where ( x => x . GetType ( ) . Name != "SettingsFileCodeFixProvider" )
113- . ToArray ( ) ) ;
114- }
115-
116- private void Compile ( )
117- {
118- string path = Path . Combine ( Path . GetDirectoryName ( this . SlnPath ) , this . AnalyzerProjectName ) ;
119- this . analyzerAssembly = this . GetAssembly ( this . analyzerCompilation , ResourceReader . GetResourcesRecursive ( path ) ) ;
120-
121- this . codeFixAssembly = this . GetAssembly ( this . codeFixCompilation ) ;
122- }
123-
124- private Assembly GetAssembly ( Compilation compilation , IEnumerable < ResourceDescription > manifestResources = null )
125- {
126- MemoryStream memStream = new MemoryStream ( ) ;
127-
128- var emitResult = compilation . Emit ( memStream , manifestResources : manifestResources ) ;
129-
130- if ( ! emitResult . Success )
131- {
132- throw new CompilationFailedException ( ) ;
133- }
134-
135- return Assembly . Load ( memStream . ToArray ( ) ) ;
136- }
137-
13883 /// <summary>
13984 /// Analyzes the project and returns information about the diagnostics in it.
14085 /// </summary>
141- /// <returns>A <see cref="Task{ImmutableList{StyleCopDiagnostic}} "/> representing the asynchronous operation</returns>
86+ /// <returns>A <see cref="Task"/> representing the asynchronous operation. </returns>
14287 public async Task < ImmutableList < StyleCopDiagnostic > > GetDiagnosticsAsync ( )
14388 {
14489 var diagnostics = ImmutableList . CreateBuilder < StyleCopDiagnostic > ( ) ;
@@ -228,6 +173,63 @@ private static bool HasImplementation(SyntaxNode syntaxRoot)
228173 return hasImplementation ;
229174 }
230175
176+ private async Task InitializeAsync ( )
177+ {
178+ this . solution = await this . workspace . OpenSolutionAsync ( this . SlnPath ) ;
179+
180+ this . analyzerProject = this . solution . Projects . Single ( x => x . Name == this . AnalyzerProjectName ) ;
181+ this . analyzerCompilation = await this . analyzerProject . GetCompilationAsync ( ) ;
182+ this . analyzerCompilation = this . analyzerCompilation . WithOptions ( this . analyzerCompilation . Options . WithOutputKind ( OutputKind . DynamicallyLinkedLibrary ) ) ;
183+
184+ this . codeFixProject = this . solution . Projects . Single ( x => x . Name == this . CodeFixProjectName ) ;
185+ this . codeFixCompilation = await this . codeFixProject . GetCompilationAsync ( ) ;
186+ this . codeFixCompilation = this . codeFixCompilation . WithOptions ( this . codeFixCompilation . Options . WithOutputKind ( OutputKind . DynamicallyLinkedLibrary ) ) ;
187+
188+ this . booleanType = this . analyzerCompilation . GetSpecialType ( SpecialType . System_Boolean ) ;
189+
190+ this . Compile ( ) ;
191+
192+ this . noCodeFixAttributeTypeSymbol = this . analyzerCompilation . GetTypeByMetadataName ( "StyleCop.Analyzers.NoCodeFixAttribute" ) ;
193+ this . diagnosticAnalyzerTypeSymbol = this . analyzerCompilation . GetTypeByMetadataName ( typeof ( DiagnosticAnalyzer ) . FullName ) ;
194+
195+ this . InitializeCodeFixTypes ( ) ;
196+ }
197+
198+ private void InitializeCodeFixTypes ( )
199+ {
200+ var codeFixTypes = this . codeFixAssembly . GetTypes ( ) . Where ( x => x . FullName . EndsWith ( "CodeFixProvider" ) ) ;
201+
202+ this . CodeFixProviders = ImmutableArray . Create (
203+ codeFixTypes
204+ . Select ( t => Activator . CreateInstance ( t , true ) )
205+ . OfType < CodeFixProvider > ( )
206+ . Where ( x => x != null )
207+ . Where ( x => x . GetType ( ) . Name != "SettingsFileCodeFixProvider" )
208+ . ToArray ( ) ) ;
209+ }
210+
211+ private void Compile ( )
212+ {
213+ string path = Path . Combine ( Path . GetDirectoryName ( this . SlnPath ) , this . AnalyzerProjectName ) ;
214+ this . analyzerAssembly = this . GetAssembly ( this . analyzerCompilation , ResourceReader . GetResourcesRecursive ( path ) ) ;
215+
216+ this . codeFixAssembly = this . GetAssembly ( this . codeFixCompilation ) ;
217+ }
218+
219+ private Assembly GetAssembly ( Compilation compilation , IEnumerable < ResourceDescription > manifestResources = null )
220+ {
221+ MemoryStream memStream = new MemoryStream ( ) ;
222+
223+ var emitResult = compilation . Emit ( memStream , manifestResources : manifestResources ) ;
224+
225+ if ( ! emitResult . Success )
226+ {
227+ throw new CompilationFailedException ( ) ;
228+ }
229+
230+ return Assembly . Load ( memStream . ToArray ( ) ) ;
231+ }
232+
231233 private string GetStatus ( INamedTypeSymbol classSymbol , SyntaxNode root , SemanticModel model , DiagnosticDescriptor descriptor )
232234 {
233235 // Some analyzers use multiple descriptors. We analyze the first one and hope that
0 commit comments