diff --git a/Cargo.toml b/Cargo.toml index fe2dab9..710d2fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ log = "0.3" env_logger = "0.4" lazy_static = "0.2" clap = "2.24" +glob = "0.2" [lib] @@ -26,4 +27,8 @@ path = "gas/src/main.rs" [[bin]] name = "wasm-pack" -path = "pack/src/main.rs" \ No newline at end of file +path = "pack/src/main.rs" + +[[bin]] +name = "wasm-build" +path = "build/src/main.rs" \ No newline at end of file diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..f2f9e58 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock \ No newline at end of file diff --git a/build/Cargo.toml b/build/Cargo.toml new file mode 100644 index 0000000..b97b031 --- /dev/null +++ b/build/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "wasm-build" +version = "0.1.0" +authors = ["NikVolf "] + +[dependencies] +glob = "0.2" +wasm-utils = { path = "../" } +clap = "2.24" + +[[bin]] +name = "wasm-build" \ No newline at end of file diff --git a/build/src/main.rs b/build/src/main.rs new file mode 100644 index 0000000..baf1729 --- /dev/null +++ b/build/src/main.rs @@ -0,0 +1,72 @@ +//! Experimental build tool for cargo + +extern crate glob; +extern crate wasm_utils; +extern crate clap; + +use std::{fs, io}; +use std::path::PathBuf; + +use clap::{App, Arg}; + +#[derive(Debug)] +pub enum Error { + Io(io::Error), + NoSuitableFile(String), + TooManyFiles(String), + NoEnvVar, +} + +impl From for Error { + fn from(err: io::Error) -> Self { + Error::Io(err) + } +} + +pub fn process_output(target_dir: &str, bin_name: &str) -> Result<(), Error> { + let mut path = PathBuf::from(target_dir); + let wasm_name = bin_name.to_string().replace("-", "_"); + path.push("wasm32-unknown-emscripten"); + path.push("release"); + path.push("deps"); + path.push(format!("{}-*.wasm", wasm_name)); + + let mut files = glob::glob(path.to_string_lossy().as_ref()).expect("glob err") + .collect::>>(); + + if files.len() == 0 { + return Err(Error::NoSuitableFile(path.to_string_lossy().to_string())); + } else if files.len() > 1 { + return Err(Error::TooManyFiles( + files.into_iter().map(|f| f.expect("glob err").to_string_lossy().to_string()) + .fold(String::new(), |mut a, b| { a.push_str(", "); a.push_str(&b); a }) + )) + } else { + let file = files.drain(..).nth(0).expect("0th element exists").expect("glob err"); + let mut path = PathBuf::from(target_dir); + path.push(format!("{}.wasm", bin_name)); + fs::copy(file, path)?; + } + + Ok(()) +} + +fn main() { + wasm_utils::init_log(); + + let matches = App::new("wasm-opt") + .arg(Arg::with_name("target") + .index(1) + .required(true) + .help("Cargo target directory")) + .arg(Arg::with_name("wasm") + .index(2) + .required(true) + .help("Wasm binary name")) + .get_matches(); + + let target_dir = matches.value_of("target").expect("is required; qed"); + let wasm_binary = matches.value_of("wasm").expect("is required; qed"); + + process_output(target_dir, wasm_binary).expect("Failed to process cargo target directory"); +} \ No newline at end of file