Skip to main content

cargo_zng/
util.rs

1use std::{
2    borrow::Cow,
3    collections::HashMap,
4    fs,
5    io::{self, BufReader, Read},
6    path::{Path, PathBuf},
7    process::{Command, Stdio},
8    sync::atomic::{AtomicBool, AtomicU8},
9};
10
11use semver::{Version, VersionReq};
12use serde::Deserialize;
13
14/// Print warning message.
15macro_rules! warn {
16    ($($format_args:tt)*) => {
17        if $crate::util::deny_warnings() {
18            error!($($format_args)*);
19        }else {
20            eprintln!("{} {}", $crate::util::WARN_PREFIX, format_args!($($format_args)*));
21        }
22    };
23}
24
25pub fn deny_warnings() -> bool {
26    std::env::var("RUSTFLAGS")
27        .map(|f| {
28            ["--deny=warnings", "-Dwarnings", "-D warnings", "--deny warnings"]
29                .iter()
30                .any(|d| f.contains(d))
31        })
32        .unwrap_or(false)
33}
34
35/// Print error message and flags the current process as failed.
36///
37/// Note that this does not exit the process, use `fatal!` to exit.
38macro_rules! error {
39    ($($format_args:tt)*) => {
40        {
41            $crate::util::set_failed_run(true);
42            eprintln!("{} {}", $crate::util::ERROR_PREFIX, format_args!($($format_args)*));
43        }
44    };
45}
46
47pub static WARN_PREFIX: &str = color_print::cstr!("<bold><yellow>warning</yellow>:</bold>");
48pub static ERROR_PREFIX: &str = color_print::cstr!("<bold><red>error</red>:</bold>");
49
50/// Print error message and exit the current process with error code.
51macro_rules! fatal {
52    ($($format_args:tt)*) => {
53        {
54            error!($($format_args)*);
55            $crate::util::exit();
56        }
57    };
58}
59
60static VERBOSE_LEVEL: AtomicU8 = AtomicU8::new(255);
61
62pub(crate) fn set_verbose(level: u8) {
63    assert!(level < 255);
64    if VERBOSE_LEVEL.swap(level, std::sync::atomic::Ordering::Relaxed) == 255 {
65        // SAFETY: This is called at startup, when command is
66        // single threaded, it is only really used in tools where
67        // it will definitely be called after `res` command startup
68        unsafe { std::env::set_var(crate::res_tool_util::ZR_VERBOSE, "v".repeat(level as _)) }
69    }
70}
71
72/// Get cached `--verbose` or ZR_VERBOSE.
73pub fn verbose_enabled() -> bool {
74    // crate code is mostly linear and we don't mind
75    // a potential double check of ZR_VERBOSE
76    use std::sync::atomic::Ordering::Relaxed;
77
78    let level = VERBOSE_LEVEL.load(Relaxed);
79    if level > 0 {
80        if level < 255 {
81            return true;
82        }
83        if let Ok(v) = std::env::var(crate::res_tool_util::ZR_VERBOSE)
84            && !v.is_empty()
85        {
86            VERBOSE_LEVEL.store(1, Relaxed);
87            return true;
88        }
89    }
90    false
91}
92
93/// Print only if [`verbose_enabled`].
94macro_rules! println_v {
95    ($($tt:tt)*) => {
96        if $crate::util::verbose_enabled() {
97            println!($($tt)*)
98        }
99    };
100}
101
102// /// Print only if [`verbose_enabled`].
103// macro_rules! print_v {
104//     ($($tt:tt)*) => {
105//         if $crate::util::verbose_enabled() {
106//             print!($($tt)*)
107//         }
108//     };
109// }
110
111/// Print only if [`verbose_enabled`].
112macro_rules! eprintln_v {
113    ($($tt:tt)*) => {
114        if $crate::util::verbose_enabled() {
115            eprintln!($($tt)*)
116        }
117    };
118}
119
120// /// Print only if [`verbose_enabled`].
121// macro_rules! eprint_v {
122//     ($($tt:tt)*) => {
123//         if $crate::util::verbose_enabled() {
124//             eprint!($($tt)*)
125//         }
126//     };
127// }
128
129static RUN_FAILED: AtomicBool = AtomicBool::new(false);
130
131/// Gets if the current process will exit with error code.
132pub fn is_failed_run() -> bool {
133    RUN_FAILED.load(std::sync::atomic::Ordering::SeqCst)
134}
135
136/// Sets if the current process will exit with error code.
137pub fn set_failed_run(failed: bool) {
138    RUN_FAILED.store(failed, std::sync::atomic::Ordering::SeqCst);
139}
140
141/// Exit the current process, with error code `102` if [`is_failed_run`].
142pub fn exit() -> ! {
143    if is_failed_run() {
144        std::process::exit(102)
145    } else {
146        std::process::exit(0)
147    }
148}
149
150/// Run the command with args, inherits stdout and stderr.
151pub fn cmd(line: &str, args: &[&str], env: &[(&str, &str)]) -> io::Result<()> {
152    cmd_impl(line, args, env, false)
153}
154/// Run the command with args.
155pub fn cmd_silent(line: &str, args: &[&str], env: &[(&str, &str)]) -> io::Result<()> {
156    cmd_impl(line, args, env, true)
157}
158fn cmd_impl(line: &str, args: &[&str], env: &[(&str, &str)], silent: bool) -> io::Result<()> {
159    let mut line_parts = line.split(' ');
160    let program = line_parts.next().expect("expected program to run");
161    let mut cmd = Command::new(program);
162    cmd.args(
163        line_parts
164            .map(|a| {
165                let a = a.trim();
166                if a.starts_with('"') { a.trim_matches('"') } else { a }
167            })
168            .filter(|a| !a.is_empty()),
169    );
170    cmd.args(args.iter().filter(|a| !a.is_empty()));
171    for (key, val) in env.iter() {
172        cmd.env(key, val);
173    }
174
175    if silent {
176        let output = cmd.output()?;
177        if output.status.success() {
178            Ok(())
179        } else {
180            let mut cmd = format!("cmd failed: {line}");
181            for arg in args {
182                cmd.push(' ');
183                cmd.push_str(arg);
184            }
185            cmd.push('\n');
186            cmd.push_str(&String::from_utf8_lossy(&output.stderr));
187            Err(io::Error::other(cmd))
188        }
189    } else {
190        let status = cmd.status()?;
191        if status.success() {
192            Ok(())
193        } else {
194            let mut cmd = format!("cmd failed: {line}");
195            for arg in args {
196                cmd.push(' ');
197                cmd.push_str(arg);
198            }
199            Err(io::Error::other(cmd))
200        }
201    }
202}
203
204pub fn workspace_dir() -> Option<PathBuf> {
205    let output = std::process::Command::new("cargo")
206        .arg("locate-project")
207        .arg("--workspace")
208        .arg("--message-format=plain")
209        .output()
210        .ok()?;
211
212    if output.status.success() {
213        let cargo_path = Path::new(std::str::from_utf8(&output.stdout).unwrap().trim());
214        Some(cargo_path.parent().unwrap().to_owned())
215    } else {
216        None
217    }
218}
219
220pub fn ansi_enabled() -> bool {
221    std::env::var("NO_COLOR").is_err()
222}
223
224pub fn clean_value(value: &str, required: bool) -> io::Result<String> {
225    let mut first_char = false;
226    let clean_value: String = value
227        .chars()
228        .filter(|c| {
229            if first_char {
230                first_char = c.is_ascii_alphabetic();
231                first_char
232            } else {
233                *c == ' ' || *c == '-' || *c == '_' || c.is_ascii_alphanumeric()
234            }
235        })
236        .collect();
237    let clean_value = clean_value.trim().to_owned();
238
239    if required && clean_value.is_empty() {
240        if clean_value.is_empty() {
241            return Err(io::Error::new(
242                io::ErrorKind::InvalidInput,
243                format!("cannot derive clean value from `{value}`, must contain at least one ascii alphabetic char"),
244            ));
245        }
246        if clean_value.len() > 62 {
247            return Err(io::Error::new(
248                io::ErrorKind::InvalidInput,
249                format!("cannot derive clean value from `{value}`, must contain <= 62 ascii alphanumeric chars"),
250            ));
251        }
252    }
253    Ok(clean_value)
254}
255
256pub fn manifest_path_from_package(package: &str) -> Option<String> {
257    let metadata = match Command::new("cargo")
258        .args(["metadata", "--format-version", "1", "--no-deps"])
259        .stderr(Stdio::inherit())
260        .output()
261    {
262        Ok(m) => {
263            if !m.status.success() {
264                fatal!("cargo metadata error")
265            }
266            String::from_utf8_lossy(&m.stdout).into_owned()
267        }
268        Err(e) => fatal!("cargo metadata error, {e}"),
269    };
270
271    #[derive(Deserialize)]
272    struct Metadata {
273        packages: Vec<Package>,
274    }
275    #[derive(Deserialize)]
276    struct Package {
277        name: String,
278        manifest_path: String,
279    }
280    let metadata: Metadata = serde_json::from_str(&metadata).unwrap_or_else(|e| fatal!("unexpected cargo metadata format, {e}"));
281
282    for p in metadata.packages {
283        if p.name == package {
284            return Some(p.manifest_path);
285        }
286    }
287    None
288}
289
290/// Workspace crates Cargo.toml paths.
291pub fn workspace_manifest_paths() -> Vec<PathBuf> {
292    let metadata = match Command::new("cargo")
293        .args(["metadata", "--format-version", "1", "--no-deps"])
294        .stderr(Stdio::inherit())
295        .output()
296    {
297        Ok(m) => {
298            if !m.status.success() {
299                fatal!("cargo metadata error")
300            }
301            String::from_utf8_lossy(&m.stdout).into_owned()
302        }
303        Err(e) => fatal!("cargo metadata error, {e}"),
304    };
305
306    #[derive(Deserialize)]
307    struct Metadata {
308        packages: Vec<Package>,
309    }
310    #[derive(Debug, Deserialize)]
311    struct Package {
312        manifest_path: PathBuf,
313    }
314
315    let metadata: Metadata = serde_json::from_str(&metadata).unwrap_or_else(|e| fatal!("unexpected cargo metadata format, {e}"));
316
317    metadata.packages.into_iter().map(|p| p.manifest_path).collect()
318}
319
320/// Workspace root and dependencies of manifest_path
321pub fn dependencies(manifest_path: &str) -> (PathBuf, Vec<DependencyManifest>) {
322    let metadata = match Command::new("cargo")
323        .args(["metadata", "--format-version", "1", "--manifest-path"])
324        .arg(manifest_path)
325        .stderr(Stdio::inherit())
326        .output()
327    {
328        Ok(m) => {
329            if !m.status.success() {
330                fatal!("cargo metadata error")
331            }
332            String::from_utf8_lossy(&m.stdout).into_owned()
333        }
334        Err(e) => fatal!("cargo metadata error, {e}"),
335    };
336
337    #[derive(Deserialize)]
338    struct Metadata {
339        packages: Vec<Package>,
340        workspace_root: PathBuf,
341    }
342    #[derive(Debug, Deserialize)]
343    struct Package {
344        name: String,
345        version: Version,
346        dependencies: Vec<Dependency>,
347        manifest_path: String,
348    }
349    #[derive(Debug, Deserialize)]
350    struct Dependency {
351        name: String,
352        kind: Option<String>,
353        req: VersionReq,
354    }
355
356    let metadata: Metadata = serde_json::from_str(&metadata).unwrap_or_else(|e| fatal!("unexpected cargo metadata format, {e}"));
357
358    let manifest_path = dunce::canonicalize(manifest_path).unwrap();
359
360    let mut dependencies: &[Dependency] = &[];
361
362    for pkg in &metadata.packages {
363        let pkg_path = Path::new(&pkg.manifest_path);
364        if pkg_path == manifest_path {
365            dependencies = &pkg.dependencies;
366            break;
367        }
368    }
369    if !dependencies.is_empty() {
370        let mut map = HashMap::new();
371        for pkg in &metadata.packages {
372            map.entry(pkg.name.as_str()).or_insert_with(Vec::new).push((&pkg.version, pkg));
373        }
374
375        let mut r = vec![];
376        fn collect(map: &mut HashMap<&str, Vec<(&Version, &Package)>>, dependencies: &[Dependency], r: &mut Vec<DependencyManifest>) {
377            for dep in dependencies {
378                if dep.kind.is_some() {
379                    // skip build/dev-dependencies
380                    continue;
381                }
382                if let Some(versions) = map.remove(dep.name.as_str()) {
383                    for (version, pkg) in versions.iter() {
384                        if dep.req.comparators.is_empty() || dep.req.matches(version) {
385                            r.push(DependencyManifest {
386                                name: pkg.name.clone(),
387                                version: pkg.version.clone(),
388                                manifest_path: pkg.manifest_path.as_str().into(),
389                            });
390
391                            // collect dependencies of dependencies
392                            collect(map, &pkg.dependencies, r)
393                        }
394                    }
395                }
396            }
397        }
398        collect(&mut map, dependencies, &mut r);
399        return (metadata.workspace_root, r);
400    }
401
402    (metadata.workspace_root, vec![])
403}
404
405pub struct DependencyManifest {
406    pub name: String,
407    pub version: Version,
408    pub manifest_path: PathBuf,
409}
410
411pub fn check_or_create_dir_all(check: bool, path: impl AsRef<Path>) -> io::Result<()> {
412    if check {
413        let path = path.as_ref();
414        if !path.is_dir() {
415            fatal!("expected `{}` dir", path.display());
416        }
417        Ok(())
418    } else {
419        fs::create_dir_all(path)
420    }
421}
422
423pub fn check_or_write(check: bool, path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> io::Result<()> {
424    let path = path.as_ref();
425    let contents = contents.as_ref();
426    if check {
427        if !path.is_file() {
428            fatal!("expected `{}` file", path.display());
429        }
430        let file = fs::File::open(path).unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", path.display()));
431        let mut bytes = vec![];
432        BufReader::new(file)
433            .read_to_end(&mut bytes)
434            .unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", path.display()));
435
436        if bytes != contents {
437            fatal!("file `{}` contents changed", path.display());
438        } else {
439            println_v!("file `{}` contents did not change", path.display());
440        }
441
442        Ok(())
443    } else {
444        println_v!("writing `{}`", path.display());
445        fs::write(path, contents)
446    }
447}
448
449pub fn check_or_copy(check: bool, from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<u64> {
450    let from = from.as_ref();
451    let to = to.as_ref();
452    if check {
453        if !to.is_file() {
454            fatal!("expected `{}` file", to.display());
455        }
456
457        let mut bytes = vec![];
458        for path in [from, to] {
459            let file = fs::File::open(path).unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", path.display()));
460            let mut b = vec![];
461            BufReader::new(file)
462                .read_to_end(&mut b)
463                .unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", path.display()));
464
465            bytes.push(b);
466        }
467
468        if bytes[0] != bytes[1] {
469            fatal!("file `{}` contents changed", to.display());
470        } else {
471            println_v!("file `{}` contents did not change", to.display());
472        }
473
474        Ok(bytes[1].len() as u64)
475    } else {
476        println_v!("copying\n  from: `{}`\n    to: `{}`", from.display(), to.display());
477        fs::copy(from, to)
478    }
479}
480
481/// Convert '\' paths to '/' on Windows.
482pub fn unix_path(path: &Path) -> Cow<'_, str> {
483    if cfg!(windows) {
484        Cow::Owned(path.to_string_lossy().replace('\\', "/"))
485    } else {
486        path.to_string_lossy()
487    }
488}
489
490pub type GlobItem = Result<walkdir::DirEntry, walkdir::Error>;
491/// `globset` + `walkdir`
492pub fn glob_walker(pattern: &str, case_insensitive: bool) -> Result<impl Iterator<Item = GlobItem> + Send, globset::Error> {
493    const META: [char; 4] = ['*', '?', '[', '{'];
494
495    // split root/glob
496    let mut root = PathBuf::new();
497    let mut glob: Vec<&str> = Vec::new();
498    for comp in Path::new(pattern).components() {
499        let s = comp.as_os_str().to_str().unwrap();
500        if glob.is_empty() && !s.contains(META) {
501            root.push(comp);
502        } else {
503            glob.push(s);
504        }
505    }
506    if root.as_os_str().is_empty() {
507        root.push(".");
508    }
509
510    let mut walker = walkdir::WalkDir::new(&root);
511    let matcher = if glob.is_empty() {
512        // pattern is a path
513        walker = walker.max_depth(0);
514        None
515    } else {
516        if !glob.iter().any(|c| c.contains("**")) {
517            // glob has known depth
518            walker = walker.max_depth(glob.len());
519        }
520        walker = walker.min_depth(1);
521        let glob = glob.join("/"); // globset always uses '/'
522        Some(
523            globset::GlobBuilder::new(&glob)
524                .case_insensitive(case_insensitive)
525                .literal_separator(true)
526                .empty_alternates(true)
527                .build()?
528                .compile_matcher(),
529        )
530    };
531
532    Ok(walker.into_iter().filter(move |entry| {
533        let (Some(matcher), Ok(entry)) = (&matcher, entry) else {
534            return true; // literal path, or an error we must not hide
535        };
536        let rel = entry.path().strip_prefix(&root).unwrap_or(entry.path());
537        matcher.is_match(rel)
538    }))
539}
540
541#[cfg(test)]
542mod tests {
543    #[test]
544    fn globset_test() {
545        let matcher = globset::GlobBuilder::new("foo/bar{,.exe,.com}")
546            .case_insensitive(true)
547            .literal_separator(true)
548            .empty_alternates(true)
549            .build()
550            .unwrap()
551            .compile_matcher();
552
553        assert!(matcher.is_match("foo/bar.exe"));
554        assert!(matcher.is_match("foo/bar.com"));
555        assert!(matcher.is_match("foo/bar"));
556    }
557}