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//! | [`run-emulator`](tasks::run_emulator) | Run the Cadmus emulator |
26//! | [`install-importer`](tasks::install_importer) | Install the Cadmus importer crate |
27//! | [`docs`](tasks::docs) | Build the full documentation portal |
28//! | [`download-assets`](tasks::download_assets) | Download static asset dirs from the latest release |
29//! | [`dist`](tasks::dist) | Assemble the Kobo distribution directory |
30//! | [`bundle`](tasks::bundle) | Package a `KoboRoot.tgz` ready for device installation |
31//! | [`setup`](tasks::setup) | Build thirdparty deps (SQLite) needed before `cargo build` |
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, setup::SetupArgs,
51    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    /// Run the Cadmus emulator.
77    RunEmulator(RunEmulatorArgs),
78    /// Install the Cadmus importer crate.
79    InstallImporter(InstallImporterArgs),
80    /// Build the full documentation portal (mdBook + cargo doc + Zola).
81    Docs(DocsArgs),
82    /// Download static asset directories from the latest GitHub release.
83    DownloadAssets,
84    /// Assemble the Kobo distribution directory from build outputs.
85    Dist(DistArgs),
86    /// Package a KoboRoot.tgz ready for device installation.
87    Bundle(BundleArgs),
88    /// Build thirdparty deps (SQLite) that must exist before `cargo build`.
89    Setup(SetupArgs),
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::RunEmulator(args) => tasks::run_emulator::run(args),
108        Command::InstallImporter(args) => tasks::install_importer::run(args),
109        Command::Docs(args) => tasks::docs::run(args),
110        Command::DownloadAssets => tasks::download_assets::run(),
111        Command::Dist(args) => tasks::dist::run(args),
112        Command::Bundle(args) => tasks::bundle::run(args),
113        Command::Setup(args) => tasks::setup::run(args),
114        Command::Ci(args) => tasks::ci::run(args),
115    }
116}