Skip to content

Commit a5923c0

Browse files
Merge pull request #959 from microsoft/mk/upgrade-system-commandline
Bump up System.Commandline API to v2.0.0-beta4.22272.1
2 parents 853e351 + f0a0eb8 commit a5923c0

File tree

6 files changed

+187
-35
lines changed

6 files changed

+187
-35
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.CommandLine;
6+
using System.CommandLine.Invocation;
7+
using System.IO;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace Microsoft.OpenApi.Hidi.Handlers
13+
{
14+
internal class TransformCommandHandler : ICommandHandler
15+
{
16+
public Option<string> DescriptionOption { get; set; }
17+
public Option<string> CsdlOption { get; set; }
18+
public Option<string> CsdlFilterOption { get; set; }
19+
public Option<FileInfo> OutputOption { get; set; }
20+
public Option<bool> CleanOutputOption { get; set; }
21+
public Option<string?> VersionOption { get; set; }
22+
public Option<OpenApiFormat?> FormatOption { get; set; }
23+
public Option<bool> TerseOutputOption { get; set; }
24+
public Option<LogLevel> LogLevelOption { get; set; }
25+
public Option<string> FilterByOperationIdsOption { get; set; }
26+
public Option<string> FilterByTagsOption { get; set; }
27+
public Option<string> FilterByCollectionOption { get; set; }
28+
public Option<bool> InlineLocalOption { get; set; }
29+
public Option<bool> InlineExternalOption { get; set; }
30+
31+
public int Invoke(InvocationContext context)
32+
{
33+
return InvokeAsync(context).GetAwaiter().GetResult();
34+
}
35+
public async Task<int> InvokeAsync(InvocationContext context)
36+
{
37+
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
38+
string csdlFilter = context.ParseResult.GetValueForOption(CsdlFilterOption);
39+
string csdl = context.ParseResult.GetValueForOption(CsdlOption);
40+
FileInfo output = context.ParseResult.GetValueForOption(OutputOption);
41+
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
42+
string? version = context.ParseResult.GetValueForOption(VersionOption);
43+
OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption);
44+
bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption);
45+
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
46+
bool inlineLocal = context.ParseResult.GetValueForOption(InlineLocalOption);
47+
bool inlineExternal = context.ParseResult.GetValueForOption(InlineExternalOption);
48+
string filterbyoperationids = context.ParseResult.GetValueForOption(FilterByOperationIdsOption);
49+
string filterbytags = context.ParseResult.GetValueForOption(FilterByTagsOption);
50+
string filterbycollection = context.ParseResult.GetValueForOption(FilterByCollectionOption);
51+
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
52+
53+
using var loggerFactory = Logger.ConfigureLogger(logLevel);
54+
var logger = loggerFactory.CreateLogger<OpenApiService>();
55+
try
56+
{
57+
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, logLevel, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, cancellationToken);
58+
59+
return 0;
60+
}
61+
catch (Exception ex)
62+
{
63+
#if DEBUG
64+
logger.LogCritical(ex, ex.Message);
65+
throw; // so debug tools go straight to the source of the exception when attached
66+
#else
67+
logger.LogCritical( ex.Message);
68+
return 1;
69+
#endif
70+
}
71+
}
72+
}
73+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.CommandLine;
6+
using System.CommandLine.Invocation;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace Microsoft.OpenApi.Hidi.Handlers
12+
{
13+
internal class ValidateCommandHandler : ICommandHandler
14+
{
15+
public Option<string> DescriptionOption { get; set; }
16+
public Option<LogLevel> LogLevelOption { get; set; }
17+
18+
public int Invoke(InvocationContext context)
19+
{
20+
return InvokeAsync(context).GetAwaiter().GetResult();
21+
}
22+
public async Task<int> InvokeAsync(InvocationContext context)
23+
{
24+
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
25+
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
26+
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
27+
28+
29+
using var loggerFactory = Logger.ConfigureLogger(logLevel);
30+
var logger = loggerFactory.CreateLogger<OpenApiService>();
31+
try
32+
{
33+
await OpenApiService.ValidateOpenApiDocument(openapi, logLevel, cancellationToken);
34+
return 0;
35+
}
36+
catch (Exception ex)
37+
{
38+
#if DEBUG
39+
logger.LogCritical(ex, ex.Message);
40+
throw; // so debug tools go straight to the source of the exception when attached
41+
#else
42+
logger.LogCritical( ex.Message);
43+
return 1;
44+
#endif
45+
}
46+
}
47+
}
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Microsoft.OpenApi.Hidi
7+
{
8+
public class Logger
9+
{
10+
public static ILoggerFactory ConfigureLogger(LogLevel logLevel)
11+
{
12+
// Configure logger options
13+
#if DEBUG
14+
logLevel = logLevel > LogLevel.Debug ? LogLevel.Debug : logLevel;
15+
#endif
16+
17+
return LoggerFactory.Create((builder) =>
18+
{
19+
builder
20+
.AddSimpleConsole(c =>
21+
{
22+
c.IncludeScopes = true;
23+
})
24+
#if DEBUG
25+
.AddDebug()
26+
#endif
27+
.SetMinimumLevel(logLevel);
28+
});
29+
}
30+
}
31+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
4242
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
4343
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
44-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
44+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
4545
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.1" />
4646
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.0.11" />
4747
</ItemGroup>

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -26,7 +26,6 @@
2626
using System.Threading;
2727
using System.Xml.Xsl;
2828
using System.Xml;
29-
using System.Runtime.CompilerServices;
3029
using System.Reflection;
3130

3231
namespace Microsoft.OpenApi.Hidi
@@ -36,7 +35,7 @@ public class OpenApiService
3635
/// <summary>
3736
/// Implementation of the transform command
3837
/// </summary>
39-
public static async Task<int> TransformOpenApiDocument(
38+
public static async Task TransformOpenApiDocument(
4039
string openapi,
4140
string csdl,
4241
string csdlFilter,
@@ -45,7 +44,7 @@ public static async Task<int> TransformOpenApiDocument(
4544
string? version,
4645
OpenApiFormat? format,
4746
bool terseOutput,
48-
LogLevel loglevel,
47+
LogLevel logLevel,
4948
bool inlineLocal,
5049
bool inlineExternal,
5150
string filterbyoperationids,
@@ -54,9 +53,8 @@ public static async Task<int> TransformOpenApiDocument(
5453
CancellationToken cancellationToken
5554
)
5655
{
57-
using var loggerFactory = ConfigureLoggerInstance(loglevel);
56+
using var loggerFactory = Logger.ConfigureLogger(logLevel);
5857
var logger = loggerFactory.CreateLogger<OpenApiService>();
59-
6058
try
6159
{
6260
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
@@ -213,18 +211,11 @@ CancellationToken cancellationToken
213211
logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");
214212
textWriter.Flush();
215213
}
216-
return 0;
217214
}
218215
catch (Exception ex)
219216
{
220-
#if DEBUG
221-
logger.LogCritical(ex, ex.Message);
222-
#else
223-
logger.LogCritical(ex.Message);
224-
225-
#endif
226-
return 1;
227-
}
217+
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
218+
}
228219
}
229220

230221
private static XslCompiledTransform GetFilterTransform()
@@ -253,14 +244,13 @@ private static Stream ApplyFilter(string csdl, string entitySetOrSingleton, XslC
253244
/// <summary>
254245
/// Implementation of the validate command
255246
/// </summary>
256-
public static async Task<int> ValidateOpenApiDocument(
247+
public static async Task ValidateOpenApiDocument(
257248
string openapi,
258-
LogLevel loglevel,
249+
LogLevel logLevel,
259250
CancellationToken cancellationToken)
260251
{
261-
using var loggerFactory = ConfigureLoggerInstance(loglevel);
252+
using var loggerFactory = Logger.ConfigureLogger(logLevel);
262253
var logger = loggerFactory.CreateLogger<OpenApiService>();
263-
264254
try
265255
{
266256
if (string.IsNullOrEmpty(openapi))
@@ -307,19 +297,11 @@ public static async Task<int> ValidateOpenApiDocument(
307297
logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report..");
308298
logger.LogInformation(statsVisitor.GetStatisticsReport());
309299
}
310-
311-
return 0;
312300
}
313301
catch (Exception ex)
314302
{
315-
#if DEBUG
316-
logger.LogCritical(ex, ex.Message);
317-
#else
318-
logger.LogCritical(ex.Message);
319-
#endif
320-
return 1;
303+
throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex);
321304
}
322-
323305
}
324306

325307
/// <summary>
@@ -596,7 +578,7 @@ private static ILoggerFactory ConfigureLoggerInstance(LogLevel loglevel)
596578
loglevel = loglevel > LogLevel.Debug ? LogLevel.Debug : loglevel;
597579
#endif
598580

599-
return LoggerFactory.Create((builder) => {
581+
return Microsoft.Extensions.Logging.LoggerFactory.Create((builder) => {
600582
builder
601583
.AddSimpleConsole(c => {
602584
c.IncludeScopes = true;

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.CommandLine;
65
using System.IO;
7-
using System.Threading;
86
using System.Threading.Tasks;
97
using Microsoft.Extensions.Logging;
8+
using Microsoft.OpenApi.Hidi.Handlers;
109

1110
namespace Microsoft.OpenApi.Hidi
1211
{
@@ -66,7 +65,11 @@ static async Task Main(string[] args)
6665
logLevelOption
6766
};
6867

69-
validateCommand.SetHandler<string, LogLevel, CancellationToken>(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption);
68+
validateCommand.Handler = new ValidateCommandHandler
69+
{
70+
DescriptionOption = descriptionOption,
71+
LogLevelOption = logLevelOption
72+
};
7073

7174
var transformCommand = new Command("transform")
7275
{
@@ -86,8 +89,23 @@ static async Task Main(string[] args)
8689
inlineExternalOption
8790
};
8891

89-
transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, bool, LogLevel, bool, bool, string, string, string, CancellationToken> (
90-
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
92+
transformCommand.Handler = new TransformCommandHandler
93+
{
94+
DescriptionOption = descriptionOption,
95+
CsdlOption = csdlOption,
96+
CsdlFilterOption = csdlFilterOption,
97+
OutputOption = outputOption,
98+
CleanOutputOption = cleanOutputOption,
99+
VersionOption = versionOption,
100+
FormatOption = formatOption,
101+
TerseOutputOption = terseOutputOption,
102+
LogLevelOption = logLevelOption,
103+
FilterByOperationIdsOption = filterByOperationIdsOption,
104+
FilterByTagsOption = filterByTagsOption,
105+
FilterByCollectionOption = filterByCollectionOption,
106+
InlineLocalOption = inlineLocalOption,
107+
InlineExternalOption = inlineExternalOption
108+
};
91109

92110
rootCommand.Add(transformCommand);
93111
rootCommand.Add(validateCommand);

0 commit comments

Comments
 (0)