Skip to content

Commit 88a6ea7

Browse files
committed
Add support for recursive directory formatting
Also moved GDScript file collection to a shared function Close #226
1 parent aa2616f commit 88a6ea7

2 files changed

Lines changed: 56 additions & 29 deletions

File tree

src/linter.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,9 @@ impl GDScriptLinter {
165165

166166
pub fn lint_files(
167167
&mut self,
168-
input_files: Vec<PathBuf>,
168+
gdscript_files: Vec<PathBuf>,
169169
pretty: bool,
170170
) -> Result<bool, Box<dyn std::error::Error>> {
171-
let gdscript_files: Vec<&PathBuf> = input_files
172-
.iter()
173-
.filter(|path| path.extension().is_some_and(|ext| ext == "gd"))
174-
.collect();
175-
176-
if gdscript_files.is_empty() {
177-
eprintln!(
178-
"Error: No GDScript files found in the arguments provided. Please provide at least one .gd file."
179-
);
180-
std::process::exit(1);
181-
}
182-
183171
let with_colors = std::io::stdout().is_terminal();
184172

185173
if pretty {
@@ -191,7 +179,7 @@ impl GDScriptLinter {
191179

192180
fn lint_files_pretty(
193181
&mut self,
194-
gdscript_files: &[&PathBuf],
182+
gdscript_files: &[PathBuf],
195183
with_colors: bool,
196184
) -> Result<bool, Box<dyn std::error::Error>> {
197185
use std::collections::HashMap;
@@ -268,7 +256,7 @@ impl GDScriptLinter {
268256

269257
fn lint_files_standard(
270258
&mut self,
271-
gdscript_files: &[&PathBuf],
259+
gdscript_files: &[PathBuf],
272260
) -> Result<bool, Box<dyn std::error::Error>> {
273261
let mut has_issues = false;
274262

src/main.rs

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
358397
fn terminal_clear_line() {
359398
eprint!("\r{}", " ".repeat(80));
360399
}

0 commit comments

Comments
 (0)