nishikaze 0.3.0

Zephyr build system companion.
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Cli command definitions.

use clap::{Args, Parser, Subcommand};

/// Top level CLI struct for `kaze` command configuration.
#[derive(Parser, Debug, Clone)]
#[command(
    name = "kaze",
    about = "Zephyr build system companion.",
    version,
    propagate_version = true,
    after_help = r#"Examples:
  # Simple project
  kaze -b nucleo_f767zi -r openocd build
  kaze flash

  # Sysbuild project
  kaze flash --list
  kaze flash --image app

For more info, see https://gilab.com/byacrates/nishikaze
"#
)]
pub struct Cli {
    /// Pre-clean the active build dir
    #[arg(short = 'c', long = "clean")]
    pub preclean: bool,

    /// Run command for all configured profiles (overrides --profile/default)
    #[arg(short = 'a', long = "all")]
    pub all: bool,

    /// Select project profile defined in kaze.toml
    #[arg(short = 'p', long = "profile")]
    pub profile: Option<String>,

    /// Zephyr OS board/target (overrides config)
    #[arg(short = 'b', long = "board")]
    pub board: Option<String>,

    /// Zephyr OS flash runner (overrides config)
    #[arg(short = 'r', long = "runner")]
    pub runner: Option<String>,

    /// Explicit project root
    #[arg(long = "project")]
    pub project: Option<std::path::PathBuf>,

    /// Verbosity (repeatable)
    #[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count, default_value_t = 2)]
    pub verbose: u8,

    /// Dry run
    #[arg(short = 'd', long = "dry-run")]
    pub dry_run: bool,

    /// Selected command
    #[command(subcommand)]
    pub command: Command,
}

impl Cli {
    /**
     * Creates new kaze cli
     *
     * # Returns
     * Constructed `Cli` with parsed args
     */
    #[must_use]
    pub fn parse_args() -> Self {
        Self::parse()
    }
}

/// Enum of kaze commands
#[derive(Subcommand, Debug, Clone)]
pub enum Command {
    /// Initializes project with kaze.toml
    #[command(alias = "i")]
    Init(InitArgs),

    /// Lists available boards
    #[command(alias = "bo")]
    Boards,

    /// Lists available runners
    #[command(alias = "rn")]
    Runners,

    /// Lists profiles configured in kaze.toml
    #[command(alias = "p")]
    Profiles,

    /// Cleans build dir
    #[command(alias = "c")]
    Clean,

    /// Configure project
    #[command(alias = "cf")]
    Conf(PhaseArgs),

    /// Build binary
    #[command(alias = "b")]
    Build(PhaseArgs),

    /// Run simulation
    #[command(alias = "r")]
    Run(RunArgs),

    /// Flash binary
    #[command(alias = "f")]
    Flash(FlashArgs),

    /// Run menuconfig
    #[command(alias = "m")]
    Menuconfig(MenuconfigArgs),

    /// Generate SPDX `BoM`
    #[command(alias = "spdx")]
    Bom(BomArgs),

    /// Workspace management
    #[command(alias = "ws")]
    Workspace(WsArgs),
}

/// Extra phase args captures everything after `--` aa a passthrough
#[derive(Args, Debug, Default, Clone)]
pub struct PhaseArgs {
    /// Extra args
    #[arg(trailing_var_arg = true)]
    pub extra: Vec<String>,
}

/// Common args for run/flash used for sysbuild projects
#[derive(Args, Debug, Default, Clone)]
pub struct SysbuildArgs {
    /// List available sysbuild images
    #[arg(short = 'l', long = "list")]
    pub list: bool,

    /// Select sysbuild image
    #[arg(short = 'i', long = "image")]
    pub image: Option<String>,
}

/// Run command args
#[derive(Args, Debug, Default, Clone)]
pub struct RunArgs {
    /// Skip pre-run rebuild when running simulator binary directly
    #[arg(short = 'n', long = "norebuild")]
    pub norebuild: bool,

    /// Sysbuild args
    #[command(flatten)]
    pub sys: SysbuildArgs,

    /// Extra args
    #[command(flatten)]
    pub phase: PhaseArgs,
}

/// Flash command args
#[derive(Args, Debug, Default, Clone)]
pub struct FlashArgs {
    /// Sysbuild args
    #[command(flatten)]
    pub sys: SysbuildArgs,

    /// Extra args
    #[command(flatten)]
    pub phase: PhaseArgs,
}

/// Menuconfig command args
#[derive(Args, Debug, Default, Clone)]
pub struct MenuconfigArgs {
    /// Sysbuild args
    #[command(flatten)]
    pub sys: SysbuildArgs,
}

/// `BoM` generation args
#[derive(Args, Debug, Default, Clone)]
pub struct BomArgs {
    /// Sysbuild args
    #[command(flatten)]
    pub sys: SysbuildArgs,
}

/// Init command args
#[derive(Args, Debug, Default, Clone)]
pub struct InitArgs {
    /// Force re-generate `kaze.toml`
    #[arg(short = 'f', long = "force")]
    pub force: bool,
}

