2727*/
2828
2929using System ;
30+ using System . Collections . Generic ;
31+ using System . IO ;
3032using System . Linq ;
3133using System . Threading ;
3234using System . Threading . Tasks ;
@@ -126,7 +128,8 @@ private static async Task<int> Generate(bool download, string branch, bool inclu
126128 await RestSpecDownloader . DownloadAsync ( downloadBranch , token ) ;
127129 }
128130
129- if ( ! generateCode ) return 0 ;
131+ if ( ! generateCode )
132+ return 0 ;
130133
131134 Console . WriteLine ( ) ;
132135 AnsiConsole . Write ( new Rule ( "[b white on chartreuse4] Loading specification [/]" ) . LeftJustified ( ) ) ;
@@ -150,6 +153,8 @@ private static async Task<int> Generate(bool download, string branch, bool inclu
150153
151154 await Generator . ApiGenerator . Generate ( lowLevelOnly , spec , token ) ;
152155
156+ RunDotNetFormat ( ) ;
157+
153158 var warnings = Generator . ApiGenerator . Warnings ;
154159 if ( warnings . Count > 0 )
155160 {
@@ -164,9 +169,63 @@ private static async Task<int> Generate(bool download, string branch, bool inclu
164169 return 0 ;
165170 }
166171
172+ private static void RunDotNetFormat ( )
173+ {
174+ var enviromentPath = System . Environment . GetEnvironmentVariable ( "PATH" ) ;
175+ var paths = enviromentPath . Split ( ';' ) ;
176+ var exePath = paths . Select ( x => Path . Combine ( x , "dotnet.exe" ) )
177+ . Where ( x => File . Exists ( x ) )
178+ . FirstOrDefault ( ) ;
179+
180+ Console . WriteLine ( ) ;
181+ AnsiConsole . Write ( new Rule ( "[b white on chartreuse4] Formatting Code using dotnet format [/]" ) . LeftJustified ( ) ) ;
182+ Console . WriteLine ( ) ;
183+
184+ var rootPath = GetRootPath ( typeof ( Program ) . Assembly . Location ) ;
185+
186+ var relativeFolderList = new List < string > ( ) ;
187+
188+ foreach ( var item in Generator . ApiGenerator . GeneratedFilePaths )
189+ {
190+ var relativePath = item . Replace ( rootPath . FullName , string . Empty ) ;
191+ relativeFolderList . Add ( $ ".{ relativePath . Replace ( "\\ " , "/" ) } ") ;
192+ }
193+
194+ var si = new System . Diagnostics . ProcessStartInfo
195+ {
196+ WorkingDirectory = rootPath . FullName ,
197+ FileName = "dotnet" ,
198+ Arguments = "format -v diag --include " + string . Join ( ' ' , relativeFolderList . Select ( item => $ "{ item } ") ) ,
199+ RedirectStandardInput = true ,
200+ RedirectStandardOutput = true ,
201+ RedirectStandardError = true ,
202+ UseShellExecute = false ,
203+ CreateNoWindow = true ,
204+ } ;
205+
206+ var process = System . Diagnostics . Process . Start ( si ) ;
207+
208+ Console . WriteLine ( ) ;
209+ Console . WriteLine ( $ "Running dotnet format --include { string . Join ( ' ' , relativeFolderList . Select ( item => $ "{ item } ") ) } -v diag") ;
210+
211+ // hookup the eventhandlers to capture the data that is received
212+ process . OutputDataReceived += ( sender , args ) => Console . WriteLine ( args . Data ) ;
213+ process . ErrorDataReceived += ( sender , args ) => Console . WriteLine ( args . Data ) ;
214+
215+ process . Start ( ) ;
216+
217+ // start our event pumps
218+ process . BeginOutputReadLine ( ) ;
219+ process . BeginErrorReadLine ( ) ;
220+
221+ process . WaitForExit ( ) ;
222+ Console . WriteLine ( ) ;
223+ }
224+
167225 private static bool Ask ( string question , bool defaultAnswer = true )
168226 {
169- if ( ! Interactive ) return defaultAnswer ;
227+ if ( ! Interactive )
228+ return defaultAnswer ;
170229
171230 var answer = "invalid" ;
172231 var defaultResponse = defaultAnswer ? "y" : "n" ;
@@ -175,10 +234,35 @@ private static bool Ask(string question, bool defaultAnswer = true)
175234 {
176235 Console . Write ( $ "{ question } [y/N] (default { defaultResponse } ): ") ;
177236 answer = Console . ReadLine ( ) ? . Trim ( ) . ToLowerInvariant ( ) ;
178- if ( string . IsNullOrWhiteSpace ( answer ) ) answer = defaultResponse ;
237+ if ( string . IsNullOrWhiteSpace ( answer ) )
238+ answer = defaultResponse ;
179239 defaultAnswer = answer == "y" ;
180240 }
181241 return defaultAnswer ;
182242 }
243+
244+ /// <summary>
245+ /// Since we are most likely not running in the root of the project, we need to find the root path that contains the sln
246+ /// </summary>
247+ /// <param name="basePath"></param>
248+ /// <returns></returns>
249+ private static DirectoryInfo GetRootPath ( string basePath ) => RecursiveFindRoot ( new FileInfo ( basePath ) . Directory ) ;
250+
251+ private static DirectoryInfo RecursiveFindRoot ( DirectoryInfo directory )
252+ {
253+ if ( directory is null )
254+ {
255+ return null ;
256+ }
257+
258+ var file = directory . GetFiles ( "*.sln" ) . FirstOrDefault ( item => item . Name . Equals ( "OpenSearch.sln" , StringComparison . OrdinalIgnoreCase ) ) ;
259+
260+ if ( file is not null )
261+ {
262+ return directory ;
263+ }
264+
265+ return RecursiveFindRoot ( directory . Parent ) ;
266+ }
183267 }
184268}
0 commit comments