Skip to content

Commit 738d5ad

Browse files
egithinjiEric Githinji
andauthored
Simplify project commands using cargo xtask (#2753)
We are already using `cargo xtask install-tools` to install the project's tools, and `cargo xtask web-tests` to run the js tests. In this PR we provide support for the various `mdbook` commands (`test`,`serve`, and `build`) with `cargo xtask` commands. This provides a uniform interface for running tasks in the project. Additionally it allows these commands to work from within any dirrectory (previously you'd need to navigate to the workspace root in order to run say `mdbook build`). Additionally we're improving the xtask code by making use of `Clap` enums to handle validation of the possible tasks to run via xtask (this closes #2741 ). --------- Co-authored-by: Eric Githinji <[email protected]>
1 parent ef20b04 commit 738d5ad

File tree

2 files changed

+98
-42
lines changed

2 files changed

+98
-42
lines changed

README.md

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Articles and blog posts from around the web which cover Comprehensive Rust:
4646
_[Rust Training at Scale | Rust Global @ RustConf 2024](https://youtu.be/7h5KyMqt2-Q?si=4M99HdWWxMaqN8Zr)_.
4747
What Google learnt from teaching Comprehensive Rust for more than two years.
4848

49-
## Building
49+
## Setup
5050

5151
The course is built using a few tools:
5252

@@ -55,10 +55,7 @@ The course is built using a few tools:
5555
- [mdbook-i18n-helpers and i18n-report](https://github.com/google/mdbook-i18n-helpers)
5656
- [mdbook-exerciser](mdbook-exerciser/)
5757
- [mdbook-course](mdbook-course/)
58-
59-
In addition,
60-
[mdbook-linkcheck](https://github.com/Michael-F-Bryan/mdbook-linkcheck) checks
61-
the internal links.
58+
- [mdbook-linkcheck](https://github.com/Michael-F-Bryan/mdbook-linkcheck)
6259

6360
First install Rust by following the instructions on https://rustup.rs/. Then
6461
clone this repository:
@@ -74,25 +71,17 @@ Then install these tools with:
7471
cargo xtask install-tools
7572
```
7673

77-
Run
78-
79-
```shell
80-
mdbook test
81-
```
74+
## Commands
8275

83-
to test all included Rust snippets. Run
84-
85-
```shell
86-
mdbook serve
87-
```
76+
Here is a summary of the various commands you can run in the project.
8877

89-
to start a web server with the course. You'll find the content on
90-
<http://localhost:3000>. You can use `mdbook build` to create a static version
91-
of the course in the `book/` directory. Note that you have to separately build
92-
and zip exercises and add them to `book/html`. To build any of the translated
93-
versions of the course, run `MDBOOK_BOOK__LANGUAGE=xx mdbook build -d book/xx`
94-
where `xx` is the ISO 639 language code (e.g. `da` for the Danish translation).
95-
[TRANSLATIONS.md](TRANSLATIONS.md) contains further instructions.
78+
| Command | Description |
79+
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
80+
| `cargo xtask install-tools` | Install all the tools the project depends on. |
81+
| `cargo xtask serve` | Start a web server with the course. You'll find the content on http://localhost:3000. |
82+
| `cargo xtask rust-tests` | Test the included Rust snippets. |
83+
| `cargo xtask web-tests` | Run the web driver tests in the tests directory. |
84+
| `cargo xtask build` | Create a static version of the course in the `book/` directory. Note that you have to separately build and zip exercises and add them to book/html. To build any of the translated versions of the course, run MDBOOK_BOOK__LANGUAGE=xx mdbook build -d book/xx where xx is the ISO 639 language code (e.g. da for the Danish translation). [TRANSLATIONS.md](TRANSLATIONS.md) contains further instructions. |
9685

9786
> **Note** On Windows, you need to enable symlinks
9887
> (`git config --global core.symlinks true`) and Developer Mode.

xtask/src/main.rs

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! the tools.
2121
2222
use anyhow::{anyhow, Ok, Result};
23-
use clap::Parser;
23+
use clap::{Parser, ValueEnum};
2424
use std::path::Path;
2525
use std::{env, process::Command};
2626

@@ -32,23 +32,38 @@ fn main() -> Result<()> {
3232
Ok(())
3333
}
3434

35-
#[derive(Parser, Debug)]
35+
#[derive(Parser)]
3636
#[command(
3737
about = "Binary for executing tasks within the Comprehensive Rust project"
3838
)]
39-
struct Args {
40-
#[arg(required = true, help = "The task to execute")]
41-
task: String,
39+
struct Cli {
40+
/// The task to execute
41+
#[arg(value_enum)]
42+
task: Task,
43+
}
44+
45+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
46+
enum Task {
47+
/// Installs the tools the project depends on.
48+
InstallTools,
49+
/// Runs the web driver tests in the tests directory.
50+
WebTests,
51+
/// Tests all included Rust snippets.
52+
RustTests,
53+
/// Starts a web server with the course.
54+
Serve,
55+
/// Create a static version of the course in the `book/` directory.
56+
Build,
4257
}
4358

4459
fn execute_task() -> Result<()> {
45-
let task = Args::parse().task;
46-
match task.as_str() {
47-
"install-tools" => install_tools()?,
48-
"web-tests" => run_web_tests()?,
49-
_ => {
50-
return Err(anyhow!(unrecognized_task_string(task.as_str())));
51-
}
60+
let cli = Cli::parse();
61+
match cli.task {
62+
Task::InstallTools => install_tools()?,
63+
Task::WebTests => run_web_tests()?,
64+
Task::RustTests => run_rust_tests()?,
65+
Task::Serve => start_web_server()?,
66+
Task::Build => build()?,
5267
}
5368
Ok(())
5469
}
@@ -109,7 +124,29 @@ fn run_web_tests() -> Result<()> {
109124

110125
if !status.success() {
111126
let error_message = format!(
112-
"Command 'cargo web-tests' exited with status code: {}",
127+
"Command 'cargo xtask web-tests' exited with status code: {}",
128+
status.code().unwrap()
129+
);
130+
return Err(anyhow!(error_message));
131+
}
132+
133+
Ok(())
134+
}
135+
136+
fn run_rust_tests() -> Result<()> {
137+
println!("Running rust tests...");
138+
139+
let path_to_workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));
140+
141+
let status = Command::new("mdbook")
142+
.current_dir(path_to_workspace_root.to_str().unwrap())
143+
.arg("test")
144+
.status()
145+
.expect("Failed to execute mdbook test");
146+
147+
if !status.success() {
148+
let error_message = format!(
149+
"Command 'cargo xtask rust-tests' exited with status code: {}",
113150
status.code().unwrap()
114151
);
115152
return Err(anyhow!(error_message));
@@ -118,12 +155,42 @@ fn run_web_tests() -> Result<()> {
118155
Ok(())
119156
}
120157

121-
// TODO - https://github.com/google/comprehensive-rust/issues/2741: Replace this with Clap
122-
fn unrecognized_task_string(task: &str) -> String {
123-
format!(
124-
"Unrecognized task '{task}'. Available tasks:
158+
fn start_web_server() -> Result<()> {
159+
println!("Starting web server ...");
160+
let path_to_workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));
161+
162+
let status = Command::new("mdbook")
163+
.current_dir(path_to_workspace_root.to_str().unwrap())
164+
.arg("serve")
165+
.status()
166+
.expect("Failed to execute mdbook serve");
125167

126-
install-tools Installs the tools the project depends on.
127-
web-tests Runs the web driver tests in the tests directory."
128-
)
168+
if !status.success() {
169+
let error_message = format!(
170+
"Command 'cargo xtask serve' exited with status code: {}",
171+
status.code().unwrap()
172+
);
173+
return Err(anyhow!(error_message));
174+
}
175+
Ok(())
176+
}
177+
178+
fn build() -> Result<()> {
179+
println!("Building course...");
180+
let path_to_workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));
181+
182+
let status = Command::new("mdbook")
183+
.current_dir(path_to_workspace_root.to_str().unwrap())
184+
.arg("build")
185+
.status()
186+
.expect("Failed to execute mdbook build");
187+
188+
if !status.success() {
189+
let error_message = format!(
190+
"Command 'cargo xtask build' exited with status code: {}",
191+
status.code().unwrap()
192+
);
193+
return Err(anyhow!(error_message));
194+
}
195+
Ok(())
129196
}

0 commit comments

Comments
 (0)