multiversx_sc_meta/cmd/template/
template_adjuster.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use super::{template_metadata::TemplateMetadata, ContractCreatorTarget};
use crate::{
    cmd::upgrade::upgrade_common::{rename_files, replace_in_files},
    version::FrameworkVersion,
    version_history::is_template_with_autogenerated_wasm,
};
use convert_case::{Case, Casing};
use multiversx_sc_meta_lib::cargo_toml::CargoTomlContents;
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";
const INTERACT_CARGO_TOML: &str = "./interactor/Cargo.toml";
const DEFAULT_AUTHOR: &str = "you";

pub struct TemplateAdjuster {
    pub metadata: TemplateMetadata,
    pub target: ContractCreatorTarget,
    pub keep_paths: bool,
    pub new_author: Option<String>,
}
impl TemplateAdjuster {
    pub fn update_cargo_toml_files(&self, args_tag: FrameworkVersion) {
        let author_as_str = self
            .new_author
            .clone()
            .unwrap_or_else(|| DEFAULT_AUTHOR.to_string());
        self.update_cargo_toml_root(author_as_str.clone());
        self.update_cargo_toml_meta();
        self.update_cargo_toml_wasm(args_tag);
        self.update_cargo_toml_interact(author_as_str);
    }

    fn update_cargo_toml_root(&self, author: String) {
        let cargo_toml_path = self.target.contract_dir().join(ROOT_CARGO_TOML);
        let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

        if !self.keep_paths {
            remove_paths_from_deps(&mut toml, &[]);
        }

        if self.metadata.has_interactor {
            toml.add_workspace(&[".", "meta", "interactor"]);
        } else {
            toml.add_workspace(&[".", "meta"]);
        }
        toml.change_author(author);
        toml.save_to_file(&cargo_toml_path);
    }

    fn update_cargo_toml_meta(&self) {
        let cargo_toml_path = self.target.contract_dir().join(META_CARGO_TOML);
        let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

        if !self.keep_paths {
            remove_paths_from_deps(&mut toml, &[&self.metadata.name]);
        }

        toml.save_to_file(&cargo_toml_path);
    }

    fn update_cargo_toml_wasm(&self, args_tag: FrameworkVersion) {
        if is_template_with_autogenerated_wasm(args_tag) {
            return;
        }

        let cargo_toml_path = self.target.contract_dir().join(WASM_CARGO_TOML);
        let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

        if !self.keep_paths {
            remove_paths_from_deps(&mut toml, &[&self.metadata.name]);
        }

        toml.save_to_file(&cargo_toml_path);
    }

    fn update_cargo_toml_interact(&self, author: String) {
        if !self.metadata.has_interactor {
            return;
        }

        let cargo_toml_path = self.target.contract_dir().join(INTERACT_CARGO_TOML);
        let mut toml = CargoTomlContents::load_from_file(&cargo_toml_path);

        if !self.keep_paths {
            remove_paths_from_deps(&mut toml, &[&self.metadata.name]);
        }

        toml.change_author(author);
        toml.save_to_file(&cargo_toml_path);
    }

    pub fn rename_template_to(&self) {
        self.rename_trait_to();
        self.rename_in_cargo_toml_root();
        self.rename_in_cargo_toml_meta();
        self.rename_in_cargo_toml_wasm();
        self.rename_in_tests();
        self.rename_in_interactor();
        self.rename_solution_files();
    }

    fn rename_trait_to(&self) {
        let new_trait = self.target.new_name.to_case(Case::UpperCamel);
        let old_trait = &self.metadata.contract_trait;
        let new_name = self.target.new_name.to_case(Case::Snake);
        let old_name = self.metadata.name.to_case(Case::Snake);
        let new_package = format!("{new_name}::");
        let old_package = format!("{old_name}::");
        let new_proxy_mod = format!("{new_name}_proxy");
        let old_proxy_mod = format!("{old_name}_proxy");

        replace_in_files(
            &self.target.contract_dir(),
            "*rs",
            &[
                Query::substring(old_trait, &new_trait),
                Query::substring(&old_package, &new_package),
                Query::substring(&old_proxy_mod, &new_proxy_mod),
            ][..],
        );

        replace_in_files(
            &self.target.contract_dir(),
            "*sc-config.toml",
            &[Query::substring(&old_proxy_mod, &new_proxy_mod)][..],
        );
    }

