Skip to main content

xtask_lib/
lib.rs

1//! # xtask — Cadmus build automation
2//!
3//! Centralises every build, test, lint, documentation, and release task for
4//! the Cadmus project as typed, testable Rust code that behaves identically
5//! in a local devenv shell and in GitHub Actions CI.
6//!
7//! ## Usage
8//!
9//! ```text
10//! cargo xtask <COMMAND> [OPTIONS]
11//! ```
12//!
13//! The `xtask` alias is configured in `.cargo/config.toml` so that
14//! `cargo xtask` works from any directory inside the workspace.
15//!
16//! ## Commands
17//!
18//! | Command | Description |
19//! |---------|-------------|
20//! | [`fmt`](tasks::fmt) | Check (or apply) `rustfmt` formatting |
21//! | [`clippy`](tasks::clippy) | Run `cargo clippy` across the feature matrix |
22//! | [`test`](tasks::test) | Run `cargo test` across the feature matrix |
23//! | [`bench`](tasks::bench) | Run benchmarks with the `bench` feature enabled |
24//! | [`build-kobo`](tasks::build_kobo) | Cross-compile for Kobo (ARM, Linux & macOS) |
25//! | [`setup-native`](tasks::setup_native) | Build MuPDF and the C wrapper for native dev |
26//! | [`run-emulator`](tasks::run_emulator) | Run the Cadmus emulator (ensures prereqs are built) |
27//! | [`install-importer`](tasks::install_importer) | Install the Cadmus importer crate |
28//! | [`docs`](tasks::docs) | Build the full documentation portal |
29//! | [`download-assets`](tasks::download_assets) | Download static asset dirs from the latest release |
30//! | [`dist`](tasks::dist) | Assemble the Kobo distribution directory |
31//! | [`bundle`](tasks::bundle) | Package a `KoboRoot.tgz` ready for device installation |
32//! | [`ci`](tasks::ci) | CI-specific setup tasks (e.g. `install-doc-tools`) |
33//!
34//! ## Design
35//!
36//! Each command lives in its own module under [`tasks`].  The `tasks::util::cmd`
37//! helper wraps [`std::process::Command`] with consistent error reporting so
38//! every task fails fast with a clear message.  The `tasks::util::http` module
39//! provides pure-Rust download and archive helpers replacing `curl`, `wget`,
40//! `tar`, and `sha256sum` subprocess calls.
41
42pub mod tasks;
43
44pub use anyhow::Result;
45pub use clap::Parser;
46
47pub use tasks::{
48    bench::BenchArgs, build_kobo::BuildKoboArgs, bundle::BundleArgs, ci::CiArgs,
49    clippy::ClippyArgs, dist::DistArgs, docs::DocsArgs, fmt::FmtArgs,
50    install_importer::InstallImporterArgs, run_emulator::RunEmulatorArgs,
51    setup_native::SetupNativeArgs, test::TestArgs,
52};
53
54/// Cadmus build automation.
55///
56/// Run `cargo xtask <COMMAND> --help` for per-command options.
57#[derive(Debug, Parser)]
58#[command(name = "xtask", about = "Cadmus build automation")]
59pub struct Cli {
60    #[command(subcommand)]
61    pub command: Command,
62}
63
64#[derive(Debug, clap::Subcommand)]
65pub enum Command {
66    /// Check (or apply) rustfmt formatting across the workspace.
67    Fmt(FmtArgs),
68    /// Run cargo clippy across the full feature matrix.
69    Clippy(ClippyArgs),
70    /// Run cargo test across the full feature matrix.
71    Test(TestArgs),
72    /// Run benchmarks with the bench feature enabled.
73    Bench(BenchArgs),
74    /// Cross-compile Cadmus for Kobo devices (Linux & macOS).
75    BuildKobo(BuildKoboArgs),
76    /// Build MuPDF and the C wrapper for native development.
77    SetupNative(SetupNativeArgs),
78    /// Run the Cadmus emulator (ensures MuPDF and wrapper are built first).
79    RunEmulator(RunEmulatorArgs),
80    /// Install the Cadmus importer crate (ensures MuPDF and wrapper are built first).
81    InstallImporter(InstallImporterArgs),
82    /// Build the full documentation portal (mdBook + cargo doc + Zola).
83    Docs(DocsArgs),
84    /// Download static asset directories from the latest GitHub release.
85    DownloadAssets,
86    /// Assemble the Kobo distribution directory from build outputs.
87    Dist(DistArgs),
88    /// Package a KoboRoot.tgz ready for device installation.
89    Bundle(BundleArgs),
90    /// CI-specific setup tasks (install-doc-tools, etc.).
91    Ci(CiArgs),
92}
93
94/// Run the xtask CLI with the given arguments.
95///
96/// This is the main entry point for the binary, but can also be called
97/// from tests.
98pub fn run() -> Result<()> {
99    let cli = Cli::parse();
100
101    match cli.command {
102        Command::Fmt(args) => tasks::fmt::run(args),
103        Command::Clippy(args) => tasks::clippy::run(args),
104        Command::Test(args) => tasks::test::run(args),
105        Command::Bench(args) => tasks::bench::run(args),
106        Command::BuildKobo(args) => tasks::build_kobo::run(args),
107        Command::SetupNative(args) => tasks::setup_native::run(args),
108        Command::RunEmulator(args) => tasks::run_emulator::run(args),
109        Command::InstallImporter(args) => tasks::install_importer::run(args),
110        Command::Docs(args) => tasks::docs::run(args),
111        Command::DownloadAssets => tasks::download_assets::run(),
112        Command::Dist(args) => tasks::dist::run(args),
113        Command::Bundle(args) => tasks::bundle::run(args),
114        Command::Ci(args) => tasks::ci::run(args),
115    }
116}