vdev/commands/
crate_versions.rs

1use std::{collections::HashMap, collections::HashSet, process::Command};
2
3use anyhow::Result;
4use clap::Args;
5use itertools::Itertools as _;
6use regex::Regex;
7
8use crate::{app::CommandExt as _, util};
9
10/// Show information about crates versions pulled in by all dependencies
11#[derive(Args, Debug)]
12#[command()]
13pub struct Cli {
14    /// Show all versions, not just those that are duplicated
15    #[arg(long)]
16    all: bool,
17
18    /// The feature to active (multiple allowed). If none are specified, the default is used.
19    #[arg(short = 'F', long)]
20    feature: Vec<String>,
21}
22
23impl Cli {
24    pub fn exec(self) -> Result<()> {
25        let re_crate = Regex::new(r" (\S+) v([0-9.]+)").unwrap();
26        let mut versions: HashMap<String, HashSet<String>> = HashMap::default();
27
28        for line in Command::new("cargo")
29            .arg("tree")
30            .features(&self.feature)
31            .check_output()?
32            .lines()
33        {
34            if let Some(captures) = re_crate.captures(line) {
35                let package = &captures[1];
36                let version = &captures[2];
37                versions
38                    .entry(package.into())
39                    .or_default()
40                    .insert(version.into());
41            }
42        }
43
44        if !self.all {
45            versions.retain(|_, versions| versions.len() > 1);
46        }
47
48        let width = versions.keys().map(String::len).max().unwrap_or(0).max(7);
49        if *util::IS_A_TTY {
50            println!("{:width$}  Version(s)", "Package");
51            println!("{:width$}  ----------", "-------");
52        }
53
54        for (package, versions) in versions {
55            let versions = versions.iter().join(" ");
56            println!("{package:width$}  {versions}");
57        }
58
59        Ok(())
60    }
61}