Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forge): add compiler subcommand #7909

Merged
merged 20 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions crates/forge/bin/cmd/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use clap::{Parser, ValueHint};
use eyre::Result;
use foundry_compilers::Graph;
use foundry_config::Config;
use std::path::PathBuf;

/// CLI arguments for `forge compiler`.
#[derive(Clone, Debug, Parser)]
pub enum CompilerSubcommands {
/// Retrieves the resolved version(s) of the compiler within the project.
#[command(visible_alias = "r")]
Resolve {
/// The root directory
#[arg(value_hint = ValueHint::DirPath, default_value = ".", value_name = "PATH")]
root: PathBuf,
},
}

impl CompilerSubcommands {
pub async fn run(self) -> Result<()> {
match &self {
Self::Resolve { root } => self.handle_version_resolving(root).await,
}
}

/// Retrieves the resolved version(s) of the compiler within the project.
async fn handle_version_resolving(&self, root: &PathBuf) -> Result<()> {
let config = Config::load_with_root(root);
let project = config.project()?;

let graph = Graph::resolve(&project.paths)?;
let (sources, _) = graph.into_sources_by_version(
project.offline,
&project.locked_versions,
&project.compiler,
)?;

for (language, sources) in sources {
for (version, sources) in sources {
println!("{language} {version}:");
for (idx, (path_file, _)) in sources.iter().enumerate() {
let path = path_file
.strip_prefix(&project.paths.root)
.unwrap_or(path_file)
.to_path_buf();
let prefix = if idx == sources.len() - 1 { "└──" } else { "├──" };
println!("{prefix} {}", path.display());
}
}
}

Ok(())
}
}
1 change: 1 addition & 0 deletions crates/forge/bin/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod bind_json;
pub mod build;
pub mod cache;
pub mod clone;
pub mod compiler;
pub mod config;
pub mod coverage;
pub mod create;
Expand Down
1 change: 1 addition & 0 deletions crates/forge/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn main() -> Result<()> {
ForgeSubcommand::Generate(cmd) => match cmd.sub {
GenerateSubcommands::Test(cmd) => cmd.run(),
},
ForgeSubcommand::Compiler { command } => utils::block_on(command.run()),
ForgeSubcommand::Soldeer(cmd) => utils::block_on(cmd.run()),
ForgeSubcommand::Eip712(cmd) => cmd.run(),
ForgeSubcommand::BindJson(cmd) => cmd.run(),
Expand Down
15 changes: 11 additions & 4 deletions crates/forge/bin/opts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::cmd::{
bind::BindArgs, bind_json, build::BuildArgs, cache::CacheArgs, clone::CloneArgs, config,
coverage, create::CreateArgs, debug::DebugArgs, doc::DocArgs, eip712, flatten, fmt::FmtArgs,
geiger, generate, init::InitArgs, inspect, install::InstallArgs, remappings::RemappingArgs,
remove::RemoveArgs, selectors::SelectorsSubcommands, snapshot, soldeer, test, tree, update,
bind::BindArgs, bind_json, build::BuildArgs, cache::CacheArgs, clone::CloneArgs,
compiler::CompilerSubcommands, config, coverage, create::CreateArgs, debug::DebugArgs,
doc::DocArgs, eip712, flatten, fmt::FmtArgs, geiger, generate, init::InitArgs, inspect,
install::InstallArgs, remappings::RemappingArgs, remove::RemoveArgs,
selectors::SelectorsSubcommands, snapshot, soldeer, test, tree, update,
};
use clap::{Parser, Subcommand, ValueHint};
use forge_script::ScriptArgs;
Expand Down Expand Up @@ -162,6 +163,12 @@ pub enum ForgeSubcommand {
/// Generate scaffold files.
Generate(generate::GenerateArgs),

/// Compiler utilities
Compiler {
#[command(subcommand)]
command: CompilerSubcommands,
},

/// Soldeer dependency manager.
Soldeer(soldeer::SoldeerArgs),

Expand Down
Loading