Repository: google/comprehensive-rust
File: xtask/src/main.rs (Rust)

Code before:
    }

    let path_to_mdbook_exerciser =
        Path::new(env!("CARGO_WORKSPACE_DIR")).join("mdbook-exerciser");
    let path_to_mdbook_course =
        Path::new(env!("CARGO_WORKSPACE_DIR")).join("mdbook-course");

    let install_args = vec![
        // The --locked flag is important for reproducible builds. It also
        // avoids breakage due to skews between mdbook and mdbook-svgbob.
        vec!["mdbook", "--locked", "--version", "0.4.52"],
        vec!["mdbook-svgbob", "--locked", "--version", "0.2.2"],
        vec!["mdbook-pandoc", "--locked", "--version", "0.10.4"],
        vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.6"],
        vec!["i18n-report", "--locked", "--version", "0.2.0"],
        vec!["mdbook-linkcheck2", "--locked", "--version", "0.9.1"],
        // Mdbook-exerciser and mdbook-course are located in this repository.
        // To make it possible to install them from any directory we need to
        // specify their path from the workspace root.
        vec!["--path", path_to_mdbook_exerciser.to_str().unwrap(), "--locked"],
        vec!["--path", path_to_mdbook_course.to_str().unwrap(), "--locked"],
    ];

    for args in &install_args {
        let status = Command::new(env!("CARGO"))
            .arg("install")
            .args(args)
            .status()
            .expect("Failed to execute cargo install");

        if !status.success() {
            let error_message = format!(
                "Command 'cargo install {}' exited with status code: {}",
                args.join(" "),
                status.code().unwrap()
            );
            return Err(anyhow!(error_message));
        }
    }

    // Uninstall original linkcheck if currently installed (see issue no 2773)
    uninstall_mdbook_linkcheck();

    Ok(())
}

fn uninstall_mdbook_linkcheck() {
    println!("Uninstalling old mdbook-linkcheck if installed...");
    let output = Command::new(env!("CARGO"))
        .arg("uninstall")
        .arg("mdbook-linkcheck")

Review comment (refactor):
I have split this into external tools and internal tools because I want to use `binstall` (with a flag) in the next commit I have in my pipeline. This prepares the stage for this.

Code after:
    let rustup_steps = [
        ["toolchain", "install", "--profile", "minimal", PINNED_NIGHTLY],
        ["component", "add", "rustfmt", "--toolchain", PINNED_NIGHTLY],
    ];
    for args in rustup_steps {
        let mut cmd = Command::new("rustup");
        cmd.args(args);
        run_command(&mut cmd)?;
    }

    let cargo = env!("CARGO");
    // The --locked flag is important for reproducible builds.
    let tools = [
        ("mdbook", "0.4.52"),
        ("mdbook-svgbob", "0.2.2"),
        ("mdbook-pandoc", "0.10.4"),
        ("mdbook-i18n-helpers", "0.3.6"),
        ("i18n-report", "0.2.0"),
        ("mdbook-linkcheck2", "0.9.1"),
    ];

    for (tool, version) in tools {
        let mut cmd = Command::new(cargo);
        cmd.args(["install", tool, "--version", version, "--locked"]);
        run_command(&mut cmd)?;
    }

    // Install local tools from the workspace.
    let workspace_dir = Path::new(env!("CARGO_WORKSPACE_DIR"));
    let local_tools = ["mdbook-exerciser", "mdbook-course"];
    for tool in local_tools {
        let mut cmd = Command::new(cargo);
        cmd.args(["install", "--path"])
            .arg(workspace_dir.join(tool))
            .arg("--locked");
        run_command(&mut cmd)?;
    }

    // Uninstall original linkcheck if currently installed (see issue no 2773)
    uninstall_mdbook_linkcheck()?;

    Ok(())
}

fn uninstall_mdbook_linkcheck() -> Result<()> {
    println!("Uninstalling old mdbook-linkcheck if installed...");
    let output = Command::new(env!("CARGO"))
        .args(["uninstall", "mdbook-linkcheck"])
        .output()
        .context("Failed to execute `cargo uninstall mdbook-linkcheck`")?;