|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use glob::Pattern; |
| 4 | +use ignore::WalkBuilder; |
| 5 | + |
| 6 | +pub fn list_files<P: AsRef<Path>>( |
| 7 | + root: P, |
| 8 | + include_patterns: Vec<String>, |
| 9 | + exclude_patterns: Option<Vec<String>>, |
| 10 | + ignore_git_ignore: bool, |
| 11 | +) -> Vec<PathBuf> { |
| 12 | + include_patterns |
| 13 | + .iter() |
| 14 | + .flat_map(|pattern| { |
| 15 | + WalkBuilder::new(root.as_ref()) |
| 16 | + .hidden(false) |
| 17 | + .git_ignore(!ignore_git_ignore) |
| 18 | + .build() |
| 19 | + .filter_map(std::result::Result::ok) |
| 20 | + .filter(|entry| { |
| 21 | + let path = entry.path(); |
| 22 | + let relative_path = path.strip_prefix(root.as_ref()).unwrap(); |
| 23 | + is_json_file(&path.to_path_buf()) |
| 24 | + && Pattern::new(pattern).unwrap().matches_path(relative_path) |
| 25 | + && !is_excluded(relative_path, &exclude_patterns) |
| 26 | + }) |
| 27 | + .map(|entry| entry.path().to_path_buf()) |
| 28 | + .collect::<Vec<_>>() |
| 29 | + }) |
| 30 | + .collect() |
| 31 | +} |
| 32 | + |
| 33 | +fn is_excluded(path: &Path, exclude_patterns: &Option<Vec<String>>) -> bool { |
| 34 | + match exclude_patterns { |
| 35 | + Some(patterns) => patterns |
| 36 | + .iter() |
| 37 | + .any(|pattern| Pattern::new(pattern).unwrap().matches_path(path)), |
| 38 | + None => false, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub fn is_json_file(path: &PathBuf) -> bool { |
| 43 | + path.extension() |
| 44 | + .map(|ext| ext == "json" || ext == "jsonc") |
| 45 | + .unwrap_or(false) |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(test)] |
| 49 | +mod tests { |
| 50 | + use super::*; |
| 51 | + use std::fs::{self, File}; |
| 52 | + use tempfile::TempDir; |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_is_json_file() { |
| 56 | + assert!(is_json_file(&PathBuf::from("test.json"))); |
| 57 | + assert!(is_json_file(&PathBuf::from("test.jsonc"))); |
| 58 | + assert!(!is_json_file(&PathBuf::from("test.txt"))); |
| 59 | + assert!(!is_json_file(&PathBuf::from("test"))); |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_list_files() { |
| 64 | + let temp_dir = TempDir::new().unwrap(); |
| 65 | + let temp_path = temp_dir.path(); |
| 66 | + |
| 67 | + std::process::Command::new("git") |
| 68 | + .args(&["init"]) |
| 69 | + .current_dir(temp_path) |
| 70 | + .output() |
| 71 | + .expect("Failed to initialize git repo"); |
| 72 | + |
| 73 | + fs::write(temp_path.join(".gitignore"), "ignored.json\n").unwrap(); |
| 74 | + |
| 75 | + File::create(temp_path.join("test1.json")).unwrap(); |
| 76 | + File::create(temp_path.join("test2.jsonc")).unwrap(); |
| 77 | + File::create(temp_path.join("test3.txt")).unwrap(); |
| 78 | + File::create(temp_path.join("ignored.json")).unwrap(); |
| 79 | + |
| 80 | + let subdir = temp_path.join("subdir"); |
| 81 | + fs::create_dir(&subdir).unwrap(); |
| 82 | + File::create(subdir.join("test4.json")).unwrap(); |
| 83 | + File::create(subdir.join("test5.jsonc")).unwrap(); |
| 84 | + |
| 85 | + let files = list_files( |
| 86 | + temp_path, |
| 87 | + vec!["**/*.json".to_string(), "**/*.jsonc".to_string()], |
| 88 | + None, |
| 89 | + false, |
| 90 | + ); |
| 91 | + |
| 92 | + assert_eq!(files.len(), 4); |
| 93 | + assert!(files.contains(&temp_path.join("test1.json"))); |
| 94 | + assert!(files.contains(&temp_path.join("test2.jsonc"))); |
| 95 | + assert!(files.contains(&temp_path.join("subdir/test4.json"))); |
| 96 | + assert!(files.contains(&temp_path.join("subdir/test5.jsonc"))); |
| 97 | + assert!(!files.contains(&temp_path.join("ignored.json"))); |
| 98 | + |
| 99 | + let files = list_files( |
| 100 | + temp_path, |
| 101 | + vec!["**/*.json".to_string(), "**/*.jsonc".to_string()], |
| 102 | + None, |
| 103 | + true, |
| 104 | + ); |
| 105 | + |
| 106 | + assert_eq!(files.len(), 5); |
| 107 | + assert!(files.contains(&temp_path.join("ignored.json"))); |
| 108 | + |
| 109 | + let files = list_files(temp_path, vec!["**/test*.json".to_string()], None, false); |
| 110 | + assert_eq!(files.len(), 2); |
| 111 | + assert!(files.contains(&temp_path.join("test1.json"))); |
| 112 | + assert!(files.contains(&temp_path.join("subdir/test4.json"))); |
| 113 | + |
| 114 | + let files = list_files(temp_path, vec!["**/ignored.json".to_string()], None, false); |
| 115 | + assert_eq!(files.len(), 0); |
| 116 | + |
| 117 | + let files = list_files( |
| 118 | + temp_path, |
| 119 | + vec!["**/*.json".to_string(), "**/*.jsonc".to_string()], |
| 120 | + Some(vec!["**/test2*".to_string()]), |
| 121 | + false, |
| 122 | + ); |
| 123 | + assert_eq!(files.len(), 3); |
| 124 | + assert!(files.contains(&temp_path.join("test1.json"))); |
| 125 | + assert!(!files.contains(&temp_path.join("test2.jsonc"))); |
| 126 | + assert!(files.contains(&temp_path.join("subdir/test4.json"))); |
| 127 | + assert!(files.contains(&temp_path.join("subdir/test5.jsonc"))); |
| 128 | + assert!(!files.contains(&temp_path.join("ignored.json"))); |
| 129 | + |
| 130 | + let files = list_files( |
| 131 | + temp_path, |
| 132 | + vec!["**/*.json".to_string(), "**/*.jsonc".to_string()], |
| 133 | + Some(vec!["**/test2*".to_string()]), |
| 134 | + true, |
| 135 | + ); |
| 136 | + assert_eq!(files.len(), 4); |
| 137 | + assert!(files.contains(&temp_path.join("test1.json"))); |
| 138 | + assert!(!files.contains(&temp_path.join("test2.jsonc"))); |
| 139 | + assert!(files.contains(&temp_path.join("subdir/test4.json"))); |
| 140 | + assert!(files.contains(&temp_path.join("subdir/test5.jsonc"))); |
| 141 | + assert!(files.contains(&temp_path.join("ignored.json"))); |
| 142 | + } |
| 143 | +} |
0 commit comments