Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 0641357

Browse files
Corniel Nobelamis92
authored andcommitted
fix: use async/await instead of .GetWaiter().GetResult() (#176)
1 parent edf706b commit 0641357

5 files changed

Lines changed: 63 additions & 56 deletions

File tree

src/CodeGeneration.Roslyn.Engine/CompilationGenerator.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class CompilationGenerator
6565
public string IntermediateOutputDirectory { get; set; }
6666

6767
/// <summary>
68-
/// Gets the set of files generated after <see cref="Generate"/> is invoked.
68+
/// Gets the set of files generated after <see cref="GenerateAsync"/> is invoked.
6969
/// </summary>
7070
public IEnumerable<string> GeneratedFiles => this.generatedFiles;
7171

@@ -106,7 +106,8 @@ public CompilationGenerator()
106106
/// </summary>
107107
/// <param name="progress">Optional handler of diagnostics provided by code generator.</param>
108108
/// <param name="cancellationToken">Cancellation token to interrupt async operations.</param>
109-
public void Generate(IProgress<Diagnostic> progress = null, CancellationToken cancellationToken = default)
109+
/// <returns>A <see cref="Task.CompletedTask"/>.</returns>
110+
public async Task GenerateAsync(IProgress<Diagnostic> progress = null, CancellationToken cancellationToken = default)
110111
{
111112
Verify.Operation(this.Compile != null, $"{nameof(Compile)} must be set first.");
112113
Verify.Operation(this.ReferencePath != null, $"{nameof(ReferencePath)} must be set first.");
@@ -142,12 +143,12 @@ public void Generate(IProgress<Diagnostic> progress = null, CancellationToken ca
142143
{
143144
try
144145
{
145-
var generatedSyntaxTree = DocumentTransform.TransformAsync(
146+
var generatedSyntaxTree = await DocumentTransform.TransformAsync(
146147
compilation,
147148
inputSyntaxTree,
148149
this.ProjectDirectory,
149150
this.LoadAssembly,
150-
progress).GetAwaiter().GetResult();
151+
progress);
151152

152153
var outputText = generatedSyntaxTree.GetText(cancellationToken);
153154
using (var outputFileStream = File.OpenWrite(outputFilePath))
@@ -160,12 +161,15 @@ public void Generate(IProgress<Diagnostic> progress = null, CancellationToken ca
160161
outputFileStream.SetLength(outputFileStream.Position);
161162
}
162163

163-
bool anyTypesGenerated = generatedSyntaxTree?.GetRoot(cancellationToken).DescendantNodes().OfType<TypeDeclarationSyntax>().Any() ?? false;
164-
if (!anyTypesGenerated)
164+
if (!(generatedSyntaxTree is null))
165165
{
166-
this.emptyGeneratedFiles.Add(outputFilePath);
166+
var root = await generatedSyntaxTree.GetRootAsync(cancellationToken);
167+
bool anyTypesGenerated = root.DescendantNodes().OfType<TypeDeclarationSyntax>().Any();
168+
if (!anyTypesGenerated)
169+
{
170+
this.emptyGeneratedFiles.Add(outputFilePath);
171+
}
167172
}
168-
169173
break;
170174
}
171175
catch (IOException ex) when (ex.HResult == ProcessCannotAccessFileHR && retriesLeft > 0)

src/CodeGeneration.Roslyn.Engine/DocumentTransform.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public static async Task<SyntaxTree> TransformAsync(
7070
var emittedAttributeLists = ImmutableArray<AttributeListSyntax>.Empty;
7171
var emittedMembers = ImmutableArray<MemberDeclarationSyntax>.Empty;
7272

73-
var memberNodes = inputDocument
74-
.GetRoot()
73+
var root = await inputDocument.GetRootAsync();
74+
var memberNodes = root
7575
.DescendantNodesAndSelf(n => n is CompilationUnitSyntax || n is NamespaceDeclarationSyntax || n is TypeDeclarationSyntax)
7676
.OfType<CSharpSyntaxNode>();
7777

src/CodeGeneration.Roslyn.Tests/DocumentTransformTests.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
// Copyright (c) Andrew Arnott. All rights reserved.
22
// Licensed under the MS-PL license. See LICENSE.txt file in the project root for full license information.
33

4+
using System.Threading.Tasks;
45
using Xunit;
56

67
public class DocumentTransformTests : CompilationTestsBase
78
{
89
[Fact]
9-
public void EmptyFile_NoGenerators()
10+
public async Task EmptyFile_NoGenerators()
1011
{
11-
AssertGeneratedAsExpected("", "");
12+
await AssertGeneratedAsExpected("", "");
1213
}
1314

1415
[Fact]
15-
public void Usings_WhenNoCode_CopiedToOutput()
16+
public async Task Usings_WhenNoCode_CopiedToOutput()
1617
{
1718
const string usings = "using System;";
18-
AssertGeneratedAsExpected(usings, usings);
19+
await AssertGeneratedAsExpected(usings, usings);
1920
}
2021

2122
[Fact]
22-
public void AncestorTree_IsBuiltProperly()
23+
public async Task AncestorTree_IsBuiltProperly()
2324
{
2425
const string source = @"
2526
using System;
@@ -66,11 +67,11 @@ partial struct InnerStruct<T1, T2>
6667
}
6768
}
6869
}";
69-
AssertGeneratedAsExpected(source, generated);
70+
await AssertGeneratedAsExpected(source, generated);
7071
}
7172

7273
[Fact]
73-
public void DefineDirective_Dropped()
74+
public async Task DefineDirective_Dropped()
7475
{
7576
// define directives must be leading any other tokens to be valid in C#
7677
const string source = @"
@@ -80,11 +81,11 @@ public void DefineDirective_Dropped()
8081
const string generated = @"
8182
using System;
8283
using System.Linq;";
83-
AssertGeneratedAsExpected(source, generated);
84+
await AssertGeneratedAsExpected(source, generated);
8485
}
8586

8687
[Fact]
87-
public void Comment_BetweenUsings_Dropped()
88+
public async Task Comment_BetweenUsings_Dropped()
8889
{
8990
const string source = @"
9091
using System;
@@ -93,11 +94,11 @@ public void Comment_BetweenUsings_Dropped()
9394
const string generated = @"
9495
using System;
9596
using System.Linq;";
96-
AssertGeneratedAsExpected(source, generated);
97+
await AssertGeneratedAsExpected(source, generated);
9798
}
9899

99100
[Fact]
100-
public void Region_TrailingUsings_Dropped()
101+
public async Task Region_TrailingUsings_Dropped()
101102
{
102103
const string source = @"
103104
using System;
@@ -107,11 +108,11 @@ public void Region_TrailingUsings_Dropped()
107108
const string generated = @"
108109
using System;
109110
using System.Linq;";
110-
AssertGeneratedAsExpected(source, generated);
111+
await AssertGeneratedAsExpected(source, generated);
111112
}
112113

113114
[Fact]
114-
public void IfElseDirective_OnUsings_InactiveUsingAndDirectives_Dropped()
115+
public async Task IfElseDirective_OnUsings_InactiveUsingAndDirectives_Dropped()
115116
{
116117
const string source = @"
117118
using System;
@@ -134,11 +135,11 @@ partial class Empty {}";
134135
partial class Empty
135136
{
136137
}";
137-
AssertGeneratedAsExpected(source, generated);
138+
await AssertGeneratedAsExpected(source, generated);
138139
}
139140

140141
[Fact]
141-
public void RegionDirective_InsideClass_Dropped()
142+
public async Task RegionDirective_InsideClass_Dropped()
142143
{
143144
const string source = @"
144145
using System;
@@ -158,11 +159,11 @@ partial class Empty
158159
partial class Empty
159160
{
160161
}";
161-
AssertGeneratedAsExpected(source, generated);
162+
await AssertGeneratedAsExpected(source, generated);
162163
}
163164

164165
[Fact]
165-
public void RegionDirective_InsideStruct_Dropped()
166+
public async Task RegionDirective_InsideStruct_Dropped()
166167
{
167168
const string source = @"
168169
using System;
@@ -182,11 +183,11 @@ partial struct Empty
182183
partial struct Empty
183184
{
184185
}";
185-
AssertGeneratedAsExpected(source, generated);
186+
await AssertGeneratedAsExpected(source, generated);
186187
}
187188

188189
[Fact]
189-
public void RegionDirective_InsideNamespace_Dropped()
190+
public async Task RegionDirective_InsideNamespace_Dropped()
190191
{
191192
const string source = @"
192193
using System;
@@ -209,11 +210,11 @@ partial class Empty
209210
{
210211
}
211212
}";
212-
AssertGeneratedAsExpected(source, generated);
213+
await AssertGeneratedAsExpected(source, generated);
213214
}
214215

215216
[Fact]
216-
public void Class_Modifiers_ArePreserved_WithoutTrivia()
217+
public async Task Class_Modifiers_ArePreserved_WithoutTrivia()
217218
{
218219
const string source = @"
219220
using System;
@@ -238,11 +239,11 @@ public static partial class Empty
238239
{
239240
}
240241
}";
241-
AssertGeneratedAsExpected(source, generated);
242+
await AssertGeneratedAsExpected(source, generated);
242243
}
243244

244245
[Fact]
245-
public void Struct_Modifiers_ArePreserved_WithoutTrivia()
246+
public async Task Struct_Modifiers_ArePreserved_WithoutTrivia()
246247
{
247248
const string source = @"
248249
using System;
@@ -267,11 +268,11 @@ internal partial struct Empty
267268
{
268269
}
269270
}";
270-
AssertGeneratedAsExpected(source, generated);
271+
await AssertGeneratedAsExpected(source, generated);
271272
}
272273

