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.
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -1905,6 +1905,15 @@ dependencies = [
|
|||||||
"lazy_static",
|
"lazy_static",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shellexpand"
|
||||||
|
version = "2.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a2b22262a9aaf9464d356f656fea420634f78c881c5eebd5ef5e66d8b9bc603"
|
||||||
|
dependencies = [
|
||||||
|
"dirs",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.4.1"
|
version = "1.4.1"
|
||||||
@@ -2700,6 +2709,7 @@ dependencies = [
|
|||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
"shellexpand",
|
||||||
"syn",
|
"syn",
|
||||||
"witx",
|
"witx",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ proc-macro2 = "1.0"
|
|||||||
heck = "0.3"
|
heck = "0.3"
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
syn = { version = "1.0", features = ["full"] }
|
syn = { version = "1.0", features = ["full"] }
|
||||||
|
shellexpand = "2.0"
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
use {
|
use {
|
||||||
proc_macro2::Span,
|
proc_macro2::Span,
|
||||||
std::{
|
std::{collections::HashMap, iter::FromIterator, path::PathBuf},
|
||||||
collections::HashMap,
|
|
||||||
iter::FromIterator,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
},
|
|
||||||
syn::{
|
syn::{
|
||||||
braced, bracketed,
|
braced, bracketed,
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
@@ -143,20 +139,6 @@ impl WitxConf {
|
|||||||
Self::Literal(doc) => witx::parse(doc.as_ref()).expect("parsing witx"),
|
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.
|
/// A collection of paths, pointing to witx documents.
|
||||||
@@ -203,32 +185,18 @@ impl Parse for Paths {
|
|||||||
let _ = bracketed!(content in input);
|
let _ = bracketed!(content in input);
|
||||||
let path_lits: Punctuated<LitStr, Token![,]> = content.parse_terminated(Parse::parse)?;
|
let path_lits: Punctuated<LitStr, Token![,]> = content.parse_terminated(Parse::parse)?;
|
||||||
|
|
||||||
/* TODO interpolate env variables here!!!
|
let expanded_paths = path_lits
|
||||||
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
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|lit| PathBuf::from(lit.value()))
|
.map(|lit| {
|
||||||
.collect())
|
PathBuf::from(
|
||||||
|
shellexpand::env(&lit.value())
|
||||||
|
.expect("shell expansion")
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<PathBuf>>();
|
||||||
|
|
||||||
|
Ok(Paths(expanded_paths))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -90,10 +90,7 @@ use syn::parse_macro_input;
|
|||||||
/// ```
|
/// ```
|
||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn from_witx(args: TokenStream) -> TokenStream {
|
pub fn from_witx(args: TokenStream) -> TokenStream {
|
||||||
let mut config = parse_macro_input!(args as wiggle_generate::Config);
|
let 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 doc = config.load_document();
|
let doc = config.load_document();
|
||||||
let names = wiggle_generate::Names::new(&config.ctx.name, quote!(wiggle));
|
let names = wiggle_generate::Names::new(&config.ctx.name, quote!(wiggle));
|
||||||
|
|||||||
@@ -47,10 +47,7 @@ use config::{MissingMemoryConf, ModuleConf, TargetConf};
|
|||||||
///
|
///
|
||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn wasmtime_integration(args: TokenStream) -> TokenStream {
|
pub fn wasmtime_integration(args: TokenStream) -> TokenStream {
|
||||||
let mut config = parse_macro_input!(args as config::Config);
|
let 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 doc = config.load_document();
|
let doc = config.load_document();
|
||||||
let names = Names::new(&config.ctx.name, quote!(wasmtime_wiggle));
|
let names = Names::new(&config.ctx.name, quote!(wasmtime_wiggle));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user