|
| 1 | +//! ```text |
| 2 | +//! NAME |
| 3 | +//! build-man |
| 4 | +//! |
| 5 | +//! SYNOPSIS |
| 6 | +//! build-man |
| 7 | +//! |
| 8 | +//! DESCRIPTION |
| 9 | +//! Build the man pages for packages `mdman` and `cargo`. |
| 10 | +//! For more, read their doc comments. |
| 11 | +//! ``` |
| 12 | +
|
| 13 | +use std::fs; |
| 14 | +use std::io; |
| 15 | +use std::path::PathBuf; |
| 16 | +use std::process; |
| 17 | +use std::process::Command; |
| 18 | + |
| 19 | +fn main() -> io::Result<()> { |
| 20 | + build_mdman()?; |
| 21 | + build_cargo()?; |
| 22 | + Ok(()) |
| 23 | +} |
| 24 | + |
| 25 | +/// Builds the man pages for `mdman`. |
| 26 | +fn build_mdman() -> io::Result<()> { |
| 27 | + cwd_to_workspace_root()?; |
| 28 | + |
| 29 | + let src_paths = &["crates/mdman/doc/mdman.md".into()]; |
| 30 | + let dst_path = "crates/mdman/doc/out"; |
| 31 | + let outs = [("md", dst_path), ("txt", dst_path), ("man", dst_path)]; |
| 32 | + |
| 33 | + build_man("mdman", src_paths, &outs, &[]) |
| 34 | +} |
| 35 | + |
| 36 | +/// Builds the man pages for Cargo. |
| 37 | +/// |
| 38 | +/// The source for the man pages are located in src/doc/man/ in markdown format. |
| 39 | +/// These also are handlebars templates, see crates/mdman/README.md for details. |
| 40 | +/// |
| 41 | +/// The generated man pages are placed in the src/etc/man/ directory. The pages |
| 42 | +/// are also expanded into markdown (after being expanded by handlebars) and |
| 43 | +/// saved in the src/doc/src/commands/ directory. These are included in the |
| 44 | +/// Cargo book, which is converted to HTML by mdbook. |
| 45 | +fn build_cargo() -> io::Result<()> { |
| 46 | + // Find all `src/doc/man/cargo-*.md` |
| 47 | + let src_paths = { |
| 48 | + let mut src_paths = Vec::new(); |
| 49 | + for entry in fs::read_dir("src/doc/man")? { |
| 50 | + let entry = entry?; |
| 51 | + let file_name = entry.file_name(); |
| 52 | + let file_name = file_name.to_str().unwrap(); |
| 53 | + if file_name.starts_with("cargo-") && file_name.ends_with(".md") { |
| 54 | + src_paths.push(entry.path()); |
| 55 | + } |
| 56 | + } |
| 57 | + src_paths |
| 58 | + }; |
| 59 | + let outs = [ |
| 60 | + ("md", "src/doc/src/commands"), |
| 61 | + ("txt", "src/doc/man/generated_txt"), |
| 62 | + ("man", "src/etc/man"), |
| 63 | + ]; |
| 64 | + let args = [ |
| 65 | + "--url", |
| 66 | + "https://doc.rust-lang.org/cargo/commands/", |
| 67 | + "--man", |
| 68 | + "rustc:1=https://doc.rust-lang.org/rustc/index.html", |
| 69 | + "--man", |
| 70 | + "rustdoc:1=https://doc.rust-lang.org/rustdoc/index.html", |
| 71 | + ]; |
| 72 | + build_man("cargo", &src_paths[..], &outs, &args) |
| 73 | +} |
| 74 | + |
| 75 | +/// Change to workspace root. |
| 76 | +/// |
| 77 | +/// Assumed this xtask is located in `[WORKSPACE]/crates/xtask-build-man`. |
| 78 | +fn cwd_to_workspace_root() -> io::Result<()> { |
| 79 | + let pkg_root = std::env!("CARGO_MANIFEST_DIR"); |
| 80 | + let ws_root = format!("{pkg_root}/../.."); |
| 81 | + std::env::set_current_dir(ws_root) |
| 82 | +} |
| 83 | + |
| 84 | +/// Builds the man pages. |
| 85 | +fn build_man( |
| 86 | + pkg_name: &str, |
| 87 | + src_paths: &[PathBuf], |
| 88 | + outs: &[(&str, &str)], |
| 89 | + extra_args: &[&str], |
| 90 | +) -> io::Result<()> { |
| 91 | + for (format, dst_path) in outs { |
| 92 | + eprintln!("Start converting `{format}` for package `{pkg_name}`..."); |
| 93 | + let mut cmd = Command::new(std::env!("CARGO")); |
| 94 | + cmd.args(["run", "--package", "mdman", "--"]) |
| 95 | + .args(["-t", format, "-o", dst_path]) |
| 96 | + .args(src_paths) |
| 97 | + .args(extra_args); |
| 98 | + |
| 99 | + let status = cmd.status()?; |
| 100 | + if !status.success() { |
| 101 | + eprintln!("failed to build the man pages for package `{pkg_name}`"); |
| 102 | + eprintln!("failed command: `{cmd:?}`"); |
| 103 | + process::exit(status.code().unwrap_or(1)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
0 commit comments