-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (76 loc) · 3.44 KB
/
Program.cs
File metadata and controls
88 lines (76 loc) · 3.44 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
//---------------------------------------------------------------------
// <copyright file="Program.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
using System;
using System.IO;
using System.Xml.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi;
using Microsoft.OpenApi.OData;
using System.Threading.Tasks;
namespace UpdateDocs
{
class Program
{
static async Task<int> Main(string[] args)
{
// we assume the path are existed for simplicity.
string path = Directory.GetCurrentDirectory();
string parentPath = Path.Combine(path, "..", "..", "..", "..", "..");
string csdl = Path.Combine(parentPath, "docs", "csdl");
string oas20 = Path.Combine(parentPath, "docs", "oas_2_0");
string oas30 = Path.Combine(parentPath, "docs", "oas3_0_0");
string oas31 = Path.Combine(parentPath, "docs", "oas3_1_0");
foreach (var filePath in Directory.GetFiles(csdl, "*.xml"))
{
Console.WriteLine(filePath);
IEdmModel model = await LoadEdmModelAsync(filePath);
if (model == null)
{
continue;
}
FileInfo fileInfo = new FileInfo(filePath);
string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
OpenApiConvertSettings settings = new OpenApiConvertSettings();
if (fileName.Contains("graph.beta"))
{
settings.PrefixEntityTypeNameBeforeKey = true;
settings.ServiceRoot = new Uri("https://graph.microsoft.com/beta");
}
else if (fileName.Contains("graph1.0"))
{
settings.PrefixEntityTypeNameBeforeKey = true;
settings.ServiceRoot = new Uri("https://graph.microsoft.com/v1.0");
}
settings.EnableKeyAsSegment = true;
settings.EnableUnqualifiedCall = true;
var output = Path.Combine(oas31, fileName + ".json");
var document = model.ConvertToOpenApi(settings);
await File.WriteAllTextAsync(output, await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1));
output = Path.Combine(oas30, fileName + ".json");
await File.WriteAllTextAsync(output, await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
output = Path.Combine(oas20, fileName + ".json");
await File.WriteAllTextAsync(output, await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi2_0));
Console.WriteLine("Output [ " + fileName + " ] Successful!");
}
Console.WriteLine("\n==> All Done!");
return 0;
}
private static async Task<IEdmModel> LoadEdmModelAsync(string file)
{
try
{
string csdl = await File.ReadAllTextAsync(file);
return CsdlReader.Parse(XElement.Parse(csdl).CreateReader());
}
catch
{
Console.WriteLine("Cannot load EDM from file: " + file);
return null;
}
}
}
}