Skip to content

Commit

Permalink
Merge pull request #1265 from multiversx/refactor-output-contract
Browse files Browse the repository at this point in the history
refactor OutputContract into ContractVariant
  • Loading branch information
mihaicalinluca authored Nov 10, 2023
2 parents d058656 + 399ffcc commit 88843dd
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 117 deletions.
14 changes: 7 additions & 7 deletions framework/meta/src/cmd/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod generate_snippets;
mod meta_abi;
mod meta_config;
pub mod output_contract;
pub mod sc_config;

use std::path::Path;

use crate::cli_args::{ContractCliAction, ContractCliArgs};
use clap::Parser;
use meta_config::MetaConfig;
use multiversx_sc::contract_base::ContractAbiProvider;
use output_contract::OutputContractGlobalConfig;
use sc_config::ScConfig;

/// Entry point in the program from the contract meta crates.
pub fn cli_main<AbiObj: ContractAbiProvider>() {
Expand All @@ -35,23 +35,23 @@ pub fn cli_main<AbiObj: ContractAbiProvider>() {
fn process_original_abi<AbiObj: ContractAbiProvider>(cli_args: &ContractCliArgs) -> MetaConfig {
let input_abi = <AbiObj as ContractAbiProvider>::abi();
let mut meta_config = MetaConfig::create(input_abi, cli_args.load_abi_git_version);
meta_config.output_contracts.validate_output_contracts();
meta_config.sc_config.validate_contract_variants();
meta_config.write_contract_abis();
meta_config.write_esdt_attribute_abis();
meta_config.generate_wasm_crates();
meta_config
}

pub fn multi_contract_config<AbiObj>(contract_crate_path: &Path) -> OutputContractGlobalConfig
pub fn multi_contract_config<AbiObj>(contract_crate_path: &Path) -> ScConfig
where
AbiObj: ContractAbiProvider,
{
let original_contract_abi = <AbiObj as ContractAbiProvider>::abi();

let output_contracts = OutputContractGlobalConfig::load_from_crate_or_default(
let sc_config = ScConfig::load_from_crate_or_default(
contract_crate_path,
&original_contract_abi,
);
output_contracts.validate_output_contracts();
output_contracts
sc_config.validate_contract_variants();
sc_config
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{

impl MetaConfig {
pub fn generate_rust_snippets(&self, args: &GenerateSnippetsArgs) {
let main_contract = self.output_contracts.main_contract();
let main_contract = self.sc_config.main_contract();
let crate_name = &main_contract.contract_name;
let snake_case_name = &main_contract.public_name_snake_case();
let wasm_output_file_path_expr = format!("\"file:../output/{crate_name}.wasm\"");
Expand Down
12 changes: 6 additions & 6 deletions framework/meta/src/cmd/contract/meta_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use crate::{
esdt_attr_file_json::create_new_esdt_attr_file,
};

use super::{meta_config::MetaConfig, output_contract::OutputContract};
use super::{meta_config::MetaConfig, sc_config::ContractVariant};

fn write_contract_abi(output_contract: &OutputContract, git_version: &str, output_path: &str) {
let mut abi_json = ContractAbiJson::from(&output_contract.abi);
fn write_contract_abi(contract_variant: &ContractVariant, git_version: &str, output_path: &str) {
let mut abi_json = ContractAbiJson::from(&contract_variant.abi);
if let Some(build_info) = &mut abi_json.build_info {
build_info.contract_crate.git_version = git_version.to_string();
}
let abi_string = serialize_abi_to_json(&abi_json);

let abi_file_path = format!("{output_path}/{}", output_contract.abi_output_name(),);
let abi_file_path = format!("{output_path}/{}", contract_variant.abi_output_name(),);
let mut abi_file = File::create(abi_file_path).unwrap();
write!(abi_file, "{abi_string}").unwrap();
}
Expand All @@ -26,9 +26,9 @@ impl MetaConfig {
pub fn write_contract_abis(&self) {
create_dir_all(&self.output_dir).unwrap();
let git_version = self.git_describe();
for output_contract in &self.output_contracts.contracts {
for contract_variant in &self.sc_config.contracts {
write_contract_abi(
output_contract,
contract_variant,
git_version.as_str(),
self.output_dir.as_str(),
);
Expand Down
38 changes: 19 additions & 19 deletions framework/meta/src/cmd/contract/meta_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use multiversx_sc::abi::ContractAbi;

use crate::{cli_args::BuildArgs, tools::check_tools_installed, CargoTomlContents};

use super::output_contract::{OutputContract, OutputContractGlobalConfig};
use super::sc_config::{ContractVariant, ScConfig};

const OUTPUT_RELATIVE_PATH: &str = "../output";
const SNIPPETS_RELATIVE_PATH: &str = "../interact-rs";
Expand All @@ -17,20 +17,20 @@ pub struct MetaConfig {
pub output_dir: String,
pub snippets_dir: String,
pub original_contract_abi: ContractAbi,
pub output_contracts: OutputContractGlobalConfig,
pub sc_config: ScConfig,
}

impl MetaConfig {
pub fn create(original_contract_abi: ContractAbi, load_abi_git_version: bool) -> MetaConfig {
let output_contracts =
OutputContractGlobalConfig::load_from_crate_or_default("..", &original_contract_abi);
let sc_config =
ScConfig::load_from_crate_or_default("..", &original_contract_abi);

MetaConfig {
load_abi_git_version,
output_dir: OUTPUT_RELATIVE_PATH.to_string(),
snippets_dir: SNIPPETS_RELATIVE_PATH.to_string(),
original_contract_abi,
output_contracts,
sc_config,
}
}

Expand All @@ -44,29 +44,29 @@ impl MetaConfig {
}

fn create_wasm_crate_dirs(&self) {
for output_contract in &self.output_contracts.contracts {
output_contract.create_wasm_crate_dir();
for contract_variant in &self.sc_config.contracts {
contract_variant.create_wasm_crate_dir();
}
}

/// Cargo.toml files for secondary contracts are generated from the main contract Cargo.toml,
/// by changing the package name.
pub fn generate_cargo_toml_for_secondary_contracts(&mut self) {
let main_contract = self.output_contracts.main_contract_mut();
let main_contract = self.sc_config.main_contract_mut();

let main_cargo_toml_contents =
CargoTomlContents::load_from_file(main_contract.cargo_toml_path());
main_contract.wasm_crate_name = main_cargo_toml_contents.package_name();

for secondary_contract in self.output_contracts.secondary_contracts() {
for secondary_contract in self.sc_config.secondary_contracts() {
secondary_contract_cargo_toml(secondary_contract, &main_cargo_toml_contents)
.save_to_file(secondary_contract.cargo_toml_path());
}
}
}

fn secondary_contract_cargo_toml(
secondary_contract: &OutputContract,
secondary_contract: &ContractVariant,
main_cargo_toml_contents: &CargoTomlContents,
) -> CargoTomlContents {
let mut cargo_toml_contents = main_cargo_toml_contents.clone();
Expand All @@ -80,16 +80,16 @@ fn secondary_contract_cargo_toml(

impl MetaConfig {
fn generate_wasm_src_lib(&self) {
for output_contract in &self.output_contracts.contracts {
output_contract.generate_wasm_src_lib_file();
for contract_variant in &self.sc_config.contracts {
contract_variant.generate_wasm_src_lib_file();
}
}

pub fn build(&mut self, mut build_args: BuildArgs) {
check_tools_installed(&mut build_args);

for output_contract in &self.output_contracts.contracts {
output_contract.build_contract(&build_args, self.output_dir.as_str());
for contract_variant in &self.sc_config.contracts {
contract_variant.build_contract(&build_args, self.output_dir.as_str());
}
}

Expand All @@ -100,15 +100,15 @@ impl MetaConfig {
}

fn clean_contract_crates(&self) {
for output_contract in &self.output_contracts.contracts {
output_contract.cargo_clean();
for contract_variant in &self.sc_config.contracts {
contract_variant.cargo_clean();
}
}

/// Updates the Cargo.lock on all wasm crates.
pub fn update(&self) {
for output_contract in &self.output_contracts.contracts {
output_contract.cargo_update();
for contract_variant in &self.sc_config.contracts {
contract_variant.cargo_update();
}
}

Expand All @@ -125,7 +125,7 @@ impl MetaConfig {
return true;
}

self.output_contracts
self.sc_config
.secondary_contracts()
.any(|contract| contract.wasm_crate_dir_name().as_str() == dir_name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod wasm_update;

pub use multi_contract_serde::*;
pub use oc_builder::*;
pub use oc_config::OutputContract;
pub use oc_global_config::OutputContractGlobalConfig;
pub use oc_settings::OutputContractSettings;
pub use oc_config::ContractVariant;
pub use oc_global_config::ScConfig;
pub use oc_settings::ContractVariantSettings;
pub use wasm_build::*;
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ pub struct MultiContractConfigSerde {
#[serde(default)]
pub settings: MultiContractGeneralSettingsSerde,
#[serde(default)]
pub contracts: HashMap<String, OutputContractSerde>,
pub contracts: HashMap<String, ContractVariantSerde>,
#[serde(default)]
#[serde(rename = "labels-for-contracts")]
pub labels_for_contracts: HashMap<String, Vec<String>>,
}

#[derive(Deserialize, Debug)]
pub struct OutputContractSerde {
pub struct ContractVariantSerde {
pub name: Option<String>,

#[serde(default)]
Expand Down
Loading

0 comments on commit 88843dd

Please sign in to comment.