11
11
using System . Xml . Linq ;
12
12
using MagicOnion . Utils ;
13
13
using Buildalyzer ;
14
+ using Buildalyzer . Environment ;
14
15
using Microsoft . Build . Utilities ;
15
16
using Microsoft . Build . Framework ;
17
+ using MsLogging = Microsoft . Extensions . Logging ;
16
18
17
19
namespace MagicOnion
18
20
{
@@ -29,6 +31,8 @@ static LoggerVerbosity ToLoggerVerbosity(this int value)
29
31
return LoggerVerbosity . Normal ;
30
32
case 3 :
31
33
return LoggerVerbosity . Detailed ;
34
+ case 4 :
35
+ return LoggerVerbosity . Diagnostic ;
32
36
case 0 :
33
37
default :
34
38
return LoggerVerbosity . Quiet ;
@@ -77,10 +81,16 @@ static LanguageVersion ConvertLanguageVersion(string versionString)
77
81
return LanguageVersion . Default ;
78
82
}
79
83
}
84
+
80
85
public static async Task < Compilation > GetCompilationFromProject ( string csprojPath , int verbosityLevel ,
81
86
Dictionary < string , string > additionalProperties ,
82
87
IEnumerable < string > conditionalSymbols )
83
88
{
89
+ conditionalSymbols = conditionalSymbols != null ?
90
+ conditionalSymbols . Where ( x => ! string . IsNullOrEmpty ( x ) ) . ToArray ( )
91
+ :
92
+ Enumerable . Empty < string > ( ) ;
93
+
84
94
// fucking workaround of resolve reference...
85
95
var externalReferences = new List < PortableExecutableReference > ( ) ;
86
96
{
@@ -119,57 +129,45 @@ public static async Task<Compilation> GetCompilationFromProject(string csprojPat
119
129
120
130
EnvironmentHelper . Setup ( ) ;
121
131
var analyzerOptions = new AnalyzerManagerOptions ( ) ;
122
- analyzerOptions . LoggerVerbosity = verbosityLevel . ToLoggerVerbosity ( ) ;
123
132
if ( verbosityLevel > 0 )
124
133
{
125
134
analyzerOptions . LogWriter = Console . Out ;
126
135
}
127
136
var manager = new AnalyzerManager ( analyzerOptions ) ;
128
137
var projectAnalyzer = manager . GetProject ( csprojPath ) ;
138
+ projectAnalyzer . AddBuildLogger ( new Microsoft . Build . Logging . ConsoleLogger ( verbosityLevel . ToLoggerVerbosity ( ) ) ) ;
139
+ var buildopts = new EnvironmentOptions ( ) ;
129
140
if ( additionalProperties != null )
130
141
{
131
142
foreach ( var kv in additionalProperties )
132
143
{
144
+ buildopts . GlobalProperties [ kv . Key ] = kv . Value ;
133
145
projectAnalyzer . SetGlobalProperty ( kv . Key , kv . Value ) ;
134
146
}
135
147
}
136
- var compiledProject = projectAnalyzer . Build ( ) . Project ;
137
- var ws = new AdhocWorkspace ( ) ;
138
- if ( compiledProject == null )
148
+ if ( conditionalSymbols . Any ( ) )
139
149
{
140
- throw new Exception ( "project compilation failed" ) ;
150
+ buildopts . GlobalProperties [ "DefineConstants" ] = string . Join ( "%3b" , conditionalSymbols ) ;
141
151
}
142
-
143
- var workspace = projectAnalyzer . GetWorkspace ( ) ;
144
- workspace . WorkspaceFailed += WorkSpaceFailed ;
145
- var project = workspace . CurrentSolution . Projects . First ( ) ;
146
- project = project . AddMetadataReferences ( externalReferences ) ;
147
- var opt = project . ParseOptions as CSharpParseOptions ;
148
- if ( opt != null )
152
+ var analyzerResults = projectAnalyzer . Build ( buildopts ) ;
153
+ var analyzerResult = analyzerResults . FirstOrDefault ( x => x . Succeeded ) ;
154
+ if ( analyzerResult == null )
149
155
{
150
- var defineconstants = compiledProject . GetPropertyValue ( "DefineConstants" ) . Split ( ';' ) ;
151
- var langVersion = compiledProject . GetPropertyValue ( "LangVersion" ) ;
152
- var features = compiledProject . GetPropertyValue ( "Features" ) . Split ( ';' ) . Select ( x =>
153
- {
154
- var kv = x . Split ( '=' , 2 ) ;
155
- if ( kv . Length == 1 )
156
- {
157
- return new KeyValuePair < string , string > ( kv [ 0 ] , "true" ) ;
158
- }
159
- else if ( kv . Length > 1 )
160
- {
161
- return new KeyValuePair < string , string > ( kv [ 0 ] , kv [ 1 ] ) ;
162
- }
163
- else
164
- {
165
- return new KeyValuePair < string , string > ( null , null ) ;
166
- }
167
- } ) . Where ( x => x . Key != null ) . ToArray ( ) ;
168
- opt = opt . WithLanguageVersion ( ConvertLanguageVersion ( langVersion ) )
169
- . WithPreprocessorSymbols ( defineconstants . Concat ( conditionalSymbols ) )
170
- . WithFeatures ( features )
156
+ throw new Exception ( "no succeeded analyzer result found" ) ;
157
+ }
158
+ var ws = new AdhocWorkspace ( ) ;
159
+ var project = analyzerResult . AddToWorkspace ( ws ) ;
160
+ var parseopts = project . ParseOptions as CSharpParseOptions ;
161
+ if ( parseopts != null )
162
+ {
163
+ var symbols = analyzerResult . Properties . ContainsKey ( "DefineConstants" ) ?
164
+ conditionalSymbols . Concat (
165
+ analyzerResult . Properties [ "DefineConstants" ] . Split ( ';' )
166
+ ) . OrderBy ( x => x ) . Distinct ( )
167
+ :
168
+ conditionalSymbols
171
169
;
172
- project = project . WithParseOptions ( opt ) ;
170
+ project = project . WithParseOptions ( parseopts . WithPreprocessorSymbols ( symbols ) ) ;
173
171
}
174
172
var compilation = await project . GetCompilationAsync ( ) . ConfigureAwait ( false ) ;
175
173
return compilation ;
0 commit comments