Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xtask #2688

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Xtask #2688

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@ use std::{env, process::Command};

type DynError = Box<dyn std::error::Error>;

const INSTALLATION_ARGS: [[&str; 4]; 7] = [
// The --locked flag is important for reproducible builds. It also
// avoids breakage due to skews between mdbook and mdbook-svgbob.
["mdbook", "--locked", "--version", "0.4.44"],
["mdbook-svgbob", "--locked", "--version", "0.2.1"],
["mdbook-pandoc", "--locked", "--version", "0.9.3"],
["mdbook-i18n-helpers", "--locked", "--version", "0.3.5"],
["i18n-report", "--locked", "--version", "0.2.0"],
// These packages are located in this repository
["--path", "mdbook-exerciser", "--locked", ""],
["--path", "mdbook-course", "--locked", ""],
];

fn main() {
if let Err(e) = execute_task() {
eprintln!("{e}");
Expand All @@ -26,35 +13,59 @@ fn execute_task() -> Result<(), DynError> {
let task = env::args().nth(1);
match task.as_deref() {
Some("install-tools") => install_tools()?,
_ => print_help(task.as_deref()),
_ => {
return Err(Box::from(get_help_string(task.as_deref())));
}
}
Ok(())
}

fn install_tools() -> Result<(), DynError> {
println!("Installing project tools...");

for args in INSTALLATION_ARGS.iter() {
Command::new(env!("CARGO"))
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.iter().filter(|a| **a != ""))
.status()?;
.args(args)
.status()
.expect("Failed to execute cargo install");

if !status.success() {
let error_message = format!(
"cargo install {} {} exited with status code: {}",
args.get(0).unwrap(),
args.get(1).unwrap(),
status.code().unwrap()
);
return Err(Box::from(error_message));
}
}

Ok(())
}

fn print_help(task: Option<&str>) {
fn get_help_string(task: Option<&str>) -> String {
if let Some(t) = task {
eprintln!(
format!(
"Unrecognized task '{t}'. Available tasks:

install-tools Installs the tools the project depends on.

Run with `cargo xtask [task]`.
"
);
)
} else {
eprintln!("Missing task. To execute a task run `cargo xtask [task]`.");
"Missing task. To execute a task run `cargo xtask [task]`.".to_string()
}
}