tracel-xtask 4.18.2

Reusable and Extensible xtask commands to manage repositories.
Documentation
use anyhow::Ok;
use strum::IntoEnumIterator;
use tracel_xtask_utils::{
    cargo::ensure_cargo_crate_is_installed, endgroup, environment::Environment, group,
    process::run_process,
};

use crate::context::Context;

#[tracel_xtask_macros::declare_command_args(None, DependenciesSubCommand)]
pub struct DependenciesCmdArgs {}

pub fn handle_command(
    args: DependenciesCmdArgs,
    _env: Environment,
    _ctx: Context,
) -> anyhow::Result<()> {
    match args.get_command() {
        DependenciesSubCommand::Deny => run_cargo_deny(),
        DependenciesSubCommand::Unused => run_cargo_machete(),
        DependenciesSubCommand::Update => run_cargo_update(),
        DependenciesSubCommand::All => DependenciesSubCommand::iter()
            .filter(|c| *c != DependenciesSubCommand::All)
            .try_for_each(|c| {
                handle_command(
                    DependenciesCmdArgs { command: Some(c) },
                    _env.clone(),
                    _ctx.clone(),
                )
            }),
    }
}

/// Run cargo-deny
fn run_cargo_deny() -> anyhow::Result<()> {
    ensure_cargo_crate_is_installed("cargo-deny", None, None, false)?;
    // Run cargo deny
    group!("Cargo: run deny checks");
    run_process(
        "cargo",
        &["deny", "check"],
        None,
        None,
        "Some dependencies don't meet the requirements!",
    )?;
    endgroup!();
    Ok(())
}

/// Run cargo-machete
fn run_cargo_machete() -> anyhow::Result<()> {
    ensure_cargo_crate_is_installed("cargo-machete", None, None, false)?;
    // Run cargo machete
    group!("Cargo: run unused dependencies checks");
    run_process(
        "cargo",
        &["machete"],
        None,
        None,
        "Unused dependencies found!",
    )?;
    endgroup!();

    Ok(())
}

/// Run cargo update
fn run_cargo_update() -> anyhow::Result<()> {
    group!("Cargo: update dependencies");
    run_process(
        "cargo",
        &["update"],
        None,
        None,
        "Dependencies should update successfully!",
    )?;
    endgroup!();

    Ok(())
}