cargo_zng/res/
tool.rs

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
15/// Visit in the `ToolKind` order.
16pub 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                if path.is_executable() {
88                    tool!(Tool {
89                        name: name.split('.').next().unwrap().to_owned(),
90                        kind: ToolKind::Installed,
91                        path,
92                    });
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        // if the request is already in `target` (recursion)
163        let mut target = request.with_extension("");
164        // if the request is in `source`
165        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        // indent stderr
224        let cmd_err = cmd.stderr.take().unwrap();
225        let error_pipe = std::thread::spawn(move || {
226            for line in io::BufReader::new(cmd_err).lines() {
227                match line {
228                    Ok(l) => eprintln!("  {l}"),
229                    Err(e) => {
230                        error!("{e}");
231                        return;
232                    }
233                }
234            }
235        });
236
237        // indent stdout and capture "zng-res::" requests
238        let mut requests = vec![];
239        const REQUEST: &[u8] = b"zng-res::";
240        let mut cmd_out = cmd.stdout.take().unwrap();
241        let mut out = io::stdout();
242        let mut buf = [0u8; 1024];
243
244        let mut at_line_start = true;
245        let mut maybe_request_start = None;
246
247        print!("\x1B[2m"); // dim
248        loop {
249            let len = cmd_out.read(&mut buf)?;
250            if len == 0 {
251                break;
252            }
253
254            for s in buf[..len].split_inclusive(|&c| c == b'\n') {
255                if at_line_start {
256                    if s.starts_with(REQUEST) || REQUEST.starts_with(s) {
257                        maybe_request_start = Some(requests.len());
258                    }
259                    if maybe_request_start.is_none() {
260                        out.write_all(b"  ")?;
261                    }
262                }
263                if maybe_request_start.is_none() {
264                    out.write_all(s)?;
265                    out.flush()?;
266                } else {
267                    requests.write_all(s).unwrap();
268                }
269
270                at_line_start = s.last() == Some(&b'\n');
271                if at_line_start {
272                    if let Some(i) = maybe_request_start.take() {
273                        if !requests[i..].starts_with(REQUEST) {
274                            out.write_all(&requests[i..])?;
275                            out.flush()?;
276                            requests.truncate(i);
277                        }
278                    }
279                }
280            }
281        }
282        print!("\x1B[0m"); // clear styles
283        let _ = std::io::stdout().flush();
284
285        let status = cmd.wait()?;
286        let _ = error_pipe.join();
287        if status.success() {
288            Ok(ToolOutput::from(String::from_utf8_lossy(&requests).as_ref()))
289        } else {
290            bail!("command failed, exit code {}", status.code().unwrap_or(0))
291        }
292    }
293}
294
295pub struct Tools {
296    tools: Vec<Tool>,
297    cache: PathBuf,
298    on_final: Mutex<Vec<(usize, PathBuf, String)>>,
299    about: About,
300}
301impl Tools {
302    pub fn capture(local: &Path, cache: PathBuf, about: About, verbose: bool) -> anyhow::Result<Self> {
303        let mut tools = vec![];
304        visit_tools(local, |t| {
305            if verbose {
306                println!("found tool `{}` in `{}`", t.name, t.path.display())
307            }
308            tools.push(t);
309            Ok(ControlFlow::Continue(()))
310        })?;
311        Ok(Self {
312            tools,
313            cache,
314            on_final: Mutex::new(vec![]),
315            about,
316        })
317    }
318
319    pub fn run(&self, tool_name: &str, source: &Path, target: &Path, request: &Path) -> anyhow::Result<()> {
320        println!("{}", display_path(request));
321        for (i, tool) in self.tools.iter().enumerate() {
322            if tool.name == tool_name {
323                let output = tool.run(&self.cache, source, target, request, &self.about, None)?;
324                for warn in output.warnings {
325                    warn!("{warn}")
326                }
327                for args in output.on_final {
328                    self.on_final.lock().push((i, request.to_owned(), args));
329                }
330                if !output.delegate {
331                    return Ok(());
332                }
333            }
334        }
335        bail!("no tool `{tool_name}` to handle request")
336    }
337
338    pub fn run_final(self, source: &Path, target: &Path) -> anyhow::Result<()> {
339        let on_final = self.on_final.into_inner();
340        if !on_final.is_empty() {
341            println!("--final--");
342            for (i, request, args) in on_final {
343                println!("{}", display_path(&request));
344                let output = self.tools[i].run(&self.cache, source, target, &request, &self.about, Some(args))?;
345                for warn in output.warnings {
346                    warn!("{warn}")
347                }
348            }
349        }
350        Ok(())
351    }
352}
353
354struct ToolOutput {
355    // zng-res::delegate
356    pub delegate: bool,
357    // zng-res::warning=
358    pub warnings: Vec<String>,
359    // zng-res::on-final=
360    pub on_final: Vec<String>,
361}
362impl From<&str> for ToolOutput {
363    fn from(value: &str) -> Self {
364        let mut out = Self {
365            delegate: false,
366            warnings: vec![],
367            on_final: vec![],
368        };
369        for line in value.lines() {
370            if line == "zng-res::delegate" {
371                out.delegate = true;
372            } else if let Some(w) = line.strip_prefix("zng-res::warning=") {
373                out.warnings.push(w.to_owned());
374            } else if let Some(a) = line.strip_prefix("zng-res::on-final=") {
375                out.on_final.push(a.to_owned());
376            }
377        }
378        out
379    }
380}
381
382#[derive(Clone, Copy)]
383pub enum ToolKind {
384    LocalCrate,
385    LocalBin,
386    BuiltIn,
387    Installed,
388}