@@ -171,7 +171,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
171171 max_line_length,
172172 } ;
173173
174- return run_linter ( input, linter_config, pretty) ;
174+ let input_gdscript_files = find_gdscript_files ( & input) ?;
175+ return run_linter ( input_gdscript_files, linter_config, pretty) ;
175176 }
176177
177178 let config = FormatterConfig {
@@ -203,18 +204,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
203204 return Ok ( ( ) ) ;
204205 }
205206
206- let input_gdscript_files: Vec < & PathBuf > = args
207- . input
208- . iter ( )
209- . filter ( |path| path. extension ( ) . is_some_and ( |ext| ext == "gd" ) )
210- . collect ( ) ;
211-
212- if input_gdscript_files. is_empty ( ) {
213- eprintln ! (
214- "Error: No GDScript files found in the arguments provided. Please provide at least one .gd file."
215- ) ;
216- std:: process:: exit ( 1 ) ;
217- }
207+ let input_gdscript_files = find_gdscript_files ( & args. input ) ?;
218208
219209 let total_files = input_gdscript_files. len ( ) ;
220210
@@ -245,7 +235,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
245235
246236 Ok ( FormatterOutput {
247237 index,
248- file_path : ( * file_path) . clone ( ) ,
238+ file_path : file_path. clone ( ) ,
249239 formatted_content,
250240 is_formatted,
251241 } )
@@ -355,6 +345,55 @@ fn run_linter(
355345 Ok ( ( ) )
356346}
357347
348+ fn find_gdscript_files (
349+ input_paths : & [ PathBuf ] ,
350+ ) -> Result < Vec < PathBuf > , Box < dyn std:: error:: Error > > {
351+ let mut gdscript_file_paths = Vec :: new ( ) ;
352+ let mut paths_to_check: Vec < PathBuf > = input_paths. iter ( ) . map ( |p| p. to_path_buf ( ) ) . collect ( ) ;
353+
354+ while let Some ( current_path) = paths_to_check. pop ( ) {
355+ if current_path. is_dir ( ) {
356+ let entries = fs:: read_dir ( & current_path) . map_err ( |error| {
357+ format ! (
358+ "Failed to read directory {}: {}" ,
359+ current_path. display( ) ,
360+ error
361+ )
362+ } ) ?;
363+ for entry in entries {
364+ let entry = entry. map_err ( |error| {
365+ format ! (
366+ "Failed to read entry in {}: {}" ,
367+ current_path. display( ) ,
368+ error
369+ )
370+ } ) ?;
371+ if entry. path ( ) . is_dir ( ) {
372+ paths_to_check. push ( entry. path ( ) ) ;
373+ } else if entry. path ( ) . extension ( ) . is_some_and ( |ext| ext == "gd" ) {
374+ gdscript_file_paths. push ( entry. path ( ) ) ;
375+ }
376+ }
377+ } else if current_path. extension ( ) . is_some_and ( |ext| ext == "gd" ) {
378+ gdscript_file_paths. push ( current_path) ;
379+ }
380+ }
381+ // We sort the files and deduplicate them so their order is deterministic
382+ // Plus to ensure you will not get any duplicated output, for example, when
383+ // linting or checking formatted files with multiple input paths.
384+ gdscript_file_paths. sort ( ) ;
385+ gdscript_file_paths. dedup ( ) ;
386+
387+ if gdscript_file_paths. is_empty ( ) {
388+ eprintln ! (
389+ "Error: No GDScript files found in the arguments provided. Please provide at least one .gd file or directory containing .gd files."
390+ ) ;
391+ std:: process:: exit ( 1 ) ;
392+ }
393+
394+ Ok ( gdscript_file_paths)
395+ }
396+
358397fn terminal_clear_line ( ) {
359398 eprint ! ( "\r {}" , " " . repeat( 80 ) ) ;
360399}
0 commit comments