use std::fs::File; use std::io::{Read, Write}; use std::path::Path; use std::process::Command; use walkdir::WalkDir; fn main() { if std::fs::metadata("codenano-gui").is_ok() { build() } if cfg!(not(debug_assertions)) { release() } else { debug() } } fn build() { for entry in WalkDir::new(Path::new("codenano-gui").join("src")) .into_iter() .chain(WalkDir::new(Path::new("codenano")).into_iter()) { let entry = entry.unwrap(); if entry.path().extension().and_then(|x| x.to_str()) == Some("rs") { println!("cargo:rerun-if-changed={}", entry.path().display()); } } println!("cargo:rerun-if-changed=codenano-gui/Cargo.toml"); let mut cmd = Command::new("wasm-pack"); cmd.arg("build").env("CARGO_TARGET_DIR", "target.wasm"); let status = cmd.current_dir("codenano-gui").status().unwrap(); assert!(status.success()); let status = Command::new("npm") .arg("install") .current_dir("www") .status() .unwrap(); assert!(status.success()); let status = Command::new("npm") .arg("run-script") .arg("build") .current_dir("www") .status() .unwrap(); assert!(status.success()); } fn debug() { let out_dir = std::env::var("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("static_files.rs"); let mut f = File::create(&dest_path).unwrap(); f.write_all(b"fn static_files() -> impl warp::Filter + Clone { warp::filters::fs::dir(\"www/dist\") }").unwrap(); f.write_all(b"fn index() -> impl warp::Filter + Clone { warp::filters::fs::file(\"www/dist/index.html\") }").unwrap(); f.write_all(b"fn index_ws() -> impl warp::Filter + Clone { warp::filters::fs::file(\"www/dist/index-ws.html\") }").unwrap(); } fn release() { // Generate the code to serve static files. let out_dir = std::env::var("OUT_DIR").unwrap(); std::fs::create_dir_all(&out_dir).unwrap(); let dest_path = Path::new(&out_dir).join("static_files.rs"); let mut index = Vec::new(); let mut f = File::create(&dest_path).unwrap(); f.write_all(b"fn static_files() -> impl warp::Filter,warp::http::Error>, )> + Clone {").unwrap(); let mut s = Vec::new(); let mut is_first = true; println!("cargo:rerun-if-changed=www"); for i in std::fs::read_dir("www").unwrap() { if let Ok(i) = i { if i.path() != Path::new("dist") { println!("cargo:rerun-if-changed={}", i.path().display()); } } } for i in std::fs::read_dir("www/dist").unwrap() { let i = i.unwrap(); if i.metadata().unwrap().is_dir() { continue; } let mut ii = File::open(i.path()).unwrap(); s.clear(); ii.read_to_end(&mut s).unwrap(); let mime: mime_guess::Mime = mime_guess::guess_mime_type(i.path()); let file_name = i.path(); let file_name = file_name.file_name().unwrap(); let mut make_index = |name| { writeln!( &mut index, "fn {}() -> impl warp::Filter,warp::http::Error>, )> + Clone {{", name ).unwrap(); writeln!( &mut index, " warp::path::end().map(|| warp::http::Response::builder() .header(\"Content-Type\", \"{}/{}\") .body(&{:?}[..])) ", mime.0.as_str(), mime.1.as_str(), byte_string::ByteStr::new(&s), ) .unwrap(); writeln!(&mut index, "}}").unwrap(); }; if file_name == "index-ws.html" { make_index("index_ws") } else if file_name == "index.html" { make_index("index") } else if is_first { writeln!( f, " warp::path({:?}).map(|| {{ warp::http::Response::builder() .header(\"Content-Type\", \"{}/{}\") .body(&{:?}[..]) }})", file_name, mime.0.as_str(), mime.1.as_str(), byte_string::ByteStr::new(&s), ) .unwrap(); is_first = false } else { writeln!( f, " .or(warp::path({:?}).map(|| {{ warp::http::Response::builder() .header(\"Content-Type\", \"{}/{}\") .body(&{:?}[..]) }})).unify()", file_name, mime.0.as_str(), mime.1.as_str(), byte_string::ByteStr::new(&s), ) .unwrap(); } } writeln!( f, " }}" ) .unwrap(); f.write_all(&index).unwrap(); }