Skip to main content

lux_cli/
exec.rs

1use std::env;
2
3use clap::Args;
4use eyre::Result;
5use lux_lib::{
6    config::Config, lua_version::LuaVersion, operations, path::Paths, workspace::Workspace,
7};
8
9#[derive(Args)]
10pub struct Exec {
11    /// The command to run.
12    command: String,
13
14    /// Arguments to pass to the program.
15    args: Option<Vec<String>>,
16
17    /// Do not add `require('lux').loader()` to `LUA_INIT`.
18    /// If a rock has conflicting transitive dependencies,
19    /// disabling the Lux loader may result in the wrong modules being loaded.
20    #[clap(default_value_t = false)]
21    #[arg(long)]
22    no_loader: bool,
23}
24
25pub async fn exec(run: Exec, config: Config) -> Result<()> {
26    let workspace = Workspace::current()?;
27    let tree = match &workspace {
28        Some(project) => project.tree(&config)?,
29        None => {
30            let lua_version = LuaVersion::from(&config)?.clone();
31            config.user_tree(lua_version)?
32        }
33    };
34
35    let paths = Paths::new(&tree)?;
36    unsafe {
37        // safe as long as this is single-threaded
38        env::set_var("PATH", paths.path_prepended().joined());
39    }
40    operations::Exec::new(&run.command, workspace.as_ref(), &config)
41        .args(run.args.unwrap_or_default())
42        .disable_loader(run.no_loader)
43        .exec()
44        .await?;
45    Ok(())
46}