Skip to content

Commit 70f510b

Browse files
committed
chore: linting tasks conventions
Signed-off-by: Vincent Biret <[email protected]>
1 parent ed36696 commit 70f510b

25 files changed

+138
-136
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.CommandLine.Invocation;
3+
using System.Threading.Tasks;
4+
5+
namespace Microsoft.OpenApi.Hidi.Handlers;
6+
7+
internal abstract class AsyncCommandHandler : ICommandHandler
8+
{
9+
public int Invoke(InvocationContext context)
10+
{
11+
throw new InvalidOperationException("This method should not be called");
12+
}
13+
public abstract Task<int> InvokeAsync(InvocationContext context);
14+
}

src/Microsoft.OpenApi.Hidi/Handlers/PluginCommandHandler.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
namespace Microsoft.OpenApi.Hidi.Handlers
1313
{
14-
internal class PluginCommandHandler : ICommandHandler
14+
internal class PluginCommandHandler : AsyncCommandHandler
1515
{
1616
public CommandOptions CommandOptions { get; }
1717
public PluginCommandHandler(CommandOptions commandOptions)
1818
{
1919
CommandOptions = commandOptions;
2020
}
21-
public int Invoke(InvocationContext context)
22-
{
23-
return InvokeAsync(context).GetAwaiter().GetResult();
24-
}
25-
public async Task<int> InvokeAsync(InvocationContext context)
21+
public override async Task<int> InvokeAsync(InvocationContext context)
2622
{
2723
var hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
2824
var cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));
@@ -31,7 +27,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3127
var logger = loggerFactory.CreateLogger<PluginCommandHandler>();
3228
try
3329
{
34-
await OpenApiService.PluginManifest(hidiOptions, logger, cancellationToken).ConfigureAwait(false);
30+
await OpenApiService.PluginManifestAsync(hidiOptions, logger, cancellationToken).ConfigureAwait(false);
3531

3632
return 0;
3733
}

src/Microsoft.OpenApi.Hidi/Handlers/ShowCommandHandler.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
namespace Microsoft.OpenApi.Hidi.Handlers
1313
{
14-
internal class ShowCommandHandler : ICommandHandler
14+
internal class ShowCommandHandler : AsyncCommandHandler
1515
{
1616
public CommandOptions CommandOptions { get; set; }
1717
public ShowCommandHandler(CommandOptions commandOptions)
1818
{
1919
CommandOptions = commandOptions;
2020
}
21-
public int Invoke(InvocationContext context)
22-
{
23-
return InvokeAsync(context).GetAwaiter().GetResult();
24-
}
25-
public async Task<int> InvokeAsync(InvocationContext context)
21+
public override async Task<int> InvokeAsync(InvocationContext context)
2622
{
2723
var hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
2824
var cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));
@@ -31,7 +27,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3127
var logger = loggerFactory.CreateLogger<ShowCommandHandler>();
3228
try
3329
{
34-
await OpenApiService.ShowOpenApiDocument(hidiOptions, logger, cancellationToken).ConfigureAwait(false);
30+
await OpenApiService.ShowOpenApiDocumentAsync(hidiOptions, logger, cancellationToken).ConfigureAwait(false);
3531

3632
return 0;
3733
}

src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
namespace Microsoft.OpenApi.Hidi.Handlers
1313
{
14-
internal class TransformCommandHandler : ICommandHandler
14+
internal class TransformCommandHandler : AsyncCommandHandler
1515
{
1616
public CommandOptions CommandOptions { get; }
1717
public TransformCommandHandler(CommandOptions commandOptions)
1818
{
1919
CommandOptions = commandOptions;
2020
}
21-
public int Invoke(InvocationContext context)
22-
{
23-
return InvokeAsync(context).GetAwaiter().GetResult();
24-
}
25-
public async Task<int> InvokeAsync(InvocationContext context)
21+
public override async Task<int> InvokeAsync(InvocationContext context)
2622
{
2723
var hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
2824
var cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));
@@ -31,7 +27,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3127
var logger = loggerFactory.CreateLogger<TransformCommandHandler>();
3228
try
3329
{
34-
await OpenApiService.TransformOpenApiDocument(hidiOptions, logger, cancellationToken).ConfigureAwait(false);
30+
await OpenApiService.TransformOpenApiDocumentAsync(hidiOptions, logger, cancellationToken).ConfigureAwait(false);
3531

3632
return 0;
3733
}

src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@
1111

1212
namespace Microsoft.OpenApi.Hidi.Handlers
1313
{
14-
internal class ValidateCommandHandler : ICommandHandler
14+
internal class ValidateCommandHandler : AsyncCommandHandler
1515
{
1616
public CommandOptions CommandOptions { get; }
1717

1818
public ValidateCommandHandler(CommandOptions commandOptions)
1919
{
2020
CommandOptions = commandOptions;
2121
}
22-
23-
public int Invoke(InvocationContext context)
24-
{
25-
return InvokeAsync(context).GetAwaiter().GetResult();
26-
}
27-
public async Task<int> InvokeAsync(InvocationContext context)
22+
public override async Task<int> InvokeAsync(InvocationContext context)
2823
{
2924
var hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
3025
var cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));
@@ -33,7 +28,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3328
try
3429
{
3530
if (hidiOptions.OpenApi is null) throw new InvalidOperationException("OpenApi file is required");
36-
var isValid = await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
31+
var isValid = await OpenApiService.ValidateOpenApiDocumentAsync(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
3732
return isValid is not false ? 0 : -1;
3833
}
3934
#if RELEASE

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
3434
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
3535
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
36+
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
3637
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
3738
<PackageReference Include="Microsoft.OData.Edm" Version="8.0.1" />
3839
<PackageReference Include="Microsoft.OpenApi.OData" Version="2.0.0-preview.2" />

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static class OpenApiService
4141
/// <summary>
4242
/// Implementation of the transform command
4343
/// </summary>
44-
public static async Task TransformOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
44+
public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
4545
{
4646
if (string.IsNullOrEmpty(options.OpenApi) && string.IsNullOrEmpty(options.Csdl) && string.IsNullOrEmpty(options.FilterOptions?.FilterByApiManifest))
4747
{
@@ -70,7 +70,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
7070
var openApiVersion = options.Version != null ? TryParseOpenApiSpecVersion(options.Version) : OpenApiSpecVersion.OpenApi3_0;
7171

7272
// If ApiManifest is provided, set the referenced OpenAPI document
73-
var apiDependency = await FindApiDependency(options.FilterOptions.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false);
73+
var apiDependency = await FindApiDependencyAsync(options.FilterOptions.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false);
7474
if (apiDependency != null)
7575
{
7676
options.OpenApi = apiDependency.ApiDescripionUrl;
@@ -80,12 +80,12 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
8080
JsonDocument? postmanCollection = null;
8181
if (!string.IsNullOrEmpty(options.FilterOptions?.FilterByCollection))
8282
{
83-
using var collectionStream = await GetStream(options.FilterOptions.FilterByCollection, logger, cancellationToken).ConfigureAwait(false);
83+
using var collectionStream = await GetStreamAsync(options.FilterOptions.FilterByCollection, logger, cancellationToken).ConfigureAwait(false);
8484
postmanCollection = await JsonDocument.ParseAsync(collectionStream, cancellationToken: cancellationToken).ConfigureAwait(false);
8585
}
8686

8787
// Load OpenAPI document
88-
var document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
88+
var document = await GetOpenApiAsync(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
8989

9090
if (options.FilterOptions != null)
9191
{
@@ -116,7 +116,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
116116
}
117117
}
118118

119-
private static async Task<ApiDependency?> FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken = default)
119+
private static async Task<ApiDependency?> FindApiDependencyAsync(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken = default)
120120
{
121121
ApiDependency? apiDependency = null;
122122
// If API Manifest is provided, load it, use it get the OpenAPI path
@@ -130,7 +130,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
130130
{
131131
apiDependencyName = apiManifestRef[1];
132132
}
133-
using (var fileStream = await GetStream(apiManifestRef[0], logger, cancellationToken).ConfigureAwait(false))
133+
using (var fileStream = await GetStreamAsync(apiManifestRef[0], logger, cancellationToken).ConfigureAwait(false))
134134
{
135135
var document = await JsonDocument.ParseAsync(fileStream, cancellationToken: cancellationToken).ConfigureAwait(false);
136136
apiManifest = ApiManifestDocument.Load(document.RootElement);
@@ -212,7 +212,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
212212
}
213213

214214
// Get OpenAPI document either from OpenAPI or CSDL
215-
private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogger logger, string? metadataVersion = null, CancellationToken cancellationToken = default)
215+
private static async Task<OpenApiDocument> GetOpenApiAsync(HidiOptions options, ILogger logger, string? metadataVersion = null, CancellationToken cancellationToken = default)
216216
{
217217
OpenApiDocument document;
218218
Stream stream;
@@ -223,7 +223,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogg
223223
using (logger.BeginScope("Convert CSDL: {Csdl}", options.Csdl))
224224
{
225225
stopwatch.Start();
226-
stream = await GetStream(options.Csdl, logger, cancellationToken).ConfigureAwait(false);
226+
stream = await GetStreamAsync(options.Csdl, logger, cancellationToken).ConfigureAwait(false);
227227
Stream? filteredStream = null;
228228
if (!string.IsNullOrEmpty(options.CsdlFilter))
229229
{
@@ -233,15 +233,15 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogg
233233
await stream.DisposeAsync().ConfigureAwait(false);
234234
}
235235

236-
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, metadataVersion, options.SettingsConfig, cancellationToken).ConfigureAwait(false);
236+
document = await ConvertCsdlToOpenApiAsync(filteredStream ?? stream, metadataVersion, options.SettingsConfig, cancellationToken).ConfigureAwait(false);
237237
stopwatch.Stop();
238238
logger.LogTrace("{Timestamp}ms: Generated OpenAPI with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
239239
}
240240
}
241241
else if (!string.IsNullOrEmpty(options.OpenApi))
242242
{
243-
stream = await GetStream(options.OpenApi, logger, cancellationToken).ConfigureAwait(false);
244-
var result = await ParseOpenApi(options.OpenApi, options.InlineExternal, logger, stream, cancellationToken).ConfigureAwait(false);
243+
stream = await GetStreamAsync(options.OpenApi, logger, cancellationToken).ConfigureAwait(false);
244+
var result = await ParseOpenApiAsync(options.OpenApi, options.InlineExternal, logger, stream, cancellationToken).ConfigureAwait(false);
245245
document = result.OpenApiDocument;
246246
}
247247
else throw new InvalidOperationException("No input file path or URL provided");
@@ -323,7 +323,7 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
323323
/// Implementation of the validate command
324324
/// </summary>
325325
/// <returns><see langword="true"/> when valid, <see langword="false"/> when invalid and <see langword="null"/> when cancelled</returns>
326-
public static async Task<bool?> ValidateOpenApiDocument(
326+
public static async Task<bool?> ValidateOpenApiDocumentAsync(
327327
string openApi,
328328
ILogger logger,
329329
CancellationToken cancellationToken = default)
@@ -337,9 +337,9 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
337337

338338
try
339339
{
340-
using var stream = await GetStream(openApi, logger, cancellationToken).ConfigureAwait(false);
340+
using var stream = await GetStreamAsync(openApi, logger, cancellationToken).ConfigureAwait(false);
341341

342-
result = await ParseOpenApi(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);
342+
result = await ParseOpenApiAsync(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);
343343

344344
using (logger.BeginScope("Calculating statistics"))
345345
{
@@ -367,7 +367,7 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
367367
return result.OpenApiDiagnostic.Errors.Count == 0;
368368
}
369369

370-
private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
370+
private static async Task<ReadResult> ParseOpenApiAsync(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
371371
{
372372
ReadResult result;
373373
var stopwatch = Stopwatch.StartNew();
@@ -398,7 +398,7 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
398398
/// </summary>
399399
/// <param name="csdl">The CSDL stream.</param>
400400
/// <returns>An OpenAPI document.</returns>
401-
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string? metadataVersion = null, IConfiguration? settings = null, CancellationToken token = default)
401+
public static async Task<OpenApiDocument> ConvertCsdlToOpenApiAsync(Stream csdl, string? metadataVersion = null, IConfiguration? settings = null, CancellationToken token = default)
402402
{
403403
using var reader = new StreamReader(csdl);
404404
var csdlText = await reader.ReadToEndAsync(token).ConfigureAwait(false);
@@ -486,7 +486,7 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
486486
/// <summary>
487487
/// Reads stream from file system or makes HTTP request depending on the input string
488488
/// </summary>
489-
private static async Task<Stream> GetStream(string input, ILogger logger, CancellationToken cancellationToken = default)
489+
private static async Task<Stream> GetStreamAsync(string input, ILogger logger, CancellationToken cancellationToken = default)
490490
{
491491
Stream stream;
492492
using (logger.BeginScope("Reading input stream"))
@@ -562,7 +562,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
562562
return extension;
563563
}
564564

565-
internal static async Task<string?> ShowOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
565+
internal static async Task<string?> ShowOpenApiDocumentAsync(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
566566
{
567567
try
568568
{
@@ -571,7 +571,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
571571
throw new ArgumentException("Please input a file path or URL");
572572
}
573573

574-
var document = await GetOpenApi(options, logger, null, cancellationToken).ConfigureAwait(false);
574+
var document = await GetOpenApiAsync(options, logger, null, cancellationToken).ConfigureAwait(false);
575575

576576
using (logger.BeginScope("Creating diagram"))
577577
{
@@ -722,17 +722,17 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
722722
writer.WriteLine("</html");
723723
}
724724

725-
internal static async Task PluginManifest(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
725+
internal static async Task PluginManifestAsync(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
726726
{
727727
// If ApiManifest is provided, set the referenced OpenAPI document
728-
var apiDependency = await FindApiDependency(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false);
728+
var apiDependency = await FindApiDependencyAsync(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false);
729729
if (apiDependency != null)
730730
{
731731
options.OpenApi = apiDependency.ApiDescripionUrl;
732732
}
733733

734734
// Load OpenAPI document
735-
var document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
735+
var document = await GetOpenApiAsync(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
736736

737737
cancellationToken.ThrowIfCancellationRequested();
738738

src/Microsoft.OpenApi.Readers/Interface/IStreamLoader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.ComponentModel;
56
using System.IO;
67
using System.Threading.Tasks;
78
using Microsoft.OpenApi.Models;
@@ -25,6 +26,8 @@ public interface IStreamLoader
2526
/// </summary>
2627
/// <param name="uri"></param>
2728
/// <returns></returns>
29+
[Obsolete("Use the Async overload")]
30+
[EditorBrowsable(EditorBrowsableState.Never)]
2831
Stream Load(Uri uri);
2932
}
3033
}

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21+
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
2122
<PackageReference Include="SharpYaml" Version="2.1.1" />
2223
</ItemGroup>
2324

0 commit comments

Comments
 (0)