1use std::{
2 fs,
3 io::{self, BufRead, Read, Write},
4 ops::ControlFlow,
5 path::{Path, PathBuf},
6};
7
8use anyhow::{Context, bail};
9use is_executable::IsExecutable as _;
10use parking_lot::Mutex;
11use zng_env::About;
12
13use crate::res_tool_util::*;
14
15pub fn visit_tools(local: &Path, mut tool: impl FnMut(Tool) -> anyhow::Result<ControlFlow<()>>) -> anyhow::Result<()> {
17 macro_rules! tool {
18 ($($args:tt)+) => {
19 let flow = tool($($args)+)?;
20 if flow.is_break() {
21 return Ok(())
22 }
23 };
24 }
25
26 let mut local_bin_crate = None;
27 if local.exists() {
28 for entry in fs::read_dir(local).with_context(|| format!("cannot read_dir {}", local.display()))? {
29 let path = entry.with_context(|| format!("cannot read_dir entry {}", local.display()))?.path();
30 if path.is_dir() {
31 let name = path.file_name().unwrap().to_string_lossy();
32 if let Some(name) = name.strip_prefix("cargo-zng-res-") {
33 if path.join("Cargo.toml").exists() {
34 tool!(Tool {
35 name: name.to_owned(),
36 kind: ToolKind::LocalCrate,
37 path,
38 });
39 }
40 } else if name == "cargo-zng-res" && path.join("Cargo.toml").exists() {
41 local_bin_crate = Some(path);
42 }
43 }
44 }
45 }
46
47 if let Some(path) = local_bin_crate {
48 let bin_dir = path.join("src/bin");
49 for entry in fs::read_dir(&bin_dir).with_context(|| format!("cannot read_dir {}", bin_dir.display()))? {
50 let path = entry
51 .with_context(|| format!("cannot read_dir entry {}", bin_dir.display()))?
52 .path();
53 if path.is_file() {
54 let name = path.file_name().unwrap().to_string_lossy();
55 if let Some(name) = name.strip_suffix(".rs") {
56 tool!(Tool {
57 name: name.to_owned(),
58 kind: ToolKind::LocalBin,
59 path,
60 });
61 }
62 }
63 }
64 }
65
66 let current_exe = std::env::current_exe()?;
67
68 for &name in crate::res::built_in::BUILT_INS {
69 tool!(Tool {
70 name: name.to_owned(),
71 kind: ToolKind::BuiltIn,
72 path: current_exe.clone(),
73 });
74 }
75
76 let install_dir = current_exe
77 .parent()
78 .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "no cargo install dir"))?;
79
80 for entry in fs::read_dir(install_dir).with_context(|| format!("cannot read_dir {}", install_dir.display()))? {
81 let path = entry
82 .with_context(|| format!("cannot read_dir entry {}", install_dir.display()))?
83 .path();
84 if path.is_file() {
85 let name = path.file_name().unwrap().to_string_lossy();
86 if let Some(name) = name.strip_prefix("cargo-zng-res-")
87 && path.is_executable()
88 {
89 tool!(Tool {
90 name: name.split('.').next().unwrap().to_owned(),
91 kind: ToolKind::Installed,
92 path,
93 });
94 }
95 }
96 }
97
98 Ok(())
99}
100
101pub fn visit_about_vars(about: &About, mut visit: impl FnMut(&str, &str)) {
102 visit(ZR_APP, &about.app);
103 visit(ZR_CRATE_NAME, &about.crate_name);
104 visit(ZR_HOMEPAGE, &about.homepage);
105 visit(ZR_LICENSE, &about.license);
106 visit(ZR_ORG, &about.org);
107 visit(ZR_PKG_AUTHORS, &about.pkg_authors.clone().join(","));
108 visit(ZR_PKG_NAME, &about.pkg_name);
109 visit(ZR_QUALIFIER, &about.qualifier);
110 visit(ZR_VERSION, &about.version.to_string());
111 visit(ZR_DESCRIPTION, &about.description);
112}
113
114pub struct Tool {
115 pub name: String,
116 pub kind: ToolKind,
117
118 pub path: PathBuf,
119}
120impl Tool {
121 pub fn help(&self) -> anyhow::Result<String> {
122 let out = self.cmd().env(ZR_HELP, "").output()?;
123 if !out.status.success() {
124 let error = String::from_utf8_lossy(&out.stderr);
125 bail!("{error}\nhelp run failed, exit code {}", out.status.code().unwrap_or(0));
126 }
127 Ok(String::from_utf8_lossy(&out.stdout).into_owned())
128 }
129
130 fn run(
131 &self,
132 cache: &Path,
133 source_dir: &Path,
134 target_dir: &Path,
135 request: &Path,
136 about: &About,
137 final_args: Option<String>,
138 ) -> anyhow::Result<ToolOutput> {
139 use sha2::Digest;
140 let mut hasher = sha2::Sha256::new();
141
142 hasher.update(source_dir.as_os_str().as_encoded_bytes());
143 hasher.update(target_dir.as_os_str().as_encoded_bytes());
144 hasher.update(request.as_os_str().as_encoded_bytes());
145
146 let mut hash_request = || -> anyhow::Result<()> {
147 let mut file = fs::File::open(request)?;
148 io::copy(&mut file, &mut hasher)?;
149 Ok(())
150 };
151 if let Err(e) = hash_request() {
152 fatal!("cannot read request `{}`, {e}", request.display());
153 }
154
155 let cache_dir = format!("{:x}", hasher.finalize());
156
157 let mut cmd = self.cmd();
158 if let Some(args) = final_args {
159 cmd.env(ZR_FINAL, args);
160 }
161
162 let mut target = request.with_extension("");
164 if let Ok(p) = target.strip_prefix(source_dir) {
166 target = target_dir.join(p);
167 }
168
169 cmd.env(ZR_WORKSPACE_DIR, std::env::current_dir().unwrap())
170 .env(ZR_SOURCE_DIR, source_dir)
171 .env(ZR_TARGET_DIR, target_dir)
172 .env(ZR_REQUEST_DD, request.parent().unwrap())
173 .env(ZR_REQUEST, request)
174 .env(ZR_TARGET_DD, target.parent().unwrap())
175 .env(ZR_TARGET, target)
176 .env(ZR_CACHE_DIR, cache.join(cache_dir));
177 visit_about_vars(about, |key, value| {
178 cmd.env(key, value);
179 });
180 self.run_cmd(&mut cmd)
181 }
182
183 fn cmd(&self) -> std::process::Command {
184 use std::process::Command;
185
186 match self.kind {
187 ToolKind::LocalCrate => {
188 let mut cmd = Command::new("cargo");
189 cmd.arg("run")
190 .arg("--quiet")
191 .arg("--manifest-path")
192 .arg(self.path.join("Cargo.toml"))
193 .arg("--");
194 cmd
195 }
196 ToolKind::LocalBin => {
197 let mut cmd = Command::new("cargo");
198 cmd.arg("run")
199 .arg("--quiet")
200 .arg("--manifest-path")
201 .arg(self.path.parent().unwrap().parent().unwrap().parent().unwrap().join("Cargo.toml"))
202 .arg("--bin")
203 .arg(&self.name)
204 .arg("--");
205 cmd
206 }
207 ToolKind::BuiltIn => {
208 let mut cmd = Command::new(&self.path);
209 cmd.env(crate::res::built_in::ENV_TOOL, &self.name);
210 cmd
211 }
212 ToolKind::Installed => Command::new(&self.path),
213 }
214 }
215
216 fn run_cmd(&self, cmd: &mut std::process::Command) -> anyhow::Result<ToolOutput> {
217 let mut cmd = cmd
218 .stdin(std::process::Stdio::null())
219 .stdout(std::process::Stdio::piped())
220 .stderr(std::process::Stdio::piped())
221 .spawn()?;
222
223 let cmd_err = cmd.stderr.take().unwrap();
225 let error_pipe = std::thread::Builder::new()
226 .name("stderr-reader".into())
227 .spawn(move || {
228 for line in io::BufReader::new(cmd_err).lines() {
229 match line {
230 Ok(l) => eprintln!(" {l}"),
231 Err(e) => {
232 error!("{e}");
233 return;
234 }
235 }
236 }
237 })
238 .expect("failed to spawn thread");
239
240 let mut requests = vec![];
242 const REQUEST: &[u8] = b"zng-res::";
243 let mut cmd_out = cmd.stdout.take().unwrap();
244 let mut out = io::stdout();
245 let mut buf = [0u8; 1024];
246
247 let mut at_line_start = true;
248 let mut maybe_request_start = None;
249
250 print!("\x1B[2m"); loop {
252 let len = cmd_out.read(&mut buf)?;
253 if len == 0 {
254 break;
255 }
256
257 for s in buf[..len].split_inclusive(|&c| c == b'\n') {
258 if at_line_start {
259 if s.starts_with(REQUEST) || REQUEST.starts_with(s) {
260 maybe_request_start = Some(requests.len());
261 }
262 if maybe_request_start.is_none() {
263 out.write_all(b" ")?;
264 }
265 }
266 if maybe_request_start.is_none() {
267 out.write_all(s)?;
268 out.flush()?;
269 } else {
270 requests.write_all(s).unwrap();
271 }
272
273 at_line_start = s.last() == Some(&b'\n');
274 if at_line_start
275 && let Some(i) = maybe_request_start.take()
276 && !requests[i..].starts_with(REQUEST)
277 {
278 out.write_all(&requests[i..])?;
279 out.flush()?;
280 requests.truncate(i);
281 }
282 }
283 }
284 print!("\x1B[0m"); let _ = std::io::stdout().flush();
286
287 let status = cmd.wait()?;
288 let _ = error_pipe.join();
289 if status.success() {
290 Ok(ToolOutput::from(String::from_utf8_lossy(&requests).as_ref()))
291 } else {
292 bail!("command failed, exit code {}", status.code().unwrap_or(0))
293 }
294 }
295}
296
297pub struct Tools {
298 tools: Vec<Tool>,
299 cache: PathBuf,
300 on_final: Mutex<Vec<(usize, PathBuf, String)>>,
301 about: About,
302}
303impl Tools {
304 pub fn capture(local: &Path, cache: PathBuf, about: About, verbose: bool) -> anyhow::Result<Self> {
305 let mut tools = vec![];
306 visit_tools(local, |t| {
307 if verbose {
308 println!("found tool `{}` in `{}`", t.name, t.path.display())
309 }
310 tools.push(t);
311 Ok(ControlFlow::Continue(()))
312 })?;
313 Ok(Self {
314 tools,
315 cache,
316 on_final: Mutex::new(vec![]),
317 about,
318 })
319 }
320
321 pub fn run(&self, tool_name: &str, source: &Path, target: &Path, request: &Path) -> anyhow::Result<()> {
322 println!("{}", display_path(request));
323 for (i, tool) in self.tools.iter().enumerate() {
324 if tool.name == tool_name {
325 let output = tool.run(&self.cache, source, target, request, &self.about, None)?;
326 for warn in output.warnings {
327 warn!("{warn}")
328 }
329 for args in output.on_final {
330 self.on_final.lock().push((i, request.to_owned(), args));
331 }
332 if !output.delegate {
333 return Ok(());
334 }
335 }
336 }
337 bail!("no tool `{tool_name}` to handle request")
338 }
339
340 pub fn run_final(self, source: &Path, target: &Path) -> anyhow::Result<()> {
341 let on_final = self.on_final.into_inner();
342 if !on_final.is_empty() {
343 println!("--final--");
344 for (i, request, args) in on_final {
345 println!("{}", display_path(&request));
346 let output = self.tools[i].run(&self.cache, source, target, &request, &self.about, Some(args))?;
347 for warn in output.warnings {
348 warn!("{warn}")
349 }
350 }
351 }
352 Ok(())
353 }
354}
355
356struct ToolOutput {
357 pub delegate: bool,
359 pub warnings: Vec<String>,
361 pub on_final: Vec<String>,
363}
364impl From<&str> for ToolOutput {
365 fn from(value: &str) -> Self {
366 let mut out = Self {
367 delegate: false,
368 warnings: vec![],
369 on_final: vec![],
370 };
371 for line in value.lines() {
372 if line == "zng-res::delegate" {
373 out.delegate = true;
374 } else if let Some(w) = line.strip_prefix("zng-res::warning=") {
375 out.warnings.push(w.to_owned());
376 } else if let Some(a) = line.strip_prefix("zng-res::on-final=") {
377 out.on_final.push(a.to_owned());
378 }
379 }
380 out
381 }
382}
383
384#[derive(Clone, Copy)]
385pub enum ToolKind {
386 LocalCrate,
387 LocalBin,
388 BuiltIn,
389 Installed,
390}