traverse the filetree recursively; don't hardcode files

This commit is contained in:
raf 2025-07-22 02:31:40 +03:00
commit 38494fecbc
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -55,27 +55,17 @@ impl NixFileFixer for DefaultNixFileFixer {
fn find_nix_files(&self) -> Vec<PathBuf> { fn find_nix_files(&self) -> Vec<PathBuf> {
let mut files = Vec::new(); let mut files = Vec::new();
let candidates = [ let mut stack = vec![PathBuf::from(".")];
"default.nix", while let Some(dir) = stack.pop() {
"package.nix", if let Ok(entries) = fs::read_dir(&dir) {
"shell.nix", for entry in entries.flatten() {
"flake.nix", let path = entry.path();
"nix/default.nix", if path.is_dir() {
"nix/package.nix", stack.push(path);
"nix/site.nix", } else if let Some(ext) = path.extension() {
]; if ext.eq_ignore_ascii_case("nix") {
for candidate in &candidates { files.push(path);
let path = Path::new(candidate); }
if path.exists() {
files.push(path.to_path_buf());
}
}
if let Ok(entries) = fs::read_dir(".") {
for entry in entries.flatten() {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext.eq_ignore_ascii_case("nix") && !files.contains(&path) {
files.push(path);
} }
} }
} }
@ -96,16 +86,21 @@ impl NixFileFixer for DefaultNixFileFixer {
format!(r#"outputHash = "{new_hash}""#), format!(r#"outputHash = "{new_hash}""#),
), ),
]; ];
let mut new_content = content.clone();
let mut replaced = false;
for (pattern, replacement) in &patterns { for (pattern, replacement) in &patterns {
if let Ok(re) = Regex::new(pattern) { if let Ok(re) = Regex::new(pattern) {
if re.is_match(&content) { if re.is_match(&new_content) {
let new_content = re.replace_all(&content, replacement); new_content = re
if fs::write(file_path, new_content.as_ref()).is_ok() { .replace_all(&new_content, replacement.as_str())
return true; .into_owned();
} replaced = true;
} }
} }
} }
if replaced && fs::write(file_path, new_content).is_ok() {
return true;
}
} }
false false
} }