Skip to content

Commit b4803ff

Browse files
committed
improved logging
1 parent 4253cd9 commit b4803ff

6 files changed

Lines changed: 64 additions & 37 deletions

File tree

src/ApiCodeGenerator.Core/GenerationTask.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Diagnostics.CodeAnalysis;
@@ -91,8 +91,9 @@ public async Task<bool> ExecuteAsync(string nswagFilePath,
9191
vars["OutFile"] = outFilePath;
9292
var roVariables = new ReadOnlyDictionary<string, string>(vars);
9393

94-
LogMessage("Values of nswag variables");
95-
LogMessage(string.Join(Environment.NewLine, vars.Select(_ => $"\t[{_.Key}] = {_.Value}")));
94+
LogMessage("Values of nswag variables: {0}{1}",
95+
Environment.NewLine,
96+
string.Join(Environment.NewLine, vars.Select(_ => $"\t[{_.Key}] = {_.Value}")));
9697

9798
JObject? baseNswagDocument = LoadBaseNswag(baseNswagFilePath);
9899
var nswagDocument = _documentFactory.LoadNswagDocument(nswagFilePath, roVariables, baseNswagDocument);
@@ -110,18 +111,18 @@ public async Task<bool> ExecuteAsync(string nswagFilePath,
110111
return false;
111112
}
112113

113-
var context = await CreateGenerationContext(nswagDocument, nswagFilePath, roVariables);
114-
115-
if (context is not null)
114+
try
116115
{
117-
if (context.DocumentReader is null)
118-
{
119-
Log?.LogWarning(NotSetInput, nswagFilePath, "Source not set. Skip generation.");
120-
return true;
121-
}
116+
var context = await CreateGenerationContext(nswagDocument, nswagFilePath, roVariables);
122117

123-
try
118+
if (context is not null)
124119
{
120+
if (context.DocumentReader is null)
121+
{
122+
Log?.LogWarning(NotSetInput, nswagFilePath, "Source not set. Skip generation.");
123+
return true;
124+
}
125+
125126
LogMessage($"Use settings: {generatorSettings.Key}");
126127
var contentGenerator = await contentGeneratorFactory.Invoke(context);
127128

@@ -137,14 +138,14 @@ public async Task<bool> ExecuteAsync(string nswagFilePath,
137138
{
138139
Log?.LogError(WriteFileErr, outFilePath, "Unable write file. Error: {0}", ex.Message);
139140
}
140-
}
141-
catch (InvalidOperationException ex)
142-
{
143-
Log?.LogError(GenerationErr, nswagFilePath, ex.Message);
144-
return false;
145-
}
146141

147-
return true;
142+
return true;
143+
}
144+
}
145+
catch (InvalidOperationException ex)
146+
{
147+
Log?.LogError(GenerationErr, nswagFilePath, ex.Message);
148+
return false;
148149
}
149150

150151
return false;

src/ApiCodeGenerator.MSBuild/ApiCodeGenerator.MSBuild.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
<ItemGroup>
3232
<ProjectReference Include="..\ApiCodeGenerator.Abstraction\ApiCodeGenerator.Abstraction.csproj" />
33-
<ProjectReference Include="..\ApiCodeGenerator.Core\ApiCodeGenerator.Core.csproj" ReferenceOutputAssembly="false" />
34-
<ProjectReference Include="..\ApiCodeGenerator.OpenApi\ApiCodeGenerator.OpenApi.csproj" ReferenceOutputAssembly="false" />
35-
<ProjectReference Include="..\ApiCodeGenerator.AsyncApi\ApiCodeGenerator.AsyncApi.csproj" ReferenceOutputAssembly="false" />
3633
</ItemGroup>
3734

3835
<ItemGroup Label="Nswag prerequiries">
Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
2+
using System.IO;
63
using ApiCodeGenerator.Abstraction;
74

85
namespace ApiCodeGenerator.MSBuild
96
{
107
internal class ConsoleLogAdapter : ILogger
118
{
129
public void LogError(string? code, string? sourceFile, string message, params object[] messageArgs)
13-
=> Console.Error.WriteLine($"{sourceFile}: {string.Format(message, messageArgs)}");
10+
=> Log(Console.Error, "error", code, sourceFile, string.Format(message, messageArgs));
1411

1512
public void LogMessage(string? code, string? sourceFile, string message, params object[] messageArgs)
16-
=> Console.WriteLine($"INFO: {string.Format(message, messageArgs)}");
13+
=> Log(Console.Out, code is null ? null : "info", code, sourceFile, string.Format(message, messageArgs));
1714

1815
public void LogWarning(string? code, string? sourceFile, string message, params object[] messageArgs)
19-
=> Console.WriteLine($"WARNING {sourceFile}: {string.Format(message, messageArgs)}");
16+
=> Log(Console.Out, "warning", code, sourceFile, string.Format(message, messageArgs));
17+
18+
private static void Log(TextWriter writer, string? type, string? code, string? file, string message)
19+
{
20+
if (file is not null)
21+
{
22+
writer.Write(Path.IsPathFullyQualified(file)
23+
? file
24+
: Path.GetFullPath(file));
25+
writer.Write("(1,1): ");
26+
}
27+
28+
if (type is not null)
29+
{
30+
writer.Write(type);
31+
if (code is not null)
32+
{
33+
writer.Write(" ");
34+
writer.Write(code);
35+
}
36+
37+
writer.Write(": ");
38+
}
39+
40+
writer.WriteLine(message);
41+
}
2042
}
2143
}

src/ApiCodeGenerator.MSBuild/Console/GenerateCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public GenerateCommand()
5050
this.SetHandler(ExecuteAsync);
5151
}
5252

