Skip to content

Commit c7cc90c

Browse files
SimonCroppkblok
andauthored
chore: use async in tool (#1154)
Co-authored-by: Darío Kondratiuk <[email protected]>
1 parent b19ae06 commit c7cc90c

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Diff for: src/tools/PlaywrightSharp.Tooling/ApiChecker.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text.Json;
99
using System.Text.Json.Serialization;
1010
using System.Text.RegularExpressions;
11+
using System.Threading.Tasks;
1112
using PlaywrightSharp.Tooling.Extensions;
1213
using PlaywrightSharp.Tooling.Models.Api;
1314
using PlaywrightSharp.Tooling.Models.Mismatch;
@@ -21,12 +22,13 @@ internal class ApiChecker
2122

2223
public string AssemblyPath { get; set; }
2324

24-
public bool Execute()
25+
public async Task<bool> ExecuteAsync()
2526
{
2627
var assembly = Assembly.LoadFrom(AssemblyPath);
2728

2829
var report = new StringBuilder("<html><body><ul>");
29-
string json = File.ReadAllText(Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "api.json"));
30+
string json = await File.ReadAllTextAsync(Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "api.json")).ConfigureAwait(false);
31+
3032
var api = JsonSerializer.Deserialize<PlaywrightEntity[]>(json, new JsonSerializerOptions
3133
{
3234
PropertyNameCaseInsensitive = true,
@@ -37,7 +39,7 @@ public bool Execute()
3739
});
3840

3941
string mismatchJsonFile = Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "expected_api_mismatch.json");
40-
string mismatchJson = File.ReadAllText(mismatchJsonFile);
42+
string mismatchJson = await File.ReadAllTextAsync(mismatchJsonFile).ConfigureAwait(false);
4143
Mismatch mismatches;
4244

4345
try
@@ -58,19 +60,22 @@ public bool Execute()
5860
}
5961

6062
report.Append("</ul></body></html>");
61-
File.WriteAllText(
63+
await File.WriteAllTextAsync(
6264
Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "report.html"),
63-
report.ToString());
65+
report.ToString()).ConfigureAwait(false);
6466

6567
return true;
6668
}
6769

68-
internal static void Run(ApiCheckerOptions o)
69-
=> new ApiChecker
70+
internal static Task RunAsync(ApiCheckerOptions o)
71+
{
72+
ApiChecker apiChecker = new ApiChecker
7073
{
7174
BasePath = o.BasePath,
7275
AssemblyPath = Path.Combine(o.BasePath, "src", "PlaywrightSharp", "bin", "Debug", "net5.0", "PlaywrightSharp.dll"),
73-
}.Execute();
76+
};
77+
return apiChecker.ExecuteAsync();
78+
}
7479

7580
private static string TranslateMethodName(string memberName)
7681
=> memberName

Diff for: src/tools/PlaywrightSharp.Tooling/DriverDownloader.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ private static async Task UpdateBrowserVersionsAsync(string basePath, string dri
6666

6767
string readme = await GetUpstreamReadmeAsync(playwrightVersion).ConfigureAwait(false);
6868
var browserMatches = regex.Matches(readme);
69-
File.WriteAllText(readmePath, ReplaceBrowserVersion(File.ReadAllText(readmePath), browserMatches));
70-
File.WriteAllText(readmeInDocsPath, ReplaceBrowserVersion(File.ReadAllText(readmeInDocsPath), browserMatches));
69+
string readmeText = await File.ReadAllTextAsync(readmePath).ConfigureAwait(false);
70+
await File.WriteAllTextAsync(readmePath, ReplaceBrowserVersion(readmeText, browserMatches)).ConfigureAwait(false);
71+
string readmeInDicsText = await File.ReadAllTextAsync(readmeInDocsPath).ConfigureAwait(false);
72+
await File.WriteAllTextAsync(readmeInDocsPath, ReplaceBrowserVersion(readmeInDicsText, browserMatches)).ConfigureAwait(false);
7173
}
7274

7375
private static string ReplaceBrowserVersion(string content, MatchCollection browserMatches)

Diff for: src/tools/PlaywrightSharp.Tooling/Program.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ namespace PlaywrightSharp.Tooling
3232
{
3333
internal static class Program
3434
{
35-
internal static void Main(string[] args)
35+
internal static async Task Main(string[] args)
3636
{
37-
Parser.Default.ParseArguments<ScaffoldTestOptions, IdentifyMissingTestsOptions, DownloadDriversOptions, ApiCheckerOptions>(args)
38-
.WithParsed<ScaffoldTestOptions>(o => ScaffoldTest.Run(o))
39-
.WithParsed<IdentifyMissingTestsOptions>(o => IdentifyMissingTests.Run(o))
40-
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
41-
.WithParsed<DownloadDriversOptions>(o => DriverDownloader.RunAsync(o).GetAwaiter().GetResult())
42-
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
43-
.WithParsed<ApiCheckerOptions>(o => ApiChecker.Run(o));
37+
ParserResult<object> result = Parser.Default.ParseArguments<ScaffoldTestOptions, IdentifyMissingTestsOptions, DownloadDriversOptions, ApiCheckerOptions>(args);
38+
result.WithParsed<ScaffoldTestOptions>(ScaffoldTest.Run);
39+
result.WithParsed<IdentifyMissingTestsOptions>(IdentifyMissingTests.Run);
40+
await result.WithParsedAsync<DownloadDriversOptions>(DriverDownloader.RunAsync).ConfigureAwait(false);
41+
await result.WithParsedAsync<ApiCheckerOptions>(ApiChecker.RunAsync).ConfigureAwait(false);
4442
}
4543
}
4644
}

0 commit comments

Comments
 (0)