/// Workspace command args
#[derive(Args, Debug, Clone)]
pub struct WsArgs {
    /// Workspace management subcommand
    #[command(subcommand)]
    pub command: WsCommand,
}

/// Workspace subcommands
#[derive(Subcommand, Debug, Clone)]
pub enum WsCommand {
    /// Initialize workspace from a url or a manifest file
    #[command(alias = "i")]
    Init(WsInitArgs),

    /// Update workspace
    #[command(alias = "u")]
    Update,

    /// Exports workspace cmake package
    #[command(alias = "e")]
    Export,

    /// Apply workspace by running init, update and export
    #[command(alias = "a")]
    Apply(WsApplyArgs),
}

/// Workspace init subcommand args
#[derive(Args, Debug, Default, Clone)]
pub struct WsInitArgs {
    /// Force Workspace init
    #[arg(short = 'f', long = "force")]
    pub force: bool,
}

/// Workspace apply subcommand args
#[derive(Args, Debug, Default, Clone)]
pub struct WsApplyArgs {
    /// Force workspace re-init
    #[arg(short = 'f', long = "force")]
    pub force: bool,

    /// No workspace update
    #[arg(short = 'n', long = "noupdate")]
    pub noupdate: bool,
}

#[cfg(test)]
mod tests {
    use clap::Parser;

    use super::*;

    #[test]
    fn parse_defaults() {
        let cli = Cli::try_parse_from(["kaze", "clean"]).expect("parse ok");
        assert!(!cli.preclean);
        assert!(!cli.all);
        assert_eq!(cli.profile, None);
        assert_eq!(cli.board, None);
        assert_eq!(cli.runner, None);
        assert_eq!(cli.verbose, 2);
        assert!(matches!(cli.command, Command::Clean));
    }

    #[test]
    fn parse_flags_and_options() {
        let cli = Cli::try_parse_from([
            "kaze",
            "-c",
            "-a",
            "-p",
            "dev",
            "-b",
            "native_sim",
            "-r",
            "native",
            "-v",
            "-v",
            "build",
        ])
        .expect("parse ok");
        assert!(cli.preclean);
        assert!(cli.all);
        assert_eq!(cli.profile.as_deref(), Some("dev"));
        assert_eq!(cli.board.as_deref(), Some("native_sim"));
        assert_eq!(cli.runner.as_deref(), Some("native"));
        assert_eq!(cli.verbose, 2);
        assert!(matches!(cli.command, Command::Build(_)));
    }

    #[test]
    fn parse_subcommands_with_args() {
        let cli = Cli::try_parse_from(["kaze", "run", "-n", "-l", "-i", "app", "--", "-DOPT=1"])
            .expect("parse ok");
        assert!(matches!(cli.command, Command::Run(_)));
        let Command::Run(args) = cli.command else {
            return;
        };
        assert!(args.norebuild);
        assert!(args.sys.list);
        assert_eq!(args.sys.image.as_deref(), Some("app"));
        assert_eq!(args.phase.extra, vec!["-DOPT=1"]);
    }

    #[test]
    fn parse_simple_subcommands() {
        let cli_boards = Cli::try_parse_from(["kaze", "boards"]).expect("parse ok");
        assert!(matches!(cli_boards.command, Command::Boards));

        let cli_runners = Cli::try_parse_from(["kaze", "runners"]).expect("parse ok");
        assert!(matches!(cli_runners.command, Command::Runners));

        let cli_profiles = Cli::try_parse_from(["kaze", "profiles"]).expect("parse ok");
        assert!(matches!(cli_profiles.command, Command::Profiles));
    }

    #[test]
    fn parse_conf_and_flash_with_passthrough() {
        let cli_conf = Cli::try_parse_from(["kaze", "conf", "--", "-DOPT=1"]).expect("parse ok");
        assert!(matches!(cli_conf.command, Command::Conf(_)));
        let Command::Conf(conf_args) = cli_conf.command else {
            return;
        };
        assert_eq!(conf_args.extra, vec!["-DOPT=1"]);

        let cli_flash =
            Cli::try_parse_from(["kaze", "flash", "-l", "-i", "app"]).expect("parse ok");
        assert!(matches!(cli_flash.command, Command::Flash(_)));
        let Command::Flash(flash_args) = cli_flash.command else {
            return;
        };
        assert!(flash_args.sys.list);
        assert_eq!(flash_args.sys.image.as_deref(), Some("app"));
    }

    #[test]
    fn parse_bom_with_sysbuild_args() {
        let cli = Cli::try_parse_from(["kaze", "bom", "-l", "-i", "app"]).expect("parse ok");
        assert!(matches!(cli.command, Command::Bom(_)));
        let Command::Bom(args) = cli.command else {
            return;
        };
        assert!(args.sys.list);
        assert_eq!(args.sys.image.as_deref(), Some("app"));
    }

    #[test]
    fn parse_bom_alias_spdx() {
        let cli = Cli::try_parse_from(["kaze", "spdx"]).expect("parse ok");
        assert!(matches!(cli.command, Command::Bom(_)));
        let Command::Bom(args) = cli.command else {
            return;
        };
        assert!(!args.sys.list);
        assert!(args.sys.image.is_none());
    }

