Skip to content

Commit

Permalink
Adding in main bin to wasm-bindgen-webidl
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Feb 28, 2020
1 parent dd030b3 commit 6741e53
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 117 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ wasm-bindgen-test-crate-b = { path = 'tests/crates/b', version = '0.1' }
[workspace]
members = [
"benchmarks",
"bin/build-web-sys",
"crates/cli",
"crates/js-sys",
"crates/test",
Expand Down
13 changes: 0 additions & 13 deletions bin/build-web-sys/Cargo.toml

This file was deleted.

102 changes: 0 additions & 102 deletions bin/build-web-sys/src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/web-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ js-sys = { path = '../js-sys', version = '0.3.35' }
wasm-bindgen-test = { path = '../test', version = '0.3.8' }
wasm-bindgen-futures = { path = '../futures', version = '0.4.8' }

# This list is auto-generated by the build-web-sys script
# This list is auto-generated by the wasm-bindgen-webidl program
[features]
AbortController = []
AbortSignal = ["EventTarget"]
Expand Down
2 changes: 2 additions & 0 deletions crates/webidl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Support for parsing WebIDL specific to wasm-bindgen
edition = "2018"

[dependencies]
env_logger = { version = "0.7.0", optional = true }
anyhow = "1.0"
heck = "0.3"
log = "0.4.1"
Expand All @@ -22,3 +23,4 @@ syn = { version = '1.0', features = ['full'] }
wasm-bindgen-backend = { version = "=0.2.58", path = "../backend" }
weedle = "0.10"
lazy_static = "1.0.0"
sourcefile = "0.1"
108 changes: 108 additions & 0 deletions crates/webidl/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use anyhow::{Context, Result};
use sourcefile::SourceFile;
use std::ffi::OsStr;
use std::fs;
use std::path;
use std::process::Command;

fn unwrap_not_found(err: std::io::Result<()>) -> std::io::Result<()> {
match err {
Ok(()) => Ok(()),
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => Ok(()),
_ => Err(err),
},
}
}

fn main() -> Result<()> {
#[cfg(feature = "env_logger")]
env_logger::init();

match std::env::args().into_iter().collect::<Vec<_>>().as_slice() {
[ _, from, to ] => {
let from = path::Path::new(from);
let to = path::Path::new(to);

let entries = fs::read_dir(from.join("enabled")).context("reading enabled directory")?;

let mut source = SourceFile::default();

for entry in entries {
let entry = entry.context("getting enabled/*.webidl entry")?;
let path = entry.path();
if path.extension() != Some(OsStr::new("webidl")) {
continue;
}
source = source
.add_file(&path)
.with_context(|| format!("reading contents of file \"{}\"", path.display()))?;
}

let features = match wasm_bindgen_webidl::compile(&source.contents) {
Ok(features) => features,
Err(e) => {
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {
if let Some(pos) = source.resolve_offset(err.0) {
let ctx = format!(
"compiling WebIDL into wasm-bindgen bindings in file \
\"{}\", line {} column {}",
pos.filename,
pos.line + 1,
pos.col + 1
);
return Err(e.context(ctx));
} else {
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
}
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
};


unwrap_not_found(fs::remove_dir_all(&to)).context("Removing features directory")?;
fs::create_dir_all(&to).context("Creating features directory")?;


for (name, feature) in features.iter() {
let out_file_path = to.join(format!("gen_{}.rs", name));

fs::write(&out_file_path, &feature.code)?;

// run rustfmt on the generated file - really handy for debugging
let result = Command::new("rustfmt")
.arg("--edition")
.arg("2018")
.arg(&out_file_path)
.status()
.context(format!("rustfmt on file gen_{}.rs", name))?;

assert!(result.success(), "rustfmt on file gen_{}.rs", name);
}


let binding_file = features.keys().map(|name| {
format!("#[cfg(feature = \"{name}\")] mod gen_{name};\n#[cfg(feature = \"{name}\")] pub use gen_{name}::*;", name = name)
}).collect::<Vec<_>>().join("\n\n");

fs::write(to.join("mod.rs"), format!("#![allow(non_snake_case)]\n\n{}", binding_file))?;


let features = features.iter().map(|(name, feature)| {
let features = feature.required_features.iter()
.map(|x| format!("\"{}\"", x))
.collect::<Vec<_>>()
.join(", ");
format!("{} = [{}]", name, features)
}).collect::<Vec<_>>().join("\n");

fs::write(&"features", features).context("writing features to output file")?;
},
args => {
panic!("Need 2 arguments {:?}", args);
},
}

Ok(())
}

0 comments on commit 6741e53

Please sign in to comment.