origen/commands/
exec.rs

1use super::_prelude::*;
2use crate::python::{add_origen_env, PYTHON_CONFIG};
3use std::borrow::Cow;
4
5pub const BASE_CMD: &'static str = "exec";
6pub const EXEC_HELP: &'static str =
7    "Execute a command within your Origen/Python environment (e.g. origen exec pytest)";
8
9pub(crate) fn run_pre_phase(invocation: &clap::ArgMatches) -> Result<i32> {
10    let mut cmd = PYTHON_CONFIG.uv_command()?;
11    cmd.arg("run");
12    cmd.arg("--no-sync");
13    cmd.arg("--no-editable");
14    cmd.arg("--");
15    let cmd_name = invocation.get_one::<String>("cmd").unwrap();
16
17    cmd.arg(cmd_name);
18    if let Some(args) = invocation.get_many::<String>("args") {
19        cmd.args(args.filter(|arg| arg.as_str() != "--"));
20    }
21    log_debug!(
22        "Running Command (from Exec): {} {}",
23        cmd.get_program().to_string_lossy(),
24        cmd.get_args()
25            .map(|a| a.to_string_lossy())
26            .collect::<Vec<Cow<'_, str>>>()
27            .join(" ")
28    );
29    add_origen_env(&mut cmd);
30
31    let res = cmd.status()?;
32    Ok(if res.success() { 0 } else { 1 })
33}
34
35pub fn add_prephase_cmds<'a>(cmd: App) -> App {
36    cmd.subcommand(config_exec_cmd(Command::new(BASE_CMD)).disable_help_flag(true))
37}
38
39pub fn config_exec_cmd<'a>(cmd: App) -> App {
40    cmd.trailing_var_arg(true)
41        .arg(
42            Arg::new("cmd")
43                .help("The command to be run")
44                .action(SetArg)
45                .required(true)
46                .value_name("COMMAND"),
47        )
48        .arg(
49            Arg::new("args")
50                .help("Arguments to be passed to the command")
51                .action(AppendArgs)
52                .allow_hyphen_values(true)
53                .num_args(1..)
54                .required(false)
55                .value_name("ARGS"),
56        )
57}
58
59gen_core_cmd_funcs__no_exts__no_app_opts!(BASE_CMD, EXEC_HELP, { |cmd: App| config_exec_cmd(cmd) });