lux_lib/luarocks/installation/
exec.rs1use std::path::Path;
2use std::process::ExitStatus;
3
4use miette::Diagnostic;
5use path_slash::{PathBufExt, PathExt};
6use thiserror::Error;
7
8use super::{Installation, LUAROCKS_EXE};
9use crate::{
10 build, fs, lua,
11 path::{self, Paths},
12 tree::InstallTree,
13 variables,
14};
15
16#[derive(Error, Debug, Diagnostic)]
17#[non_exhaustive]
18pub enum Error {
19 #[error(transparent)]
20 #[diagnostic(transparent)]
21 LuaVersion(#[from] lua::version::Error),
22 #[error(
23 "could not substitute '$(LUA_LIBDIR)' and '$(LUA_INCDIR)' variables in the luarocks config template"
24 )]
25 #[diagnostic(forward(0))]
26 VariableSubstitutionInConfig(#[from] variables::Error),
27 #[error("failed to run luarocks")]
28 Io(#[from] std::io::Error),
29 #[error(transparent)]
30 #[diagnostic(transparent)]
31 Fs(#[from] fs::Error),
32 #[error("error setting up luarocks paths")]
33 #[diagnostic(forward(0))]
34 Paths(#[from] path::Error),
35 #[error("luarocks binary not found at '{0}'")]
36 #[diagnostic(help(
37 "Lux successfully installed luarocks, but couldn't find the binary to execute"
38 ))]
39 LuarocksBinNotFound(std::path::PathBuf),
40 #[error(
41 r#"executing luarocks compatibility layer failed.
42status: {status}
43stdout:
44{stdout}
45stderr:
46{stderr}
47"#
48 )]
49 #[diagnostic(help("see the build output for details"))]
50 CommandFailure {
51 status: ExitStatus,
52 stdout: String,
53 stderr: String,
54 },
55 #[error("luau does not support LuaRocks build backends")]
56 LuauUnsupported,
57}
58
59impl Installation {
60 #[tracing::instrument(level = "trace", skip(self))]
61 pub async fn make(
62 self,
63 rockspec_path: &Path,
64 build_dir: &Path,
65 dest_dir: &Path,
66 lua: &lua::Installation,
67 ) -> Result<(), Error> {
68 fs::sync::create_dir_all(dest_dir)?;
69 let dest_dir_str = dest_dir.to_slash_lossy().to_string();
70 let rockspec_path_str = rockspec_path.to_slash_lossy().to_string();
71 let args = vec![
72 "make",
73 "--deps-mode",
74 "none",
75 "--tree",
76 &dest_dir_str,
77 &rockspec_path_str,
78 ];
79 self.exec(args, build_dir, lua).await
80 }
81
82 async fn exec(self, args: Vec<&str>, cwd: &Path, lua: &lua::Installation) -> Result<(), Error> {
83 let luarocks_paths = Paths::new(&self.tree)?;
84 let temp_dir = fs::tempfile::tempdir()?;
86 let lua_version_str = match lua.version {
87 lua::Version::Lua51 | lua::Version::LuaJIT => "5.1",
88 lua::Version::Lua52 | lua::Version::LuaJIT52 => "5.2",
89 lua::Version::Lua53 => "5.3",
90 lua::Version::Lua54 => "5.4",
91 lua::Version::Lua55 => "5.5",
92 lua::Version::Luau => return Err(Error::LuauUnsupported),
93 };
94 let luarocks_config_content = format!(
95 r#"
96lua_version = "{0}"
97variables = {{
98 LUA_LIBDIR = "$(LUA_LIBDIR)",
99 LUA_INCDIR = "$(LUA_INCDIR)",
100 LUA_VERSION = "{1}",
101 MAKE = "{2}",
102}}
103"#,
104 lua_version_str,
105 lua::Version::from(&self.config)?,
106 self.config.make_cmd()
107 );
108 let luarocks_config_content =
109 variables::substitute(&[lua, &self.config], &luarocks_config_content)?;
110 let luarocks_config = temp_dir.path().join("luarocks-config.lua");
111 fs::sync::write(luarocks_config.clone(), luarocks_config_content)?;
112 let luarocks_bin = self.tree.bin().join(LUAROCKS_EXE);
113 if !luarocks_bin.is_file() {
114 return Err(Error::LuarocksBinNotFound(luarocks_bin));
115 }
116 let output = self
117 .config
118 .wrapped_command(luarocks_bin, args)
119 .current_dir(cwd)
120 .env("PATH", luarocks_paths.path_prepended().joined())
121 .env("LUA_PATH", luarocks_paths.package_path().joined())
122 .env("LUA_CPATH", luarocks_paths.package_cpath().joined())
123 .env("HOME", temp_dir.path().to_slash_lossy().to_string())
124 .env(
125 "LUAROCKS_CONFIG",
126 luarocks_config.to_slash_lossy().to_string(),
127 )
128 .output()
129 .await?;
130 if output.status.success() {
131 build::command::trace_output(&output);
132 Ok(())
133 } else {
134 Err(Error::CommandFailure {
135 status: output.status,
136 stdout: String::from_utf8_lossy(&output.stdout).into(),
137 stderr: String::from_utf8_lossy(&output.stderr).into(),
138 })
139 }
140 }
141}