273274
[Fact]
274-
public void Class_TypeParameters_ArePreserved()
275+
public async Task Class_TypeParameters_ArePreserved()
275276
{
276277
const string source = @"
277278
using System;
@@ -295,11 +296,11 @@ partial class Empty<T>
295296
{
296297
}
297298
}";
298-
AssertGeneratedAsExpected(source, generated);
299+
await AssertGeneratedAsExpected(source, generated);
299300
}
300301

301302
[Fact]
302-
public void Struct_TypeParameters_ArePreserved()
303+
public async Task Struct_TypeParameters_ArePreserved()
303304
{
304305
const string source = @"
305306
using System;
@@ -323,11 +324,11 @@ partial struct Empty<T>
323324
{
324325
}
325326
}";
326-
AssertGeneratedAsExpected(source, generated);
327+
await AssertGeneratedAsExpected(source, generated);
327328
}
328329

329330
[Fact]
330-
public void RichGenerator_Wraps_InOtherNamespace()
331+
public async Task RichGenerator_Wraps_InOtherNamespace()
331332
{
332333
const string source = @"
333334
using System;
@@ -350,11 +351,11 @@ class Something
350351
{
351352
}
352353
}";
353-
AssertGeneratedAsExpected(source, generated);
354+
await AssertGeneratedAsExpected(source, generated);
354355
}
355356

