-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerateCommand.cs
More file actions
108 lines (91 loc) · 4.36 KB
/
GenerateCommand.cs
File metadata and controls
108 lines (91 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
using ApiCodeGenerator.Abstraction;
namespace ApiCodeGenerator.MSBuild
{
internal class GenerateCommand : RootCommand
{
private const string NswagToolsDirName = "NSwag.Tool";
private readonly Option<string?> _baseNswagOption;
private readonly Option<string[]> _extensionOption;
private readonly Argument<string> _openApiArgument;
private readonly Argument<string> _outputArg;
private readonly Argument<string> _nswagArgument;
private readonly Option<string?> _nswagToolPath;
private readonly Option<string?> _variablesOption;
public GenerateCommand()
: base()
{
_baseNswagOption = new("-b", "Base nswag file");
AddOption(_baseNswagOption);
_openApiArgument = new("openApi", "Path to OpenApi document.");
AddArgument(_openApiArgument);
_nswagArgument = new("nswag", "Path to nswag file");
AddArgument(_nswagArgument);
_outputArg = new("output", "Path to output file");
AddArgument(_outputArg);
_extensionOption = new("-e", "Path to extension file");
AddOption(_extensionOption);
_variablesOption = new("-v", "variables");
AddOption(_variablesOption);
_nswagToolPath = new("--nswagTool", "Path to NSwag.Tool directory");
AddOption(_nswagToolPath);
this.SetHandler(ExecuteAsync);
}
private async Task ExecuteAsync(InvocationContext context)
{
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
var nswagFile = context.ParseResult.GetValueForArgument(_nswagArgument);
var baseNswagFile = context.ParseResult.GetValueForOption(_baseNswagOption);
var nswagToolPath = context.ParseResult.GetValueForOption(_nswagToolPath);
var openApiFile = context.ParseResult.GetValueForArgument(_openApiArgument);
var outFile = context.ParseResult.GetValueForArgument(_outputArg);
var variables = context.ParseResult.GetValueForOption(_variablesOption);
var extPaths = context.ParseResult.GetValueForOption(_extensionOption);
var factory = GetGenerationTaskFactory(nswagToolPath);
AddExtesionsProbingPaths(extPaths);
var generator = factory.Create(extPaths, new ConsoleLogAdapter());
var result = await generator.ExecuteAsync(nswagFile, openApiFile, outFile, variables, baseNswagFile);
context.ExitCode = result ? 0 : 1;
}
private IGenerationTaskFactory GetGenerationTaskFactory(string? nswagToolsPath)
{
var context = new AssemblyLoadContext("Generator Context");
// регистриуем процесс резолва сборок
AssemblyResolver.Register(context);
var thisAssemblyPath = GetThisAssemblyPath();
AssemblyResolver.AddProbingPath(nswagToolsPath ?? Path.Combine(thisAssemblyPath, NswagToolsDirName));
AssemblyResolver.AddProbingPath(thisAssemblyPath);
var coreAsm = context.LoadFromAssemblyName(new AssemblyName("ApiCodeGenerator.Core"));
var factoryType = coreAsm.GetType("ApiCodeGenerator.Core.GenerationTaskFactory")!;
return (IGenerationTaskFactory)Activator.CreateInstance(factoryType)!;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:Static elements should appear before instance elements", Justification = "Not need")]
private static void AddExtesionsProbingPaths(string[]? extensions)
{
if (extensions?.Any() == true)
{
var dirs = extensions.Select(i => Path.GetDirectoryName(Path.GetFullPath(i))!).Distinct();
foreach (var dir in dirs)
{
AssemblyResolver.AddProbingPath(dir);
}
}
}
private static string GetThisAssemblyPath()
{
var thisAsmPath = Assembly.GetExecutingAssembly().Location;
return Path.GetDirectoryName(thisAsmPath)!;
}
}
}