Skip to content

Commit

Permalink
Merge pull request #1633 from multiversx/sc-meta-snippets-fixes
Browse files Browse the repository at this point in the history
fixed sc config auto overwrite issue, fixed proxy gen not happening i…
  • Loading branch information
andrei-marinica authored May 24, 2024
2 parents 3752387 + 8b88f82 commit 128d2d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions framework/meta/src/cmd/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn cli_main<AbiObj: ContractAbiProvider>() {
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(proxy_args) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -72,15 +78,14 @@ path = ".."
version = "0.50.1"
[dependencies.multiversx-sc]
version = "0.49.0"
version = "0.50.1"
[dependencies]
clap = {{ version = "4.4.7", features = ["derive"] }}
serde = {{ version = "1.0", features = ["derive"] }}
toml = "0.8.6"
# [workspace]
"#
)
.unwrap();
Expand All @@ -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<bool> {
let file_content = fs::read_to_string(file_path)?;
let proxy_entry = format!("path = \"{}\"", PROXY_PATH);

Ok(file_content.contains(&proxy_entry))
}
4 changes: 4 additions & 0 deletions framework/meta/src/cmd/contract/meta_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 128d2d4

Please sign in to comment.