Browse Source

wiggle: witx paths use shell expansion

instead of always being relative to CARGO_MANIFEST_DIR, each use site is
responsible for either putting that variable or another one (set by a
build.rs) at the start of witx paths.
pull/2169/head
Pat Hickey 4 years ago
parent
commit
91dac9c7e8
  1. 10
      Cargo.lock
  2. 1
      crates/wiggle/generate/Cargo.toml
  3. 56
      crates/wiggle/generate/src/config.rs
  4. 5
      crates/wiggle/macro/src/lib.rs
  5. 5
      crates/wiggle/wasmtime/macro/src/lib.rs

10
Cargo.lock

@ -1905,6 +1905,15 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shellexpand"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2b22262a9aaf9464d356f656fea420634f78c881c5eebd5ef5e66d8b9bc603"
dependencies = [
"dirs",
]
[[package]]
name = "smallvec"
version = "1.4.1"
@ -2700,6 +2709,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"shellexpand",
"syn",
"witx",
]

1
crates/wiggle/generate/Cargo.toml

@ -20,6 +20,7 @@ proc-macro2 = "1.0"
heck = "0.3"
anyhow = "1"
syn = { version = "1.0", features = ["full"] }
shellexpand = "2.0"
[badges]
maintenance = { status = "actively-developed" }

56
crates/wiggle/generate/src/config.rs

@ -1,10 +1,6 @@
use {
proc_macro2::Span,
std::{
collections::HashMap,
iter::FromIterator,
path::{Path, PathBuf},
},
std::{collections::HashMap, iter::FromIterator, path::PathBuf},
syn::{
braced, bracketed,
parse::{Parse, ParseStream},
@ -143,20 +139,6 @@ impl WitxConf {
Self::Literal(doc) => witx::parse(doc.as_ref()).expect("parsing witx"),
}
}
/// If using the [`Paths`][paths] syntax, make all paths relative to a root directory.
///
/// [paths]: enum.WitxConf.html#variant.Paths
// FIXME this can be deleted once we have env var interpolation
pub fn make_paths_relative_to<P: AsRef<Path>>(&mut self, root: P) {
if let Self::Paths(paths) = self {
paths.as_mut().iter_mut().for_each(|p| {
if !p.is_absolute() {
*p = PathBuf::from(root.as_ref()).join(p.clone());
}
});
}
}
}
/// A collection of paths, pointing to witx documents.
@ -203,32 +185,18 @@ impl Parse for Paths {
let _ = bracketed!(content in input);
let path_lits: Punctuated<LitStr, Token![,]> = content.parse_terminated(Parse::parse)?;
/* TODO interpolate env variables here!!!
fn main() {
let p = "foo/$BAR/baz";
let buf = PathBuf::from(p);
let components = buf
.iter()
.map(|osstr| interpolate(osstr.to_str().expect("always a str")))
.collect::<Vec<String>>();
println!("{:?}", components);
}
fn interpolate(v: &str) -> String {
if let Some('$') = v.chars().nth(0) {
let var = &v[1..];
std::env::var(var).unwrap_or(String::new())
} else {
v.to_owned()
}
}
*/
Ok(path_lits
let expanded_paths = path_lits
.iter()
.map(|lit| PathBuf::from(lit.value()))
.collect())
.map(|lit| {
PathBuf::from(
shellexpand::env(&lit.value())
.expect("shell expansion")
.as_ref(),
)
})
.collect::<Vec<PathBuf>>();
Ok(Paths(expanded_paths))
}
}

5
crates/wiggle/macro/src/lib.rs

@ -90,10 +90,7 @@ use syn::parse_macro_input;
/// ```
#[proc_macro]
pub fn from_witx(args: TokenStream) -> TokenStream {
let mut config = parse_macro_input!(args as wiggle_generate::Config);
config.witx.make_paths_relative_to(
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var"),
);
let config = parse_macro_input!(args as wiggle_generate::Config);
let doc = config.load_document();
let names = wiggle_generate::Names::new(&config.ctx.name, quote!(wiggle));

5
crates/wiggle/wasmtime/macro/src/lib.rs

@ -47,10 +47,7 @@ use config::{MissingMemoryConf, ModuleConf, TargetConf};
///
#[proc_macro]
pub fn wasmtime_integration(args: TokenStream) -> TokenStream {
let mut config = parse_macro_input!(args as config::Config);
config.witx.make_paths_relative_to(
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var"),
);
let config = parse_macro_input!(args as config::Config);
let doc = config.load_document();
let names = Names::new(&config.ctx.name, quote!(wasmtime_wiggle));

Loading…
Cancel
Save