aboutsummaryrefslogtreecommitdiff
path: root/website/website-gen/src/main.rs
blob: 7d35a37cf3bf4b80e73428d6f28c689dd6008b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::{fs, path::Path, process::Command};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

/// This tool builds the github-pages website to the `./target/website` folder
fn main() {
    if let Err(err) = try_main() {
        eprintln!("{}", err);
        std::process::exit(-1);
    }
}

fn try_main() -> Result<()> {
    check_cwd()?;
    build_scaffold()?;
    build_docs()?;
    println!("Finished\n./target/website/index.html");
    Ok(())
}

fn cargo() -> Command {
    Command::new("cargo")
}

fn check_cwd() -> Result<()> {
    let toml = std::fs::read_to_string("./Cargo.toml")?;
    if !toml.contains("[workspace]") {
        Err("website-gen should be run from the root of workspace")?;
    }
    Ok(())
}

fn build_docs() -> Result<()> {
    let status = cargo().args(&["doc", "--all", "--no-deps"]).status()?;
    if !status.success() {
        Err("cargo doc failed")?;
    }
    sync_dir("./target/doc", "./target/website/api-docs")?;
    Ok(())
}

fn build_scaffold() -> Result<()> {
    sync_dir("./website/src", "./target/website")
}

fn sync_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
    return sync_dir(src.as_ref(), dst.as_ref());

    fn sync_dir(src: &Path, dst: &Path) -> Result<()> {
        let _ = fs::remove_dir_all(dst);
        fs::create_dir_all(dst)?;
        for entry in walkdir::WalkDir::new(src) {
            let entry = entry?;
            let src_path = entry.path();
            let dst_path = dst.join(src_path.strip_prefix(src)?);
            if src_path.is_dir() {
                fs::create_dir_all(dst_path)?;
            } else {
                fs::copy(src_path, dst_path)?;
            }
        }
        Ok(())
    }
}