1use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
2use std::path::PathBuf;
3
4use drt_sc_meta_lib::cli::{CliArgsToRaw, ContractCliAction};
5
6#[derive(Default, PartialEq, Eq, Debug, Parser)]
8#[command(
9 version,
10 about,
11 after_help = "
12The Dharitri smart contract Meta crate can be used in two ways:
13 A. Import it into a contract's specific meta-crate.
14 There it will receive access to the contract ABI generator.
15 Based on that it is able to build the contract and apply various tools.
16 This part also contains the multi-contract config infrastructure.
17
18 B. Use it as a standalone tool.
19 It can be used to automatically upgrade contracts from one version to the next.
20
21You are currently using the standalone tool (B).
22"
23)]
24#[command(propagate_version = true)]
25pub struct StandaloneCliArgs {
26 #[command(subcommand)]
27 pub command: Option<StandaloneCliAction>,
28}
29
30#[derive(Clone, PartialEq, Eq, Debug, Subcommand)]
31pub enum StandaloneCliAction {
32 #[command(name = "install", about = "Installs framework dependencies")]
33 Install(InstallArgs),
34
35 #[command(
36 about = "General info about the contract an libraries residing in the targetted directory.."
37 )]
38 Info(InfoArgs),
39
40 #[command(
41 about = "Calls the meta crates for all contracts under given path with the given arguments."
42 )]
43 All(AllArgs),
44
45 #[command(
46 about = "Upgrades a contract to the latest version. Multiple contract crates are allowed."
47 )]
48 Upgrade(UpgradeArgs),
49
50 #[command(name = "new", about = "Creates a contract by a pre-existing template")]
51 Template(TemplateArgs),
52
53 #[command(name = "templates", about = "Lists all pre-existing templates")]
54 TemplateList(TemplateListArgs),
55
56 #[command(
57 name = "test-gen",
58 about = "Generates Rust integration tests based on scenarios provided in the scenarios folder of each contract."
59 )]
60 TestGen(TestGenArgs),
61
62 #[command(name = "test", about = "Runs cargo test")]
63 Test(TestArgs),
64
65 #[command(name = "test-coverage", about = "Run test coverage and output report")]
66 TestCoverage(TestCoverageArgs),
67
68 #[command(
69 about = "Generates a scenario test initialized with real data fetched from the blockchain."
70 )]
71 Account(AccountArgs),
72
73 #[command(
74 name = "local-deps",
75 about = "Generates a report on the local depedencies of contract crates. Will explore indirect depdencies too."
76 )]
77 LocalDeps(LocalDepsArgs),
78}
79
80#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
81pub struct InfoArgs {
82 #[arg(long, verbatim_doc_comment)]
85 pub path: Option<String>,
86
87 #[arg(long, verbatim_doc_comment)]
89 #[clap(global = true, default_value = "target")]
90 pub ignore: Vec<String>,
91}
92
93#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
94pub struct TestArgs {
95 #[arg(short, long, verbatim_doc_comment)]
98 pub path: Option<String>,
99
100 #[arg(short, long, default_value = "false", verbatim_doc_comment)]
103 pub go: bool,
104
105 #[arg(short, long, default_value = "false", verbatim_doc_comment)]
109 pub scen: bool,
110
111 #[arg(short, long, default_value = "false", verbatim_doc_comment)]
114 pub nocapture: bool,
115}
116
117#[derive(Default, Clone, PartialEq, Eq, Debug, ValueEnum)]
118pub enum TestCoverageOutputFormat {
119 #[default]
121 Markdown,
122
123 Json,
125}
126
127#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
128pub struct TestCoverageArgs {
129 #[arg(short, long, verbatim_doc_comment)]
131 pub output: String,
132
133 #[arg(short, long, verbatim_doc_comment)]
135 pub format: Option<TestCoverageOutputFormat>,
136
137 #[arg(short = 'i', long = "ignore-filename-regex", verbatim_doc_comment)]
139 pub ignore_filename_regex: Vec<String>,
140}
141
142#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
143pub struct AllArgs {
144 #[command(subcommand)]
145 pub command: ContractCliAction,
146
147 #[arg(long, verbatim_doc_comment)]
150 #[clap(global = true)]
151 pub path: Option<String>,
152
153 #[arg(long, verbatim_doc_comment)]
155 #[clap(global = true, default_value = "target")]
156 pub ignore: Vec<String>,
157
158 #[arg(
159 long = "no-abi-git-version",
160 help = "Skips loading the Git version into the ABI",
161 action = ArgAction::SetFalse
162 )]
163 #[clap(global = true)]
164 pub load_abi_git_version: bool,
165
166 #[arg(long = "target-dir-meta", verbatim_doc_comment)]
169 #[clap(global = true)]
170 pub target_dir_meta: Option<String>,
171
172 #[arg(long = "target-dir-all", verbatim_doc_comment)]
174 #[clap(global = true)]
175 pub target_dir_all: Option<String>,
176}
177
178impl AllArgs {
179 pub fn target_dir_all_override(&self) -> Self {
180 let mut result = self.clone();
181 if let Some(target_dir_all) = &self.target_dir_all {
182 result.target_dir_meta = Some(target_dir_all.clone());
183 match &mut result.command {
184 ContractCliAction::Build(build_args) => {
185 build_args.target_dir_wasm = Some(target_dir_all.clone());
186 },
187 ContractCliAction::BuildDbg(build_args) => {
188 build_args.target_dir_wasm = Some(target_dir_all.clone());
189 },
190 ContractCliAction::Twiggy(build_args) => {
191 build_args.target_dir_wasm = Some(target_dir_all.clone());
192 },
193 _ => {},
194 }
195 }
196 result
197 }
198
199 pub fn to_cargo_run_args(&self) -> Vec<String> {
200 let processed = self.target_dir_all_override();
201 let mut raw = vec!["run".to_string()];
202 if let Some(target_dir_meta) = &processed.target_dir_meta {
203 raw.push("--target-dir".to_string());
204 raw.push(target_dir_meta.clone());
205 }
206 raw.append(&mut processed.command.to_raw());
207 if !processed.load_abi_git_version {
208 raw.push("--no-abi-git-version".to_string());
209 }
210 raw
211 }
212}
213
214#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
215pub struct UpgradeArgs {
216 #[arg(long, verbatim_doc_comment)]
219 pub path: Option<String>,
220
221 #[arg(long, verbatim_doc_comment)]
223 #[clap(global = true, default_value = "target")]
224 pub ignore: Vec<String>,
225
226 #[arg(long = "to", verbatim_doc_comment)]
229 pub override_target_version: Option<String>,
230
231 #[arg(short, long, default_value = "false", verbatim_doc_comment)]
233 pub no_check: bool,
234}
235
236#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
237pub struct LocalDepsArgs {
238 #[arg(long, verbatim_doc_comment)]
241 pub path: Option<String>,
242
243 #[arg(long, verbatim_doc_comment)]
245 #[clap(global = true, default_value = "target")]
246 pub ignore: Vec<String>,
247}
248
249#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
250pub struct TemplateArgs {
251 #[arg(long, verbatim_doc_comment)]
254 pub name: Option<String>,
255
256 #[arg(long, verbatim_doc_comment)]
258 pub template: String,
259
260 #[arg(long, verbatim_doc_comment)]
262 pub tag: Option<String>,
263
264 #[arg(long, verbatim_doc_comment)]
267 pub path: Option<PathBuf>,
268}
269
270impl CliArgsToRaw for TemplateArgs {
271 fn to_raw(&self) -> Vec<String> {
272 Vec::new()
273 }
274}
275
276#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
277pub struct TemplateListArgs {
278 #[arg(long = "tag", verbatim_doc_comment)]
280 pub tag: Option<String>,
281}
282
283#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
284pub struct TestGenArgs {
285 #[arg(long, verbatim_doc_comment)]
288 pub path: Option<String>,
289
290 #[arg(long, verbatim_doc_comment)]
292 #[clap(global = true, default_value = "target")]
293 pub ignore: Vec<String>,
294
295 #[arg(long, verbatim_doc_comment)]
297 pub create: bool,
298}
299
300#[derive(Default, PartialEq, Eq, Debug, Clone, Parser)]
301#[command(propagate_version = true)]
302pub struct InstallArgs {
303 #[command(subcommand)]
304 pub command: Option<InstallCommand>,
305}
306
307#[derive(Clone, PartialEq, Eq, Debug, Subcommand)]
308pub enum InstallCommand {
309 #[command(about = "Installs all the known tools")]
310 All,
311
312 #[command(about = "Installs the `drt-scenario-go` tool")]
313 DrtScenarioGo(InstallDrtScenarioGoArgs),
314
315 #[command(name = "wasm32", about = "Installs the `wasm32` target")]
316 Wasm32(InstallWasm32Args),
317
318 #[command(name = "wasm-opt", about = "Installs the `wasm-opt` tool")]
319 WasmOpt(InstallWasmOptArgs),
320}
321
322#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
323pub struct InstallDrtScenarioGoArgs {
324 #[arg(long, verbatim_doc_comment)]
326 pub tag: Option<String>,
327}
328
329#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
330pub struct InstallWasm32Args {}
331
332#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
333pub struct InstallWasmOptArgs {}
334
335#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
336pub struct AccountArgs {
337 #[arg(long = "api")]
339 #[clap(global = true)]
340 pub api: Option<String>,
341
342 #[arg(long = "address", verbatim_doc_comment)]
344 pub address: String,
345}