55using Microsoft . CodeAnalysis . CSharp ;
66using System . Linq ;
77using System . Text ;
8+ using NLog ;
89
910namespace IncrementalCompiler
1011{
1112 public class Compiler
1213 {
14+ private Logger _logger = LogManager . GetLogger ( "Compiler" ) ;
1315 private CSharpCompilation _compilation ;
14- private CompilerOptions _options ;
16+ private CompileOptions _options ;
1517 private FileTimeList _referenceFileList ;
1618 private FileTimeList _sourceFileList ;
1719 private Dictionary < string , MetadataReference > _referenceMap ;
1820 private Dictionary < string , SyntaxTree > _sourceMap ;
1921
20- public void Build ( CompilerOptions options )
22+ public CompileResult Build ( CompileOptions options )
2123 {
2224 if ( _compilation == null ||
2325 _options . AssemblyName != options . AssemblyName ||
2426 _options . Output != options . Output ||
2527 Enumerable . SequenceEqual ( _options . Defines , options . Defines ) == false )
2628 {
27- BuildFull ( options ) ;
29+ return BuildFull ( options ) ;
2830 }
2931 else
3032 {
31- BuildIncremental ( options ) ;
33+ return BuildIncremental ( options ) ;
3234 }
3335 }
3436
35- private void BuildFull ( CompilerOptions options )
37+ private CompileResult BuildFull ( CompileOptions options )
3638 {
39+ var result = new CompileResult ( ) ;
40+
3741 _options = options ;
3842
3943 _referenceFileList = new FileTimeList ( ) ;
@@ -57,11 +61,15 @@ private void BuildFull(CompilerOptions options)
5761 references : _referenceMap . Values ,
5862 options : new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
5963
60- Emit ( ) ;
64+ Emit ( result ) ;
65+
66+ return result ;
6167 }
6268
63- private void BuildIncremental ( CompilerOptions options )
69+ private CompileResult BuildIncremental ( CompileOptions options )
6470 {
71+ var result = new CompileResult ( ) ;
72+
6573 _options = options ;
6674
6775 // TODO: guard failure of compilation, ...
@@ -111,7 +119,9 @@ private void BuildIncremental(CompilerOptions options)
111119 _sourceMap . Remove ( file ) ;
112120 }
113121
114- Emit ( ) ;
122+ Emit ( result ) ;
123+
124+ return result ;
115125 }
116126
117127 private MetadataReference CreateReference ( string file )
@@ -127,30 +137,29 @@ private SyntaxTree ParseSource(string file, CSharpParseOptions parseOption)
127137 Encoding . UTF8 ) ;
128138 }
129139
130- private void Emit ( )
140+ private void Emit ( CompileResult result )
131141 {
132142 using ( var peStream = new FileStream ( _options . Output , FileMode . Create ) )
133143 using ( var pdbStream = new FileStream ( Path . ChangeExtension ( _options . Output , ".pdb" ) , FileMode . Create ) )
134144 {
135- var result = _compilation . Emit ( peStream , pdbStream ) ;
145+ var r = _compilation . Emit ( peStream , pdbStream ) ;
136146
137- if ( ! result . Success )
147+ foreach ( var d in r . Diagnostics )
138148 {
139- var failures = result . Diagnostics . Where ( diagnostic =>
140- diagnostic . IsWarningAsError ||
141- diagnostic . Severity == DiagnosticSeverity . Error ) ;
142-
143- foreach ( var diagnostic in failures )
144- {
145- var line = diagnostic . Location . GetLineSpan ( ) ;
146- Console . Error . WriteLine ( "{0}({1}): {2} {3}" ,
147- line . Path ,
148- line . StartLinePosition . Line + 1 ,
149- diagnostic . Id ,
150- diagnostic . GetMessage ( ) ) ;
151- }
149+ if ( d . Severity == DiagnosticSeverity . Warning && d . IsWarningAsError == false )
150+ result . Warnings . Add ( GetDiagnosticString ( d ) ) ;
151+ else if ( d . Severity == DiagnosticSeverity . Error || d . IsWarningAsError )
152+ result . Errors . Add ( GetDiagnosticString ( d ) ) ;
152153 }
154+
155+ result . Succeeded = r . Success ;
153156 }
154157 }
158+
159+ private static string GetDiagnosticString ( Diagnostic diagnostic )
160+ {
161+ var line = diagnostic . Location . GetLineSpan ( ) ;
162+ return $ "{ line . Path } ({ line . StartLinePosition . Line + 1 } ): { diagnostic . Id } { diagnostic . GetMessage ( ) } ";
163+ }
155164 }
156165}
0 commit comments