bricks/cli/run/
mod.rs

1use std::process::Command;
2
3use anyhow::{bail, Result};
4
5use crate::{
6    build,
7    cli::{
8        args::{BuildCommand, RunCommand},
9        pretty,
10    },
11    config::{brick::BrickKind, Config},
12};
13
14pub fn run(config: Config, run_command: RunCommand) -> Result<()> {
15    if let BrickKind::Library = config.brick.kind {
16        bail!("cannot run a library")
17    }
18
19    let override_build = match config.brick.overrides {
20        Some(ref v) => &v.build,
21        None => &None,
22    };
23
24    let build_path = match build::build(
25        &config,
26        BuildCommand {
27            force: run_command.force,
28            path: run_command.path.clone(),
29            emit_compile_commands: true,
30            silent: false,
31        },
32        override_build.clone(),
33    )? {
34        Some(p) => p,
35        None => {
36            let override_run = match config.brick.overrides {
37                Some(v) => v.run,
38                None => None,
39            };
40
41            match override_run {
42                Some(override_run_cmd) => {
43                    pretty::msg("run", &override_run_cmd);
44
45                    let mut cmd = match std::env::consts::OS {
46                        "windows" => {
47                            let mut cmd = Command::new("cmd");
48                            cmd.arg("/C");
49                            cmd
50                        }
51                        _ => {
52                            let mut cmd = Command::new("sh");
53                            cmd.arg("-c");
54                            cmd
55                        }
56                    };
57                    cmd.current_dir(&run_command.path);
58                    cmd.arg(override_run_cmd);
59                    cmd.status()?;
60                    return Ok(());
61                }
62                None => {
63                    bail!("build path was not returned, and a `run` override was not specified")
64                }
65            }
66        }
67    };
68
69    pretty::msg("run", build_path.display());
70
71    Command::new(build_path).status()?;
72
73    Ok(())
74}