Skip to main content

xtask/
main.rs

1mod serve;
2mod web;
3
4use std::path::{Path, PathBuf};
5use std::process::ExitCode;
6
7const USAGE: &str = "usage:
8  cargo xtask web [--bin <name>] [--package <name>] [--release] [--public-url <path>] [--features <list>]
9                  [--manifest-path <Cargo.toml>] [--out-dir <dir>]
10    with --manifest-path, --release needs that workspace to declare [profile.wasm-release]
11    (copy it from loam's root Cargo.toml)
12  cargo xtask serve [--port <port>]";
13
14fn main() -> ExitCode {
15    let args: Vec<String> = std::env::args().skip(1).collect();
16    let result = match args.first().map(String::as_str) {
17        Some("web") => web::parse(&args[1..]).and_then(|opts| web::run(&opts)),
18        Some("serve") => parse_port(&args[1..]).and_then(|port| {
19            let root = workspace_root()?;
20            serve::run(&root.join("dist"), port)
21        }),
22        _ => Err(USAGE.to_string()),
23    };
24    match result {
25        Ok(()) => ExitCode::SUCCESS,
26        Err(message) => {
27            eprintln!("{message}");
28            ExitCode::FAILURE
29        }
30    }
31}
32
33fn workspace_root() -> Result<PathBuf, String> {
34    Path::new(env!("CARGO_MANIFEST_DIR"))
35        .ancestors()
36        .nth(2)
37        .map(Path::to_path_buf)
38        .ok_or_else(|| "crates/xtask has no workspace root".to_string())
39}
40
41fn parse_port(args: &[String]) -> Result<u16, String> {
42    match args {
43        [] => Ok(8081),
44        [flag, port] if flag == "--port" => port.parse().map_err(|e| format!("--port: {e}")),
45        _ => Err(USAGE.to_string()),
46    }
47}