1use crate::api;
2use mlua::Lua;
3use zoi_core::utils;
4
5pub fn setup_lua_environment(
6 lua: &Lua,
7 platform: &str,
8 version_override: Option<&str>,
9 file_path: Option<&str>,
10 create_pkg_dir: Option<&str>,
11 build_dir: Option<&str>,
12 staging_dir: Option<&str>,
13 sub_package: Option<&str>,
14 quiet: bool,
15) -> Result<(), mlua::Error> {
16 let system_table = lua.create_table()?;
17 let parts: Vec<&str> = platform.split('-').collect();
18 system_table.set("OS", *parts.first().unwrap_or(&""))?;
19 system_table.set("ARCH", *parts.get(1).unwrap_or(&""))?;
20 if let Some(distro) = utils::get_linux_distribution() {
21 system_table.set("DISTRO", distro)?;
22 }
23 if let Some(de) = utils::get_desktop_environment() {
24 system_table.set("DE", de)?;
25 }
26 if let Some(server) = utils::get_display_server() {
27 system_table.set("SERVER", server)?;
28 }
29 if let Some(dv) = utils::get_distro_version() {
30 system_table.set("DISTRO_VER", dv)?;
31 }
32 if let Some(kernel) = utils::get_kernel_version() {
33 system_table.set("KERNEL_VER", kernel)?;
34 }
35 if let Some(cpu) = utils::get_cpu_info() {
36 system_table.set("CPU", cpu)?;
37 }
38 if let Some(gpu) = utils::get_gpu_info() {
39 system_table.set("GPU", gpu)?;
40 }
41 if let Some(manager) = utils::get_native_package_manager() {
42 system_table.set("MANAGER", manager)?;
43 }
44 lua.globals().set("SYSTEM", system_table)?;
45
46 let zoi_table = lua.create_table()?;
47 if let Some(ver) = version_override {
48 zoi_table.set("VERSION", ver)?;
49 }
50
51 if let Some(dir) = create_pkg_dir {
52 zoi_table.set("CREATE_PKG_DIR", dir)?;
53 }
54
55 if let Some(sub) = sub_package {
56 lua.globals().set("SUBPKG", sub)?;
57 }
58
59 let path_table = lua.create_table()?;
60 if let Some(home_dir) = home::home_dir() {
61 path_table.set("user", home_dir.join(".zoi").to_string_lossy().to_string())?;
62 }
63
64 let system_bin_path = if cfg!(target_os = "windows") {
65 "C:\\ProgramData\\zoi\\pkgs\\bin".to_string()
66 } else {
67 "/usr/local/bin".to_string()
68 };
69 path_table.set("system", system_bin_path)?;
70
71 zoi_table.set("PATH", path_table)?;
72
73 let pkg_table = lua.create_table()?;
74 if let Some(home_dir) = home::home_dir() {
75 pkg_table.set("home", home_dir.to_string_lossy().to_string())?;
76 pkg_table.set(
77 "store",
78 home_dir
79 .join(".zoi")
80 .join("pkgs")
81 .join("store")
82 .to_string_lossy()
83 .to_string(),
84 )?;
85 }
86
87 if let Ok(current_dir) = std::env::current_dir() {
88 pkg_table.set("template", current_dir.to_string_lossy().to_string())?;
89 }
90
91 let root = if cfg!(target_os = "windows") {
92 "C:\\"
93 } else {
94 "/"
95 };
96 pkg_table.set("root", root)?;
97
98 if let Some(path_str) = file_path {
99 let abs_path = if let Ok(p) = std::fs::canonicalize(path_str) {
100 p
101 } else {
102 std::path::Path::new(path_str).to_path_buf()
103 };
104 pkg_table.set("lua", abs_path.to_string_lossy().to_string())?;
105 }
106 zoi_table.set("PKG", pkg_table.clone())?;
107
108 let location_table = lua.create_table()?;
109 if let Some(sd) = staging_dir {
110 let staging_path = std::path::Path::new(sd);
111 location_table.set(
112 "PKGSTORE",
113 staging_path
114 .join("data/pkgstore")
115 .to_string_lossy()
116 .to_string(),
117 )?;
118 location_table.set(
119 "HOME",
120 staging_path
121 .join("data/usrhome")
122 .to_string_lossy()
123 .to_string(),
124 )?;
125 location_table.set(
126 "ROOT",
127 staging_path
128 .join("data/usrroot")
129 .to_string_lossy()
130 .to_string(),
131 )?;
132 location_table.set(
133 "TEMPLATE",
134 staging_path
135 .join("data/createpkgdir")
136 .to_string_lossy()
137 .to_string(),
138 )?;
139 } else {
140 if let Some(home_dir) = home::home_dir() {
141 location_table.set(
142 "PKGSTORE",
143 home_dir
144 .join(".zoi")
145 .join("pkgs")
146 .join("store")
147 .to_string_lossy()
148 .to_string(),
149 )?;
150 location_table.set("HOME", home_dir.to_string_lossy().to_string())?;
151 }
152 let root = if cfg!(target_os = "windows") {
153 "C:\\"
154 } else {
155 "/"
156 };
157 location_table.set("ROOT", root.to_string())?;
158 if let Ok(current_dir) = std::env::current_dir() {
159 location_table.set("TEMPLATE", current_dir.to_string_lossy().to_string())?;
160 }
161 }
162 if let Some(path_str) = file_path {
163 let abs_path = if let Ok(p) = std::fs::canonicalize(path_str) {
164 p
165 } else {
166 std::path::Path::new(path_str).to_path_buf()
167 };
168 location_table.set(
169 "PKGLUADIR",
170 abs_path
171 .parent()
172 .unwrap_or(&abs_path)
173 .to_string_lossy()
174 .to_string(),
175 )?;
176 }
177 if let Some(bd) = build_dir {
178 location_table.set("BUILDDIR", bd)?;
179 }
180 if let Some(sd) = staging_dir {
181 location_table.set("STAGINGDIR", sd)?;
182 }
183 zoi_table.set("LOCATION", location_table)?;
184
185 lua.globals().set("ZOI", zoi_table)?;
186
187 let utils_table = lua.create_table()?;
188 lua.globals().set("UTILS", utils_table)?;
189
190 api::http::add_fetch_util(lua)?;
191 api::parse::add_parse_util(lua)?;
192 api::http::add_git_fetch_util(lua)?;
193 api::fs::add_file_util(lua)?;
194 api::fs::add_zcp(lua)?;
195 api::fs::add_zlicense(lua)?;
196 api::fs::add_zdoc(lua)?;
197 api::fs::add_zshell(lua)?;
198 api::fs::add_zsed(lua, quiet)?;
199 api::fs::add_zln(lua)?;
200 api::fs::add_zchmod(lua)?;
201 api::fs::add_zchown(lua)?;
202 api::fs::add_zmkdir(lua)?;
203 api::crypto::add_verify_hash(lua, quiet)?;
204 api::fs::add_zrm(lua)?;
205 api::system::add_cmd_util(lua, quiet)?;
206 api::system::add_zpatch(lua, quiet)?;
207 api::fs::add_fs_util(lua)?;
208 api::fs::add_find_util(lua)?;
209 api::archive::add_archive_util(lua)?;
210 api::archive::add_extract_util(lua, quiet)?;
211 api::crypto::add_verify_signature(lua, quiet)?;
212 api::crypto::add_add_pgp_key(lua, quiet)?;
213 api::lifecycle::add_package_lifecycle_functions(lua)?;
214
215 if let Some(path_str) = file_path {
216 let path = std::path::Path::new(path_str);
217 api::lifecycle::add_import_util(lua, path)?;
218 api::lifecycle::add_include_util(lua, path)?;
219 }
220
221 if let Some(sub) = sub_package {
222 lua.globals().set("SUBPKG", sub)?;
223 }
224
225 Ok(())
226}