1717
1818namespace CodeGeneration . LowLevelClient
1919{
20- public static class ApiGenerator
20+ public class ApiGenerator
2121 {
22- private static readonly string NestFolder = @"..\..\..\..\..\src\Nest\" ;
23- private static readonly string EsNetFolder = @"..\..\..\..\..\src\Elasticsearch.Net\" ;
24- private static readonly string ViewFolder = @"..\..\Views\" ;
25- private static readonly string ApiEndpointsFolder = @"..\..\ApiEndpoints\" ;
22+ private static string NestFolder ;
23+ private static string EsNetFolder ;
24+ private static string ViewFolder ;
25+ private static string ApiEndpointsFolder ;
2626 private static readonly RazorMachine RazorHelper ;
27-
2827 private static readonly string Version = "2.0" ;
2928 private static readonly List < string > ApiListings = new List < string >
3029 {
@@ -38,20 +37,42 @@ static ApiGenerator()
3837 {
3938 RazorHelper = new RazorMachine ( ) ;
4039 Assembly = typeof ( ApiGenerator ) . Assembly ;
40+
41+ var directoryInfo = new DirectoryInfo ( Directory . GetCurrentDirectory ( ) ) ;
42+
43+ if ( directoryInfo . Name == "CodeGeneration.LowLevelClient" &&
44+ directoryInfo . Parent != null &&
45+ directoryInfo . Parent . Name == "CodeGeneration" )
46+ {
47+ // running as a dnx project
48+ NestFolder = @"..\..\..\src\Nest\" ;
49+ EsNetFolder = @"..\..\..\src\Elasticsearch.Net\" ;
50+ ViewFolder = @"Views\" ;
51+ ApiEndpointsFolder = @"ApiEndpoints\" ;
52+ }
53+ else
54+ {
55+ NestFolder = @"..\..\..\..\..\src\Nest\" ;
56+ EsNetFolder = @"..\..\..\..\..\src\Elasticsearch.Net\" ;
57+ ViewFolder = @"..\..\Views\" ;
58+ ApiEndpointsFolder = @"..\..\ApiEndpoints\" ;
59+ }
4160 }
61+
4262 public static string PascalCase ( string s )
4363 {
4464 var textInfo = new CultureInfo ( "en-US" ) . TextInfo ;
4565 return textInfo . ToTitleCase ( s . ToLowerInvariant ( ) ) . Replace ( "_" , string . Empty ) . Replace ( "." , string . Empty ) ;
4666 }
47- public static void GenerateEndpointFiles ( )
67+
68+ public void GenerateEndpointFiles ( )
4869 {
4970 Console . WriteLine ( "Getting a listing of all the api endpoints from the elasticsearch-rest-api-spec repos" ) ;
5071 foreach ( var listing in ApiListings . Select ( l=> l . Replace ( "{version}" , Version ) ) )
5172 DownloadJsonDefinitions ( listing ) ;
5273 }
5374
54- private static void DownloadJsonDefinitions ( string listingUrl )
75+ private void DownloadJsonDefinitions ( string listingUrl )
5576 {
5677 using ( var client = new WebClient ( ) )
5778 {
@@ -60,7 +81,7 @@ private static void DownloadJsonDefinitions(string listingUrl)
6081 }
6182 }
6283
63- private static void FindJsonFilesOnListing ( string listingUrl , string html )
84+ private void FindJsonFilesOnListing ( string listingUrl , string html )
6485 {
6586 var dom = CQ . Create ( html ) ;
6687
@@ -73,7 +94,9 @@ private static void FindJsonFilesOnListing(string listingUrl, string html)
7394
7495 endpoints . ForEach ( s => WriteEndpointFile ( listingUrl , s ) ) ;
7596 }
76- private static void WriteEndpointFile ( string listingUrl , string s )
97+
98+
99+ private void WriteEndpointFile ( string listingUrl , string s )
77100 {
78101 using ( var client = new WebClient ( ) )
79102 {
@@ -86,7 +109,7 @@ private static void WriteEndpointFile(string listingUrl, string s)
86109 }
87110
88111
89- public static RestApiSpec GetRestApiSpec ( )
112+ public RestApiSpec GetRestApiSpec ( )
90113 {
91114 var spec = new RestApiSpec
92115 {
@@ -100,10 +123,11 @@ public static RestApiSpec GetRestApiSpec()
100123 return spec ;
101124 }
102125
103- private static KeyValuePair < string , ApiEndpoint > CreateApiEndpoint ( string jsonFile )
126+ private KeyValuePair < string , ApiEndpoint > CreateApiEndpoint ( string jsonFile )
104127 {
105128 var json = File . ReadAllText ( jsonFile ) ;
106129 var endpoint = JsonConvert . DeserializeObject < Dictionary < string , ApiEndpoint > > ( json ) . First ( ) ;
130+ endpoint . Value . Generator = this ;
107131 endpoint . Value . CsharpMethodName = CreateMethodName ( endpoint . Key ) ;
108132 return endpoint ;
109133 }
@@ -116,7 +140,7 @@ private static string LocalUri(string file)
116140 return fileUri ;
117141 }
118142
119- private static readonly Dictionary < string , string > MethodNameOverrides =
143+ private readonly Dictionary < string , string > MethodNameOverrides =
120144 ( from f in new DirectoryInfo ( NestFolder ) . GetFiles ( "*.cs" , SearchOption . AllDirectories )
121145 let contents = File . ReadAllText ( f . FullName )
122146 let c = Regex . Replace ( contents , @"^.+\[DescriptorFor\(""([^ \r\n]+)""\)\].*$" , "$1" , RegexOptions . Singleline )
@@ -125,7 +149,7 @@ private static string LocalUri(string file)
125149 . DistinctBy ( v => v . Key )
126150 . ToDictionary ( k => k . Key , v => v . Value . Replace ( ".cs" , "" ) ) ;
127151
128- private static readonly Dictionary < string , string > KnownDescriptors =
152+ private readonly Dictionary < string , string > KnownDescriptors =
129153 ( from f in new DirectoryInfo ( NestFolder ) . GetFiles ( "*Request.cs" , SearchOption . AllDirectories )
130154 let contents = File . ReadAllText ( f . FullName )
131155 let c = Regex . Replace ( contents , @"^.+class ([^ \r\n]+Descriptor(?:<[^>\r\n]+>)?[^ \r\n]*).*$" , "$1" , RegexOptions . Singleline )
@@ -134,7 +158,7 @@ private static string LocalUri(string file)
134158 . OrderBy ( v=> v . Key )
135159 . ToDictionary ( k => k . Key , v => v . Value ) ;
136160
137- private static readonly Dictionary < string , string > KnownRequests =
161+ private readonly Dictionary < string , string > KnownRequests =
138162 ( from f in new DirectoryInfo ( NestFolder ) . GetFiles ( "*Request.cs" , SearchOption . AllDirectories )
139163 let contents = File . ReadAllText ( f . FullName )
140164 let c = Regex . Replace ( contents , @"^.+interface ([^ \r\n]+Request(?:<[^>\r\n]+>)?[^ \r\n]*).*$" , "$1" , RegexOptions . Singleline )
@@ -146,7 +170,7 @@ where c.StartsWith("I") && c.Contains("Request")
146170
147171 //Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
148172 //or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
149- public static void PatchMethod ( CsharpMethod method )
173+ public void PatchMethod ( CsharpMethod method )
150174 {
151175 Func < string , bool > ms = s => method . FullName . StartsWith ( s ) ;
152176 Func < string , bool > pc = s => method . Path . Contains ( s ) ;
@@ -260,62 +284,62 @@ public static string CreateMethodName(string apiEnpointKey)
260284 return PascalCase ( apiEnpointKey ) ;
261285 }
262286
263- public static void GenerateClientInterface ( RestApiSpec model )
287+ public void GenerateClientInterface ( RestApiSpec model )
264288 {
265289 var targetFile = EsNetFolder + @"IElasticsearchClient.Generated.cs" ;
266290 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"IElasticsearchClient.Generated.cshtml" ) , model ) . ToString ( ) ;
267291 File . WriteAllText ( targetFile , source ) ;
268292 }
269293
270294
271- public static void GenerateRawDispatch ( RestApiSpec model )
295+ public void GenerateRawDispatch ( RestApiSpec model )
272296 {
273297 var targetFile = NestFolder + @"_Generated/_LowLevelDispatch.Generated.cs" ;
274298 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"_LowLevelDispatch.Generated.cshtml" ) , model ) . ToString ( ) ;
275299 File . WriteAllText ( targetFile , source ) ;
276300 }
277- public static void GenerateRawClient ( RestApiSpec model )
301+ public void GenerateRawClient ( RestApiSpec model )
278302 {
279303 var targetFile = EsNetFolder + @"ElasticsearchClient.Generated.cs" ;
280304 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"ElasticsearchClient.Generated.cshtml" ) , model ) . ToString ( ) ;
281305 File . WriteAllText ( targetFile , source ) ;
282306 }
283307
284- public static void GenerateDescriptors ( RestApiSpec model )
308+ public void GenerateDescriptors ( RestApiSpec model )
285309 {
286310 var targetFile = NestFolder + @"_Generated\_Descriptors.Generated.cs" ;
287311 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"_Descriptors.Generated.cshtml" ) , model ) . ToString ( ) ;
288312 File . WriteAllText ( targetFile , source ) ;
289313 }
290314
291- public static void GenerateRequests ( RestApiSpec model )
315+ public void GenerateRequests ( RestApiSpec model )
292316 {
293317 var targetFile = NestFolder + @"_Generated\_Requests.Generated.cs" ;
294318 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"_Requests.Generated.cshtml" ) , model ) . ToString ( ) ;
295319 File . WriteAllText ( targetFile , source ) ;
296320 }
297321
298- public static void GenerateRequestParameters ( RestApiSpec model )
322+ public void GenerateRequestParameters ( RestApiSpec model )
299323 {
300324 var targetFile = EsNetFolder + @"Domain\RequestParameters\RequestParameters.Generated.cs" ;
301325 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"RequestParameters.Generated.cshtml" ) , model ) . ToString ( ) ;
302326 File . WriteAllText ( targetFile , source ) ;
303327 }
304328
305- public static void GenerateRequestParametersExtensions ( RestApiSpec model )
329+ public void GenerateRequestParametersExtensions ( RestApiSpec model )
306330 {
307331 var targetFile = NestFolder + @"_Generated\_RequestParametersExtensions.Generated.cs" ;
308332 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"_RequestParametersExtensions.Generated.cshtml" ) , model ) . ToString ( ) ;
309333 File . WriteAllText ( targetFile , source ) ;
310334 }
311- public static void GenerateEnums ( RestApiSpec model )
335+ public void GenerateEnums ( RestApiSpec model )
312336 {
313337 var targetFile = EsNetFolder + @"Domain\Enums.Generated.cs" ;
314338 var source = RazorHelper . Execute ( File . ReadAllText ( ViewFolder + @"Enums.Generated.cshtml" ) , model ) . ToString ( ) ;
315339 File . WriteAllText ( targetFile , source ) ;
316340 }
317341
318- private static void WriteToEndpointsFolder ( string filename , string contents )
342+ private void WriteToEndpointsFolder ( string filename , string contents )
319343 {
320344 if ( ! Directory . Exists ( ApiEndpointsFolder ) )
321345 Directory . CreateDirectory ( ApiEndpointsFolder ) ;
0 commit comments