|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use sourcefile::SourceFile; |
| 3 | +use std::ffi::OsStr; |
| 4 | +use std::fs; |
| 5 | +use std::path; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +fn unwrap_not_found(err: std::io::Result<()>) -> std::io::Result<()> { |
| 9 | + match err { |
| 10 | + Ok(()) => Ok(()), |
| 11 | + Err(err) => match err.kind() { |
| 12 | + std::io::ErrorKind::NotFound => Ok(()), |
| 13 | + _ => Err(err), |
| 14 | + }, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +fn main() -> Result<()> { |
| 19 | + #[cfg(feature = "env_logger")] |
| 20 | + env_logger::init(); |
| 21 | + |
| 22 | + match std::env::args().into_iter().collect::<Vec<_>>().as_slice() { |
| 23 | + [ _, from, to ] => { |
| 24 | + let from = path::Path::new(from); |
| 25 | + let to = path::Path::new(to); |
| 26 | + |
| 27 | + let entries = fs::read_dir(from.join("enabled")).context("reading enabled directory")?; |
| 28 | + |
| 29 | + let mut source = SourceFile::default(); |
| 30 | + |
| 31 | + for entry in entries { |
| 32 | + let entry = entry.context("getting enabled/*.webidl entry")?; |
| 33 | + let path = entry.path(); |
| 34 | + if path.extension() != Some(OsStr::new("webidl")) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + source = source |
| 38 | + .add_file(&path) |
| 39 | + .with_context(|| format!("reading contents of file \"{}\"", path.display()))?; |
| 40 | + } |
| 41 | + |
| 42 | + let features = match wasm_bindgen_webidl::compile(&source.contents) { |
| 43 | + Ok(features) => features, |
| 44 | + Err(e) => { |
| 45 | + if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() { |
| 46 | + if let Some(pos) = source.resolve_offset(err.0) { |
| 47 | + let ctx = format!( |
| 48 | + "compiling WebIDL into wasm-bindgen bindings in file \ |
| 49 | + \"{}\", line {} column {}", |
| 50 | + pos.filename, |
| 51 | + pos.line + 1, |
| 52 | + pos.col + 1 |
| 53 | + ); |
| 54 | + return Err(e.context(ctx)); |
| 55 | + } else { |
| 56 | + return Err(e.context("compiling WebIDL into wasm-bindgen bindings")); |
| 57 | + } |
| 58 | + } |
| 59 | + return Err(e.context("compiling WebIDL into wasm-bindgen bindings")); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + |
| 64 | + unwrap_not_found(fs::remove_dir_all(&to)).context("Removing features directory")?; |
| 65 | + fs::create_dir_all(&to).context("Creating features directory")?; |
| 66 | + |
| 67 | + |
| 68 | + for (name, feature) in features.iter() { |
| 69 | + let out_file_path = to.join(format!("gen_{}.rs", name)); |
| 70 | + |
| 71 | + fs::write(&out_file_path, &feature.code)?; |
| 72 | + |
| 73 | + // run rustfmt on the generated file - really handy for debugging |
| 74 | + let result = Command::new("rustfmt") |
| 75 | + .arg("--edition") |
| 76 | + .arg("2018") |
| 77 | + .arg(&out_file_path) |
| 78 | + .status() |
| 79 | + .context(format!("rustfmt on file gen_{}.rs", name))?; |
| 80 | + |
| 81 | + assert!(result.success(), "rustfmt on file gen_{}.rs", name); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + let binding_file = features.keys().map(|name| { |
| 86 | + format!("#[cfg(feature = \"{name}\")] mod gen_{name};\n#[cfg(feature = \"{name}\")] pub use gen_{name}::*;", name = name) |
| 87 | + }).collect::<Vec<_>>().join("\n\n"); |
| 88 | + |
| 89 | + fs::write(to.join("mod.rs"), format!("#![allow(non_snake_case)]\n\n{}", binding_file))?; |
| 90 | + |
| 91 | + |
| 92 | + let features = features.iter().map(|(name, feature)| { |
| 93 | + let features = feature.required_features.iter() |
| 94 | + .map(|x| format!("\"{}\"", x)) |
| 95 | + .collect::<Vec<_>>() |
| 96 | + .join(", "); |
| 97 | + format!("{} = [{}]", name, features) |
| 98 | + }).collect::<Vec<_>>().join("\n"); |
| 99 | + |
| 100 | + fs::write(&"features", features).context("writing features to output file")?; |
| 101 | + }, |
| 102 | + args => { |
| 103 | + panic!("Need 2 arguments {:?}", args); |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
0 commit comments