1use clap::Args;
2use eyre::{eyre, Result, WrapErr};
3use lux_lib::{
4 config::Config, lua_installation::LuaInstallation, path::Paths, progress::MultiProgress,
5 tree::InstallTree,
6};
7use which::which;
8
9use std::{env, path::PathBuf};
10use tokio::process::Command;
11
12use super::workspace::current_workspace_or_user_tree;
13
14#[derive(Args)]
15pub struct Shell {
16 #[arg(long)]
19 test: bool,
20
21 #[arg(long, conflicts_with = "test")]
23 build: bool,
24
25 #[arg(long)]
29 no_loader: bool,
30}
31
32pub async fn shell(data: Shell, config: Config) -> Result<()> {
33 if env::var("LUX_SHELL").is_ok_and(|lx_shell_var| lx_shell_var == "1") {
34 return Err(eyre!("Already in a Lux shell."));
35 }
36
37 let tree = current_workspace_or_user_tree(&config)?;
38
39 let path = if data.build {
40 let build_tree_path = tree.build_tree(&config)?;
41 Paths::new(&build_tree_path)?
42 } else {
43 let mut path = Paths::new(&tree)?;
44 if data.test {
45 let test_tree_path = tree.test_tree(&config)?;
46 let test_path = Paths::new(&test_tree_path)?;
47 path.prepend(&test_path);
48 }
49 path
50 };
51
52 let shell: PathBuf = match env::var("SHELL") {
53 Ok(val) => PathBuf::from(val),
54 Err(_) => {
55 #[cfg(any(target_os = "linux", target_os = "android"))]
56 let fallback = which("bash").wrap_err("Cannot find `bash` on your system!")?;
57
58 #[cfg(target_os = "windows")]
59 let fallback = which("cmd.exe").wrap_err("Cannot find `cmd.exe` on your system!")?;
60
61 #[cfg(target_os = "macos")]
62 let fallback = which("zsh").wrap_err("Cannot find `zsh` on your system!")?;
63
64 fallback
65 }
66 };
67
68 let lua_path = path.package_path_prepended();
69 let lua_cpath = path.package_cpath_prepended();
70
71 let lua_init = if data.no_loader {
72 None
73 } else if tree.version().lux_lib_dir().is_none() {
74 eprintln!(
75 "⚠️ WARNING: lux-lua library not found.
76 Cannot use the `lux.loader`.
77 To suppress this warning, set the `--no-loader` option.
78 "
79 );
80 None
81 } else {
82 Some(path.init())
83 };
84
85 let lua_version = tree.version();
86
87 let mut bin_path = path.path_prepended();
88
89 let progress = MultiProgress::new_arc(&config);
90 let bar = progress.map(|progress| progress.new_bar());
91 let lua = LuaInstallation::new(lua_version, &config, &bar).await?;
92 bar.map(|bar| bar.finish_and_clear());
93 if let Some(lua_bin_path) = lua.bin().as_ref().and_then(|lua_bin| lua_bin.parent()) {
94 bin_path.add_path(lua_bin_path.to_path_buf());
95 }
96
97 let _ = Command::new(&shell)
98 .env("PATH", bin_path.joined())
99 .env("LUA_PATH", lua_path.joined())
100 .env("LUA_CPATH", lua_cpath.joined())
101 .env("LUA_INIT", lua_init.unwrap_or_default())
102 .env("LUX_SHELL", "1")
103 .spawn()?
104 .wait()
105 .await?;
106
107 Ok(())
108}