    fn rename_in_cargo_toml_root(&self) {
        let old_path = self.metadata.src_file.clone();
        let new_path = rs_file_name(&self.target.new_name.to_case(Case::Snake));

        replace_in_files(
            &self.target.contract_dir(),
            "*Cargo.toml",
            &[
                Query::substring(
                    &package_name_expr(&self.metadata.name),
                    &package_name_expr(&self.target.new_name),
                ),
                Query::substring(&old_path, &new_path),
            ][..],
        );
    }

    fn rename_in_cargo_toml_meta(&self) {
        let old_meta = format!("{}-meta", self.metadata.name.clone());
        let new_meta = format!("{}-meta", &self.target.new_name);

        replace_in_files(
            &self.target.contract_dir(),
            "*Cargo.toml",
            &[
                Query::substring(&package_name_expr(&old_meta), &package_name_expr(&new_meta)),
                Query::substring(
                    &dependecy_decl_expr(&self.metadata.name),
                    &dependecy_decl_expr(&self.target.new_name),
                ),
            ][..],
        );
    }

    fn rename_in_cargo_toml_wasm(&self) {
        let old_wasm = format!("{}-wasm", self.metadata.name.clone());
        let new_wasm = format!("{}-wasm", &self.target.new_name);

        replace_in_files(
            &self.target.contract_dir(),
            "*Cargo.toml",
            &[
                Query::substring(&package_name_expr(&old_wasm), &package_name_expr(&new_wasm)),
                Query::substring(
                    &dependecy_decl_expr(&self.metadata.name),
                    &dependecy_decl_expr(&self.target.new_name),
                ),
            ][..],
        );
    }

    fn rename_in_tests(&self) {
        let new_name = self.target.new_name.to_case(Case::Snake);
        let old_name = self.metadata.name.to_case(Case::Snake);

        let mut queries = Vec::<Query>::new();
        for (old, new) in self.metadata.rename_pairs.iter() {
            queries.push(Query::substring(old, new))
        }

        let new_path = as_path(&self.target.new_name);
        let old_path = as_path(&self.metadata.name);
        queries.push(Query::substring(&old_path, &new_path));

        let new_scenarios = scenario_path(&new_name);
        let old_scenarios = scenario_path(&old_name);
        queries.push(Query::substring(&old_scenarios, &new_scenarios));

        let old_wasm = wasm_file_name(&self.metadata.name);
        let new_wasm = wasm_file_name(&self.target.new_name);

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

        self.rename_in_scenarios(&old_wasm, &new_wasm);
        self.rename_in_scenarios(&old_mxsc, &new_mxsc);

        queries.push(Query::substring(&old_wasm, &new_wasm));
        queries.push(Query::substring(&old_mxsc, &new_mxsc));

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

    fn rename_in_scenarios(&self, old: &str, new: &str) {
        replace_in_files(
            &self.target.contract_dir(),
            "*.scen.json",
            &[Query::substring(old, new)][..],
        );

        replace_in_files(
            &self.target.contract_dir(),
            "*.steps.json",
            &[Query::substring(old, new)][..],
        );
    }

    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);

        let pattern: &[(&str, &str)] = &[
            (&self.metadata.src_file, &new_src_name),
            (&self.metadata.name, &new_name),
        ];
        rename_files(&self.target.contract_dir(), pattern);
    }
}

fn wasm_file_name(name: &str) -> String {
    format!("{name}.wasm",)
}

fn mxsc_file_name(name: &str) -> String {
    format!("{name}.mxsc.json",)
}

fn rs_file_name(name: &str) -> String {
    format!("{name}.rs",)
}

fn scenario_path(path: &str) -> String {
    format!("scenarios/{path}",)
}

fn as_path(name: &str) -> String {
    format!("/{name}\"")
}
fn package_name_expr(template: &str) -> String {
    format!("name = \"{template}\"")
}
fn dependecy_decl_expr(template: &str) -> String {
    format!("dependencies.{template}")
}

pub fn remove_paths_from_deps_map(deps_map: &mut Table, ignore_deps: &[&str]) {
    for (key, value) in deps_map {
        if ignore_deps.contains(&key.as_str()) {
            continue;
        }
        if let Some(dep) = value.as_table_mut() {
            dep.remove("path");
        }
    }
}

pub fn remove_paths_from_deps(toml: &mut CargoTomlContents, ignore_deps: &[&str]) {
    if toml.has_dependencies() {
        remove_paths_from_deps_map(toml.dependencies_mut(), ignore_deps);
    }
    if toml.has_dev_dependencies() {
        remove_paths_from_deps_map(toml.dev_dependencies_mut(), ignore_deps);
    }
}