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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

use multiversx_sc_meta_lib::cli::{CliArgsToRaw, ContractCliAction};

/// Parsed arguments of the meta crate CLI.
#[derive(Default, PartialEq, Eq, Debug, Parser)]
#[command(
    version,
    about,
    after_help = "
The MultiversX smart contract Meta crate can be used in two ways:
    A. Import it into a contract's specific meta-crate. 
        There it will receive access to the contract ABI generator. 
        Based on that it is able to build the contract and apply various tools.
        This part also contains the multi-contract config infrastructure.

    B. Use it as a standalone tool.
        It can be used to automatically upgrade contracts from one version to the next.

You are currently using the standalone tool (B).
"
)]
#[command(propagate_version = true)]
pub struct StandaloneCliArgs {
    #[command(subcommand)]
    pub command: Option<StandaloneCliAction>,
}

#[derive(Clone, PartialEq, Eq, Debug, Subcommand)]
pub enum StandaloneCliAction {
    #[command(name = "install", about = "Installs framework dependencies")]
    Install(InstallArgs),

    #[command(
        about = "General info about the contract an libraries residing in the targetted directory.."
    )]
    Info(InfoArgs),

    #[command(
        about = "Calls the meta crates for all contracts under given path with the given arguments."
    )]
    All(AllArgs),

    #[command(
        about = "Upgrades a contract to the latest version. Multiple contract crates are allowed."
    )]
    Upgrade(UpgradeArgs),

    #[command(name = "new", about = "Creates a contract by a pre-existing template")]
    Template(TemplateArgs),

    #[command(name = "templates", about = "Lists all pre-existing templates")]
    TemplateList(TemplateListArgs),

    #[command(
        name = "test-gen",
        about = "Generates Rust integration tests based on scenarios provided in the scenarios folder of each contract."
    )]
    TestGen(TestGenArgs),

    #[command(name = "test", about = "Runs cargo test")]
    Test(TestArgs),

    #[command(name = "test-coverage", about = "Run test coverage and output report")]
    TestCoverage(TestCoverageArgs),

    #[command(
        about = "Generates a scenario test initialized with real data fetched from the blockchain."
    )]
    Account(AccountArgs),

    #[command(
        name = "local-deps",
        about = "Generates a report on the local depedencies of contract crates. Will explore indirect depdencies too."
    )]
    LocalDeps(LocalDepsArgs),
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct InfoArgs {
    /// Target directory to retrieve info from.
    /// Will be current directory if not specified.
    #[arg(long, verbatim_doc_comment)]
    pub path: Option<String>,

    /// Ignore all directories with these names.
    #[arg(long, verbatim_doc_comment)]
    #[clap(global = true, default_value = "target")]
    pub ignore: Vec<String>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TestArgs {
    /// Target directory where to generate contract integration tests.
    /// Will be current directory if not specified.
    #[arg(short, long, verbatim_doc_comment)]
    pub path: Option<String>,

    /// This arg runs rust and go tests.
    /// Default value will be "false" if not specified.
    #[arg(short, long, default_value = "false", verbatim_doc_comment)]
    pub go: bool,

    /// This arg runs scenarios.
    /// Default value will be "false" if not specified.
    /// If scen and go are both specified, scen overrides the go argument.
    #[arg(short, long, default_value = "false", verbatim_doc_comment)]
    pub scen: bool,

    /// This arg prints the entire output of the vm.
    /// Default value will be "false" if not specified
    #[arg(short, long, default_value = "false", verbatim_doc_comment)]
    pub nocapture: bool,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, ValueEnum)]
pub enum TestCoverageOutputFormat {
    /// Markdown pretty-print summary
    #[default]
    Markdown,

    /// JSON summary
    Json,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TestCoverageArgs {
    /// Output file path
    #[arg(short, long, verbatim_doc_comment)]
    pub output: String,

    /// Output format
    #[arg(short, long, verbatim_doc_comment)]
    pub format: Option<TestCoverageOutputFormat>,

    /// Ignore files by path patterns
    #[arg(short = 'i', long = "ignore-filename-regex", verbatim_doc_comment)]
    pub ignore_filename_regex: Vec<String>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct AllArgs {
    #[command(subcommand)]
    pub command: ContractCliAction,

    /// Target directory where to call all contract meta crates.
    /// Will be current directory if not specified.
    #[arg(long, verbatim_doc_comment)]
    #[clap(global = true)]
    pub path: Option<String>,

    /// Ignore all directories with these names.
    #[arg(long, verbatim_doc_comment)]
    #[clap(global = true, default_value = "target")]
    pub ignore: Vec<String>,

    #[arg(
        long = "no-abi-git-version",
        help = "Skips loading the Git version into the ABI",
        action = ArgAction::SetFalse
    )]
    #[clap(global = true)]
    pub load_abi_git_version: bool,

    /// For the meta crates, allows specifying the target directory where the Rust compiler will build the intermediary files.
    /// Sharing the same target directory can speed up building multiple contract crates at once.
    #[arg(long = "target-dir-meta", verbatim_doc_comment)]
    #[clap(global = true)]
    pub target_dir_meta: Option<String>,