356357
[Fact]
357-
public void RichGenerator_Adds_Using()
358+
public async Task RichGenerator_Adds_Using()
358359
{
359360
const string source = @"
360361
using System;
@@ -373,11 +374,11 @@ partial class Something
373374
using System.Collections.Generic;
374375
375376
";
376-
AssertGeneratedAsExpected(source, generated);
377+
await AssertGeneratedAsExpected(source, generated);
377378
}
378379

379380
[Fact]
380-
public void RichGenerator_Adds_ExternAlias()
381+
public async Task RichGenerator_Adds_ExternAlias()
381382
{
382383
const string source = @"
383384
using System;
@@ -397,11 +398,11 @@ partial class Something
397398
using CodeGeneration.Roslyn.Tests.Generators;
398399
399400
";
400-
AssertGeneratedAsExpected(source, generated);
401+
await AssertGeneratedAsExpected(source, generated);
401402
}
402403

403404
[Fact]
404-
public void RichGenerator_Adds_Attribute()
405+
public async Task RichGenerator_Adds_Attribute()
405406
{
406407
const string source = @"
407408
using System;
@@ -420,11 +421,11 @@ partial class Something
420421
421422
[GeneratedAttribute]
422423
";
423-
AssertGeneratedAsExpected(source, generated);
424+
await AssertGeneratedAsExpected(source, generated);
424425
}
425426

