diff --git a/Cargo.lock b/Cargo.lock index c7b29fa16e..df8a455352 100644 --- a/Cargo.lock +++ b/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", ] diff --git a/crates/wiggle/generate/Cargo.toml b/crates/wiggle/generate/Cargo.toml index 632b649153..732fa34991 100644 --- a/crates/wiggle/generate/Cargo.toml +++ b/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" } diff --git a/crates/wiggle/generate/src/config.rs b/crates/wiggle/generate/src/config.rs index 20d75812b2..520532a3ec 100644 --- a/crates/wiggle/generate/src/config.rs +++ b/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>(&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 = 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::>(); - - 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::>(); + + Ok(Paths(expanded_paths)) } } diff --git a/crates/wiggle/macro/src/lib.rs b/crates/wiggle/macro/src/lib.rs index ed4a3dc6f0..48f0367312 100644 --- a/crates/wiggle/macro/src/lib.rs +++ b/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)); diff --git a/crates/wiggle/wasmtime/macro/src/lib.rs b/crates/wiggle/wasmtime/macro/src/lib.rs index 160babeb12..4ba1c1b9fb 100644 --- a/crates/wiggle/wasmtime/macro/src/lib.rs +++ b/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));