1use std::path::PathBuf;
2
3use path_slash::PathExt;
4use tokio::process::Command;
5
6use clap::Args;
7use eyre::{eyre, Result};
8use itertools::Itertools;
9use lux_lib::{
10 config::Config,
11 lua_installation::{LuaBinary, LuaInstallation},
12 lua_version::LuaVersion,
13 operations,
14 progress::MultiProgress,
15 workspace::Workspace,
16};
17
18use crate::build::{self, Build};
19
20#[derive(Args, Default)]
21#[clap(disable_help_flag = true)]
22pub struct RunLua {
23 args: Option<Vec<String>>,
25
26 #[arg(long)]
28 lua: Option<String>,
29
30 #[arg(long)]
32 test: bool,
33
34 #[arg(long)]
36 build: bool,
37
38 #[clap(default_value_t = false)]
42 #[arg(long)]
43 no_loader: bool,
44
45 #[clap(flatten)]
46 build_args: Build,
47
48 #[arg(long)]
50 help: bool,
51}
52
53pub async fn run_lua(run_lua: RunLua, config: Config) -> Result<()> {
54 let workspace = Workspace::current()?;
55 let (lua_version, root, tree, mut welcome_message) = match &workspace {
56 Some(workspace) => {
57 let package_names = workspace
58 .members()
59 .iter()
60 .map(|project| project.toml().package())
61 .join(",");
62 (
63 workspace.lua_version(&config)?,
64 workspace.root().to_path_buf(),
65 workspace.tree(&config)?,
66 format!("Welcome to the lux Lua repl for {package_names}.",),
67 )
68 }
69 None => {
70 let version = LuaVersion::from(&config)?.clone();
71 (
72 version.clone(),
73 std::env::current_dir()?,
74 config.user_tree(version)?,
75 "Welcome to the lux Lua repl.".into(),
76 )
77 }
78 };
79
80 welcome_message = format!(
81 r#"{welcome_message}
82Run `lx lua --help` for options.
83To exit type 'exit()' or <C-d>.
84"#,
85 );
86
87 let lua_cmd = match run_lua.lua.map(LuaBinary::Custom) {
88 Some(lua_cmd) => lua_cmd,
89 None => {
90 let progress = MultiProgress::new_arc(&config);
91 let bar = progress.map(|progress| progress.new_bar());
92 let lua_cmd = LuaInstallation::new(&lua_version, &config, &bar)
93 .await?
94 .bin()
95 .as_ref()
96 .map(|lua_bin| LuaBinary::Custom(lua_bin.to_slash_lossy().to_string()))
97 .unwrap_or_else(|| LuaBinary::new(lua_version, &config));
98 bar.map(|bar| bar.finish_and_clear());
99 lua_cmd
100 }
101 };
102
103 if run_lua.help {
104 return print_lua_help(&lua_cmd).await;
105 }
106
107 if workspace.is_some() {
108 build::build(run_lua.build_args, config.clone()).await?;
109 }
110
111 let args = &run_lua.args.unwrap_or_default();
112
113 operations::RunLua::new()
114 .root(&root)
115 .tree(&tree)
116 .config(&config)
117 .lua_cmd(lua_cmd)
118 .args(args)
119 .prepend_test_paths(run_lua.test)
120 .prepend_build_paths(run_lua.build)
121 .disable_loader(run_lua.no_loader)
122 .lua_init("exit = os.exit".to_string())
123 .welcome_message(welcome_message)
124 .run_lua()
125 .await?;
126
127 Ok(())
128}
129
130async fn print_lua_help(lua_cmd: &LuaBinary) -> Result<()> {
131 let lua_cmd_path: PathBuf = lua_cmd.clone().try_into()?;
132 let output = match Command::new(lua_cmd_path.to_string_lossy().to_string())
133 .arg("-h")
135 .output()
136 .await
137 {
138 Ok(output) => Ok(output),
139 Err(err) => Err(eyre!("Failed to run {}: {}", lua_cmd, err)),
140 }?;
141 let lua_help = String::from_utf8_lossy(&output.stderr)
142 .lines()
143 .skip(2)
144 .map(|line| format!(" {line}"))
145 .collect_vec()
146 .join("\n");
147 print!(
148 "
149Usage: lx lua -- [LUA_OPTIONS] [SCRIPT [ARGS]]...
150
151Arguments:
152 [LUA_OPTIONS]...
153{lua_help}
154
155Options:
156 --lua Path to the Lua interpreter to use
157 -h, --help Print help
158
159Build options (if running a repl for a project):
160 --test Prepend test dependencies to the LUA_PATH and LUA_CPATH
161 --build Prepend build dependencies to the LUA_PATH and LUA_CPATH
162 --no-lock Ignore the project's lockfile and don't create one
163 --only-deps Build only the dependencies
164",
165 );
166 Ok(())
167}
168
169#[cfg(test)]
170mod test {
171 use std::path::PathBuf;
172
173 use lux_lib::config::ConfigBuilder;
174 use serial_test::serial;
175
176 use super::*;
177
178 #[serial]
179 #[tokio::test]
180 async fn test_run_lua() {
181 let args = RunLua {
182 args: Some(vec!["-v".into()]),
183 ..RunLua::default()
184 };
185 let temp: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
186 let data_dir: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
187 let cwd = &std::env::current_dir().unwrap();
188 tokio::fs::create_dir_all(&temp).await.unwrap();
189 std::env::set_current_dir(&temp).unwrap();
190 let config = ConfigBuilder::new()
191 .unwrap()
192 .user_tree(Some(temp.clone()))
193 .data_dir(Some(data_dir))
194 .build()
195 .unwrap();
196 run_lua(args, config).await.unwrap();
197 std::env::set_current_dir(cwd).unwrap();
198 }
199}