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
Changes from 3 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
242 changes: 195 additions & 47 deletions crates/forge/bin/cmd/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::{ArgAction, Parser, Subcommand, ValueHint};
use eyre::Result;
use foundry_compilers::Graph;
use foundry_config::Config;
use semver::Version;
use std::{collections::BTreeMap, path::PathBuf};

/// CLI arguments for `forge compiler`.
Expand All @@ -21,6 +22,7 @@ impl CompilerArgs {

#[derive(Debug, Subcommand)]
pub enum CompilerSubcommands {
#[command(visible_alias = "r")]
/// Retrieves the resolved version(s) of the compiler within the project.
Resolve(ResolveArgs),
}
Expand All @@ -37,7 +39,7 @@ pub struct ResolveArgs {
/// Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
///
/// Verbosity levels:
/// - 2: Print sources
/// - 2: Print source paths.
#[arg(long, short, verbatim_doc_comment, action = ArgAction::Count, help_heading = "Display options")]
pub verbosity: u8,

Expand All @@ -59,62 +61,208 @@ impl ResolveArgs {
&project.compiler,
)?;

if json {
let mut output = BTreeMap::new();

for (language, sources) in sources {
let versions: Vec<BTreeMap<String, Vec<String>>> = sources
.iter()
.map(|(version, sources)| {
let paths: Vec<String> = sources
.iter()
.map(|(path_file, _)| {
path_file
.strip_prefix(&project.paths.root)
.unwrap_or(path_file)
.to_path_buf()
.display()
.to_string()
})
.collect();

let mut version_map = BTreeMap::new();
version_map.insert(version.to_string(), paths);
version_map
})
.collect();

// Insert language and its versions into the output map
output.insert(language.to_string(), versions);
}

println!("{}", serde_json::to_string(&output)?);
} else {
for (language, sources) in sources {
println!("{language}:\n");
let mut output: BTreeMap<String, Vec<(Version, Vec<String>)>> = BTreeMap::new();

for (version, sources) in sources {
if verbosity >= 1 {
println!("{version}:");
for (idx, (path_file, _)) in sources.iter().enumerate() {
let path = path_file
for (language, sources) in sources {
let mut versions_with_paths: Vec<(Version, Vec<String>)> = sources
.iter()
.map(|(version, sources)| {
let paths: Vec<String> = sources
.iter()
.map(|(path_file, _)| {
path_file
.strip_prefix(&project.paths.root)
.unwrap_or(path_file)
.to_path_buf();
.to_path_buf()
.display()
.to_string()
})
.collect();

(version.clone(), paths)
})
.collect();

if idx == sources.len() - 1 {
println!("└── {}\n", path.display());
} else {
println!("├── {}", path.display());
}
versions_with_paths.sort_by(|(v1, _), (v2, _)| Version::cmp(v1, v2));

output.insert(language.to_string(), versions_with_paths);
}

if json {
println!("{}", serde_json::to_string(&output)?);
return Ok(());
}

for (language, versions) in &output {
if verbosity < 1 {
println!("{language}:");
} else {
println!("{language}:\n");
}

for (version, paths) in versions {
if verbosity >= 1 {
println!("{version}:");
for (idx, path) in paths.iter().enumerate() {
if idx == paths.len() - 1 {
println!("└── {path}\n");
} else {
println!("├── {path}");
}
} else {
println!("- {language} {version}");
}
} else {
println!("- {version}");
}
}

if verbosity < 1 {
println!();
}
}

Ok(())
}
}

#[cfg(test)]
mod tests {
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
use foundry_test_utils::{forgetest_init, snapbox::IntoData, str};

forgetest_init!(can_resolve_path, |prj, cmd| {
cmd.args(["compiler", "resolve", prj.root().to_str().unwrap()]).assert_success().stdout_eq(
str![[r#"
Solidity:
- 0.8.27


"#]],
);
});

forgetest_init!(can_list_resolved_compiler_versions, |prj, cmd| {
cmd.args(["compiler", "resolve"]).assert_success().stdout_eq(str![[r#"
Solidity:
- 0.8.27


"#]]);
});

forgetest_init!(can_list_resolved_compiler_versions_verbose, |prj, cmd| {
cmd.args(["compiler", "resolve", "-v"]).assert_success().stdout_eq(str![[r#"
Solidity:

0.8.27:
├── lib/forge-std/src/Base.sol
├── lib/forge-std/src/Script.sol
├── lib/forge-std/src/StdAssertions.sol
├── lib/forge-std/src/StdChains.sol
├── lib/forge-std/src/StdCheats.sol
├── lib/forge-std/src/StdError.sol
├── lib/forge-std/src/StdInvariant.sol
├── lib/forge-std/src/StdJson.sol
├── lib/forge-std/src/StdMath.sol
├── lib/forge-std/src/StdStorage.sol
├── lib/forge-std/src/StdStyle.sol
├── lib/forge-std/src/StdToml.sol
├── lib/forge-std/src/StdUtils.sol
├── lib/forge-std/src/Test.sol
├── lib/forge-std/src/Vm.sol
├── lib/forge-std/src/console.sol
├── lib/forge-std/src/console2.sol
├── lib/forge-std/src/interfaces/IERC165.sol
├── lib/forge-std/src/interfaces/IERC20.sol
├── lib/forge-std/src/interfaces/IERC721.sol
├── lib/forge-std/src/interfaces/IMulticall3.sol
├── lib/forge-std/src/mocks/MockERC20.sol
├── lib/forge-std/src/mocks/MockERC721.sol
├── lib/forge-std/src/safeconsole.sol
├── script/Counter.s.sol
├── src/Counter.sol
└── test/Counter.t.sol


"#]]);
});

forgetest_init!(can_list_resolved_compiler_versions_json, |prj, cmd| {
cmd.args(["compiler", "resolve", "--json"])
.assert_success()
.stdout_eq(str![[r#"
{"Solidity":[["0.8.27",["lib/forge-std/src/Base.sol","lib/forge-std/src/Script.sol","lib/forge-std/src/StdAssertions.sol","lib/forge-std/src/StdChains.sol","lib/forge-std/src/StdCheats.sol","lib/forge-std/src/StdError.sol","lib/forge-std/src/StdInvariant.sol","lib/forge-std/src/StdJson.sol","lib/forge-std/src/StdMath.sol","lib/forge-std/src/StdStorage.sol","lib/forge-std/src/StdStyle.sol","lib/forge-std/src/StdToml.sol","lib/forge-std/src/StdUtils.sol","lib/forge-std/src/Test.sol","lib/forge-std/src/Vm.sol","lib/forge-std/src/console.sol","lib/forge-std/src/console2.sol","lib/forge-std/src/interfaces/IERC165.sol","lib/forge-std/src/interfaces/IERC20.sol","lib/forge-std/src/interfaces/IERC721.sol","lib/forge-std/src/interfaces/IMulticall3.sol","lib/forge-std/src/mocks/MockERC20.sol","lib/forge-std/src/mocks/MockERC721.sol","lib/forge-std/src/safeconsole.sol","script/Counter.s.sol","src/Counter.sol","test/Counter.t.sol"]]]}"#]].is_jsonlines());
});

const CONTRACT_A: &str = r#"
// SPDX-license-identifier: MIT
pragma solidity 0.8.4;

contract ContractA {}
"#;

const CONTRACT_B: &str = r#"
// SPDX-license-identifier: MIT
pragma solidity 0.8.11;

contract ContractB {}
"#;

forgetest_init!(can_list_resolved_multiple_compiler_versions, |prj, cmd| {
prj.add_source("ContractA", CONTRACT_A).unwrap();
prj.add_source("ContractB", CONTRACT_B).unwrap();

cmd.args(["compiler", "resolve"]).assert_success().stdout_eq(str![[r#"
Solidity:
- 0.8.4
- 0.8.11
- 0.8.27


"#]]);
});

forgetest_init!(can_list_resolved_multiple_compiler_versions_verbose, |prj, cmd| {
prj.add_source("ContractA", CONTRACT_A).unwrap();
prj.add_source("ContractB", CONTRACT_B).unwrap();

cmd.args(["compiler", "resolve", "-v"]).assert_success().stdout_eq(str![[r#"
Solidity:

0.8.4:
└── src/ContractA.sol

0.8.11:
└── src/ContractB.sol

0.8.27:
├── lib/forge-std/src/Base.sol
├── lib/forge-std/src/Script.sol
├── lib/forge-std/src/StdAssertions.sol
├── lib/forge-std/src/StdChains.sol
├── lib/forge-std/src/StdCheats.sol
├── lib/forge-std/src/StdError.sol
├── lib/forge-std/src/StdInvariant.sol
├── lib/forge-std/src/StdJson.sol
├── lib/forge-std/src/StdMath.sol
├── lib/forge-std/src/StdStorage.sol
├── lib/forge-std/src/StdStyle.sol
├── lib/forge-std/src/StdToml.sol
├── lib/forge-std/src/StdUtils.sol
├── lib/forge-std/src/Test.sol
├── lib/forge-std/src/Vm.sol
├── lib/forge-std/src/console.sol
├── lib/forge-std/src/console2.sol
├── lib/forge-std/src/interfaces/IERC165.sol
├── lib/forge-std/src/interfaces/IERC20.sol
├── lib/forge-std/src/interfaces/IERC721.sol
├── lib/forge-std/src/interfaces/IMulticall3.sol
├── lib/forge-std/src/mocks/MockERC20.sol
├── lib/forge-std/src/mocks/MockERC721.sol
├── lib/forge-std/src/safeconsole.sol
├── script/Counter.s.sol
├── src/Counter.sol
└── test/Counter.t.sol


"#]]);
});
}
Loading