-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
181 lines (157 loc) · 6.35 KB
/
Program.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Runtime.InteropServices.JavaScript;
using System.Threading.Tasks;
using Kiota.Builder;
using Kiota.Builder.Configuration;
using System.Threading;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Runtime.Versioning;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
using System.Linq;
namespace apicurio
{
[SupportedOSPlatform("browser")]
public partial class KiotaClientGen
{
// Mandatory `main` for WASM modules
static void Main(string[] args)
{
Console.WriteLine("Hello browser!");
}
private static readonly ThreadLocal<HashAlgorithm> HashAlgorithm = new(() => SHA256.Create());
private static readonly CancellationTokenSource source = new CancellationTokenSource();
private static readonly CancellationToken token = source.Token;
[JSExport]
internal async static Task<string> Generate(string spec, string language, string clientClassName, string namespaceName, string includePatterns, string excludePatterns)
{
var cl = new ConsoleLogger();
ILogger<KiotaBuilder> consoleLogger = cl;
try
{
Console.WriteLine($"Starting to Generate with parameters: {language}, {clientClassName}, {namespaceName}");
var defaultConfiguration = new GenerationConfiguration();
var hashedUrl = BitConverter.ToString(HashAlgorithm.Value!.ComputeHash(Encoding.UTF8.GetBytes(spec))).Replace("-", string.Empty);
string OutputPath = Path.Combine(Path.GetTempPath(), "kiota", "generation", hashedUrl);
if (File.Exists(OutputPath))
{
Console.WriteLine("Deleting OutputPath");
File.Delete(OutputPath);
}
Directory.CreateDirectory(OutputPath);
string filename = "openapi.";
if (isJson(spec))
{
filename += "json";
}
else
{
filename += "yaml";
}
string OpenapiFile = Path.Combine(Path.GetTempPath(), filename);
if (File.Exists(OpenapiFile))
{
Console.WriteLine("Deleting OpenapiFile");
File.Delete(OpenapiFile);
}
await File.WriteAllTextAsync(OpenapiFile, spec);
if (!Enum.TryParse<GenerationLanguage>(language, out var parsedLanguage))
{
throw new ArgumentOutOfRangeException($"Not supported language: {language}");
}
var generationConfiguration = new GenerationConfiguration
{
OpenAPIFilePath = OpenapiFile,
IncludePatterns = (includePatterns is null) ? new() : includePatterns?.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(static x => x.Trim()).ToHashSet(),
ExcludePatterns = (excludePatterns is null) ? new() : excludePatterns?.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(static x => x.Trim()).ToHashSet(),
Language = parsedLanguage,
OutputPath = OutputPath,
ClientClassName = clientClassName,
ClientNamespaceName = namespaceName,
IncludeAdditionalData = false,
UsesBackingStore = false,
Serializers = defaultConfiguration.Serializers,
Deserializers = defaultConfiguration.Deserializers,
StructuredMimeTypes = defaultConfiguration.StructuredMimeTypes,
DisabledValidationRules = new(),
CleanOutput = true,
ClearCache = true,
};
try
{
var builder = new KiotaBuilder(consoleLogger, generationConfiguration, new HttpClient());
var result = await builder.GenerateClientAsync(token).ConfigureAwait(false);
}
catch (Exception e)
{
if (e.Message.Contains("Lock"))
{
Console.WriteLine("Problems during the Lock file write, ignoring", e);
}
else
{
throw;
}
}
var zipFilePath = Path.Combine(Path.GetTempPath(), "kiota", "clients", hashedUrl, "client.zip");
if (File.Exists(zipFilePath))
File.Delete(zipFilePath);
else
Directory.CreateDirectory(Path.GetDirectoryName(zipFilePath)!);
ZipFile.CreateFromDirectory(OutputPath, zipFilePath);
byte[] fileBytes = File.ReadAllBytes(zipFilePath);
string base64Content = System.Convert.ToBase64String(fileBytes);
return base64Content;
}
catch (Exception e)
{
var errorMessage = "Error:\n" + e + "\nLogs:\n" + cl.GetAllLogs();
throw new Exception(errorMessage);
}
}
private static bool isJson(string str)
{
try
{
JsonValue.Parse(str);
return true;
}
catch (Exception)
{
return false;
}
}
}
class ConsoleLogger : ILogger<KiotaBuilder>
{
private StringBuilder sb = new StringBuilder();
IDisposable ILogger.BeginScope<TState>(TState state)
{
return new DummyDisposable();
}
bool ILogger.IsEnabled(LogLevel logLevel)
{
return true;
}
void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
sb.AppendLine(formatter(state, exception));
Console.WriteLine(formatter(state, exception));
}
public string GetAllLogs()
{
return sb.ToString();
}
}
class DummyDisposable : IDisposable
{
public void Dispose()
{
// Do nothing
}
}
}