@@ -84,7 +84,7 @@ public class CompilationGenerator
8484 /// <param name="progress">Optional handler of diagnostics provided by code generator.</param>
8585 /// <param name="cancellationToken">Cancellation token to interrupt async operations.</param>
8686 /// <returns>A <see cref="Task.CompletedTask"/>.</returns>
87- public async Task GenerateAsync ( IProgress < Diagnostic > progress = null , CancellationToken cancellationToken = default )
87+ public async Task GenerateAsync ( IProgress < Diagnostic > progress , CancellationToken cancellationToken )
8888 {
8989 Verify . Operation ( this . Compile != null , $ "{ nameof ( Compile ) } must be set first.") ;
9090 Verify . Operation ( this . ReferencePath != null , $ "{ nameof ( ReferencePath ) } must be set first.") ;
@@ -104,13 +104,14 @@ public async Task GenerateAsync(IProgress<Diagnostic> progress = null, Cancellat
104104 {
105105 cancellationToken . ThrowIfCancellationRequested ( ) ;
106106
107- string sourceHash = Convert . ToBase64String ( hasher . ComputeHash ( Encoding . UTF8 . GetBytes ( inputSyntaxTree . FilePath ) ) , 0 , 6 ) . Replace ( '/' , '-' ) ;
107+ string sourceHash = Convert . ToBase64String ( hasher . ComputeHash ( Encoding . UTF8 . GetBytes ( inputSyntaxTree . FilePath ) ) , 0 , 6 ) . Replace ( Path . AltDirectorySeparatorChar , '-' ) . Replace ( Path . DirectorySeparatorChar , '-' ) ;
108108 Logger . Info ( $ "File \" { inputSyntaxTree . FilePath } \" hashed to { sourceHash } ") ;
109- string outputFilePath = Path . Combine ( this . IntermediateOutputDirectory , Path . GetFileNameWithoutExtension ( inputSyntaxTree . FilePath ) + $ ".{ sourceHash } .generated.cs") ;
109+ string outputFilePath = Path . Combine ( this . IntermediateOutputDirectory , Path . GetFileNameWithoutExtension ( inputSyntaxTree . FilePath ) + FormattableString . Invariant ( $ ".{ sourceHash } .generated.cs") ) ;
110+ var outputFile = new FileInfo ( outputFilePath ) ;
110111
111112 // Code generation is relatively fast, but it's not free.
112113 // So skip files that haven't changed since we last generated them.
113- DateTime outputLastModified = File . Exists ( outputFilePath ) ? File . GetLastWriteTime ( outputFilePath ) : DateTime . MinValue ;
114+ DateTime outputLastModified = outputFile . Exists ? outputFile . LastWriteTime : DateTime . MinValue ;
114115 if ( File . GetLastWriteTime ( inputSyntaxTree . FilePath ) > outputLastModified || assembliesLastModified > outputLastModified )
115116 {
116117 int retriesLeft = 3 ;
@@ -123,13 +124,13 @@ public async Task GenerateAsync(IProgress<Diagnostic> progress = null, Cancellat
123124 inputSyntaxTree ,
124125 this . ProjectDirectory ,
125126 this . LoadPlugin ,
126- progress ) ;
127-
128- var outputText = generatedSyntaxTree . GetText ( cancellationToken ) ;
129- using ( var outputFileStream = File . OpenWrite ( outputFilePath ) )
127+ progress ,
128+ cancellationToken ) ;
129+ var outputText = await generatedSyntaxTree . GetTextAsync ( cancellationToken ) ;
130+ using ( var outputFileStream = outputFile . OpenWrite ( ) )
130131 using ( var outputWriter = new StreamWriter ( outputFileStream ) )
131132 {
132- outputText . Write ( outputWriter ) ;
133+ outputText . Write ( outputWriter , cancellationToken ) ;
133134
134135 // Truncate any data that may be beyond this point if the file existed previously.
135136 outputWriter . Flush ( ) ;
@@ -142,15 +143,15 @@ public async Task GenerateAsync(IProgress<Diagnostic> progress = null, Cancellat
142143 bool anyTypesGenerated = root . DescendantNodes ( ) . OfType < TypeDeclarationSyntax > ( ) . Any ( ) ;
143144 if ( ! anyTypesGenerated )
144145 {
145- this . emptyGeneratedFiles . Add ( outputFilePath ) ;
146+ this . emptyGeneratedFiles . Add ( outputFile . FullName ) ;
146147 }
147148 }
148149 break ;
149150 }
150151 catch ( IOException ex ) when ( ex . HResult == ProcessCannotAccessFileHR && retriesLeft > 0 )
151152 {
152153 retriesLeft -- ;
153- Task . Delay ( 200 ) . Wait ( ) ;
154+ await Task . Delay ( 200 , cancellationToken ) ;
154155 }
155156 catch ( Exception ex )
156157 {
@@ -162,7 +163,7 @@ public async Task GenerateAsync(IProgress<Diagnostic> progress = null, Cancellat
162163 while ( true ) ;
163164 }
164165
165- this . generatedFiles . Add ( outputFilePath ) ;
166+ this . generatedFiles . Add ( outputFile . FullName ) ;
166167 }
167168 }
168169
@@ -232,10 +233,7 @@ private static void ReportError(IProgress<Diagnostic> progress, string id, Synta
232233
233234 var location = inputSyntaxTree != null ? Location . Create ( inputSyntaxTree , TextSpan . FromBounds ( 0 , 0 ) ) : Location . None ;
234235
235- var messageArgs = new object [ ]
236- {
237- ex ,
238- } ;
236+ var messageArgs = new object [ ] { ex } ;
239237
240238 var reportDiagnostic = Diagnostic . Create ( descriptor , location , messageArgs ) ;
241239
0 commit comments