Skip to content

Commit 128d2d4

Browse files
Merge pull request #1633 from multiversx/sc-meta-snippets-fixes
fixed sc config auto overwrite issue, fixed proxy gen not happening i…
2 parents 3752387 + 8b88f82 commit 128d2d4

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

framework/meta/src/cmd/contract.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn cli_main<AbiObj: ContractAbiProvider>() {
3131
ContractCliAction::Update => meta_config_opt.update(),
3232
ContractCliAction::GenerateSnippets(gs_arg) => {
3333
meta_config_opt.generate_rust_snippets(&gs_arg);
34+
meta_config_opt.reload_sc_config();
3435
meta_config_opt.generate_proxy()
3536
},
3637
ContractCliAction::GenerateProxies(proxy_args) => {

framework/meta/src/cmd/contract/generate_snippets/snippet_crate_gen.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
use colored::Colorize;
12
use std::{
2-
fs::{self, File},
3+
fs::{self, File, OpenOptions},
34
io::Write,
45
};
56

67
static SNIPPETS_SOURCE_FILE_NAME: &str = "interactor_main.rs";
8+
static SC_CONFIG_PATH: &str = "../sc-config.toml";
9+
static FULL_PROXY_ENTRY: &str = r#"[[proxy]]
10+
path = "interact-rs/src/proxy.rs"
11+
"#;
12+
static PROXY_PATH: &str = "interact-rs/src/proxy.rs";
713

814
pub(crate) fn create_snippets_folder(snippets_folder_path: &str) {
915
// returns error if folder already exists, so we ignore the result
@@ -72,15 +78,14 @@ path = ".."
7278
version = "0.50.1"
7379
7480
[dependencies.multiversx-sc]
75-
version = "0.49.0"
81+
version = "0.50.1"
7682
7783
[dependencies]
7884
clap = {{ version = "4.4.7", features = ["derive"] }}
7985
serde = {{ version = "1.0", features = ["derive"] }}
8086
toml = "0.8.6"
8187
8288
# [workspace]
83-
8489
"#
8590
)
8691
.unwrap();
@@ -100,31 +105,48 @@ pub(crate) fn create_and_get_lib_file(snippets_folder_path: &str, overwrite: boo
100105
} else {
101106
match File::options().create_new(true).write(true).open(&lib_path) {
102107
Ok(f) => f,
103-
Err(_) => panic!("{lib_path} file already exists, --overwrite option was not provided"),
108+
Err(_) => {
109+
println!(
110+
"{}",
111+
format!("{lib_path} file already exists, --overwrite option was not provided",)
112+
.yellow()
113+
);
114+
File::options().write(true).open(&lib_path).unwrap()
115+
},
104116
}
105117
}
106118
}
107119

108120
pub(crate) fn create_sc_config_file(overwrite: bool) {
109-
let sc_config_path = "../sc-config.toml";
110-
let mut file = if overwrite {
111-
File::create(sc_config_path).unwrap()
121+
// check if the file should be overwritten or if it already exists
122+
let mut file = if overwrite || !file_exists(SC_CONFIG_PATH) {
123+
File::create(SC_CONFIG_PATH).unwrap()
112124
} else {
113-
match File::options()
114-
.create_new(true)
115-
.write(true)
116-
.open(sc_config_path)
117-
{
118-
Ok(f) => f,
119-
Err(_) => return,
125+
// file already exists
126+
let file = OpenOptions::new()
127+
.read(true)
128+
.append(true)
129+
.open(SC_CONFIG_PATH)
130+
.unwrap();
131+
132+
if file_contains_proxy_path(SC_CONFIG_PATH).unwrap_or(false) {
133+
return;
120134
}
135+
136+
file
121137
};
122138

123-
writeln!(
124-
&mut file,
125-
r#"[[proxy]]
126-
path = "interact-rs/src/proxy.rs"
127-
"#
128-
)
129-
.unwrap();
139+
// write full proxy toml entry to the file
140+
writeln!(&mut file, "\n{FULL_PROXY_ENTRY}").unwrap();
141+
}
142+
143+
fn file_exists(path: &str) -> bool {
144+
fs::metadata(path).is_ok()
145+
}
146+
147+
fn file_contains_proxy_path(file_path: &str) -> std::io::Result<bool> {
148+
let file_content = fs::read_to_string(file_path)?;
149+
let proxy_entry = format!("path = \"{}\"", PROXY_PATH);
150+
151+
Ok(file_content.contains(&proxy_entry))
130152
}

framework/meta/src/cmd/contract/meta_config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ impl MetaConfig {
4040
}
4141
}
4242

43+
pub fn reload_sc_config(&mut self) {
44+
self.sc_config = ScConfig::load_from_crate_or_default("..", &self.original_contract_abi);
45+
}
46+
4347
/// Generates all code for the wasm crate(s).
4448
pub fn generate_wasm_crates(&mut self) {
4549
self.remove_unexpected_wasm_crates();

0 commit comments

Comments
 (0)