-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathApiGenerator.cs
229 lines (195 loc) · 8.93 KB
/
ApiGenerator.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using CodeGeneration.LowLevelClient.Domain;
using CodeGeneration.LowLevelClient.Overrides.Allow404;
using CodeGeneration.LowLevelClient.Overrides.Descriptors;
using CsQuery;
using Newtonsoft.Json;
using Xipton.Razor;
namespace CodeGeneration.LowLevelClient
{
public static class ApiGenerator
{
private readonly static string _listingUrl = "https://github.com/elasticsearch/elasticsearch/tree/v1.2.0/rest-api-spec/api";
private readonly static string _rawUrlPrefix = "https://raw.github.com/elasticsearch/elasticsearch/v1.2.0/rest-api-spec/api/";
private readonly static string _nestFolder = @"..\..\..\..\..\src\Nest\";
private readonly static string _esNetFolder = @"..\..\..\..\..\src\Elasticsearch.Net\";
private readonly static string _viewFolder = @"..\..\Views\";
private readonly static string _cacheFolder = @"..\..\Cache\";
private static readonly RazorMachine _razorMachine;
private static readonly Assembly _assembly;
static ApiGenerator()
{
_razorMachine = new RazorMachine();
_assembly = typeof (ApiGenerator).Assembly;
}
public static string PascalCase(string s)
{
var textInfo = new CultureInfo("en-US").TextInfo;
return textInfo.ToTitleCase(s.ToLowerInvariant()).Replace("_", string.Empty).Replace(".", string.Empty);
}
public static RestApiSpec GetRestSpec(bool useCache)
{
Console.WriteLine("Getting a listing of all the api endpoints from the elasticsearch-rest-api-spec repos");
string html = string.Empty;
using (var client = new WebClient())
html = client.DownloadString(useCache ? LocalUri("root.html") : _listingUrl);
var dom = CQ.Create(html);
if (!useCache)
WriteToCache("root.html", html);
var endpoints = dom[".js-directory-link"]
.Select(s => s.InnerText)
.Where(s => !string.IsNullOrEmpty(s) && s.EndsWith(".json"))
.Select(s => CreateApiDocumentation(useCache, s))
.ToDictionary(d => d.Key, d => d.Value);
var restSpec = new RestApiSpec
{
Endpoints = endpoints,
Commit = dom[".sha:first"].Text()
};
return restSpec;
}
private static string LocalUri(string file)
{
var basePath = Path.Combine(Assembly.GetEntryAssembly().Location, @"..\" + _cacheFolder + file);
var assemblyPath = Path.GetFullPath((new Uri(basePath)).LocalPath);
var fileUri = new Uri(assemblyPath).AbsoluteUri;
return fileUri;
}
private static KeyValuePair<string, ApiEndpoint> CreateApiDocumentation(bool useCache, string s)
{
using (var client = new WebClient())
{
var rawFile = _rawUrlPrefix + s;
var fileName = rawFile.Split(new[] {'/'}).Last();
Console.WriteLine("Downloading {0}", rawFile);
var json = client.DownloadString(useCache ? LocalUri(fileName) : rawFile);
if (!useCache)
WriteToCache(fileName, json);
var apiDocumentation = JsonConvert.DeserializeObject<Dictionary<string, ApiEndpoint>>(json).First();
apiDocumentation.Value.CsharpMethodName = CreateMethodName(
apiDocumentation.Key,
apiDocumentation.Value
);
return apiDocumentation;
}
}
private static readonly Dictionary<string, string> MethodNameOverrides =
(from f in new DirectoryInfo(_nestFolder + "/DSL").GetFiles("*.cs", SearchOption.TopDirectoryOnly)
where f.FullName.EndsWith("Descriptor.cs")
let contents = File.ReadAllText(f.FullName)
let c = Regex.Replace(contents, @"^.+\[DescriptorFor\(""([^ \r\n]+)""\)\].*$", "$1", RegexOptions.Singleline)
where !c.Contains(" ") //filter results that did not match
select new { Value = f.Name.Replace("Descriptor.cs",""), Key = c })
.DistinctBy(v=>v.Key)
.ToDictionary(k => k.Key, v => v.Value);
private static readonly Dictionary<string, string> KnownDescriptors =
(from f in new DirectoryInfo(_nestFolder + "/DSL").GetFiles("*.cs", SearchOption.TopDirectoryOnly)
where f.FullName.EndsWith("Descriptor.cs")
let contents = File.ReadAllText(f.FullName)
let c = Regex.Replace(contents, @"^.+class ([^ \r\n]+).*$", "$1", RegexOptions.Singleline)
select new { Key = Regex.Replace(c, "<.*$", ""), Value = Regex.Replace(c, @"^.*?(?:(\<.+>).*?)?$", "$1")})
.DistinctBy(v=>v.Key)
.ToDictionary(k => k.Key, v => v.Value);
//Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
//or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
public static void PatchMethod(CsharpMethod method)
{
Func<string, bool> ms = (s) => method.FullName.StartsWith(s);
Func<string, bool> mc = (s) => method.FullName.Contains(s);
Func<string, bool> pc = (s) => method.Path.Contains(s);
if (ms("Indices") && !pc("{index}"))
method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
if (ms("Nodes") && !pc("{node_id}"))
method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
//remove duplicate occurance of the HTTP method name
var m = method.HttpMethod.ToPascalCase();
method.FullName =
Regex.Replace(method.FullName, m, (a) => a.Index != method.FullName.IndexOf(m) ? "" : m);
string manualOverride;
var key = method.QueryStringParamName.Replace("RequestParameters", "");
if (MethodNameOverrides.TryGetValue(key, out manualOverride))
method.QueryStringParamName = manualOverride + "RequestParameters";
method.DescriptorType = method.QueryStringParamName.Replace("RequestParameters","Descriptor");
method.Allow404 = ApiEndpointsThatAllow404.Endpoints.Contains(method.DescriptorType.Replace("Descriptor", ""));
string generic;
if (KnownDescriptors.TryGetValue(method.DescriptorType, out generic))
method.DescriptorTypeGeneric = generic;
else method.Unmapped = true;
try
{
var typeName = "CodeGeneration.LowLevelClient.Overrides.Descriptors." + method.DescriptorType + "Overrides";
var type = _assembly.GetType(typeName);
if (type == null)
return;
var overrides = Activator.CreateInstance(type) as IDescriptorOverrides;
if (overrides == null)
return;
method.Url.Params = method.Url.Params.Where(p => !overrides.SkipQueryStringParams.Contains(p.Key))
.ToDictionary(k => k.Key, v => v.Value);
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
}
public static string CreateMethodName(string apiEnpointKey, ApiEndpoint endpoint)
{
return PascalCase(apiEnpointKey);
}
public static void GenerateClientInterface(RestApiSpec model)
{
var targetFile = _esNetFolder + @"IElasticsearchClient.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"IElasticsearchClient.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
public static void GenerateRawDispatch(RestApiSpec model)
{
var targetFile = _nestFolder + @"RawDispatch.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"RawDispatch.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
public static void GenerateRawClient(RestApiSpec model)
{
var targetFile = _esNetFolder + @"ElasticsearchClient.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"ElasticsearchClient.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
public static void GenerateDescriptors(RestApiSpec model)
{
var targetFile = _nestFolder + @"DSL\_Descriptors.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"_Descriptors.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
public static void GenerateRequestParameters(RestApiSpec model)
{
var targetFile = _esNetFolder + @"Domain\RequestParameters.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"RequestParameters.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
public static void GenerateRequestParametersExtensions(RestApiSpec model)
{
var targetFile = _nestFolder + @"Domain\RequestParametersExtensions.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"RequestParametersExtensions.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
public static void GenerateEnums(RestApiSpec model)
{
var targetFile = _esNetFolder + @"Domain\Enums.Generated.cs";
var source = _razorMachine.Execute(File.ReadAllText(_viewFolder + @"Enums.Generated.cshtml"), model).ToString();
File.WriteAllText(targetFile, source);
}
private static void WriteToCache(string filename, string contents)
{
if (!Directory.Exists(_cacheFolder))
Directory.CreateDirectory(_cacheFolder);
File.WriteAllText(_cacheFolder + filename, contents);
}
}
}