|
| 1 | +use clap::{ArgAction, Parser, Subcommand, ValueHint}; |
| 2 | +use eyre::Result; |
| 3 | +use foundry_compilers::Graph; |
| 4 | +use foundry_config::Config; |
| 5 | +use semver::Version; |
| 6 | +use std::{collections::BTreeMap, path::PathBuf}; |
| 7 | + |
| 8 | +/// CLI arguments for `forge compiler`. |
| 9 | +#[derive(Debug, Parser)] |
| 10 | +pub struct CompilerArgs { |
| 11 | + #[command(subcommand)] |
| 12 | + pub sub: CompilerSubcommands, |
| 13 | +} |
| 14 | + |
| 15 | +impl CompilerArgs { |
| 16 | + pub fn run(self) -> Result<()> { |
| 17 | + match self.sub { |
| 18 | + CompilerSubcommands::Resolve(args) => args.run(), |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Subcommand)] |
| 24 | +pub enum CompilerSubcommands { |
| 25 | + /// Retrieves the resolved version(s) of the compiler within the project. |
| 26 | + #[command(visible_alias = "r")] |
| 27 | + Resolve(ResolveArgs), |
| 28 | +} |
| 29 | + |
| 30 | +/// CLI arguments for `forge compiler resolve`. |
| 31 | +#[derive(Debug, Parser)] |
| 32 | +pub struct ResolveArgs { |
| 33 | + /// The root directory |
| 34 | + #[arg(long, short, value_hint = ValueHint::DirPath, value_name = "PATH")] |
| 35 | + root: Option<PathBuf>, |
| 36 | + |
| 37 | + /// Skip files that match the given regex pattern. |
| 38 | + #[arg(long, short, value_name = "REGEX")] |
| 39 | + skip: Option<regex::Regex>, |
| 40 | + |
| 41 | + /// Verbosity of the output. |
| 42 | + /// |
| 43 | + /// Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv). |
| 44 | + /// |
| 45 | + /// Verbosity levels: |
| 46 | + /// - 2: Print source paths. |
| 47 | + #[arg(long, short, verbatim_doc_comment, action = ArgAction::Count, help_heading = "Display options")] |
| 48 | + pub verbosity: u8, |
| 49 | + |
| 50 | + /// Print as JSON. |
| 51 | + #[arg(long, short, help_heading = "Display options")] |
| 52 | + json: bool, |
| 53 | +} |
| 54 | + |
| 55 | +impl ResolveArgs { |
| 56 | + pub fn run(self) -> Result<()> { |
| 57 | + let Self { root, skip, verbosity, json } = self; |
| 58 | + |
| 59 | + let root = root.unwrap_or_else(|| PathBuf::from(".")); |
| 60 | + let config = Config::load_with_root(&root); |
| 61 | + let project = config.project()?; |
| 62 | + |
| 63 | + let graph = Graph::resolve(&project.paths)?; |
| 64 | + let (sources, _) = graph.into_sources_by_version( |
| 65 | + project.offline, |
| 66 | + &project.locked_versions, |
| 67 | + &project.compiler, |
| 68 | + )?; |
| 69 | + |
| 70 | + let mut output: BTreeMap<String, Vec<(Version, Vec<String>)>> = BTreeMap::new(); |
| 71 | + |
| 72 | + for (language, sources) in sources { |
| 73 | + let mut versions_with_paths: Vec<(Version, Vec<String>)> = sources |
| 74 | + .iter() |
| 75 | + .map(|(version, sources)| { |
| 76 | + let paths: Vec<String> = sources |
| 77 | + .iter() |
| 78 | + .filter_map(|(path_file, _)| { |
| 79 | + let path_str = path_file |
| 80 | + .strip_prefix(&project.paths.root) |
| 81 | + .unwrap_or(path_file) |
| 82 | + .to_path_buf() |
| 83 | + .display() |
| 84 | + .to_string(); |
| 85 | + |
| 86 | + // Skip files that match the given regex pattern. |
| 87 | + if let Some(ref regex) = skip { |
| 88 | + if regex.is_match(&path_str) { |
| 89 | + return None; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Some(path_str) |
| 94 | + }) |
| 95 | + .collect(); |
| 96 | + |
| 97 | + (version.clone(), paths) |
| 98 | + }) |
| 99 | + .filter(|(_, paths)| !paths.is_empty()) |
| 100 | + .collect(); |
| 101 | + |
| 102 | + // Sort by SemVer version. |
| 103 | + versions_with_paths.sort_by(|(v1, _), (v2, _)| Version::cmp(v1, v2)); |
| 104 | + |
| 105 | + // Skip language if no paths are found after filtering. |
| 106 | + if !versions_with_paths.is_empty() { |
| 107 | + output.insert(language.to_string(), versions_with_paths); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if json { |
| 112 | + println!("{}", serde_json::to_string(&output)?); |
| 113 | + return Ok(()); |
| 114 | + } |
| 115 | + |
| 116 | + for (language, versions) in &output { |
| 117 | + if verbosity < 1 { |
| 118 | + println!("{language}:"); |
| 119 | + } else { |
| 120 | + println!("{language}:\n"); |
| 121 | + } |
| 122 | + |
| 123 | + for (version, paths) in versions { |
| 124 | + if verbosity >= 1 { |
| 125 | + println!("{version}:"); |
| 126 | + for (idx, path) in paths.iter().enumerate() { |
| 127 | + if idx == paths.len() - 1 { |
| 128 | + println!("└── {path}\n"); |
| 129 | + } else { |
| 130 | + println!("├── {path}"); |
| 131 | + } |
| 132 | + } |
| 133 | + } else { |
| 134 | + println!("- {version}"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if verbosity < 1 { |
| 139 | + println!(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | +} |
0 commit comments