Skip to content

Commit

Permalink
Respect verbosity in json, nicer json output
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Oct 16, 2024
1 parent 81cf6ca commit 588bc53
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 20 deletions.
52 changes: 39 additions & 13 deletions crates/forge/bin/cmd/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use eyre::Result;
use foundry_compilers::{artifacts::EvmVersion, Graph};
use foundry_config::Config;
use semver::Version;
use serde::Serialize;
use std::{collections::BTreeMap, path::PathBuf};

/// CLI arguments for `forge compiler`.
Expand All @@ -27,6 +28,18 @@ pub enum CompilerSubcommands {
Resolve(ResolveArgs),
}

/// Resolved compiler within the project.
#[derive(Serialize)]
struct ResolvedCompiler {
/// Compiler version.
version: Version,
#[serde(skip_serializing_if = "Option::is_none")]
/// Max supported EVM version of compiler.
evm_version: Option<EvmVersion>,
/// Source paths.
paths: Vec<String>,
}

/// CLI arguments for `forge compiler resolve`.
#[derive(Debug, Parser)]
pub struct ResolveArgs {
Expand Down Expand Up @@ -67,10 +80,10 @@ impl ResolveArgs {
&project.compiler,
)?;

let mut output: BTreeMap<String, Vec<(Version, EvmVersion, Vec<String>)>> = BTreeMap::new();
let mut output: BTreeMap<String, Vec<ResolvedCompiler>> = BTreeMap::new();

for (language, sources) in sources {
let mut versions_with_paths: Vec<(Version, EvmVersion, Vec<String>)> = sources
let mut versions_with_paths: Vec<ResolvedCompiler> = sources
.iter()
.map(|(version, sources)| {
let paths: Vec<String> = sources
Expand All @@ -94,17 +107,23 @@ impl ResolveArgs {
})
.collect();

(
version.clone(),
EvmVersion::default().normalize_version_solc(version).unwrap_or_default(),
paths,
)
let evm_version = if verbosity > 1 {
Some(
EvmVersion::default()
.normalize_version_solc(version)
.unwrap_or_default(),
)
} else {
None
};

ResolvedCompiler { version: version.clone(), evm_version, paths }
})
.filter(|(_, _, paths)| !paths.is_empty())
.filter(|version| !version.paths.is_empty())
.collect();

// Sort by SemVer version.
versions_with_paths.sort_by(|(v1, _, _), (v2, _, _)| Version::cmp(v1, v2));
versions_with_paths.sort_by(|v1, v2| Version::cmp(&v1.version, &v2.version));

// Skip language if no paths are found after filtering.
if !versions_with_paths.is_empty() {
Expand All @@ -117,20 +136,27 @@ impl ResolveArgs {
return Ok(());
}

for (language, versions) in &output {
for (language, compilers) in &output {
match verbosity {
0 => println!("{language}:"),
_ => println!("{language}:\n"),
}

for (version, evm_version, paths) in versions {
for resolved_compiler in compilers {
let version = &resolved_compiler.version;
match verbosity {
0 => println!("- {version}"),
1 => println!("{version}:"),
_ => println!("{version} (<= {evm_version}):"),
_ => {
if let Some(evm) = &resolved_compiler.evm_version {
println!("{version} (<= {evm}):")
} else {
println!("{version}:")
}
}
}

if verbosity > 0 {
let paths = &resolved_compiler.paths;
for (idx, path) in paths.iter().enumerate() {
if idx == paths.len() - 1 {
println!("└── {path}\n");
Expand Down
82 changes: 75 additions & 7 deletions crates/forge/tests/cli/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ forgetest!(can_list_resolved_compiler_versions_json, |prj, cmd| {

cmd.args(["compiler", "resolve", "--json"]).assert_success().stdout_eq(
str![[r#"
{"Solidity":[["0.8.27","[..]",["src/ContractC.sol","src/ContractD.sol"]]]}"#]]
{
"Solidity": [
{
"version": "0.8.27",
"paths": [
"src/ContractC.sol",
"src/ContractD.sol"
]
}
]
}
"#]]
.is_json(),
);
});
Expand Down Expand Up @@ -164,10 +175,32 @@ forgetest!(can_list_resolved_multiple_compiler_versions_skipped_json, |prj, cmd|
prj.add_raw_source("Counter.vy", VYPER_CONTRACT).unwrap();

cmd.args(["compiler", "resolve", "--skip", "Contract(A|B|C)", "--json"])
.assert_success()
.stdout_eq(str![[r#"
{"Solidity":[["0.8.27","[..]",["src/ContractD.sol"]]],"Vyper":[["0.4.0","[..]",["src/Counter.vy","src/ICounter.vyi"]]]}
"#]].is_json());
.assert_success()
.stdout_eq(
str![[r#"
{
"Solidity": [
{
"version": "0.8.27",
"paths": [
"src/ContractD.sol"
]
}
],
"Vyper": [
{
"version": "0.4.0",
"paths": [
"src/Counter.vy",
"src/ICounter.vyi"
]
}
]
}
"#]]
.is_json(),
);
});

forgetest!(can_list_resolved_multiple_compiler_versions_verbose, |prj, cmd| {
Expand Down Expand Up @@ -209,9 +242,44 @@ forgetest!(can_list_resolved_multiple_compiler_versions_json, |prj, cmd| {
prj.add_raw_source("ICounter.vyi", VYPER_INTERFACE).unwrap();
prj.add_raw_source("Counter.vy", VYPER_CONTRACT).unwrap();

cmd.args(["compiler", "resolve", "--json"]).assert_success().stdout_eq(
cmd.args(["compiler", "resolve", "--json", "-vv"]).assert_success().stdout_eq(
str![[r#"
{"Solidity":[["0.8.4","Istanbul",["src/ContractA.sol"]],["0.8.11","London",["src/ContractB.sol"]],["0.8.27","[..]",["src/ContractC.sol","src/ContractD.sol"]]],"Vyper":[["0.4.0","Cancun",["src/Counter.vy","src/ICounter.vyi"]]]}
{
"Solidity": [
{
"version": "0.8.4",
"evm_version": "Istanbul",
"paths": [
"src/ContractA.sol"
]
},
{
"version": "0.8.11",
"evm_version": "London",
"paths": [
"src/ContractB.sol"
]
},
{
"version": "0.8.27",
"evm_version": "Cancun",
"paths": [
"src/ContractC.sol",
"src/ContractD.sol"
]
}
],
"Vyper": [
{
"version": "0.4.0",
"evm_version": "Cancun",
"paths": [
"src/Counter.vy",
"src/ICounter.vyi"
]
}
]
}
"#]]
.is_json(),
);
Expand Down

0 comments on commit 588bc53

Please sign in to comment.