origen/commands/
target.rs1use super::_prelude::*;
2use origen::core::application::target;
3
4pub const BASE_CMD: &'static str = "target";
5pub const FULL_PATHS_OPT: &'static str = "full-paths";
6pub const TARGETS_OPT: &'static str = "targets";
7
8macro_rules! full_paths_opt {
9 () => {
10 Arg::new(FULL_PATHS_OPT)
11 .long(FULL_PATHS_OPT)
12 .visible_alias("full_paths")
13 .short('f')
14 .help("Display targets' full paths")
15 .action(SetArgTrue)
16 };
17}
18
19macro_rules! targets_arg {
20 ($help:expr) => {
21 Arg::new(TARGETS_OPT)
22 .help($help)
23 .action(AppendArgs)
24 .value_name("TARGETS")
25 .num_args(1..)
26 .required(true)
27 .use_value_delimiter(true)
28 };
29}
30
31macro_rules! tnames {
32 ($cmd:expr) => {
33 $cmd.get_many::<String>(TARGETS_OPT)
34 .unwrap()
35 .map(|t| t.as_str())
36 .collect::<Vec<&str>>()
37 };
38}
39
40gen_core_cmd_funcs__no_exts__no_app_opts!(
41 BASE_CMD,
42 "Set/view the default target",
43 { |cmd: App| { cmd.arg(full_paths_opt!()).visible_alias("t") } },
44 core_subcmd__no_exts__no_app_opts!("add", "Activates the given target(s)", {
45 |cmd: App| {
46 cmd.visible_alias("a")
47 .arg(targets_arg!("Targets to be activated"))
48 .arg(full_paths_opt!())
49 }
50 }),
51 core_subcmd__no_exts__no_app_opts!("clear", "Deactivates any and all current targets", {
52 |cmd: App| cmd.visible_alias("c")
53 }),
54 core_subcmd__no_exts__no_app_opts!("remove", "Deactivates the given target(s)", {
55 |cmd: App| {
56 cmd.visible_alias("r")
57 .arg(targets_arg!("Targets to be deactivated"))
58 .arg(full_paths_opt!())
59 }
60 }),
61 core_subcmd__no_exts__no_app_opts!(
62 "set",
63 "Activates the given target(s) while deactivating all others",
64 {
65 |cmd: App| {
66 cmd.visible_alias("s")
67 .arg(targets_arg!("Targets to be set"))
68 .arg(full_paths_opt!())
69 }
70 }
71 ),
72 core_subcmd__no_exts__no_app_opts!(
73 "default",
74 "Activates the default target(s) while deactivating all others",
75 { |cmd: App| { cmd.visible_alias("d").arg(full_paths_opt!()) } }
76 ),
77 core_subcmd__no_exts__no_app_opts!("view", "Views the currently activated target(s)", {
78 |cmd: App| cmd.visible_alias("v").arg(full_paths_opt!())
79 })
80);
81
82macro_rules! view {
83 ($invocation:expr) => {
84 view_targets(*$invocation.get_one::<bool>(FULL_PATHS_OPT).unwrap())
85 };
86}
87
88fn view_targets(fp: bool) -> Result<()> {
89 if let Some(targets) = target::get(fp) {
90 println!("The targets currently enabled are:");
91 println!("{}", targets.join("\n"))
92 } else {
93 println!(
94 "No targets have been enabled and this workspace does not enable any default targets"
95 )
96 }
97 Ok(())
98}
99
100pub(crate) fn run(invocation: &clap::ArgMatches) -> origen::Result<()> {
101 if let Some((n, subcmd)) = invocation.subcommand() {
102 match n {
103 "add" => {
104 target::add(tnames!(subcmd));
105 }
106 "clear" => {
107 target::clear();
108 view_targets(false)?;
109 return Ok(());
110 }
111 "default" => {
112 target::reset();
113 }
114 "remove" => {
115 target::remove(tnames!(subcmd));
116 }
117 "set" => {
118 target::set(tnames!(subcmd));
119 }
120 "view" => {
121 view!(subcmd)?;
122 return Ok(());
123 }
124 _ => unreachable_invalid_subc!(n),
125 }
126 view!(subcmd)
128 } else {
129 view!(invocation)?;
130 println!();
131 print_subcmds_available_msg!();
132 Ok(())
133 }
134}