diff --git a/Cargo.toml b/Cargo.toml index 036c91b6788d..acb18a278b25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/bin/build-web-sys/Cargo.toml b/bin/build-web-sys/Cargo.toml deleted file mode 100644 index b8e2a7416156..000000000000 --- a/bin/build-web-sys/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "build-web-sys" -version = "0.1.0" -authors = ["The wasm-bindgen Developers"] -license = "MIT/Apache-2.0" -categories = ["wasm"] -edition = "2018" - -[dependencies] -env_logger = { version = "0.7.0", optional = true } -anyhow = "1.0" -sourcefile = "0.1" -wasm-bindgen-webidl = { path = "../../crates/webidl" } diff --git a/bin/build-web-sys/src/main.rs b/bin/build-web-sys/src/main.rs deleted file mode 100644 index 2881ddd7d820..000000000000 --- a/bin/build-web-sys/src/main.rs +++ /dev/null @@ -1,102 +0,0 @@ -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(); - - let web_sys_dir = path::Path::new("../../crates/web-sys"); - - let entries = fs::read_dir(web_sys_dir.join("webidls/enabled")).context("reading webidls/enabled directory")?; - - let mut source = SourceFile::default(); - - for entry in entries { - let entry = entry.context("getting webidls/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::() { - 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")); - } - }; - - - let binding_dir = web_sys_dir.join("src/features"); - - unwrap_not_found(fs::remove_dir_all(&binding_dir)).context("Removing features directory")?; - fs::create_dir_all(&binding_dir).context("Creating features directory")?; - - - for (name, feature) in features.iter() { - let out_file_path = binding_dir.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::>().join("\n\n"); - - fs::write(binding_dir.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::>() - .join(", "); - format!("{} = [{}]", name, features) - }).collect::>().join("\n"); - - fs::write(&web_sys_dir.join("features"), features).context("writing features to output file")?; - - Ok(()) -} diff --git a/crates/web-sys/Cargo.toml b/crates/web-sys/Cargo.toml index e823deb56ec2..67f7f366179b 100644 --- a/crates/web-sys/Cargo.toml +++ b/crates/web-sys/Cargo.toml @@ -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"] diff --git a/crates/webidl/Cargo.toml b/crates/webidl/Cargo.toml index abe0e287a66e..824da8c10c5b 100644 --- a/crates/webidl/Cargo.toml +++ b/crates/webidl/Cargo.toml @@ -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" @@ -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" diff --git a/crates/webidl/src/main.rs b/crates/webidl/src/main.rs new file mode 100644 index 000000000000..5a14b359d286 --- /dev/null +++ b/crates/webidl/src/main.rs @@ -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::>().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::() { + 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::>().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::>() + .join(", "); + format!("{} = [{}]", name, features) + }).collect::>().join("\n"); + + fs::write(&"features", features).context("writing features to output file")?; + }, + args => { + panic!("Need 2 arguments {:?}", args); + }, + } + + Ok(()) +}