|
1 | 1 | use goblin::{self, Object};
|
| 2 | +use ignore::WalkBuilder; |
2 | 3 | use std::collections::HashMap;
|
3 | 4 | use std::fs;
|
4 | 5 | use std::path::{Path, PathBuf};
|
5 | 6 |
|
6 |
| -pub fn run(paths: &[&str]) { |
7 |
| - let blob_paths = find_blobs_in_paths(&paths); |
| 7 | +pub struct Config { |
| 8 | + pub recursive: bool, |
| 9 | +} |
| 10 | + |
| 11 | +pub fn run(paths: &[&str], config: Config) { |
| 12 | + let file_paths: Vec<PathBuf> = if config.recursive { |
| 13 | + find_files_recursively(&paths) |
| 14 | + } else { |
| 15 | + find_files(&paths) |
| 16 | + }; |
| 17 | + |
| 18 | + let blob_paths: Vec<&PathBuf> = file_paths |
| 19 | + .iter() |
| 20 | + .filter(|path| match path.extension() { |
| 21 | + // Assume that valid blobs have ".so" extension. |
| 22 | + Some(ext) => ext == "so", |
| 23 | + None => false, |
| 24 | + }) |
| 25 | + .collect(); |
| 26 | + |
8 | 27 | let blobs_to_dependencies = get_dependencies(&blob_paths);
|
9 | 28 | let missing_blobs = identify_missing(&blobs_to_dependencies);
|
10 | 29 | display_missing_blobs(&missing_blobs);
|
11 | 30 | }
|
12 | 31 |
|
13 |
| -fn find_blobs_in_paths(paths: &[&str]) -> Vec<PathBuf> { |
| 32 | +fn find_files(paths: &[&str]) -> Vec<PathBuf> { |
14 | 33 | let dirs = paths
|
15 | 34 | .iter()
|
16 | 35 | .map(Path::new)
|
17 | 36 | .filter(|path| path.is_dir())
|
18 | 37 | .collect::<Vec<_>>();
|
19 | 38 |
|
20 |
| - let blob_paths: Vec<PathBuf> = dirs |
| 39 | + let file_paths: Vec<PathBuf> = dirs |
21 | 40 | .iter()
|
22 | 41 | .map(|dir| fs::read_dir(dir).expect("Could not read directory."))
|
23 | 42 | .flat_map(|read_dir| {
|
24 | 43 | read_dir.map(|dir_entry| dir_entry.expect("Could not read directory entry.").path())
|
25 | 44 | })
|
26 |
| - .filter(|path| match path.extension() { |
27 |
| - // Assume that valid blobs have ".so" extension. |
28 |
| - Some(ext) => ext == "so", |
29 |
| - None => false, |
| 45 | + .collect(); |
| 46 | + |
| 47 | + file_paths |
| 48 | +} |
| 49 | + |
| 50 | +fn find_files_recursively(paths: &[&str]) -> Vec<PathBuf> { |
| 51 | + let mut walker = WalkBuilder::new(paths[0]); |
| 52 | + for path in &paths[1..] { |
| 53 | + walker.add(path); |
| 54 | + } |
| 55 | + |
| 56 | + // Don't read from ignore configs |
| 57 | + walker |
| 58 | + .ignore(false) |
| 59 | + .git_ignore(false) |
| 60 | + .git_exclude(false) |
| 61 | + .git_global(false); |
| 62 | + |
| 63 | + let file_paths = walker |
| 64 | + .build() |
| 65 | + .map(|dir_entry| { |
| 66 | + dir_entry |
| 67 | + .expect("Could not read directory entry.") |
| 68 | + .into_path() |
30 | 69 | })
|
31 | 70 | .collect();
|
32 | 71 |
|
33 |
| - blob_paths |
| 72 | + file_paths |
34 | 73 | }
|
35 | 74 |
|
36 |
| -fn get_dependencies(blob_paths: &Vec<PathBuf>) -> HashMap<String, Vec<String>> { |
| 75 | +fn get_dependencies(blob_paths: &Vec<&PathBuf>) -> HashMap<String, Vec<String>> { |
37 | 76 | let mut dependencies: HashMap<String, Vec<String>> = HashMap::new();
|
38 | 77 |
|
39 | 78 | blob_paths.iter().for_each(|path| {
|
|
0 commit comments