Skip to main content

cargo_zng/res/
tool.rs

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