mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-26 04:07:57 +00:00
497dae2494
The differential testing framework will make a second consumer. There seems to be no re-usable Rust crate for this. But we already have everything here, just needs a small refactor to make it fully re-usable. - Mostly decouple the solc JSON-input-output interface types from the `solidity` frontend crate - Expose the JSON-input-output interface types in a dedicated crate --------- Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
95 lines
2.8 KiB
Rust
95 lines
2.8 KiB
Rust
//! The Solidity project build.
|
|
|
|
pub mod contract;
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::path::Path;
|
|
|
|
use revive_solc_json_interface::combined_json::CombinedJson;
|
|
use revive_solc_json_interface::SolcStandardJsonOutput;
|
|
|
|
use crate::solc::version::Version as SolcVersion;
|
|
use crate::ResolcVersion;
|
|
|
|
use self::contract::Contract;
|
|
|
|
/// The Solidity project build.
|
|
#[derive(Debug, Default)]
|
|
pub struct Build {
|
|
/// The contract data,
|
|
pub contracts: BTreeMap<String, Contract>,
|
|
}
|
|
|
|
impl Build {
|
|
/// Writes all contracts to the specified directory.
|
|
pub fn write_to_directory(
|
|
self,
|
|
output_directory: &Path,
|
|
output_assembly: bool,
|
|
output_binary: bool,
|
|
overwrite: bool,
|
|
) -> anyhow::Result<()> {
|
|
for (_path, contract) in self.contracts.into_iter() {
|
|
contract.write_to_directory(
|
|
output_directory,
|
|
output_assembly,
|
|
output_binary,
|
|
overwrite,
|
|
)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Writes all contracts assembly and bytecode to the combined JSON.
|
|
pub fn write_to_combined_json(self, combined_json: &mut CombinedJson) -> anyhow::Result<()> {
|
|
for (path, contract) in self.contracts.into_iter() {
|
|
let combined_json_contract = combined_json
|
|
.contracts
|
|
.iter_mut()
|
|
.find_map(|(json_path, contract)| {
|
|
if path.ends_with(json_path) {
|
|
Some(contract)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.ok_or_else(|| anyhow::anyhow!("Contract `{}` not found in the project", path))?;
|
|
|
|
contract.write_to_combined_json(combined_json_contract)?;
|
|
}
|
|
|
|
combined_json.revive_version = Some(ResolcVersion::default().long);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Writes all contracts assembly and bytecode to the standard JSON.
|
|
pub fn write_to_standard_json(
|
|
mut self,
|
|
standard_json: &mut SolcStandardJsonOutput,
|
|
solc_version: &SolcVersion,
|
|
) -> anyhow::Result<()> {
|
|
let contracts = match standard_json.contracts.as_mut() {
|
|
Some(contracts) => contracts,
|
|
None => return Ok(()),
|
|
};
|
|
|
|
for (path, contracts) in contracts.iter_mut() {
|
|
for (name, contract) in contracts.iter_mut() {
|
|
let full_name = format!("{path}:{name}");
|
|
|
|
if let Some(contract_data) = self.contracts.remove(full_name.as_str()) {
|
|
contract_data.write_to_standard_json(contract)?;
|
|
}
|
|
}
|
|
}
|
|
|
|
standard_json.version = Some(solc_version.default.to_string());
|
|
standard_json.long_version = Some(solc_version.long.to_owned());
|
|
standard_json.revive_version = Some(ResolcVersion::default().long);
|
|
|
|
Ok(())
|
|
}
|
|
}
|