You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-08-08 08:22:52 +02:00
Xtask (#2688)
### Background Comprehensive Rust requires a number of tools to be installed (e.g. mdbook and mdbook-course). As mentioned in #2509 (and discussed in #2469) it would be nice to have a cross platform command for installing these dependencies. Currently these are installed using a shell script (`install-mdbook.sh`) but this isn't truly cross platform e.g. for Windows users. ### Xtask [xtask](https://github.com/matklad/cargo-xtask) outlines an approach for automating tasks in a Rust project. It involves using cargo's aliasing feature to allow us to run commands like `cargo xtask <some task>` to perform adhoc tasks via a Rust binary that we might otherwise need a shell script for. In this PR we add support for a `cargo xtask install-tools` command that will replace the `install-mdbook.sh` script and install the dependent tools. We can potentially extend it to support for other tasks e.g. `cargo xtask fmt`. --------- Co-authored-by: Eric Githinji <egithinji@google.com>
This commit is contained in:
6
xtask/Cargo.toml
Normal file
6
xtask/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "xtask"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
69
xtask/src/main.rs
Normal file
69
xtask/src/main.rs
Normal file
@ -0,0 +1,69 @@
|
||||
use std::{env, process::Command};
|
||||
|
||||
type DynError = Box<dyn std::error::Error>;
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = execute_task() {
|
||||
eprintln!("{e}");
|
||||
std::process::exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_task() -> Result<(), DynError> {
|
||||
let task = env::args().nth(1);
|
||||
match task.as_deref() {
|
||||
Some("install-tools") => install_tools()?,
|
||||
_ => {
|
||||
return Err(Box::from(get_help_string(task.as_deref())));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn install_tools() -> Result<(), DynError> {
|
||||
println!("Installing project tools...");
|
||||
|
||||
let install_args: Vec<Vec<&str>> = 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.44"],
|
||||
vec!["mdbook-svgbob", "--locked", "--version", "0.2.1"],
|
||||
vec!["mdbook-pandoc", "--locked", "--version", "0.9.3"],
|
||||
vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.5"],
|
||||
vec!["i18n-report", "--locked", "--version", "0.2.0"],
|
||||
// These packages are located in this repository
|
||||
vec!["--path", "mdbook-exerciser", "--locked"],
|
||||
vec!["--path", "mdbook-course", "--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(Box::from(error_message));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_help_string(task: Option<&str>) -> String {
|
||||
if let Some(t) = task {
|
||||
format!(
|
||||
"Unrecognized task '{t}'. Available tasks:
|
||||
|
||||
install-tools Installs the tools the project depends on."
|
||||
)
|
||||
} else {
|
||||
"Missing task. To execute a task run `cargo xtask [task]`.".to_string()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user