Skip to main content

lux_cli/
run.rs

1use std::path::PathBuf;
2
3use clap::Args;
4use lux_lib::{build::BuildBehaviour, config::Config, operations, workspace::Workspace};
5use miette::Result;
6
7use crate::build::{self, Build};
8
9#[derive(Args)]
10pub struct Run {
11    #[arg(trailing_var_arg = true)]
12    args: Vec<String>,
13
14    /// Do not add `require('lux').loader()` to `LUA_INIT`.{n}
15    /// If a rock has conflicting transitive dependencies,{n}
16    /// disabling the Lux loader may result in the wrong modules being loaded.
17    #[clap(default_value_t = false)]
18    #[arg(long)]
19    no_loader: bool,
20
21    /// Path in which to run the command.{n}
22    /// Defaults to the project root.
23    #[arg(long)]
24    dir: Option<PathBuf>,
25
26    #[clap(flatten)]
27    pub(crate) build: Build,
28}
29
30pub async fn run(run_args: Run, config: Config) -> Result<()> {
31    let workspace = Workspace::current_or_err()?;
32
33    let package = run_args.build.package.clone();
34    build::build_with_behaviour(run_args.build, config.clone(), BuildBehaviour::Ignore).await?;
35
36    operations::Run::new()
37        .workspace(&workspace)
38        .maybe_package(package)
39        .args(&run_args.args)
40        .config(&config)
41        .disable_loader(run_args.no_loader)
42        .run()
43        .await?;
44
45    Ok(())
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::{Cli, Commands};
51    use clap::error::ErrorKind;
52    use clap::Parser;
53
54    #[test]
55    fn forwards_arguments_after_first_positional() {
56        let cli = Cli::try_parse_from(["lx", "run", "script.lua", "--help", "-x"]).unwrap();
57        let Commands::Run(run) = cli.command else {
58            unreachable!()
59        };
60        assert_eq!(run.args, ["script.lua", "--help", "-x"]);
61    }
62
63    #[test]
64    fn run_help_is_not_forwarded() {
65        let err = match Cli::try_parse_from(["lx", "run", "--help"]) {
66            Err(err) => err,
67            Ok(_) => unreachable!(),
68        };
69        assert_eq!(err.kind(), ErrorKind::DisplayHelp);
70    }
71}