lux_cli/
exec.rs

1use std::env;
2
3use clap::Args;
4use eyre::Result;
5use lux_lib::{
6    config::{Config, LuaVersion},
7    operations::{self, install_command},
8    path::Paths,
9    project::Project,
10};
11use which::which;
12
13use crate::build::Build;
14
15#[derive(Args)]
16pub struct Exec {
17    /// The command to run.
18    command: String,
19    /// Arguments to pass to the program.
20    args: Option<Vec<String>>,
21}
22
23pub async fn exec(run: Exec, config: Config) -> Result<()> {
24    let project = Project::current()?;
25    let tree = match &project {
26        Some(project) => project.tree(&config)?,
27        None => {
28            let lua_version = LuaVersion::from(&config)?.clone();
29            config.user_tree(lua_version)?
30        }
31    };
32
33    let paths = Paths::new(&tree)?;
34    unsafe {
35        // safe as long as this is single-threaded
36        env::set_var("PATH", paths.path_prepended().joined());
37    }
38    if which(&run.command).is_err() {
39        match project {
40            Some(_) => super::build::build(Build::default(), config.clone()).await?,
41            None => install_command(&run.command, &config).await?,
42        }
43    };
44    operations::Exec::new(&run.command, project.as_ref(), &config)
45        .args(run.args.unwrap_or_default())
46        .exec()
47        .await?;
48    Ok(())
49}