    #[test]
    fn parse_menuconfig_with_sysbuild_args() {
        let cli = Cli::try_parse_from(["kaze", "menuconfig", "-l", "-i", "app"]).expect("parse ok");
        assert!(matches!(cli.command, Command::Menuconfig(_)));
        let Command::Menuconfig(args) = cli.command else {
            return;
        };
        assert!(args.sys.list);
        assert_eq!(args.sys.image.as_deref(), Some("app"));
    }

    #[test]
    fn parse_menuconfig_alias_m() {
        let cli = Cli::try_parse_from(["kaze", "m"]).expect("parse ok");
        assert!(matches!(cli.command, Command::Menuconfig(_)));
        let Command::Menuconfig(args) = cli.command else {
            return;
        };
        assert!(!args.sys.list);
        assert!(args.sys.image.is_none());
    }

    #[test]
    fn parse_aliases_and_defaults() {
        let cli_init = Cli::try_parse_from(["kaze", "i"]).expect("parse ok");
        assert!(matches!(cli_init.command, Command::Init(_)));

        let cli_boards = Cli::try_parse_from(["kaze", "bo"]).expect("parse ok");
        assert!(matches!(cli_boards.command, Command::Boards));

        let cli_runners = Cli::try_parse_from(["kaze", "rn"]).expect("parse ok");
        assert!(matches!(cli_runners.command, Command::Runners));

        let cli_profiles = Cli::try_parse_from(["kaze", "p"]).expect("parse ok");
        assert!(matches!(cli_profiles.command, Command::Profiles));

        let cli_clean = Cli::try_parse_from(["kaze", "c"]).expect("parse ok");
        assert!(matches!(cli_clean.command, Command::Clean));

        let cli_conf = Cli::try_parse_from(["kaze", "cf"]).expect("parse ok");
        assert!(matches!(cli_conf.command, Command::Conf(_)));

        let cli_build = Cli::try_parse_from(["kaze", "b"]).expect("parse ok");
        assert!(matches!(cli_build.command, Command::Build(_)));

        let cli_run = Cli::try_parse_from(["kaze", "r"]).expect("parse ok");
        assert!(matches!(&cli_run.command, Command::Run(_)));
        if let Command::Run(ref run_args) = cli_run.command {
            assert!(!run_args.norebuild);
            assert!(!run_args.sys.list);
            assert_eq!(run_args.sys.image, None);
            assert!(run_args.phase.extra.is_empty());
        }

        let cli_flash = Cli::try_parse_from(["kaze", "f"]).expect("parse ok");
        assert!(matches!(&cli_flash.command, Command::Flash(_)));
        if let Command::Flash(ref flash_args) = cli_flash.command {
            assert!(!flash_args.sys.list);
            assert_eq!(flash_args.sys.image, None);
            assert!(flash_args.phase.extra.is_empty());
        }
    }

    #[test]
    fn parse_workspace_init_and_alias() {
        let cli_full = Cli::try_parse_from(["kaze", "workspace", "init", "-f"]).expect("parse ok");
        assert!(matches!(
            cli_full.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Init(WsInitArgs { force: true })
            })
        ));

        let cli_alias = Cli::try_parse_from(["kaze", "ws", "i", "-f"]).expect("parse ok");
        assert!(matches!(
            cli_alias.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Init(WsInitArgs { force: true })
            })
        ));
    }

    #[test]
    fn parse_workspace_update_and_alias() {
        let cli_full = Cli::try_parse_from(["kaze", "ws", "update"]).expect("parse ok");
        assert!(matches!(
            cli_full.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Update
            })
        ));

        let cli_alias = Cli::try_parse_from(["kaze", "ws", "u"]).expect("parse ok");
        assert!(matches!(
            cli_alias.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Update
            })
        ));
    }

    #[test]
    fn parse_workspace_export_and_alias() {
        let cli_full = Cli::try_parse_from(["kaze", "ws", "export"]).expect("parse ok");
        assert!(matches!(
            cli_full.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Export
            })
        ));

        let cli_alias = Cli::try_parse_from(["kaze", "ws", "e"]).expect("parse ok");
        assert!(matches!(
            cli_alias.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Export
            })
        ));
    }

    #[test]
    fn parse_workspace_apply_and_alias() {
        let cli_full = Cli::try_parse_from(["kaze", "ws", "apply", "-f", "-n"]).expect("parse ok");
        assert!(matches!(
            cli_full.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Apply(WsApplyArgs {
                    force: true,
                    noupdate: true
                })
            })
        ));

        let cli_alias = Cli::try_parse_from(["kaze", "ws", "a", "-f", "-n"]).expect("parse ok");
        assert!(matches!(
            cli_alias.command,
            Command::Workspace(WsArgs {
                command: WsCommand::Apply(WsApplyArgs {
                    force: true,
                    noupdate: true
                })
            })
        ));
    }
}