    /// Overrides both the --target-dir-meta and the --target-dir-wasm args.
    #[arg(long = "target-dir-all", verbatim_doc_comment)]
    #[clap(global = true)]
    pub target_dir_all: Option<String>,
}

impl AllArgs {
    pub fn target_dir_all_override(&self) -> Self {
        let mut result = self.clone();
        if let Some(target_dir_all) = &self.target_dir_all {
            result.target_dir_meta = Some(target_dir_all.clone());
            match &mut result.command {
                ContractCliAction::Build(build_args) => {
                    build_args.target_dir_wasm = Some(target_dir_all.clone());
                },
                ContractCliAction::BuildDbg(build_args) => {
                    build_args.target_dir_wasm = Some(target_dir_all.clone());
                },
                ContractCliAction::Twiggy(build_args) => {
                    build_args.target_dir_wasm = Some(target_dir_all.clone());
                },
                _ => {},
            }
        }
        result
    }

    pub fn to_cargo_run_args(&self) -> Vec<String> {
        let processed = self.target_dir_all_override();
        let mut raw = vec!["run".to_string()];
        if let Some(target_dir_meta) = &processed.target_dir_meta {
            raw.push("--target-dir".to_string());
            raw.push(target_dir_meta.clone());
        }
        raw.append(&mut processed.command.to_raw());
        if !processed.load_abi_git_version {
            raw.push("--no-abi-git-version".to_string());
        }
        raw
    }
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct UpgradeArgs {
    /// Target directory where to upgrade contracts.
    /// Will be current directory if not specified.
    #[arg(long, verbatim_doc_comment)]
    pub path: Option<String>,

    /// Ignore all directories with these names.
    #[arg(long, verbatim_doc_comment)]
    #[clap(global = true, default_value = "target")]
    pub ignore: Vec<String>,

    /// Overrides the version to upgrade to.
    /// By default it will be the last version out.
    #[arg(long = "to", verbatim_doc_comment)]
    pub override_target_version: Option<String>,

    /// Skips 'cargo check' after upgrade
    #[arg(short, long, default_value = "false", verbatim_doc_comment)]
    pub no_check: bool,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct LocalDepsArgs {
    /// Target directory where to generate local deps reports.
    /// Will be current directory if not specified.
    #[arg(long, verbatim_doc_comment)]
    pub path: Option<String>,

    /// Ignore all directories with these names.
    #[arg(long, verbatim_doc_comment)]
    #[clap(global = true, default_value = "target")]
    pub ignore: Vec<String>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TemplateArgs {
    /// The new name the contract is to receive.
    /// If missing, the template name will be considered.
    #[arg(long, verbatim_doc_comment)]
    pub name: Option<String>,

    /// The contract template to clone.
    #[arg(long, verbatim_doc_comment)]
    pub template: String,

    /// The framework version on which the contracts should be created.
    #[arg(long, verbatim_doc_comment)]
    pub tag: Option<String>,

    /// Target directory where to create the new contract directory.
    /// Will be current directory if not specified.
    #[arg(long, verbatim_doc_comment)]
    pub path: Option<PathBuf>,
}

impl CliArgsToRaw for TemplateArgs {
    fn to_raw(&self) -> Vec<String> {
        Vec::new()
    }
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TemplateListArgs {
    /// The framework version referred to.
    #[arg(long = "tag", verbatim_doc_comment)]
    pub tag: Option<String>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct TestGenArgs {
    /// Target directory where to generate contract integration tests.
    /// Will be current directory if not specified.
    #[arg(long, verbatim_doc_comment)]
    pub path: Option<String>,

    /// Ignore all directories with these names.
    #[arg(long, verbatim_doc_comment)]
    #[clap(global = true, default_value = "target")]
    pub ignore: Vec<String>,

    /// Creates test files if they don't exist.
    #[arg(long, verbatim_doc_comment)]
    pub create: bool,
}

#[derive(Default, PartialEq, Eq, Debug, Clone, Parser)]
#[command(propagate_version = true)]
pub struct InstallArgs {
    #[command(subcommand)]
    pub command: Option<InstallCommand>,
}

#[derive(Clone, PartialEq, Eq, Debug, Subcommand)]
pub enum InstallCommand {
    #[command(about = "Installs all the known tools")]
    All,

    #[command(about = "Installs the `mx-scenario-go` tool")]
    MxScenarioGo(InstallMxScenarioGoArgs),

    #[command(name = "wasm32", about = "Installs the `wasm32` target")]
    Wasm32(InstallWasm32Args),

    #[command(name = "wasm-opt", about = "Installs the `wasm-opt` tool")]
    WasmOpt(InstallWasmOptArgs),
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct InstallMxScenarioGoArgs {
    /// The framework version on which the contracts should be created.
    #[arg(long, verbatim_doc_comment)]
    pub tag: Option<String>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct InstallWasm32Args {}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct InstallWasmOptArgs {}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct AccountArgs {
    /// Provide the target API you want the real data to come from
    #[arg(long = "api")]
    #[clap(global = true)]
    pub api: Option<String>,

    /// Provide the address you want to retrieve data from
    #[arg(long = "address", verbatim_doc_comment)]
    pub address: String,
}