Skip to content

Commit

Permalink
Merge pull request #1479 from multiversx/adder-interact-template
Browse files Browse the repository at this point in the history
adder interactor in template
  • Loading branch information
andrei-marinica authored Mar 14, 2024
2 parents c092f82 + 34eabff commit 0817a63
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 32 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions contracts/examples/adder/interact/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "adder-interact"
name = "basic-interact"
version = "0.0.0"
authors = ["Ovidiu Stinga <[email protected]>"]
edition = "2021"
publish = false

[[bin]]
name = "adder-interact"
path = "src/adder_interact.rs"
name = "basic-interact"
path = "src/basic_interact.rs"

[dependencies]
clap = { version = "4.4.7", features = ["derive"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod adder_interact_cli;
mod adder_interact_config;
mod adder_interact_state;
mod basic_interact_cli;
mod basic_interact_config;
mod basic_interact_state;

use adder::ProxyTrait;
use adder_interact_config::Config;
use adder_interact_state::State;
use basic_interact_config::Config;
use basic_interact_state::State;
use clap::Parser;
use multiversx_sc_snippets::{
env_logger,
Expand All @@ -30,24 +30,24 @@ const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json";
async fn main() {
env_logger::init();

let mut adder_interact = AdderInteract::init().await;
let mut basic_interact = AdderInteract::init().await;

let cli = adder_interact_cli::InteractCli::parse();
let cli = basic_interact_cli::InteractCli::parse();
match &cli.command {
Some(adder_interact_cli::InteractCliCommand::Add(args)) => {
adder_interact.add(args.value).await;
Some(basic_interact_cli::InteractCliCommand::Add(args)) => {
basic_interact.add(args.value).await;
},
Some(adder_interact_cli::InteractCliCommand::Deploy) => {
adder_interact.deploy().await;
Some(basic_interact_cli::InteractCliCommand::Deploy) => {
basic_interact.deploy().await;
},
Some(adder_interact_cli::InteractCliCommand::Feed) => {
adder_interact.feed_contract_egld().await;
Some(basic_interact_cli::InteractCliCommand::Feed) => {
basic_interact.feed_contract_egld().await;
},
Some(adder_interact_cli::InteractCliCommand::MultiDeploy(args)) => {
adder_interact.multi_deploy(&args.count).await;
Some(basic_interact_cli::InteractCliCommand::MultiDeploy(args)) => {
basic_interact.multi_deploy(&args.count).await;
},
Some(adder_interact_cli::InteractCliCommand::Sum) => {
adder_interact.print_sum().await;
Some(basic_interact_cli::InteractCliCommand::Sum) => {
basic_interact.print_sum().await;
},
None => {},
}
Expand Down
5 changes: 5 additions & 0 deletions contracts/examples/adder/mxsc-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ files_include = [
"Cargo.toml",
"README.md",
"multiversx.json",
"interact/Cargo.toml",
"interact/config.toml",
"interact/.gitignore",
"interact/src",
]
has_interactor = true
21 changes: 20 additions & 1 deletion framework/meta/src/cmd/standalone/template/template_adjuster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ruplacer::Query;
use toml::value::Table;

const TEST_DIRECTORY: &str = "./tests";
const INTERACT_DIRECTORY: &str = "./interact";
const ROOT_CARGO_TOML: &str = "./Cargo.toml";
const META_CARGO_TOML: &str = "./meta/Cargo.toml";
const WASM_CARGO_TOML: &str = "./wasm/Cargo.toml";
Expand All @@ -34,7 +35,11 @@ impl TemplateAdjuster {
remove_paths_from_deps(&mut toml, &[]);
}

toml.add_workspace(&[".", "meta"]);
if self.metadata.has_interactor {
toml.add_workspace(&[".", "meta", "interact"]);
} else {
toml.add_workspace(&[".", "meta"]);
}

toml.save_to_file(&cargo_toml_path);
}
Expand Down Expand Up @@ -71,6 +76,7 @@ impl TemplateAdjuster {
self.rename_in_cargo_toml_meta();
self.rename_in_cargo_toml_wasm();
self.rename_in_tests();
self.rename_in_interactor();
self.rename_solution_files();
}

Expand Down Expand Up @@ -193,6 +199,19 @@ impl TemplateAdjuster {
);
}

fn rename_in_interactor(&self) {
let old_mxsc = mxsc_file_name(&self.metadata.name);
let new_mxsc = mxsc_file_name(&self.target.new_name);

let queries = vec![Query::substring(&old_mxsc, &new_mxsc)];

replace_in_files(
&self.target.contract_dir().join(INTERACT_DIRECTORY),
"*.rs",
&queries,
);
}

fn rename_solution_files(&self) {
let new_name = self.target.new_name.to_case(Case::Snake);
let new_src_name = rs_file_name(&new_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub struct TemplateMetadata {
pub src_file: String,
pub rename_pairs: Vec<(String, String)>,
pub files_include: Vec<String>,

#[serde(default)]
pub has_interactor: bool,
}

#[cfg(test)]
Expand Down
21 changes: 21 additions & 0 deletions framework/meta/tests/template_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ fn test_template_list() {
#[cfg_attr(not(feature = "template-test-current"), ignore)]
fn template_current_adder() {
template_test_current("adder", "examples", "new-adder");

cargo_check_interactor("examples", "new-adder");
}

#[test]
Expand Down Expand Up @@ -198,3 +200,22 @@ pub fn build_contract(target: &ContractCreatorTarget) {

assert!(exit_status.success(), "contract build process failed");
}

fn cargo_check_interactor(sub_path: &str, new_name: &str) {
let workspace_path = find_current_workspace().unwrap();
let target_path = workspace_path
.join(TEMPLATE_TEMP_DIR_NAME)
.join(sub_path)
.join(new_name)
.join("interact");

let exit_status = Command::new("cargo")
.arg("check")
.current_dir(target_path)
.spawn()
.expect("failed to spawn contract clean process")
.wait()
.expect("contract test process was not running");

assert!(exit_status.success(), "contract test process failed");
}

0 comments on commit 0817a63

Please sign in to comment.