From 0165a32e1204a56efaa336ad0ead399eda2e759d Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Fri, 17 May 2024 02:20:09 +0200 Subject: [PATCH] fixed sc config auto overwrite issue, fixed proxy gen not happening issue, made panic into a warning for already existing lib file --- framework/meta/src/cmd/contract.rs | 1 + .../generate_snippets/snippet_crate_gen.rs | 64 +++++++++++++------ .../meta/src/cmd/contract/meta_config.rs | 4 ++ 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/framework/meta/src/cmd/contract.rs b/framework/meta/src/cmd/contract.rs index 3e1008a3a3..7a65b1bd8e 100644 --- a/framework/meta/src/cmd/contract.rs +++ b/framework/meta/src/cmd/contract.rs @@ -31,6 +31,7 @@ pub fn cli_main() { ContractCliAction::Update => meta_config_opt.update(), ContractCliAction::GenerateSnippets(gs_arg) => { meta_config_opt.generate_rust_snippets(&gs_arg); + meta_config_opt.reload_sc_config(); meta_config_opt.generate_proxy() }, ContractCliAction::GenerateProxies => meta_config_opt.generate_proxy(), diff --git a/framework/meta/src/cmd/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta/src/cmd/contract/generate_snippets/snippet_crate_gen.rs index 7bf8ab8d5c..bbfbb637a3 100644 --- a/framework/meta/src/cmd/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta/src/cmd/contract/generate_snippets/snippet_crate_gen.rs @@ -1,9 +1,15 @@ +use colored::Colorize; use std::{ - fs::{self, File}, + fs::{self, File, OpenOptions}, io::Write, }; static SNIPPETS_SOURCE_FILE_NAME: &str = "interactor_main.rs"; +static SC_CONFIG_PATH: &str = "../sc-config.toml"; +static FULL_PROXY_ENTRY: &str = r#"[[proxy]] +path = "interact-rs/src/proxy.rs" + "#; +static PROXY_PATH: &str = "interact-rs/src/proxy.rs"; pub(crate) fn create_snippets_folder(snippets_folder_path: &str) { // returns error if folder already exists, so we ignore the result @@ -72,7 +78,7 @@ path = ".." version = "0.50.1" [dependencies.multiversx-sc] -version = "0.49.0" +version = "0.50.1" [dependencies] clap = {{ version = "4.4.7", features = ["derive"] }} @@ -80,7 +86,6 @@ serde = {{ version = "1.0", features = ["derive"] }} toml = "0.8.6" # [workspace] - "# ) .unwrap(); @@ -100,31 +105,48 @@ pub(crate) fn create_and_get_lib_file(snippets_folder_path: &str, overwrite: boo } else { match File::options().create_new(true).write(true).open(&lib_path) { Ok(f) => f, - Err(_) => panic!("{lib_path} file already exists, --overwrite option was not provided"), + Err(_) => { + println!( + "{}", + format!("{lib_path} file already exists, --overwrite option was not provided",) + .yellow() + ); + File::options().write(true).open(&lib_path).unwrap() + }, } } } pub(crate) fn create_sc_config_file(overwrite: bool) { - let sc_config_path = "../sc-config.toml"; - let mut file = if overwrite { - File::create(sc_config_path).unwrap() + // check if the file should be overwritten or if it already exists + let mut file = if overwrite || !file_exists(SC_CONFIG_PATH) { + File::create(SC_CONFIG_PATH).unwrap() } else { - match File::options() - .create_new(true) - .write(true) - .open(sc_config_path) - { - Ok(f) => f, - Err(_) => return, + // file already exists + let file = OpenOptions::new() + .read(true) + .append(true) + .open(SC_CONFIG_PATH) + .unwrap(); + + if file_contains_proxy_path(SC_CONFIG_PATH).unwrap_or(false) { + return; } + + file }; - writeln!( - &mut file, - r#"[[proxy]] -path = "interact-rs/src/proxy.rs" - "# - ) - .unwrap(); + // write full proxy toml entry to the file + writeln!(&mut file, "\n{FULL_PROXY_ENTRY}").unwrap(); +} + +fn file_exists(path: &str) -> bool { + fs::metadata(path).is_ok() +} + +fn file_contains_proxy_path(file_path: &str) -> std::io::Result { + let file_content = fs::read_to_string(file_path)?; + let proxy_entry = format!("path = \"{}\"", PROXY_PATH); + + Ok(file_content.contains(&proxy_entry)) } diff --git a/framework/meta/src/cmd/contract/meta_config.rs b/framework/meta/src/cmd/contract/meta_config.rs index a33fb98c5d..c273548d5a 100644 --- a/framework/meta/src/cmd/contract/meta_config.rs +++ b/framework/meta/src/cmd/contract/meta_config.rs @@ -40,6 +40,10 @@ impl MetaConfig { } } + pub fn reload_sc_config(&mut self) { + self.sc_config = ScConfig::load_from_crate_or_default("..", &self.original_contract_abi); + } + /// Generates all code for the wasm crate(s). pub fn generate_wasm_crates(&mut self) { self.remove_unexpected_wasm_crates();