1use mlua::Lua;
8use zoi_core::utils;
9
10use crate::api;
11
12pub fn setup_lua_environment(
23 lua: &Lua,
24 platform: &str,
25 version_override: Option<&str>,
26 file_path: Option<&str>,
27 create_pkg_dir: Option<&str>,
28 build_dir: Option<&str>,
29 staging_dir: Option<&str>,
30 sub_package: Option<&str>,
31 scope: Option<zoi_core::types::Scope>,
32 build_type: Option<&str>,
33 quiet: bool
34) -> Result<(), mlua::Error> {
35 let pkg_table = if let Ok(table) = lua.globals().get::<mlua::Table>("PKG") {
37 table
38 } else {
39 let table = lua.create_table()?;
40 lua.globals().set("PKG", table.clone())?;
41 table
42 };
43
44 if let Some(bt) = build_type {
45 lua.globals().set("BUILD_TYPE", bt)?;
46 }
47
48 if lua
50 .globals()
51 .get::<mlua::Table>("__ZoiPackageMeta")
52 .is_err()
53 {
54 let pkg_meta_table = lua.create_table()?;
55 lua.globals().set("__ZoiPackageMeta", pkg_meta_table)?;
56 }
57 if lua
58 .globals()
59 .get::<mlua::Table>("__ZoiPackageDeps")
60 .is_err()
61 {
62 let pkg_deps_table = lua.create_table()?;
63 lua.globals().set("__ZoiPackageDeps", pkg_deps_table)?;
64 }
65 if lua
66 .globals()
67 .get::<mlua::Table>("__ZoiPackageUpdates")
68 .is_err()
69 {
70 let pkg_updates_table = lua.create_table()?;
71 lua.globals()
72 .set("__ZoiPackageUpdates", pkg_updates_table)?;
73 }
74 if lua
75 .globals()
76 .get::<mlua::Table>("__ZoiPackageHooks")
77 .is_err()
78 {
79 let pkg_hooks_table = lua.create_table()?;
80 lua.globals().set("__ZoiPackageHooks", pkg_hooks_table)?;
81 }
82 if lua
83 .globals()
84 .get::<mlua::Table>("__ZoiPackageService")
85 .is_err()
86 {
87 let pkg_service_table = lua.create_table()?;
88 lua.globals()
89 .set("__ZoiPackageService", pkg_service_table)?;
90 }
91
92 let system_table = lua.create_table()?;
96 let parts: Vec<&str> = platform.split('-').collect();
97 system_table.set("OS", *parts.first().unwrap_or(&""))?;
98 system_table.set("ARCH", *parts.get(1).unwrap_or(&""))?;
99 if let Some(distro) = utils::get_linux_distribution() {
100 system_table.set("DISTRO", distro)?;
101 }
102 if let Some(de) = utils::get_desktop_environment() {
103 system_table.set("DE", de)?;
104 }
105 if let Some(server) = utils::get_display_server() {
106 system_table.set("SERVER", server)?;
107 }
108 if let Some(dv) = utils::get_distro_version() {
109 system_table.set("DISTRO_VER", dv)?;
110 }
111 if let Some(kernel) = utils::get_kernel_version() {
112 system_table.set("KERNEL_VER", kernel)?;
113 }
114 if let Some(init) = utils::get_init_system() {
115 system_table.set("INIT", init)?;
116 }
117 if let Some(cpu) = utils::get_cpu_info() {
118 system_table.set("CPU", cpu)?;
119 }
120 if let Some(gpu) = utils::get_gpu_info() {
121 system_table.set("GPU", gpu)?;
122 }
123 if let Some(manager) = utils::get_native_package_manager() {
124 system_table.set("MANAGER", manager)?;
125 }
126 lua.globals().set("SYSTEM", system_table)?;
127
128 let zoi_table = lua.create_table()?;
129 if let Some(ver) = version_override {
130 zoi_table.set("VERSION", ver)?;
131 }
132
133 if let Some(s) = scope {
134 let scope_str = format!("{s:?}").to_lowercase();
135 zoi_table.set("scope", scope_str)?;
136 }
137
138 if let Some(dir) = create_pkg_dir {
139 zoi_table.set("CREATE_PKG_DIR", dir)?;
140 }
141
142 if let Some(sub) = sub_package {
143 lua.globals().set("SUBPKG", sub)?;
144 }
145
146 let path_table = lua.create_table()?;
147 if let Ok(user_data_dir) = utils::get_user_data_dir() {
148 path_table.set("user", user_data_dir.to_string_lossy().to_string())?;
149 }
150
151 let system_bin_path =
152 utils::get_system_bin_dir().to_string_lossy().to_string();
153 path_table.set("system", system_bin_path)?;
154
155 zoi_table.set("PATH", path_table)?;
156
157 if let Some(home_dir) = utils::get_user_home() {
158 pkg_table.set("home", home_dir.to_string_lossy().to_string())?;
159 if let Ok(store_dir) =
160 utils::get_store_base_dir(zoi_core::types::Scope::User)
161 {
162 pkg_table.set("store", store_dir.to_string_lossy().to_string())?;
163 }
164 }
165
166 if let Ok(current_dir) = std::env::current_dir() {
167 pkg_table.set("template", current_dir.to_string_lossy().to_string())?;
168 }
169
170 let root = if cfg!(target_os = "windows") {
171 "C:\\"
172 } else {
173 "/"
174 };
175 pkg_table.set("root", root)?;
176
177 if let Some(path_str) = file_path {
178 let abs_path = if let Ok(p) = std::fs::canonicalize(path_str) {
179 p
180 } else {
181 std::path::Path::new(path_str).to_path_buf()
182 };
183 pkg_table.set("lua", abs_path.to_string_lossy().to_string())?;
184 }
185 zoi_table.set("PKG", pkg_table.clone())?;
186
187 let location_table = lua.create_table()?;
188 if let Some(sd) = staging_dir {
189 let staging_path = std::path::Path::new(sd);
190 location_table.set(
191 "PKGSTORE",
192 staging_path.join("pkgstore").to_string_lossy().to_string()
193 )?;
194 location_table.set(
195 "HOME",
196 staging_path.join("usrhome").to_string_lossy().to_string()
197 )?;
198 location_table.set(
199 "ROOT",
200 staging_path.join("usrroot").to_string_lossy().to_string()
201 )?;
202 location_table.set(
203 "TEMPLATE",
204 staging_path
205 .join("createpkgdir")
206 .to_string_lossy()
207 .to_string()
208 )?;
209 } else {
210 if let Some(home_dir) = utils::get_user_home() {
211 location_table.set(
212 "PKGSTORE",
213 utils::get_store_base_dir(zoi_core::types::Scope::User)
214 .map_err(|error| {
215 mlua::Error::RuntimeError(error.to_string())
216 })?
217 .to_string_lossy()
218 .to_string()
219 )?;
220 location_table
221 .set("HOME", home_dir.to_string_lossy().to_string())?;
222 }
223 let root = if cfg!(target_os = "windows") {
224 "C:\\"
225 } else {
226 "/"
227 };
228 location_table.set("ROOT", root.to_string())?;
229 if let Ok(current_dir) = std::env::current_dir() {
230 location_table
231 .set("TEMPLATE", current_dir.to_string_lossy().to_string())?;
232 }
233 }
234 if let Some(path_str) = file_path {
235 let abs_path = if let Ok(p) = std::fs::canonicalize(path_str) {
236 p
237 } else {
238 std::path::Path::new(path_str).to_path_buf()
239 };
240 location_table.set(
241 "PKGLUADIR",
242 abs_path
243 .parent()
244 .unwrap_or(&abs_path)
245 .to_string_lossy()
246 .to_string()
247 )?;
248 }
249 if let Some(bd) = build_dir {
250 location_table.set("BUILDDIR", bd)?;
251 lua.globals().set("BUILD_DIR", bd)?;
252 }
253 if let Some(sd) = staging_dir {
254 location_table.set("STAGINGDIR", sd)?;
255 lua.globals().set("STAGING_DIR", sd)?;
256 }
257 zoi_table.set("LOCATION", location_table.clone())?;
258 lua.globals().set("LOCATION", location_table)?;
259
260 lua.globals().set("ZOI", zoi_table)?;
261
262 let utils_table = lua.create_table()?;
263 lua.globals().set("UTILS", utils_table)?;
264
265 api::http::add_fetch_util(lua)?;
266 api::parse::add_parse_util(lua)?;
267 api::http::add_git_fetch_util(lua)?;
268 api::download::add_download_util(lua, quiet)?;
269 api::fs::add_file_util(lua, quiet)?;
270 api::fs::add_zcp(lua)?;
271 api::fs::add_zlicense(lua)?;
272 api::fs::add_zdoc(lua)?;
273 api::fs::add_zman(lua)?;
274 api::fs::add_zshell(lua)?;
275 api::fs::add_zsed(lua, quiet)?;
276 api::fs::add_zln(lua)?;
277 api::fs::add_zchmod(lua)?;
278 api::fs::add_zchown(lua)?;
279 api::fs::add_zmkdir(lua)?;
280 api::crypto::add_verify_hash(lua, quiet)?;
281 api::fs::add_zrm(lua)?;
282 api::system::add_cmd_util(lua, quiet)?;
283 api::system::add_zpatch(lua, quiet)?;
284 api::fs::add_fs_util(lua)?;
285 api::fs::add_find_util(lua)?;
286 api::archive::add_archive_util(lua)?;
287 api::archive::add_extract_util(lua, quiet)?;
288 api::crypto::add_verify_signature(lua, quiet)?;
289 api::crypto::add_add_pgp_key(lua, quiet)?;
290 api::lifecycle::add_package_lifecycle_functions(lua)?;
291
292 if let Some(path_str) = file_path {
293 let path = std::path::Path::new(path_str);
294 api::lifecycle::add_import_util(lua, path)?;
295 api::lifecycle::add_include_util(lua, path)?;
296 }
297
298 if let Some(sub) = sub_package {
299 lua.globals().set("SUBPKG", sub)?;
300 }
301
302 Ok(())
303}