1use crate::build::backend::BuildInfo;
2use crate::build::backend::RunBuildArgs;
3use crate::fs;
4use crate::rockspec;
5use crate::rockspec::LuaVersionCompatibility;
6use crate::rockspec::Rockspec;
7use crate::tree;
8use crate::tree::InstallTree;
9use std::path::Path;
10
11use crate::{
12 config::Config,
13 lockfile::LockedPackageSpec,
14 luarocks::{self, install_rock_manifest, installation::exec, RockManifestSources},
15};
16
17use crate::fs::tempfile::tempdir;
18use miette::Diagnostic;
19use thiserror::Error;
20
21#[derive(Error, Debug, Diagnostic)]
22#[non_exhaustive]
23pub enum Error {
24 #[error(transparent)]
25 #[diagnostic(transparent)]
26 Fs(#[from] fs::Error),
27 #[error("error instantiating luarocks compatibility layer")]
28 #[diagnostic(forward(0))]
29 LuaRocks(#[from] luarocks::installation::Error),
30 #[error("error running 'luarocks make'")]
31 #[diagnostic(forward(0))]
32 Exec(#[from] exec::Error),
33 #[error(transparent)]
34 #[diagnostic(transparent)]
35 Tree(#[from] tree::Error),
36 #[error(transparent)]
37 #[diagnostic(transparent)]
38 InstallRockManifest(#[from] luarocks::Error),
39 #[error(transparent)]
40 #[diagnostic(transparent)]
41 Rockspec(Box<dyn Diagnostic + Send + Sync>),
42 #[error("error installing luarocks compatibility layer")]
43 #[diagnostic(forward(0))]
44 LuaVersion(#[from] rockspec::Error),
45}
46
47#[tracing::instrument(name = "Delegating to luarocks",
48 skip_all,
49 level = "info"
50 fields(backend = build_backend_name),
51)]
52pub(crate) async fn build<R: Rockspec, T: InstallTree>(
53 build_backend_name: &str,
54 rockspec: &R,
55 args: RunBuildArgs<'_, T>,
56) -> Result<BuildInfo, Error> {
57 let package = args.package;
58 let lua = args.lua;
59 let config = args.config;
60 let build_dir = args.build_dir;
61 let tree = args.tree;
62
63 let rockspec_temp_dir = tempdir()?;
64 let rockspec_file = rockspec_temp_dir.path().join(format!(
65 "{}-{}.rockspec",
66 rockspec.package(),
67 rockspec.version()
68 ));
69 fs::tokio::write(
70 &rockspec_file,
71 rockspec
72 .to_lua_remote_rockspec_string()
73 .map_err(|err| Error::Rockspec(Box::new(err)))?,
74 )
75 .await?;
76 let luarocks = luarocks::Installation::new(config, tree.build_tree(config)?)?;
77 let luarocks_tree = tempdir()?;
78 luarocks
79 .make(&rockspec_file, build_dir, luarocks_tree.path(), lua)
80 .await?;
81 install(rockspec, luarocks_tree.path(), tree, package, config).await
82}
83
84async fn install<R: Rockspec, T: InstallTree>(
85 rockspec: &R,
86 luarocks_tree: &Path,
87 tree: &T,
88 package: &LockedPackageSpec,
89 config: &Config,
90) -> Result<BuildInfo, Error> {
91 let layout = tree.layout_for(package);
92 let lua_version = rockspec.lua_version_matches(config)?;
93 fs::tokio::create_dir_all(tree.bin()).await?;
94 let lua_version = lua_version.version_compatibility_str();
95 let package_dir = luarocks_tree
96 .join("lib")
97 .join("luarocks")
98 .join(format!("rocks-{lua_version}"))
99 .join(format!("{}", rockspec.package()))
100 .join(format!("{}", rockspec.version()));
101 let sources = RockManifestSources {
102 root: package_dir,
103 lib: luarocks_tree.join("lib").join("lua").join(&lua_version),
104 lua: luarocks_tree.join("share").join("lua").join(&lua_version),
105 bin: luarocks_tree.join("bin"),
106 };
107 install_rock_manifest(sources, &layout, &tree.bin()).await?;
108 Ok(BuildInfo::default())
109}
110
111#[cfg(test)]
112mod tests {
113 use std::path::Path;
114
115 use crate::rockspec;
116
117 use assert_fs::TempDir;
118
119 use crate::{
120 config,
121 lockfile::{self, LockedPackageSpec, OptState, PinnedState},
122 lua,
123 rockspec::Rockspec,
124 tree::InstallTree,
125 };
126
127 use super::install;
128
129 fn write_file(root: &Path, relative: &str, content: &str) {
130 let path = root.join(relative);
131 if let Some(parent) = path.parent() {
132 std::fs::create_dir_all(parent).unwrap();
133 }
134 std::fs::write(path, content).unwrap();
135 }
136
137 #[tokio::test]
138 async fn installs_all_build_artifacts() {
139 let rockspec = rockspec::lua::Remote::new(
140 r#"
141rockspec_format = "3.0"
142package = "test-luarocks-backend"
143version = "1.0-1"
144source = { url = "https://github.com/lumen-oss/luarocks-stub" }
145build = { type = "test-custom" }
146"#,
147 )
148 .unwrap();
149 let tree_dir = TempDir::new().unwrap();
150 let config = config::Builder::new()
151 .unwrap()
152 .lua_version(Some(lua::Version::Lua51))
153 .user_tree(Some(tree_dir.path().to_path_buf()))
154 .build()
155 .unwrap();
156 let tree = config.user_tree(lua::Version::Lua51).unwrap();
157 let package = LockedPackageSpec::new(
158 rockspec.package(),
159 rockspec.version(),
160 lockfile::Constraint::Unconstrained,
161 Vec::new(),
162 Vec::new(),
163 &PinnedState::default(),
164 &OptState::default(),
165 );
166 tree.prepare(&package).unwrap();
167
168 let luarocks_tree = TempDir::new().unwrap();
169 let package_dir = luarocks_tree
170 .path()
171 .join("lib/luarocks/rocks-5.1/test-luarocks-backend/1.0-1");
172 write_file(
173 &package_dir,
174 "rock_manifest",
175 r#"
176rock_manifest = {
177 lib = { ["test_luarocks_backend.so"] = "00000000000000000000000000000000" },
178 lua = { ["test_luarocks_backend/init.lua"] = "00000000000000000000000000000000" },
179 bin = { tool = "00000000000000000000000000000000" },
180 doc = { ["README.md"] = "00000000000000000000000000000000" },
181 conf = { ["config.toml"] = "00000000000000000000000000000000" },
182 plugin = { ["foo.lua"] = "00000000000000000000000000000000" },
183 ["test-luarocks-backend-1.0-1.rockspec"] = "00000000000000000000000000000000",
184}
185"#,
186 );
187 write_file(
188 luarocks_tree.path(),
189 "lib/lua/5.1/test_luarocks_backend.so",
190 "",
191 );
192 write_file(
193 luarocks_tree.path(),
194 "share/lua/5.1/test_luarocks_backend/init.lua",
195 "return true",
196 );
197 write_file(
198 luarocks_tree.path(),
199 "share/lua/5.1/unlisted.lua",
200 "return true",
201 );
202 write_file(luarocks_tree.path(), "bin/tool", "#!/bin/sh\n");
203 write_file(&package_dir, "doc/README.md", "docs");
204 write_file(&package_dir, "conf/config.toml", "conf");
205 write_file(&package_dir, "plugin/foo.lua", "return true");
206 write_file(
207 &package_dir,
208 "test-luarocks-backend-1.0-1.rockspec",
209 "rockspec",
210 );
211 write_file(&package_dir, "unlisted.txt", "not in the manifest");
212
213 install(&rockspec, luarocks_tree.path(), &tree, &package, &config)
214 .await
215 .unwrap();
216
217 let layout = tree.layout_for(&package);
218 assert!(layout.lib.join("test_luarocks_backend.so").is_file());
219 assert!(layout
220 .src
221 .join("test_luarocks_backend")
222 .join("init.lua")
223 .is_file());
224 assert!(tree.bin().join("tool").is_file());
225 assert!(layout.doc.join("README.md").is_file());
226 assert!(layout.conf.join("config.toml").is_file());
227 assert!(layout.etc.join("plugin").join("foo.lua").is_file());
228 assert!(layout
229 .etc
230 .join("test-luarocks-backend-1.0-1.rockspec")
231 .is_file());
232 assert!(!layout.etc.join("rock_manifest").exists());
233 assert!(!layout.etc.join("unlisted.txt").exists());
234 assert!(!layout.src.join("unlisted.lua").exists());
235 }
236}