aderyn_driver/
lib.rs

1pub(crate) mod config_helpers;
2pub mod driver;
3pub(crate) mod foundry_compiler_helpers;
4pub(crate) mod process_auto;
5pub(crate) mod project_compiler_tests;
6use std::path::Path;
7use std::path::PathBuf;
8
9pub use aderyn_core::ast as core_ast;
10pub use aderyn_core::context;
11pub use aderyn_core::detect as detection_modules;
12pub use aderyn_core::detect::detector;
13use cyfrin_foundry_compilers::utils;
14pub use foundry_compiler_helpers::*;
15pub use process_auto::with_project_root_at;
16
17fn ensure_valid_root_path(root_path: &Path) -> PathBuf {
18    if !root_path.exists() {
19        eprintln!("{} does not exist!", root_path.to_string_lossy());
20        std::process::exit(1);
21    }
22    utils::canonicalize(root_path).unwrap()
23}
24
25fn passes_src(src: &Option<Vec<PathBuf>>, solidity_file: &Path) -> bool {
26    if let Some(sources) = src {
27        return sources.iter().any(|s| solidity_file.starts_with(s));
28    }
29    true
30}
31
32fn passes_scope(
33    scope: &Option<Vec<String>>,
34    solidity_file: &Path,
35    absolute_root_path_str: &str,
36) -> bool {
37    let window = solidity_file.strip_prefix(absolute_root_path_str).unwrap();
38    let window_string = window.to_string_lossy().to_string();
39
40    if let Some(scope) = scope {
41        for include in scope {
42            if window_string.contains(include) {
43                return true;
44            }
45        }
46        return false;
47    }
48
49    true
50}
51
52fn passes_exclude(
53    exclude: &Option<Vec<String>>,
54    solidity_file: &Path,
55    absolute_root_path_str: &str,
56) -> bool {
57    let window = solidity_file.strip_prefix(absolute_root_path_str).unwrap();
58    let window_string = window.to_string_lossy().to_string();
59
60    if let Some(exclude) = exclude {
61        for dont_include in exclude {
62            if window_string.contains(dont_include) {
63                return false;
64            }
65        }
66        return true;
67    }
68
69    true
70}
71
72// Return a list of remappings in the format ["a=b", "c=d", "e=f"]
73// where direct imports a,c,e map to b,d,f
74fn read_remappings(root_path: &Path) -> Option<Vec<String>> {
75    // Look for a file called `remappings` in the project root. If not present, assume project doesn't require remappings
76    let remappings_file = root_path.join("remappings");
77    let remappings_txt_file = root_path.join("remappings.txt");
78
79    let r = {
80        if remappings_file.exists() {
81            remappings_file
82        } else if remappings_txt_file.exists() {
83            remappings_txt_file
84        } else {
85            return None;
86        }
87    };
88
89    // .unwrap_or(root_path.join("remappings.txt").canonicalize().ok()?);
90    let remappings_content = std::fs::read_to_string(r.canonicalize().unwrap()).unwrap();
91    Some(
92        remappings_content
93            .lines()
94            .filter_map(|x| {
95                if !x.is_empty() {
96                    Some(x.to_owned())
97                } else {
98                    None
99                }
100            })
101            .collect(),
102    )
103}