Pass filesystem mappings from wapm.toml to --mapdir

This commit is contained in:
Felix Schütt
2022-10-06 15:47:26 +02:00
parent 2414d44958
commit 2640a6d662
6 changed files with 49 additions and 8 deletions

View File

@@ -253,7 +253,7 @@ fn parse_cli_args() -> Result<(), anyhow::Error> {
let mut args_without_package = args.clone();
args_without_package.remove(1);
return RunWithoutFile::try_parse_from(args_without_package.iter())?
.into_run_args(package.path)
.into_run_args(package.path, Some(package.manifest.clone()))
.execute();
}
@@ -270,12 +270,12 @@ fn parse_cli_args() -> Result<(), anyhow::Error> {
sp.close();
print!("\r\n");
match result {
Ok(o) => {
Ok((package, buf)) => {
// Try auto-installing the remote package
let mut args_without_package = args.clone();
args_without_package.remove(1);
return RunWithoutFile::try_parse_from(args_without_package.iter())?
.into_run_args(o)
.into_run_args(buf, Some(package.manifest.clone()))
.execute();
}
Err(e) => {

View File

@@ -75,7 +75,30 @@ pub struct RunWithoutFile {
impl RunWithoutFile {
/// Given a local path, returns the `Run` command (overriding the `--path` argument).
pub fn into_run_args(self, pathbuf: PathBuf) -> Run {
pub fn into_run_args(mut self, pathbuf: PathBuf, manifest: Option<wapm_toml::Manifest>) -> Run {
#[cfg(feature = "wasi")]
if let Some(fs) = manifest.as_ref().and_then(|m| m.fs.as_ref()) {
for (alias, real_dir) in fs.iter() {
let real_dir = if let Some(parent) = pathbuf.parent() {
parent.join(real_dir)
} else {
pathbuf.join(real_dir)
};
if !real_dir.exists() {
println!("warning: cannot map {alias:?} to {}: directory does not exist", real_dir.display());
continue;
}
println!("mapping {alias:?} -> {}", real_dir.display());
self.wasi.map_dir(alias, real_dir.clone());
#[cfg(feature = "wasi")] {
println!("setting PYTHONHOME to /{}", real_dir.display());
self.wasi.set_env("PYTHONHOME", &format!("{}", real_dir.display()));
}
}
}
Run {
#[cfg(feature = "cache")]
disable_cache: self.disable_cache,

View File

@@ -52,6 +52,16 @@ pub struct Wasi {
#[allow(dead_code)]
impl Wasi {
pub fn map_dir(&mut self, alias: &str, target_on_disk: PathBuf) {
self.mapped_dirs.push((alias.to_string(), target_on_disk));
self.pre_opened_directories.push(std::path::Path::new(alias).to_path_buf());
}
pub fn set_env(&mut self, key: &str, value: &str) {
self.env_vars.push((key.to_string(), value.to_string()));
}
/// Gets the WASI version (if any) for the provided module
pub fn get_versions(module: &Module) -> Option<BTreeSet<WasiVersion>> {
// Get the wasi version in strict mode, so no other imports are