426427
[Fact]
427-
public void RichGenerator_Appends_MultipleResults()
428+
public async Task RichGenerator_Appends_MultipleResults()
428429
{
429430
const string source = @"
430431
using System;
@@ -469,6 +470,6 @@ class Something
469470
}
470471
}
471472
";
472-
AssertGeneratedAsExpected(source, generated);
473+
await AssertGeneratedAsExpected(source, generated);
473474
}
474475
}

src/CodeGeneration.Roslyn.Tests/Helpers/CompilationTestsBase.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;
9+
using System.Threading.Tasks;
910
using CodeGeneration.Roslyn;
1011
using CodeGeneration.Roslyn.Engine;
1112
using CodeGeneration.Roslyn.Tests.Generators;
@@ -54,9 +55,9 @@ static CompilationTestsBase()
5455

5556
internal static readonly ImmutableArray<MetadataReference> MetadataReferences;
5657

57-
protected static void AssertGeneratedAsExpected(string source, string expected)
58+
protected static async Task AssertGeneratedAsExpected(string source, string expected)
5859
{
59-
var generatedTree = Generate(source);
60+
var generatedTree = await GenerateAsync(source);
6061
// normalize line endings to just LF
6162
var generatedText = NormalizeToLf(generatedTree.GetText().ToString());
6263
// and append preamble to the expected
@@ -69,15 +70,15 @@ protected static string NormalizeToLf(string input)
6970
return input?.Replace(CrLf, Lf);
7071
}
7172

72-
protected static SyntaxTree Generate(string source)
73+
protected static async Task<SyntaxTree> GenerateAsync(string source)
7374
{
7475
var document = CreateProject(source).Documents.Single();
75-
var tree = document.GetSyntaxTreeAsync().GetAwaiter().GetResult();
76-
var compilation = (CSharpCompilation)document.Project.GetCompilationAsync().GetAwaiter().GetResult();
76+
var tree = await document.GetSyntaxTreeAsync();
77+
var compilation = (CSharpCompilation)(await document.Project.GetCompilationAsync());
7778
var diagnostics = compilation.GetDiagnostics();
7879
Assert.Empty(diagnostics.Where(x => x.Severity >= DiagnosticSeverity.Warning));
7980
var progress = new Progress<Diagnostic>();
80-
var result = DocumentTransform.TransformAsync(compilation, tree, null, Assembly.Load, progress).GetAwaiter().GetResult();
81+
var result = await DocumentTransform.TransformAsync(compilation, tree, null, Assembly.Load, progress);
8182
return result;
8283
}
8384

src/CodeGeneration.Roslyn.Tool/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ namespace CodeGeneration.Roslyn.Generate
66
using System;
77
using System.Collections.Generic;
88
using System.IO;
9+
using System.Threading.Tasks;
910
using CodeGeneration.Roslyn.Engine;
1011
using CodeGeneration.Roslyn.Tool.CommandLine;
1112
using Microsoft.CodeAnalysis;
1213

1314
internal static class Program
1415
{
15-
private static int Main(string[] args)
16+
private static async Task<int> Main(string[] args)
1617
{
1718
IReadOnlyList<string> compile = Array.Empty<string>();
1819
IReadOnlyList<string> refs = Array.Empty<string>();
@@ -65,7 +66,7 @@ private static int Main(string[] args)
6566

6667
try
6768
{
68-
generator.Generate(progress);
69+
await generator.GenerateAsync(progress);
6970
}
7071
catch (Exception e)
7172
{

0 commit comments

Comments
 (0)