From 798dc2ca8024c68b02c8e878603c0a4b00dc44e5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 14:42:23 +0300 Subject: start GitHub pages --- website/website-gen/src/main.rs | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 website/website-gen/src/main.rs (limited to 'website/website-gen/src') diff --git a/website/website-gen/src/main.rs b/website/website-gen/src/main.rs new file mode 100644 index 000000000..7d35a37cf --- /dev/null +++ b/website/website-gen/src/main.rs @@ -0,0 +1,64 @@ +use std::{fs, path::Path, process::Command}; + +type Result = std::result::Result>; + +/// 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, dst: impl AsRef) -> 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(()) + } +} -- cgit v1.2.3