-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
259 lines (214 loc) · 6.52 KB
/
cli.rs
File metadata and controls
259 lines (214 loc) · 6.52 KB
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
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use crate::build_runner::BuildTarget;
#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum BuildScope {
/// Build the on-chain contract WASM.
Contract,
/// Build the off-chain data-driver WASM.
DataDriver,
/// Build both contract and data-driver WASMs.
#[default]
All,
}
impl BuildScope {
pub fn expand(self) -> Vec<BuildTarget> {
match self {
Self::Contract => vec![BuildTarget::Contract],
Self::DataDriver => vec![BuildTarget::DataDriver],
Self::All => vec![BuildTarget::Contract, BuildTarget::DataDriver],
}
}
pub fn needs_rust_src(self) -> bool {
matches!(self, Self::Contract | Self::All)
}
}
#[derive(Debug, Parser)]
#[command(name = "dusk-forge")]
#[command(bin_name = "dusk-forge")]
#[command(about = "CLI for scaffolding and building Dusk Forge contracts")]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Debug, Subcommand)]
pub enum Commands {
/// Scaffold a new contract project.
New(NewArgs),
/// Build WASM artifacts (contract, data-driver, or all).
Build(BuildArgs),
/// Build contract WASM and run cargo tests.
Test(TestArgs),
/// Validate project structure and toolchain.
Check(ProjectOptions),
/// Show macro-expanded code using cargo-expand.
Expand(ExpandArgs),
/// Remove contract-specific build artifact directories.
Clean(ProjectOptions),
/// Build data-driver WASM and print CONTRACT_SCHEMA as JSON.
Schema(SchemaArgs),
/// Encode call input bytes through the data-driver.
Call(CallArgs),
/// Verify contract and data-driver artifacts.
Verify(VerifyArgs),
/// Generate shell completion scripts.
Completions(CompletionsArgs),
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum TemplateChoice {
Counter,
Empty,
}
#[derive(Debug, Args)]
pub struct NewArgs {
/// Name of the new contract project (kebab-case).
pub name: String,
/// Directory in which the new project folder will be created.
#[arg(long, default_value = ".")]
pub path: PathBuf,
/// Built-in template to use.
#[arg(long, value_enum, default_value_t = TemplateChoice::Counter)]
pub template: TemplateChoice,
/// Skip `git init` in the created project.
#[arg(long)]
pub no_git: bool,
/// Enable verbose output.
#[arg(short, long)]
pub verbose: bool,
}
#[derive(Debug, Args)]
pub struct ProjectOptions {
/// Path to the contract project directory.
#[arg(long, default_value = ".")]
pub path: PathBuf,
/// Enable verbose output.
#[arg(short, long)]
pub verbose: bool,
}
#[derive(Debug, Args)]
pub struct BuildArgs {
#[command(flatten)]
pub project: ProjectOptions,
/// Which WASM target to build.
#[arg(value_enum, default_value_t)]
pub target: BuildScope,
}
#[derive(Debug, Args)]
#[command(trailing_var_arg = true)]
pub struct TestArgs {
#[command(flatten)]
pub project: ProjectOptions,
/// Extra args passed through to `cargo test --release`.
pub cargo_test_args: Vec<String>,
}
#[derive(Debug, Args)]
pub struct ExpandArgs {
#[command(flatten)]
pub project: ProjectOptions,
/// Expand with the data-driver feature.
#[arg(long)]
pub data_driver: bool,
}
#[derive(Debug, Args)]
pub struct SchemaArgs {
#[command(flatten)]
pub project: ProjectOptions,
/// Pretty-print JSON output.
#[arg(long)]
pub pretty: bool,
}
#[derive(Debug, Args)]
pub struct CallArgs {
#[command(flatten)]
pub project: ProjectOptions,
/// Contract function name to encode.
pub function: String,
/// JSON input payload for the function (use `null` for no input).
#[arg(long, default_value = "null")]
pub input: String,
}
#[derive(Debug, Args)]
pub struct VerifyArgs {
#[command(flatten)]
pub project: ProjectOptions,
/// Optional expected BLAKE3 hash of the contract WASM.
#[arg(long)]
pub expected_blake3: Option<String>,
/// Skip rebuilding artifacts and verify existing files only.
#[arg(long)]
pub skip_build: bool,
}
#[derive(Debug, Args)]
pub struct CompletionsArgs {
/// Shell to generate completions for.
#[arg(value_enum)]
pub shell: Shell,
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use clap::Parser;
use super::{Cli, Commands};
#[test]
fn parses_expand_command() {
let cli = Cli::parse_from(["dusk-forge", "expand", "--data-driver"]);
match cli.command {
Commands::Expand(args) => assert!(args.data_driver),
other => panic!("expected expand command, got {other:?}"),
}
}
#[test]
fn parses_clean_command() {
let cli = Cli::parse_from(["dusk-forge", "clean", "--path", "demo"]);
match cli.command {
Commands::Clean(args) => assert_eq!(args.path, PathBuf::from("demo")),
other => panic!("expected clean command, got {other:?}"),
}
}
#[test]
fn parses_completions_command() {
let cli = Cli::parse_from(["dusk-forge", "completions", "bash"]);
match cli.command {
Commands::Completions(_) => {}
other => panic!("expected completions command, got {other:?}"),
}
}
#[test]
fn parses_schema_command() {
let cli = Cli::parse_from(["dusk-forge", "schema", "--pretty"]);
match cli.command {
Commands::Schema(args) => assert!(args.pretty),
other => panic!("expected schema command, got {other:?}"),
}
}
#[test]
fn parses_call_command() {
let cli = Cli::parse_from(["dusk-forge", "call", "transfer", "--input", "{\"foo\":1}"]);
match cli.command {
Commands::Call(args) => {
assert_eq!(args.function, "transfer");
assert_eq!(args.input, "{\"foo\":1}");
}
other => panic!("expected call command, got {other:?}"),
}
}
#[test]
fn parses_verify_command() {
let cli = Cli::parse_from([
"dusk-forge",
"verify",
"--expected-blake3",
"deadbeef",
"--skip-build",
]);
match cli.command {
Commands::Verify(args) => {
assert_eq!(args.expected_blake3.as_deref(), Some("deadbeef"));
assert!(args.skip_build);
}
other => panic!("expected verify command, got {other:?}"),
}
}
}