Skip to content

Commit

Permalink
chore: use async in tool (#1154)
Browse files Browse the repository at this point in the history
Co-authored-by: Darío Kondratiuk <dariokondratiuk@gmail.com>
  • Loading branch information
SimonCropp and kblok authored Mar 1, 2021
1 parent b19ae06 commit c7cc90c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/tools/PlaywrightSharp.Tooling/ApiChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using PlaywrightSharp.Tooling.Extensions;
using PlaywrightSharp.Tooling.Models.Api;
using PlaywrightSharp.Tooling.Models.Mismatch;
Expand All @@ -21,12 +22,13 @@ internal class ApiChecker

public string AssemblyPath { get; set; }

public bool Execute()
public async Task<bool> ExecuteAsync()
{
var assembly = Assembly.LoadFrom(AssemblyPath);

var report = new StringBuilder("<html><body><ul>");
string json = File.ReadAllText(Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "api.json"));
string json = await File.ReadAllTextAsync(Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "api.json")).ConfigureAwait(false);

var api = JsonSerializer.Deserialize<PlaywrightEntity[]>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand All @@ -37,7 +39,7 @@ public bool Execute()
});

string mismatchJsonFile = Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "expected_api_mismatch.json");
string mismatchJson = File.ReadAllText(mismatchJsonFile);
string mismatchJson = await File.ReadAllTextAsync(mismatchJsonFile).ConfigureAwait(false);
Mismatch mismatches;

try
Expand All @@ -58,19 +60,22 @@ public bool Execute()
}

report.Append("</ul></body></html>");
File.WriteAllText(
await File.WriteAllTextAsync(
Path.Combine(BasePath, "src", "PlaywrightSharp", "Drivers", "report.html"),
report.ToString());
report.ToString()).ConfigureAwait(false);

return true;
}

internal static void Run(ApiCheckerOptions o)
=> new ApiChecker
internal static Task RunAsync(ApiCheckerOptions o)
{
ApiChecker apiChecker = new ApiChecker
{
BasePath = o.BasePath,
AssemblyPath = Path.Combine(o.BasePath, "src", "PlaywrightSharp", "bin", "Debug", "net5.0", "PlaywrightSharp.dll"),
}.Execute();
};
return apiChecker.ExecuteAsync();
}

private static string TranslateMethodName(string memberName)
=> memberName
Expand Down
6 changes: 4 additions & 2 deletions src/tools/PlaywrightSharp.Tooling/DriverDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ private static async Task UpdateBrowserVersionsAsync(string basePath, string dri

string readme = await GetUpstreamReadmeAsync(playwrightVersion).ConfigureAwait(false);
var browserMatches = regex.Matches(readme);
File.WriteAllText(readmePath, ReplaceBrowserVersion(File.ReadAllText(readmePath), browserMatches));
File.WriteAllText(readmeInDocsPath, ReplaceBrowserVersion(File.ReadAllText(readmeInDocsPath), browserMatches));
string readmeText = await File.ReadAllTextAsync(readmePath).ConfigureAwait(false);
await File.WriteAllTextAsync(readmePath, ReplaceBrowserVersion(readmeText, browserMatches)).ConfigureAwait(false);
string readmeInDicsText = await File.ReadAllTextAsync(readmeInDocsPath).ConfigureAwait(false);
await File.WriteAllTextAsync(readmeInDocsPath, ReplaceBrowserVersion(readmeInDicsText, browserMatches)).ConfigureAwait(false);
}

private static string ReplaceBrowserVersion(string content, MatchCollection browserMatches)
Expand Down
14 changes: 6 additions & 8 deletions src/tools/PlaywrightSharp.Tooling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ namespace PlaywrightSharp.Tooling
{
internal static class Program
{
internal static void Main(string[] args)
internal static async Task Main(string[] args)
{
Parser.Default.ParseArguments<ScaffoldTestOptions, IdentifyMissingTestsOptions, DownloadDriversOptions, ApiCheckerOptions>(args)
.WithParsed<ScaffoldTestOptions>(o => ScaffoldTest.Run(o))
.WithParsed<IdentifyMissingTestsOptions>(o => IdentifyMissingTests.Run(o))
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
.WithParsed<DownloadDriversOptions>(o => DriverDownloader.RunAsync(o).GetAwaiter().GetResult())
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
.WithParsed<ApiCheckerOptions>(o => ApiChecker.Run(o));
ParserResult<object> result = Parser.Default.ParseArguments<ScaffoldTestOptions, IdentifyMissingTestsOptions, DownloadDriversOptions, ApiCheckerOptions>(args);
result.WithParsed<ScaffoldTestOptions>(ScaffoldTest.Run);
result.WithParsed<IdentifyMissingTestsOptions>(IdentifyMissingTests.Run);
await result.WithParsedAsync<DownloadDriversOptions>(DriverDownloader.RunAsync).ConfigureAwait(false);
await result.WithParsedAsync<ApiCheckerOptions>(ApiChecker.RunAsync).ConfigureAwait(false);
}
}
}

0 comments on commit c7cc90c

Please sign in to comment.