1use crate::pkg::{cache, local, resolve, types};
2use crate::utils;
3use anyhow::{Result, anyhow};
4use colored::*;
5use std::fs;
6use std::io::Cursor;
7use std::path::PathBuf;
8use std::process::Command;
9use tar::Archive;
10use zstd::stream::read::Decoder as ZstdDecoder;
11
12fn ensure_binary_is_cached(pkg: &types::Package, upstream: bool) -> Result<PathBuf> {
13 let cache_dir = cache::get_cache_root()?;
14 let binary_filename = if cfg!(target_os = "windows") {
15 format!("{}.exe", pkg.name)
16 } else {
17 pkg.name.clone()
18 };
19 let bin_path = cache_dir.join(&binary_filename);
20
21 if upstream && bin_path.exists() {
22 fs::remove_file(&bin_path)?;
23 }
24
25 if bin_path.exists() {
26 println!("Using cached binary for '{}'.", pkg.name.cyan());
27 return Ok(bin_path);
28 }
29
30 if !pkg.types.contains(&"pre-compiled".to_string()) {
31 return Err(anyhow!(
32 "zoi exec only works with 'pre-compiled' package types."
33 ));
34 }
35
36 println!(
37 "No cached binary found for '{}'. Downloading pre-built package...",
38 pkg.name.cyan()
39 );
40 fs::create_dir_all(&cache_dir)?;
41
42 let db_path = resolve::get_db_root()?;
43 let config = crate::pkg::config::read_config()?;
44 let repo_config = if let Some(handle) = config.default_registry.as_ref().map(|r| &r.handle) {
45 crate::pkg::config::read_repo_config(&db_path.join(handle)).ok()
46 } else {
47 None
48 };
49
50 if let Some(repo_config) = repo_config {
51 let mut pkg_links_to_try = Vec::new();
52 if let Some(main_pkg) = repo_config.pkg.iter().find(|p| p.link_type == "main") {
53 pkg_links_to_try.push(main_pkg.clone());
54 }
55 pkg_links_to_try.extend(
56 repo_config
57 .pkg
58 .iter()
59 .filter(|p| p.link_type == "mirror")
60 .cloned(),
61 );
62
63 for pkg_link in pkg_links_to_try {
64 let platform = utils::get_platform()?;
65 let (os, arch) = (
66 platform.split('-').next().unwrap_or(""),
67 platform.split('-').nth(1).unwrap_or(""),
68 );
69 let url_dir = pkg_link
70 .url
71 .replace("{os}", os)
72 .replace("{arch}", arch)
73 .replace("{version}", pkg.version.as_deref().unwrap_or(""))
74 .replace("{repo}", &pkg.repo);
75
76 let archive_filename = format!("{}.pkg.tar.zst", pkg.name);
77 let final_url = format!("{}/{}", url_dir.trim_end_matches('/'), archive_filename);
78
79 println!(
80 "Attempting to download pre-built package from: {}",
81 final_url.cyan()
82 );
83
84 let temp_dir = tempfile::Builder::new().prefix("zoi-exec-dl-").tempdir()?;
85 let temp_archive_path = temp_dir.path().join(&archive_filename);
86
87 if crate::pkg::install::util::download_file_with_progress(
88 &final_url,
89 &temp_archive_path,
90 None,
91 )
92 .is_ok()
93 {
94 let downloaded_data = fs::read(&temp_archive_path)?;
95 let temp_ext_dir = tempfile::Builder::new().prefix("zoi-exec-ext").tempdir()?;
96 let mut archive = Archive::new(ZstdDecoder::new(Cursor::new(downloaded_data))?);
97 archive.unpack(temp_ext_dir.path())?;
98
99 let bin_dir_in_archive = temp_ext_dir.path().join("data/pkgstore/bin");
100 if bin_dir_in_archive.exists()
101 && let Some(bin_name) = &pkg.bins.as_ref().and_then(|b| b.first())
102 {
103 let bin_in_archive = bin_dir_in_archive.join(bin_name);
104 if bin_in_archive.exists() {
105 let final_bin_path = cache_dir.join(bin_name);
106 fs::copy(&bin_in_archive, &final_bin_path)?;
107 #[cfg(unix)]
108 {
109 use std::os::unix::fs::PermissionsExt;
110 fs::set_permissions(
111 &final_bin_path,
112 fs::Permissions::from_mode(0o755),
113 )?;
114 }
115 println!("Binary cached successfully.");
116 return Ok(final_bin_path);
117 }
118 }
119 }
120 }
121 }
122
123 Err(anyhow!("Could not download pre-built package for exec."))
124}
125
126fn find_executable(
127 pkg: &types::Package,
128 upstream: bool,
129 cache_only: bool,
130 local_only: bool,
131 registry_handle: Option<&str>,
132) -> Result<PathBuf> {
133 let handle = registry_handle.unwrap_or("local");
134
135 if upstream {
136 return ensure_binary_is_cached(pkg, true);
137 }
138
139 let scopes_to_check = if local_only {
140 vec![types::Scope::Project]
141 } else {
142 vec![
143 types::Scope::Project,
144 types::Scope::User,
145 types::Scope::System,
146 ]
147 };
148
149 for scope in scopes_to_check {
150 if let Ok(package_dir) = local::get_package_dir(scope, handle, &pkg.repo, &pkg.name) {
151 let latest_path = package_dir.join("latest");
152 if latest_path.exists() {
153 let binary_filename = if cfg!(target_os = "windows") {
154 format!("{}.exe", pkg.name)
155 } else {
156 pkg.name.clone()
157 };
158 let bin_path = latest_path.join("bin").join(binary_filename);
159 if bin_path.exists() {
160 let scope_str = match scope {
161 types::Scope::Project => "project-local",
162 types::Scope::User => "user",
163 types::Scope::System => "system",
164 };
165 println!("Using {} binary for '{}'.", scope_str, pkg.name.cyan());
166 return Ok(bin_path);
167 }
168 }
169 }
170 }
171
172 if local_only {
173 return Err(anyhow!("No local project binary found."));
174 }
175
176 if cache_only {
177 let cache_dir = cache::get_cache_root()?;
178 let binary_filename = if cfg!(target_os = "windows") {
179 format!("{}.exe", pkg.name)
180 } else {
181 pkg.name.clone()
182 };
183 let bin_path = cache_dir.join(&binary_filename);
184 if bin_path.exists() {
185 println!("Using cached binary for '{}'.", pkg.name.cyan());
186 return Ok(bin_path);
187 }
188 return Err(anyhow!("No cached binary found."));
189 }
190
191 ensure_binary_is_cached(pkg, false)
192}
193
194pub fn run(
195 source: &str,
196 args: Vec<String>,
197 upstream: bool,
198 cache_only: bool,
199 local_only: bool,
200) -> Result<()> {
201 let resolved_source = resolve::resolve_source(source)?;
202
203 if let Some(repo_name) = &resolved_source.repo_name {
204 utils::print_repo_warning(repo_name);
205 }
206
207 let mut pkg: types::Package =
208 crate::pkg::lua::parser::parse_lua_package(resolved_source.path.to_str().unwrap(), None)?;
209
210 if let Some(repo_name) = resolved_source.repo_name.clone() {
211 pkg.repo = repo_name;
212 }
213
214 if pkg.package_type == types::PackageType::App {
215 return Err(anyhow!(
216 "This package is an 'app' template. Use 'zoi create <pkg> <appName>' to create an app from it."
217 ));
218 }
219
220 let bin_path = find_executable(
221 &pkg,
222 upstream,
223 cache_only,
224 local_only,
225 resolved_source.registry_handle.as_deref(),
226 )?;
227
228 match crate::pkg::telemetry::posthog_capture_event(
229 "exec",
230 &pkg,
231 env!("CARGO_PKG_VERSION"),
232 resolved_source
233 .registry_handle
234 .as_deref()
235 .unwrap_or("local"),
236 ) {
237 Ok(true) => println!("{} telemetry sent", "Info:".green()),
238 Ok(false) => (),
239 Err(e) => eprintln!("{} telemetry failed: {}", "Warning:".yellow(), e),
240 }
241
242 println!("\n--- Executing '{}' ---\n", pkg.name.bold());
243
244 let mut command_str = format!("\"{}\"", bin_path.display());
245 if !args.is_empty() {
246 command_str.push(' ');
247 command_str.push_str(&args.join(" "));
248 }
249
250 println!("> {}", command_str.cyan());
251
252 let status = if cfg!(target_os = "windows") {
253 Command::new("pwsh")
254 .arg("-Command")
255 .arg(&command_str)
256 .status()?
257 } else {
258 Command::new("bash").arg("-c").arg(&command_str).status()?
259 };
260
261 if let Some(code) = status.code() {
262 std::process::exit(code);
263 }
264
265 Ok(())
266}