53-
private Task ExecuteAsync(InvocationContext context)
53+
private async Task ExecuteAsync(InvocationContext context)
5454
{
5555
#if DEBUG
5656
System.Diagnostics.Debugger.Launch();
@@ -66,12 +66,13 @@ private Task ExecuteAsync(InvocationContext context)
6666
var factory = GetGenerationTaskFactory(nswagToolPath);
6767
AddExtesionsProbingPaths(extPaths);
6868
var generator = factory.Create(extPaths, new ConsoleLogAdapter());
69-
return generator.ExecuteAsync(nswagFile, openApiFile, outFile, variables, baseNswagFile);
69+
var result = await generator.ExecuteAsync(nswagFile, openApiFile, outFile, variables, baseNswagFile);
70+
context.ExitCode = result ? 0 : 1;
7071
}
7172

7273
private IGenerationTaskFactory GetGenerationTaskFactory(string? nswagToolsPath)
7374
{
74-
var context = AssemblyLoadContext.GetLoadContext(Assembly.GetCallingAssembly())!;
75+
var context = new AssemblyLoadContext("Generator Context");
7576

7677
// регистриуем процесс резолва сборок
7778
AssemblyResolver.Register(context);

src/ApiCodeGenerator.MSBuild/Console/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
using ApiCodeGenerator.MSBuild;
33

44
var cmd = new GenerateCommand();
5-
await cmd.InvokeAsync(args);
5+
var exitCode = await cmd.InvokeAsync(args);
6+
7+
return exitCode;

src/ApiCodeGenerator.MSBuild/build/Console.targets

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project>
1+
<Project>
22

33
<UsingTask TaskName="AcgGetTools"
44
TaskFactory="RoslynCodeTaskFactory"
@@ -191,8 +191,12 @@
191191
</ItemGroup>
192192
<Warning Condition="'@(__acgNotExistsBiExtAssemblies)' != ''" Text="File '%(__acgNotExistsBiExtAssemblies.Identity)' not found." />
193193
<Exec Command="%(CurrentOpenApiReference.Command)"
194-
LogStandardErrorAsError="true"
195-
CustomWarningRegularExpression="^WARNING .*"
196-
StdErrEncoding="utf-8" />
194+
StandardOutputImportance="low"
195+
StdErrEncoding="utf-8"
196+
IgnoreExitCode="true">
197+
<Output TaskParameter="ExitCode" PropertyName="_ResultCode" />
198+
</Exec>
199+
200+
<Error Condition="'$(_ResultCode)' != '0'" Text="Code generation failed!" />
197201
</Target>
198202
</Project>

0 commit comments

Comments
 (0)