This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
104 lines (85 loc) · 3.5 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
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using OpenApiBrowser.Model;
using Path = System.IO.Path;
namespace ApiToDart
{
internal class Program
{
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) { AllowTrailingCommas = true };
private static async Task ConvertSchemaAsync(DartConverter converter, DataSchema schema, string schemaName)
{
var settings = Utils.GetJobSettings(converter.Job, schemaName);
if (settings.Ignore)
{
Console.WriteLine("Skipped " + schemaName);
return;
}
else
{
Console.WriteLine("Converting " + schemaName + "...");
}
string fileName = schemaName.ApplyNaming(DartConverter.DartNamingConvention.FileName) + ".dart";
string filePath = Path.Combine(settings.OutputDirectory, fileName);
string source = converter.ToDartFile(schema, schemaName);
await File.WriteAllTextAsync(filePath, source);
}
private static async Task Main(string[] args)
{
var jobPaths = Directory.GetFiles("jobs", "*.json");
var tasks = jobPaths.Select((path) => ParseAndRunJobAsync(path));
await Task.WhenAll(tasks);
}
private static async Task ParseAndRunJobAsync(string path)
{
await using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var job = await JsonSerializer.DeserializeAsync<Job>(fileStream, _jsonOptions);
await RunJobAsync(job);
}
}
private static async Task RunJobAsync(Job job)
{
var json = await Utils.FetchJson(job);
var specification = JsonSerializer.Deserialize<Specification>(json, _jsonOptions);
var converter = new DartConverter(job, specification);
var outputDirectoryPath = job.Default.OutputDirectory;
if (!Directory.Exists(outputDirectoryPath))
{
Directory.CreateDirectory(outputDirectoryPath);
}
var schemas = converter.Schemas.Where(kv => kv.Schema.Type == "object");
foreach (var (key, schema) in schemas)
{
if (schema.Properties == null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Schema {key} had no data");
Console.ResetColor();
continue;
}
await ConvertSchemaAsync(converter, schema, schema.Title ?? key);
}
if (job.Default.EndpointSchemaInclude != null)
{
foreach (var kv in job.Default.EndpointSchemaInclude)
{
var key = kv.Key.Split('@');
var pathUrl = key[0];
var path = specification.Paths[pathUrl];
var aaa = key[1] switch
{
"post" => path.Post,
_ => throw new NotImplementedException(),
};
var response = aaa.Responses[key[2]];
var schema = response.Content["application/json"].Schema;
await ConvertSchemaAsync(converter, schema, kv.Value ?? schema.Title);
}
}
}
}
}