Skip to content

Commit 0f0daec

Browse files
committed
add docs command
1 parent c511936 commit 0f0daec

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

crates/xtask/src/main.rs

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::*;
22
use clap::Parser;
33
use itertools::Itertools;
44
use log::*;
5-
use std::{collections::HashMap, ffi::OsStr, process::Command, str::FromStr};
5+
use std::{collections::HashMap, ffi::OsStr, path::Path, process::Command, str::FromStr};
66
use strum::VariantNames;
77

88
#[derive(
@@ -177,6 +177,8 @@ enum Xtasks {
177177
Build,
178178
/// Build the main workspace, apply all prefferred lints
179179
Check,
180+
/// Build the rust crates.io docs as well as any other docs
181+
Docs,
180182
/// Build the main workspace, and then run all tests
181183
Test,
182184
/// Perform a full check as it would be done in CI
@@ -188,6 +190,7 @@ impl Xtasks {
188190
match self {
189191
Xtasks::Build => Self::build(features),
190192
Xtasks::Check => Self::check(features),
193+
Xtasks::Docs => Self::docs(),
191194
Xtasks::Test => Self::test(features),
192195
Xtasks::CiCheck => Self::cicd(),
193196
Xtasks::Init => Self::init(),
@@ -240,6 +243,7 @@ impl Xtasks {
240243
context: &str,
241244
features: Features,
242245
add_args: I,
246+
dir: Option<&Path>,
243247
) -> Result<()> {
244248
info!("Running workspace command: {}", command);
245249

@@ -253,12 +257,17 @@ impl Xtasks {
253257
.expect("invalid command argument")
254258
.to_owned()
255259
}));
260+
let workspace_dir = Self::workspace_dir()?;
261+
let workspace_dir = match dir {
262+
Some(d) => workspace_dir.join(d),
263+
None => workspace_dir,
264+
};
256265

257266
let mut cmd = Command::new("cargo");
258267
cmd.args(args)
259268
.stdout(std::process::Stdio::inherit())
260269
.stderr(std::process::Stdio::inherit())
261-
.current_dir(Self::workspace_dir()?);
270+
.current_dir(workspace_dir);
262271

263272
info!("Using command: {:?}", cmd);
264273

@@ -280,6 +289,7 @@ impl Xtasks {
280289
"Failed to build workspace",
281290
features,
282291
vec!["--all-targets"],
292+
None,
283293
)?;
284294
Ok(())
285295
}
@@ -291,6 +301,7 @@ impl Xtasks {
291301
"Failed to run clippy",
292302
features,
293303
vec!["--all-targets", "--", "-D", "warnings"],
304+
None,
294305
)?;
295306

296307
// run cargo fmt checks
@@ -303,21 +314,64 @@ impl Xtasks {
303314
Ok(())
304315
}
305316

317+
fn docs() -> Result<()> {
318+
// find [package.metadata."docs.rs"] key in Cargo.toml
319+
let metadata = Self::cargo_metadata()?;
320+
let package = metadata.root_package().expect("no root package");
321+
let docs_rs = package
322+
.metadata
323+
.get("docs.rs")
324+
.expect("no docs.rs metadata");
325+
326+
let string_list = docs_rs
327+
.as_array()
328+
.expect("docs.rs metadata is not an array")
329+
.iter()
330+
.map(|v| v.as_str().expect("docs.rs metadata is not a string"))
331+
.map(|s| Feature::from_str(s).expect("invalid feature"))
332+
.collect::<Vec<_>>();
333+
334+
let features = Features(string_list);
335+
336+
Self::run_workspace_command(
337+
"doc",
338+
"Failed to build crates.io docs",
339+
features.clone(),
340+
vec!["--all"],
341+
None,
342+
)?;
343+
344+
// build mdbook
345+
Self::run_workspace_command(
346+
"mdbook",
347+
"Failed to build mdbook docs",
348+
features,
349+
vec!["--all"],
350+
Some(Path::new("docs")),
351+
)?;
352+
Ok(())
353+
}
354+
306355
fn test(features: Features) -> Result<()> {
307356
// run cargo test with instrumentation
308357
std::env::set_var("CARGO_INCREMENTAL", "0");
309358
std::env::set_var("RUSTFLAGS", "-Cinstrument-coverage");
310359
let target_dir = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_owned());
311-
let coverage_file = std::path::PathBuf::from(target_dir)
312-
.join("coverage")
313-
.join("cargo-test-%p-%m.profraw");
360+
let coverage_dir = std::path::PathBuf::from(target_dir).join("coverage");
361+
let coverage_file = coverage_dir.join("cargo-test-%p-%m.profraw");
362+
363+
// clear coverage directory
364+
assert!(coverage_dir != std::path::Path::new("/"));
365+
std::fs::remove_dir_all(coverage_dir)?;
366+
314367
std::env::set_var("LLVM_PROFILE_FILE", coverage_file);
315368

316369
Self::run_workspace_command(
317370
"test",
318371
"Failed to run tests",
319372
features,
320373
vec!["--exclude", "xtask"],
374+
None,
321375
)?;
322376

323377
// generate coverage report and lcov file

0 commit comments

Comments
 (0)