origen/commands/
app.rs

1use crate::commands::_prelude::*;
2use crate::framework::app_cmds::add_commands as add_app_user_commands;
3use crate::framework::app_cmds::add_helps as add_app_cmd_helps;
4use crate::framework::AppCmds;
5use clap::ArgMatches;
6use indexmap::IndexMap;
7
8pub const BASE_CMD: &'static str = "app";
9
10pub(crate) fn add_helps(helps: &mut CmdHelps, app_cmds: &AppCmds) {
11    helps
12        .add_core_cmd(BASE_CMD)
13        .set_help_msg("Manage and interface with the application")
14        .set_as_not_extendable();
15    add_app_cmd_helps(helps, app_cmds);
16}
17
18pub(crate) fn add_commands<'a>(
19    app: App,
20    helps: &'a CmdHelps,
21    app_cmds: &'a AppCmds,
22    exts: &'a Extensions,
23) -> Result<App> {
24    let mut app_subc = helps.core_cmd(BASE_CMD).arg_required_else_help(true);
25    // .subcommand(
26    //     Command::new("init")
27    //         .about("Initialize the application's revision control")
28    // )
29    // .subcommand(
30    //     Command::new("status")
31    //         .about("Show any local changes")
32    //         .arg(Arg::new("modified")
33    //             .long("modified")
34    //             .action(SetArgTrue)
35    //             .help("Show tracked, modified files")
36    //         )
37    //         .arg(Arg::new("untracked")
38    //             .long("untracked")
39    //             .action(SetArgTrue)
40    //             .help("Show untracked files")
41    //         )
42    // )
43    // .subcommand(
44    //     Command::new("checkin")
45    //         .about("Check in the given pathspecs")
46    //         .arg(Arg::new("pathspecs")
47    //             .help("The paths to be checked in")
48    //             .action(AppendArgs)
49    //             .value_name("PATHSPECS")
50    //             .multiple(true)
51    //         )
52    //         .arg(Arg::new("all")
53    //             .long("all")
54    //             .short('a')
55    //             .action(SetArgTrue)
56    //             .conflicts_with("pathspecs")
57    //             .help("Check in all changes in the workspace")
58    //         )
59    //         .arg(Arg::new("dry-run")
60    //             .long("dry-run")
61    //             .action(SetArgTrue)
62    //             .conflicts_with("pathspecs")
63    //             .help("Perform a dry-run only")
64    //         )
65    //         .arg(Arg::new("message")
66    //             .long("message")
67    //             .short('m')
68    //             .action(SetArg)
69    //             .required(true)
70    //             .help("Message to provide with the check-in operation")
71    //         )
72    // )
73    // .subcommand(
74    //     Command::new("package")
75    //         .about("Build the app into publishable package (e.g., a 'python wheel')"),
76    // )
77    // .subcommand(Command::new("run_publish_checks")
78    //     .about("Run production-ready and publish-ready checks")
79    // )
80    // .subcommand(Command::new("publish")
81    //     .about("Publish (release) the app")
82    //     .arg(Arg::new("dry-run")
83    //         .long("dry-run")
84    //         .action(SetArgTrue)
85    //         .help("Runs through the entire process except the uploading and mailer steps")
86    //     )
87    //     .arg(Arg::new("version")
88    //         .long("version")
89    //         .action(SetArg)
90    //         .value_name("VERSION")
91    //         .help("Publish with the given version increment")
92    //     )
93    //     .arg(Arg::new("release-note")
94    //         .long("release-note")
95    //         .action(SetArg)
96    //         .value_name("NOTE")
97    //         .help("Publish with the given release note")
98    //     )
99    //     .arg(Arg::new("release-title")
100    //         .long("release-title")
101    //         .action(SetArg)
102    //         .value_name("TITLE")
103    //         .help("Publish with the given release title")
104    //     )
105    //     .arg(Arg::new("no-release-title")
106    //         .long("no-release-title")
107    //         .action(SetArgTrue)
108    //         .help("Indicate no release title will be provided")
109    //         .conflicts_with("release-title")
110    //     )
111    // );
112    app_subc = add_app_user_commands(app_subc, helps, app_cmds, exts)?;
113    Ok(app.subcommand(app_subc))
114}
115
116// fn _run(cmd: &str, proc_cmd: &ArgMatches, args: Option<IndexMap<&str, String>>) -> Result<()>{
117//     super::launch(
118//         cmd,
119//         if let Some(targets) = proc_cmd.get_many::<String>("target") {
120//             Some(targets.map(|t| t.as_str()).collect())
121//         } else {
122//             Option::None
123//         },
124//         &None,
125//         None,
126//         None,
127//         None,
128//         false,
129//         args,
130//     );
131//     Ok(())
132// }
133
134pub(crate) fn run(
135    invocation: &ArgMatches,
136    mut app: &App,
137    exts: &Extensions,
138    plugins: Option<&Plugins>,
139    app_cmds: &AppCmds,
140) -> origen::Result<()> {
141    let subcmd = invocation.subcommand().unwrap();
142    let sub = subcmd.1;
143    match subcmd.0 {
144        // "init" => {
145        //     _run("app:init", sub, None)
146        // }
147        // "status" => {
148        //     let mut args = IndexMap::new();
149        //     if sub.contains_id("modified") {
150        //         args.insert("modified", "True".to_string());
151        //     }
152        //     if sub.contains_id("untracked") {
153        //         args.insert("untracked", "True".to_string());
154        //     }
155        //     _run("app:status", sub, Some(args))
156        // }
157        // "checkin" => {
158        //     let mut args = IndexMap::new();
159        //     if sub.contains_id("all") {
160        //         args.insert("all", "True".to_string());
161        //     }
162        //     if sub.contains_id("dry-run") {
163        //         args.insert("dry-run", "True".to_string());
164        //     }
165        //     if let Some(pathspecs) = sub.get_many::<String>("pathspecs") {
166        //         let p = pathspecs
167        //             .map(|ps| format!("\"{}\"", ps))
168        //             .collect::<Vec<String>>();
169        //         args.insert("pathspecs", format!("[{}]", p.join(",")));
170        //     }
171        //     args.insert(
172        //         "msg",
173        //         format!("\"{}\"", sub.get_one::<String>("message").unwrap()),
174        //     );
175        //     _run("app:checkin", sub, Some(args))
176        // }
177        // "package" => {
178        //     _run("app:package", sub, None)
179        // }
180        // "publish" => {
181        //     let mut args = IndexMap::new();
182        //     if sub.contains_id("dry-run") {
183        //         args.insert("dry-run", "True".to_string());
184        //     }
185        //     if let Some(v) = sub.get_one::<&str>("version") {
186        //         args.insert("version", format!("\"{}\"", v.to_string()));
187        //     }
188        //     if let Some(r) = sub.get_one::<&str>("release-note") {
189        //         args.insert("release-note", format!("\"{}\"", r.to_string()));
190        //     }
191        //     if let Some(t) = sub.get_one::<&str>("release-title") {
192        //         args.insert("release-title", format!("\"{}\"", t.to_string()));
193        //     }
194        //     if sub.contains_id("no-release-title") {
195        //         args.insert("no-release-title", "True".to_string());
196        //     }
197        //     _run("app:publish", sub, Some(args))
198        // }
199        // "run_publish_checks" => {
200        //     _run("app:run_publish_checks", sub, None)
201        // }
202        "commands" => {
203            if sub.subcommand().is_some() {
204                // invocation.subcommand() {
205                let mut overrides = IndexMap::new();
206
207                let mut matches = sub;
208                let mut path_pieces: Vec<String> = vec![];
209                app = app.find_subcommand(BASE_CMD).unwrap();
210                // matches = matches.subcommand_matches("commands").unwrap();
211                app = app.find_subcommand("commands").unwrap();
212                while matches.subcommand_name().is_some() {
213                    let n = matches.subcommand_name().unwrap();
214                    matches = matches.subcommand_matches(&n).unwrap();
215                    app = app.find_subcommand(n).unwrap();
216                    path_pieces.push(n.to_string());
217                }
218                let path = path_pieces.join(".");
219                launch_as(
220                    "_dispatch_app_cmd_",
221                    Some(&path_pieces),
222                    matches,
223                    app,
224                    exts.get_app_ext(&path),
225                    plugins,
226                    Some({
227                        overrides.insert(
228                            "dispatch_root".to_string(),
229                            Some(format!("r'{}'", app_cmds.cmds_root()?.display())),
230                        );
231                        overrides
232                    }),
233                );
234                Ok(())
235            } else {
236                // This case shouldn't happen as any non-valid command should be
237                // caught previously by clap and a non-command invocation should
238                // print the help message.
239                unreachable!("Expected an APP command but none was found!");
240            }
241        }
242
243        _ => unreachable!(),
244    }
245}