Skip to main content

cargo_zng/
res.rs

1use std::{
2    fs, io,
3    ops::ControlFlow,
4    path::{Path, PathBuf},
5    time::Instant,
6};
7
8use anyhow::{Context as _, bail};
9
10use built_in::{ZR_WORKSPACE_DIR, display_path};
11use clap::*;
12use color_print::cstr;
13use zng_env::About;
14
15use crate::util::{self, unix_path};
16
17use self::tool::Tools;
18
19mod about;
20pub mod built_in;
21mod tool;
22
23mod upgrade;
24
25#[derive(Args, Debug)]
26pub struct ResArgs {
27    /// Resources source dir
28    #[arg(default_value = "res")]
29    source: PathBuf,
30    /// Resources target dir
31    ///
32    /// This directory is wiped before each build.
33    #[arg(default_value = "target/res")]
34    target: PathBuf,
35
36    /// Copy all static files to the target dir
37    #[arg(long, action)]
38    pack: bool,
39
40    /// Search for `zng-res-{tool}` in this directory first
41    #[arg(long, default_value = "tools", value_name = "DIR")]
42    tool_dir: PathBuf,
43    /// Prints help for all tools available
44    #[arg(long, action)]
45    tools: bool,
46    /// Prints the full help for a tool
47    #[arg(long)]
48    tool: Option<String>,
49
50    /// Tools cache dir
51    #[arg(long, default_value = "target/res.cache")]
52    tool_cache: PathBuf,
53
54    /// Max number of nested tools that can run from a single source tool
55    #[arg(long, default_value = "32")]
56    recursion_limit: u32,
57    /// Max number of 'z passes
58    #[arg(long, default_value = "32")]
59    z_limit: u32,
60
61    /// TOML file that that defines metadata uses by tools (ZR_APP, ZR_ORG, ..)
62    ///
63    /// This is only needed if the workspace has multiple bin crates
64    /// and none or many set '[package.metadata.zng.about]'.
65    ///
66    /// See `zng::env::About` for more details.
67    #[arg(long, value_name = "TOML_FILE")]
68    metadata: Option<PathBuf>,
69
70    /// Writes the metadata extracted the workspace or --metadata
71    #[arg(long, action)]
72    metadata_dump: bool,
73
74    /// Rename all "name.zr-tool" files to new syntax "name 'tool"
75    ///
76    /// Rename sfxf and shf to 'z'sfx and 'z'sh
77    #[arg(long, value_name = "DIR")]
78    upgrade_zr: Option<PathBuf>,
79}
80
81fn canonicalize(path: &Path) -> PathBuf {
82    dunce::canonicalize(path).unwrap_or_else(|e| fatal!("cannot resolve path, {e}"))
83}
84
85pub(crate) fn run(mut args: ResArgs) {
86    if let Some(p) = args.upgrade_zr {
87        return upgrade::zr(p);
88    }
89
90    if args.tool_dir.exists() {
91        args.tool_dir = canonicalize(&args.tool_dir);
92    }
93    if args.tools {
94        return tools_help(&args.tool_dir);
95    }
96    if let Some(t) = args.tool {
97        return tool_help(&args.tool_dir, &t);
98    }
99
100    if args.metadata_dump {
101        let about = about::find_about(args.metadata.as_deref());
102        crate::res::tool::visit_about_vars(&about, |key, value| {
103            println!("{key}={value}");
104        });
105        return;
106    }
107
108    if !args.source.exists() {
109        fatal!("source dir does not exist");
110    }
111    if let Err(e) = fs::create_dir_all(&args.tool_cache) {
112        fatal!("cannot create cache dir, {e}");
113    }
114    if let Err(e) = fs::remove_dir_all(&args.target)
115        && e.kind() != io::ErrorKind::NotFound
116    {
117        fatal!("cannot remove target dir, {e}");
118    }
119    if let Err(e) = fs::create_dir_all(&args.target) {
120        fatal!("cannot create target dir, {e}");
121    }
122
123    args.source = canonicalize(&args.source);
124    args.target = canonicalize(&args.target);
125    args.tool_cache = canonicalize(&args.tool_cache);
126
127    if args.source == args.target {
128        fatal!("cannot build res to same dir");
129    }
130
131    let about = about::find_about(args.metadata.as_deref());
132
133    // tool request paths are relative to the workspace root
134    if let Some(p) = util::workspace_dir() {
135        if let Err(e) = std::env::set_current_dir(p) {
136            fatal!("cannot change dir, {e}");
137        }
138    } else {
139        warn!("source is not in a cargo workspace, tools will run using source as root");
140        if let Err(e) = std::env::set_current_dir(&args.source) {
141            fatal!("cannot change dir, {e}");
142        }
143    }
144
145    unsafe {
146        // SAFETY: cargo-zng res is single-threaded
147        //
148        // to use `display_path` in the tool runner (current process)
149        std::env::set_var(ZR_WORKSPACE_DIR, &*unix_path(&std::env::current_dir().unwrap()));
150    }
151
152    let start = Instant::now();
153    if let Err(e) = build(&args, about) {
154        let e = e.to_string();
155        for line in e.lines() {
156            eprintln!("   {line}");
157        }
158        fatal!("res build failed");
159    }
160
161    println!(cstr!("<bold><green>Finished</green></bold> res build in {:?}"), start.elapsed());
162    println!("         {}", args.target.display());
163}
164
165fn build(args: &ResArgs, about: About) -> anyhow::Result<()> {
166    let tools = Tools::capture(&args.tool_dir, args.tool_cache.clone(), about)?;
167    source_to_target_pass(args, &tools, &args.source, &args.target)?;
168    z_passes(args, &tools)
169}
170
171fn source_to_target_pass(args: &ResArgs, tools: &Tools, source: &Path, target: &Path) -> anyhow::Result<()> {
172    for entry in walkdir::WalkDir::new(source).min_depth(1).max_depth(1).sort_by_file_name() {
173        let entry = entry.with_context(|| format!("cannot read dir entry {}", source.display()))?;
174        if entry.file_type().is_dir() {
175            let source = entry.path();
176            // mirror dir in target
177            println!("{}", display_path(source));
178            let target = target.join(source.file_name().unwrap());
179            fs::create_dir(&target).with_context(|| format!("cannot create_dir {}", target.display()))?;
180            println!(cstr!("  <dim>{}</>"), display_path(&target));
181
182            source_to_target_pass(args, tools, source, &target)?;
183        } else if entry.file_type().is_file() {
184            let request = entry.path();
185
186            // run tool
187            if let Some((request_wt, tool)) = take_tool(request, false) {
188                // run prints request path
189                tools.run(tool, &args.source, &args.target, request, &request_wt)?;
190
191                // recurse immediately
192                let mut passes = 0;
193                while target_to_target_pass(args, tools, &args.target)? {
194                    passes += 1;
195                    if passes >= args.recursion_limit {
196                        bail!("reached --recursion-limit of {}", args.recursion_limit)
197                    }
198                }
199                continue;
200            }
201
202            // or pack
203            if args.pack {
204                println!("{}", display_path(request));
205                let target = target.join(request.file_name().unwrap());
206                fs::copy(request, &target).with_context(|| format!("cannot copy {} to {}", request.display(), target.display()))?;
207                println!(cstr!("  <dim>{}</>"), display_path(&target));
208            }
209        } else if entry.file_type().is_symlink() {
210            built_in::symlink_warn(entry.path());
211        }
212    }
213    Ok(())
214}
215
216fn target_to_target_pass(args: &ResArgs, tools: &Tools, dir: &Path) -> anyhow::Result<bool> {
217    let mut advanced = false;
218    for entry in walkdir::WalkDir::new(dir).min_depth(1).sort_by_file_name() {
219        let entry = entry.with_context(|| format!("cannot read dir entry {}", dir.display()))?;
220        if entry.file_type().is_file() {
221            let request = entry.path();
222
223            // run tool
224            if let Some((request_wt, tool)) = take_tool(request, false) {
225                // run prints request path and removes file if not needed
226                let done = tools.run(tool, &args.source, &args.target, request, &request_wt)?;
227                if done {
228                    advanced = true;
229                }
230            }
231        }
232    }
233    Ok(advanced)
234}
235
236fn z_passes(args: &ResArgs, tools: &Tools) -> anyhow::Result<()> {
237    let mut passes = 0;
238    loop {
239        passes += 1;
240        if passes > args.z_limit {
241            bail!("reached --z-limit of {}", args.z_limit);
242        }
243
244        let pending = tools.pop_z();
245        if pending.is_empty() {
246            break;
247        }
248        for request in pending {
249            let (request_wt, tool) = take_tool(&request, true).unwrap();
250            tools.run(tool, &args.source, &args.target, &request, &request_wt)?;
251        }
252    }
253    Ok(())
254}
255
256fn tools_help(tools: &Path) {
257    let r = tool::visit_tools(tools, |tool| {
258        if crate::util::ansi_enabled() {
259            println!(cstr!("<bold>'{}</bold> @ {}"), tool.name, display_tool_path(&tool.path));
260        } else {
261            println!("'{} @ {}", tool.name, display_tool_path(&tool.path));
262        }
263        match tool.help() {
264            Ok(h) => {
265                if let Some(line) = h.trim().lines().next() {
266                    println!("  {line}");
267                    println!();
268                }
269            }
270            Err(e) => error!("{e}"),
271        }
272        Ok(ControlFlow::Continue(()))
273    });
274    if let Err(e) = r {
275        fatal!("{e}")
276    }
277    println!("call 'cargo zng res --help tool' to read full help from a tool");
278}
279
280fn tool_help(tools: &Path, name: &str) {
281    let mut name = name.strip_prefix("'").unwrap_or(name);
282    if name.chars().all(|c| c == 'z') {
283        name = "z";
284    }
285    let mut found = false;
286    let r = tool::visit_tools(tools, |tool| {
287        if tool.name == name {
288            if crate::util::ansi_enabled() {
289                println!(cstr!("<bold>'{}</bold> @ {}"), tool.name, display_tool_path(&tool.path));
290            } else {
291                println!("'{} @ {}", tool.name, display_tool_path(&tool.path));
292            }
293            match tool.help() {
294                Ok(h) => {
295                    for line in h.trim().lines() {
296                        println!("  {line}");
297                    }
298                    if !h.is_empty() {
299                        println!();
300                    }
301                }
302                Err(e) => error!("{e}"),
303            }
304            found = true;
305            Ok(ControlFlow::Break(()))
306        } else {
307            Ok(ControlFlow::Continue(()))
308        }
309    });
310    if let Err(e) = r {
311        fatal!("{e}")
312    }
313    if !found {
314        fatal!("did not find tool `{name}`")
315    }
316}
317
318fn display_tool_path(p: &Path) -> String {
319    let base = util::workspace_dir().unwrap_or_else(|| std::env::current_dir().unwrap());
320    let r = if let Ok(local) = p.strip_prefix(base) {
321        local.display().to_string()
322    } else {
323        p.file_name().unwrap().to_string_lossy().into_owned()
324    };
325
326    #[cfg(windows)]
327    return r.replace('\\', "/").trim_end_matches(".exe").to_owned();
328
329    #[cfg(not(windows))]
330    r
331}
332
333fn take_tool(p: &Path, skip_z: bool) -> Option<(PathBuf, &str)> {
334    if let Some(ext) = p.extension()
335        && let Some(ext) = ext.to_str()
336        && let Some(tool) = ext.strip_prefix("zr-")
337    {
338        warn!("deprecated syntax, call \"cargo zng res --upgrade-zr .\" to auto upgrade");
339        return Some((p.with_extension(""), tool));
340    }
341
342    if let Some(name) = p.file_name()
343        && let Some(name) = name.to_str()
344        && let Some((file, tools)) = name.split_once(" '")
345    {
346        let name = file.trim_end(); // "name.txt   'tool"
347        let mut tools = tools.split('\'');
348        let mut tool = tools.next().unwrap().trim();
349        if skip_z && tool.chars().all(|c| c == 'z') {
350            tool = tools.next()?.trim();
351        }
352
353        if let Some(next_tool) = tools.next() {
354            let mut name = format!("{name} '{}", next_tool.trim());
355            for next_tool in tools {
356                name.push('\'');
357                name.push_str(next_tool.trim());
358            }
359            // "name.txt 'next_tool1'tool2"
360            return Some((p.with_file_name(name), tool));
361        }
362
363        return Some((p.with_file_name(name), tool));
364    }
365
366    None
367}
368
369#[cfg(test)]
370mod tests {
371    use std::path::{Path, PathBuf};
372
373    use super::take_tool;
374
375    #[test]
376    fn take_legacy_one() {
377        let path = PathBuf::from("foo/bar/name.txt.zr-copy");
378        let (file, tool) = take_tool(&path, false).unwrap();
379        assert_eq!(tool, "copy");
380        assert_eq!(file.to_str().unwrap(), "foo/bar/name.txt");
381    }
382
383    #[test]
384    fn take_legacy_two() {
385        let path = PathBuf::from("foo/bar/name.txt.zr-copy.zr-rp");
386        let (file, tool) = take_tool(&path, false).unwrap();
387        assert_eq!(tool, "rp");
388        assert_eq!(file.to_str().unwrap(), "foo/bar/name.txt.zr-copy");
389
390        let (file, tool) = take_tool(&file, false).unwrap();
391        assert_eq!(tool, "copy");
392        assert_eq!(file.to_str().unwrap(), "foo/bar/name.txt");
393    }
394
395    #[test]
396    fn take_one() {
397        let path = PathBuf::from("foo/bar/name.txt.zr-copy");
398        let (file, tool) = take_tool(&path, false).unwrap();
399        assert_eq!(tool, "copy");
400        assert_eq!(file.to_str().unwrap(), "foo/bar/name.txt");
401    }
402
403    #[test]
404    fn take_two() {
405        let path = PathBuf::from("foo/bar/name.txt 'rp'copy");
406        let (file, tool) = take_tool(&path, false).unwrap();
407        assert_eq!(tool, "rp");
408        assert_eq!(file.to_str().unwrap(), Path::new("foo/bar/name.txt 'copy"));
409
410        let (file, tool) = take_tool(&file, false).unwrap();
411        assert_eq!(tool, "copy");
412        assert_eq!(file.to_str().unwrap(), Path::new("foo/bar/name.txt"));
413    }
414
415    #[test]
416    fn take_two_space() {
417        let path = PathBuf::from("foo/bar/name.txt 'rp 'copy");
418        let (file, tool) = take_tool(&path, false).unwrap();
419        assert_eq!(tool, "rp");
420        assert_eq!(file.to_str().unwrap(), Path::new("foo/bar/name.txt 'copy"));
421
422        let (file, tool) = take_tool(&file, false).unwrap();
423        assert_eq!(tool, "copy");
424        assert_eq!(file.to_str().unwrap(), Path::new("foo/bar/name.txt"));
425    }
426}