2017-12-18 12:39:14 -08:00
|
|
|
extern crate wasm_bindgen_cli_support;
|
2017-12-14 19:31:01 -08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate docopt;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use docopt::Docopt;
|
2017-12-18 12:39:14 -08:00
|
|
|
use wasm_bindgen_cli_support::Bindgen;
|
2017-12-14 19:31:01 -08:00
|
|
|
|
|
|
|
const USAGE: &'static str = "
|
|
|
|
Generating JS bindings for a wasm file
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
wasm-bindgen [options] <input>
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h --help Show this screen.
|
2017-12-19 19:06:48 -08:00
|
|
|
--output-ts FILE Output TypeScript file
|
2017-12-14 19:31:01 -08:00
|
|
|
--output-wasm FILE Output WASM file
|
2017-12-14 21:55:21 -08:00
|
|
|
--nodejs Generate output for node.js, not the browser
|
2017-12-14 19:31:01 -08:00
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Args {
|
2017-12-19 19:06:48 -08:00
|
|
|
flag_output_ts: Option<PathBuf>,
|
2017-12-14 19:31:01 -08:00
|
|
|
flag_output_wasm: Option<PathBuf>,
|
2017-12-14 21:55:21 -08:00
|
|
|
flag_nodejs: bool,
|
2017-12-14 19:31:01 -08:00
|
|
|
arg_input: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args: Args = Docopt::new(USAGE)
|
|
|
|
.and_then(|d| d.deserialize())
|
|
|
|
.unwrap_or_else(|e| e.exit());
|
|
|
|
|
|
|
|
let mut b = Bindgen::new();
|
|
|
|
b.input_path(&args.arg_input);
|
2017-12-14 21:55:21 -08:00
|
|
|
b.nodejs(args.flag_nodejs);
|
2017-12-14 19:31:01 -08:00
|
|
|
let ret = b.generate().expect("failed to generate bindings");
|
2017-12-19 19:06:48 -08:00
|
|
|
if let Some(ref ts) = args.flag_output_ts {
|
|
|
|
ret.write_ts_to(ts).expect("failed to write TypeScript output file");
|
2017-12-14 21:55:21 -08:00
|
|
|
} else {
|
2017-12-19 19:06:48 -08:00
|
|
|
println!("{}", ret.generate_ts());
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
if let Some(ref wasm) = args.flag_output_wasm {
|
|
|
|
ret.write_wasm_to(wasm).expect("failed to write wasm output file");
|
|
|
|
}
|
|
|
|
}
|