Skip to content

Commit 42e9212

Browse files
committed
Code Cleanup: ApiGenerator
1 parent ecb5ec0 commit 42e9212

37 files changed

+441
-489
lines changed

src/CodeGeneration/ApiGenerator/ApiGenerator.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Linq;
66
using ApiGenerator.Domain;
77
using Newtonsoft.Json;
8-
using ShellProgressBar;
98
using Newtonsoft.Json.Linq;
109
using RazorLight;
10+
using ShellProgressBar;
1111

1212
namespace ApiGenerator
1313
{
@@ -22,20 +22,19 @@ public static void Generate(string downloadBranch, params string[] folders)
2222
var spec = CreateRestApiSpecModel(downloadBranch, folders);
2323
var actions = new Dictionary<Action<RestApiSpec>, string>
2424
{
25-
{ GenerateClientInterface, "Client interface" },
26-
{ GenerateRequestParameters, "Request parameters" },
27-
{ GenerateDescriptors, "Descriptors" },
28-
{ GenerateRequests, "Requests" },
29-
{ GenerateEnums, "Enums" },
30-
{ GenerateRawClient, "Lowlevel client" },
31-
{ GenerateRawDispatch, "Dispatch" },
32-
{ GenerateRequestParametersExtensions, "Request parameters override" },
33-
25+
{ GenerateClientInterface, "Client interface" },
26+
{ GenerateRequestParameters, "Request parameters" },
27+
{ GenerateDescriptors, "Descriptors" },
28+
{ GenerateRequests, "Requests" },
29+
{ GenerateEnums, "Enums" },
30+
{ GenerateRawClient, "Lowlevel client" },
31+
{ GenerateRawDispatch, "Dispatch" },
32+
{ GenerateRequestParametersExtensions, "Request parameters override" },
3433
};
3534

3635
using (var pbar = new ProgressBar(actions.Count, "Generating code", new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
3736
{
38-
foreach(var kv in actions)
37+
foreach (var kv in actions)
3938
{
4039
pbar.Message = "Generating " + kv.Value;
4140
kv.Key(spec);
@@ -47,15 +46,17 @@ public static void Generate(string downloadBranch, params string[] folders)
4746
private static RestApiSpec CreateRestApiSpecModel(string downloadBranch, string[] folders)
4847
{
4948
var directories = Directory.GetDirectories(CodeConfiguration.RestSpecificationFolder, "*", SearchOption.AllDirectories)
50-
.Where(f=>folders == null || folders.Length == 0 || folders.Contains(new DirectoryInfo(f).Name))
49+
.Where(f => folders == null || folders.Length == 0 || folders.Contains(new DirectoryInfo(f).Name))
5150
.ToList();
5251

5352
var endpoints = new Dictionary<string, ApiEndpoint>();
54-
using (var pbar = new ProgressBar(directories.Count, $"Listing {directories.Count} directories", new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
53+
using (var pbar = new ProgressBar(directories.Count, $"Listing {directories.Count} directories",
54+
new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
5555
{
5656
foreach (var jsonFiles in directories.Select(dir => Directory.GetFiles(dir).Where(f => f.EndsWith(".json")).ToList()))
5757
{
58-
using (var fileProgress = pbar.Spawn(jsonFiles.Count, $"Listing {jsonFiles.Count} files", new ProgressBarOptions { ProgressCharacter = '─', BackgroundColor = ConsoleColor.DarkGray }))
58+
using (var fileProgress = pbar.Spawn(jsonFiles.Count, $"Listing {jsonFiles.Count} files",
59+
new ProgressBarOptions { ProgressCharacter = '─', BackgroundColor = ConsoleColor.DarkGray }))
5960
{
6061
foreach (var file in jsonFiles)
6162
{
@@ -68,6 +69,7 @@ private static RestApiSpec CreateRestApiSpecModel(string downloadBranch, string[
6869
var endpoint = CreateApiEndpoint(file);
6970
endpoints.Add(endpoint.Key, endpoint.Value);
7071
}
72+
7173
fileProgress.Tick();
7274
}
7375
}
@@ -137,41 +139,40 @@ private static Dictionary<string, ApiQueryParameters> CreateCommonApiQueryParame
137139
return commonParameters;
138140
}
139141

140-
private static string CreateMethodName(string apiEndpointKey)
141-
{
142-
return PascalCase(apiEndpointKey);
143-
}
142+
private static string CreateMethodName(string apiEndpointKey) => PascalCase(apiEndpointKey);
144143

145-
private static string DoRazor(string name, string template, RestApiSpec model)
146-
{
147-
return Razor.CompileRenderAsync(name, template, model).GetAwaiter().GetResult();
148-
}
144+
private static string DoRazor(string name, string template, RestApiSpec model) =>
145+
Razor.CompileRenderAsync(name, template, model).GetAwaiter().GetResult();
149146

150147
private static void GenerateClientInterface(RestApiSpec model)
151148
{
152149
var targetFile = CodeConfiguration.EsNetFolder + @"IElasticLowLevelClient.Generated.cs";
153-
var source = DoRazor(nameof(GenerateClientInterface), File.ReadAllText(CodeConfiguration.ViewFolder + @"IElasticLowLevelClient.Generated.cshtml"), model);
150+
var source = DoRazor(nameof(GenerateClientInterface),
151+
File.ReadAllText(CodeConfiguration.ViewFolder + @"IElasticLowLevelClient.Generated.cshtml"), model);
154152
File.WriteAllText(targetFile, source);
155153
}
156154

157155
private static void GenerateRawDispatch(RestApiSpec model)
158156
{
159157
var targetFile = CodeConfiguration.NestFolder + @"_Generated/_LowLevelDispatch.Generated.cs";
160-
var source = DoRazor(nameof(GenerateRawDispatch), File.ReadAllText(CodeConfiguration.ViewFolder + @"_LowLevelDispatch.Generated.cshtml"), model);
158+
var source = DoRazor(nameof(GenerateRawDispatch), File.ReadAllText(CodeConfiguration.ViewFolder + @"_LowLevelDispatch.Generated.cshtml"),
159+
model);
161160
File.WriteAllText(targetFile, source);
162161
}
163162

164163
private static void GenerateRawClient(RestApiSpec model)
165164
{
166165
var targetFile = CodeConfiguration.EsNetFolder + @"ElasticLowLevelClient.Generated.cs";
167-
var source = DoRazor(nameof(GenerateRawClient), File.ReadAllText(CodeConfiguration.ViewFolder + @"ElasticLowLevelClient.Generated.cshtml"), model);
166+
var source = DoRazor(nameof(GenerateRawClient),
167+
File.ReadAllText(CodeConfiguration.ViewFolder + @"ElasticLowLevelClient.Generated.cshtml"), model);
168168
File.WriteAllText(targetFile, source);
169169
}
170170

171171
private static void GenerateDescriptors(RestApiSpec model)
172172
{
173173
var targetFile = CodeConfiguration.NestFolder + @"_Generated\_Descriptors.Generated.cs";
174-
var source = DoRazor(nameof(GenerateDescriptors), File.ReadAllText(CodeConfiguration.ViewFolder + @"_Descriptors.Generated.cshtml"), model);
174+
var source = DoRazor(nameof(GenerateDescriptors), File.ReadAllText(CodeConfiguration.ViewFolder + @"_Descriptors.Generated.cshtml"),
175+
model);
175176
File.WriteAllText(targetFile, source);
176177
}
177178

@@ -185,14 +186,16 @@ private static void GenerateRequests(RestApiSpec model)
185186
private static void GenerateRequestParameters(RestApiSpec model)
186187
{
187188
var targetFile = CodeConfiguration.EsNetFolder + @"Domain\RequestParameters\RequestParameters.Generated.cs";
188-
var source = DoRazor(nameof(GenerateRequestParameters), File.ReadAllText(CodeConfiguration.ViewFolder + @"RequestParameters.Generated.cshtml"), model);
189+
var source = DoRazor(nameof(GenerateRequestParameters),
190+
File.ReadAllText(CodeConfiguration.ViewFolder + @"RequestParameters.Generated.cshtml"), model);
189191
File.WriteAllText(targetFile, source);
190192
}
191193

192194
private static void GenerateRequestParametersExtensions(RestApiSpec model)
193195
{
194196
var targetFile = CodeConfiguration.NestFolder + @"_Generated\_RequestParametersExtensions.Generated.cs";
195-
var source = DoRazor(nameof(GenerateRequestParametersExtensions), File.ReadAllText(CodeConfiguration.ViewFolder + @"_RequestParametersExtensions.Generated.cshtml"), model);
197+
var source = DoRazor(nameof(GenerateRequestParametersExtensions),
198+
File.ReadAllText(CodeConfiguration.ViewFolder + @"_RequestParametersExtensions.Generated.cshtml"), model);
196199
File.WriteAllText(targetFile, source);
197200
}
198201

src/CodeGeneration/ApiGenerator/CodeConfiguration.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,60 @@ namespace ApiGenerator
88
{
99
public static class CodeConfiguration
1010
{
11-
public static readonly Assembly Assembly = typeof(ApiGenerator).Assembly;
12-
1311
private static string _root = null;
14-
15-
private static string Root
16-
{
17-
get
18-
{
19-
if (CodeConfiguration._root != null) return CodeConfiguration._root;
20-
var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
21-
22-
var runningAsDnx =
23-
directoryInfo.Name == "ApiGenerator" &&
24-
directoryInfo.Parent != null &&
25-
directoryInfo.Parent.Name == "CodeGeneration";
26-
27-
CodeConfiguration._root = runningAsDnx ? "" : @"..\..\..\";
28-
return CodeConfiguration._root;
29-
}
30-
}
31-
32-
public static string NestFolder { get; } = $@"{Root}..\..\..\src\Nest\";
33-
public static string EsNetFolder { get; } = $@"{Root}..\..\..\src\Elasticsearch.Net\";
34-
public static string ViewFolder { get; } = $@"{Root}Views\";
35-
public static string RestSpecificationFolder { get; } = $@"{Root}RestSpecification\";
36-
public static string LastDownloadedVersionFile { get; } = Path.Combine(Root, "last_downloaded_version.txt");
12+
public static readonly Assembly Assembly = typeof(ApiGenerator).Assembly;
3713

3814
public static readonly Dictionary<string, string> MethodNameOverrides =
3915
(from f in new DirectoryInfo(NestFolder).GetFiles("*.cs", SearchOption.AllDirectories)
4016
let contents = File.ReadAllText(f.FullName)
4117
let c = Regex.Replace(contents, @"^.+\[DescriptorFor\(""([^ \r\n]+)""\)\].*$", "$1", RegexOptions.Singleline)
4218
where !c.Contains(" ") //filter results that did not match
4319
select new { Value = f.Name.Replace("Request", ""), Key = c })
44-
.DistinctBy(v => v.Key)
45-
.ToDictionary(k => k.Key, v => v.Value.Replace(".cs", ""));
20+
.DistinctBy(v => v.Key)
21+
.ToDictionary(k => k.Key, v => v.Value.Replace(".cs", ""));
4622

47-
public static readonly Dictionary<string, string> KnownDescriptors =
23+
public static readonly Dictionary<string, string> KnownDescriptors =
4824
(from f in new DirectoryInfo(NestFolder).GetFiles("*Request.cs", SearchOption.AllDirectories)
4925
let contents = File.ReadAllText(f.FullName)
5026
let c = Regex.Replace(contents, @"^.+class ([^ \r\n]+Descriptor(?:<[^>\r\n]+>)?[^ \r\n]*).*$", "$1", RegexOptions.Singleline)
5127
select new { Key = Regex.Replace(c, "<.*$", ""), Value = Regex.Replace(c, @"^.*?(?:(\<.+>).*?)?$", "$1") })
52-
.DistinctBy(v => v.Key)
53-
.OrderBy(v => v.Key)
54-
.ToDictionary(k => k.Key, v => v.Value);
28+
.DistinctBy(v => v.Key)
29+
.OrderBy(v => v.Key)
30+
.ToDictionary(k => k.Key, v => v.Value);
5531

56-
public static readonly Dictionary<string, string> KnownRequests =
32+
public static readonly Dictionary<string, string> KnownRequests =
5733
(from f in new DirectoryInfo(NestFolder).GetFiles("*Request.cs", SearchOption.AllDirectories)
5834
let contents = File.ReadAllText(f.FullName)
5935
let c = Regex.Replace(contents, @"^.+interface ([^ \r\n]+Request(?:<[^>\r\n]+>)?[^ \r\n]*).*$", "$1", RegexOptions.Singleline)
6036
where c.StartsWith("I") && c.Contains("Request")
6137
select new { Key = Regex.Replace(c, "<.*$", ""), Value = Regex.Replace(c, @"^.*?(?:(\<.+>).*?)?$", "$1") })
62-
.DistinctBy(v => v.Key)
63-
.ToDictionary(k => k.Key, v => v.Value);
38+
.DistinctBy(v => v.Key)
39+
.ToDictionary(k => k.Key, v => v.Value);
6440

41+
42+
public static string EsNetFolder { get; } = $@"{Root}..\..\..\src\Elasticsearch.Net\";
43+
public static string LastDownloadedVersionFile { get; } = Path.Combine(Root, "last_downloaded_version.txt");
44+
45+
public static string NestFolder { get; } = $@"{Root}..\..\..\src\Nest\";
46+
public static string RestSpecificationFolder { get; } = $@"{Root}RestSpecification\";
47+
public static string ViewFolder { get; } = $@"{Root}Views\";
48+
49+
private static string Root
50+
{
51+
get
52+
{
53+
if (_root != null) return _root;
54+
55+
var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
56+
57+
var runningAsDnx =
58+
directoryInfo.Name == "ApiGenerator" &&
59+
directoryInfo.Parent != null &&
60+
directoryInfo.Parent.Name == "CodeGeneration";
61+
62+
_root = runningAsDnx ? "" : @"..\..\..\";
63+
return _root;
64+
}
65+
}
6566
}
6667
}

0 commit comments

Comments
 (0)