From cac0501eec9c46a4e37de2580c2e5ccf29533d39 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 8 Aug 2017 16:13:15 +0300 Subject: [PATCH] build utility --- Cargo.toml | 1 + build/.gitignore | 2 ++ build/Cargo.toml | 7 +++++++ build/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 build/.gitignore create mode 100644 build/Cargo.toml create mode 100644 build/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index fe2dab9..ea0aa50 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" +wasm-build = { path = "build" } [lib] 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..5eba5e9 --- /dev/null +++ b/build/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "wasm-build" +version = "0.1.0" +authors = ["NikVolf "] + +[dependencies] +glob = "*" \ No newline at end of file diff --git a/build/src/lib.rs b/build/src/lib.rs new file mode 100644 index 0000000..464c6cd --- /dev/null +++ b/build/src/lib.rs @@ -0,0 +1,44 @@ +//! Tools library for building contracts via cargo + +extern crate glob; + +use std::{env, fs, io}; +use std::path::PathBuf; + +#[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(bin_name: &str) -> Result<(), Error> { + let out_dir = env::var("OUT").map_err(|_| Error::NoEnvVar)?; + let mut path = PathBuf::from(out_dir.clone()); + path.push("deps"); + path.push(format!("{}-*.wasm", bin_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"); + fs::copy(file, out_dir)?; + } + + Ok(()) +} \ No newline at end of file