Skip to main content

tracel_xtask/commands/
dependencies.rs

1use anyhow::Ok;
2use strum::IntoEnumIterator;
3
4use crate::{
5    endgroup, group,
6    prelude::{Context, Environment},
7    utils::{cargo::ensure_cargo_crate_is_installed, process::run_process},
8};
9
10#[tracel_xtask_macros::declare_command_args(None, DependenciesSubCommand)]
11pub struct DependenciesCmdArgs {}
12
13pub fn handle_command(
14    args: DependenciesCmdArgs,
15    _env: Environment,
16    _ctx: Context,
17) -> anyhow::Result<()> {
18    match args.get_command() {
19        DependenciesSubCommand::Deny => run_cargo_deny(),
20        DependenciesSubCommand::Unused => run_cargo_machete(),
21        DependenciesSubCommand::All => DependenciesSubCommand::iter()
22            .filter(|c| *c != DependenciesSubCommand::All)
23            .try_for_each(|c| {
24                handle_command(
25                    DependenciesCmdArgs { command: Some(c) },
26                    _env.clone(),
27                    _ctx.clone(),
28                )
29            }),
30    }
31}
32
33/// Run cargo-deny
34fn run_cargo_deny() -> anyhow::Result<()> {
35    ensure_cargo_crate_is_installed("cargo-deny", None, None, false)?;
36    // Run cargo deny
37    group!("Cargo: run deny checks");
38    run_process(
39        "cargo",
40        &["deny", "check"],
41        None,
42        None,
43        "Some dependencies don't meet the requirements!",
44    )?;
45    endgroup!();
46    Ok(())
47}
48
49/// Run cargo-machete
50fn run_cargo_machete() -> anyhow::Result<()> {
51    ensure_cargo_crate_is_installed("cargo-machete", None, None, false)?;
52    // Run cargo machete
53    group!("Cargo: run unused dependencies checks");
54    run_process(
55        "cargo",
56        &["machete"],
57        None,
58        None,
59        "Unused dependencies found!",
60    )?;
61    endgroup!();
62
63    Ok(())
64}