Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Add support for "wapm execute [pirita-cmd]" and test "wapm run"
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Jul 14, 2022
1 parent 46739a7 commit 0262325
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 51 deletions.
8 changes: 8 additions & 0 deletions src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ pub fn execute(opt: ExecuteOpt) -> anyhow::Result<()> {

// first search for locally installed command
match FindCommandResult::find_command_in_directory(&current_dir, &command_name) {
FindCommandResult::CommandFoundPirita(cmd) => {
crate::commands::run::try_run_pirita_cmd(&cmd, command_name, &opt.args.as_ref())?;
return Ok(());
},
FindCommandResult::CommandNotFound(_) => {
// go to normal wax flow
debug!(
Expand Down Expand Up @@ -549,6 +553,10 @@ fn run(
prehashed_cache_key,
);
}
FindCommandResult::CommandFoundPirita(cmd) => {
crate::commands::run::try_run_pirita_cmd(&cmd, command_name, args)?;
return Ok(());
},
FindCommandResult::Error(e) => return Err(e),
};
}
Expand Down
5 changes: 1 addition & 4 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ pub struct InstallOpt {
/// Install the package(s) globally
#[structopt(short = "g", long = "global")]
global: bool,
/// Expect the file to be a PiritaFile (experimental flag)
#[structopt(long = "pirita")]
pirita: bool,
/// If packages already exist, the CLI will throw a prompt whether you'd like to
/// re-download the package. This flag disables the prompt and will re-download
/// the file even if it already exists.
Expand Down Expand Up @@ -81,7 +78,7 @@ mod package_args {

/// Run the install command
pub fn install(options: InstallOpt) -> anyhow::Result<()> {
if options.pirita {
if std::env::var("USE_PIRITA").ok() == Some("1".to_string()) {
return install_pirita(options);
}
let current_directory = crate::config::Config::get_current_dir()?;
Expand Down
110 changes: 71 additions & 39 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,57 @@ pub fn try_run_pirita(run_options: &RunOpt) -> Result<(), PiritaRunError> {
let cmd = std::fs::read_to_string(current_dir.join("wapm_packages").join(".bin").join(command_name))
.map_err(|e| PiritaRunError::Initialize(PiritaInitializeError::CouldNotFindCommandInDotBin(e)))?;

let mut sw = shellwords::split(&cmd)
.map_err(|e| PiritaRunError::Run(e.into()))?;
try_run_pirita_cmd(&cmd, command_name, args.as_ref())
.map_err(|e| PiritaRunError::Run(e))
}

pub(crate) fn try_run_pirita_cmd(cmd: &str, command_name: &str, args: &[OsString]) -> Result<(), anyhow::Error> {

println!("try_run_pirita_cmd");

let mut sw = shellwords::split(&cmd)?;

if sw.get(0).map(|s| s.as_str()) != Some("wasmer") || sw.get(1).map(|s| s.as_str()) != Some("run") {
return Err(PiritaRunError::Run(anyhow!(
return Err(anyhow!(
"Expected \"wasmer run\" command in command for {command_name:?}, got: {sw:?}"
)));
));
}

sw.remove(0);
sw.remove(0);

run_pirita(&sw)
.map_err(|e| PiritaRunError::Run(e))
run_pirita(&sw, args)
}

fn run_pirita(args: &[String]) -> Result<(), anyhow::Error> {

let mut command = std::process::Command::new("wasmer");
fn run_pirita(args: &[String], rt_args: &[OsString]) -> Result<(), anyhow::Error> {

let (runtime, runtime_args) = get_runtime_with_args();
let mut command = std::process::Command::new(runtime);

for arg in runtime_args {
command.arg(arg);
}

command.arg("run");

for arg in args {
command.arg(arg);
}

let output = command.output()?;
for arg in rt_args {
command.arg(arg);
}

let output = command.spawn()?;

let output = output
.wait_with_output()
.expect("failed to wait on child");

if !output.stderr.is_empty() {
Err(anyhow!("{}", String::from_utf8_lossy(&output.stderr)))
} else if !output.stdout.is_empty() {
println!("{}", String::from_utf8_lossy(&output.stdout));
println!("{}", String::from_utf8_lossy(&output.stderr));
Ok(())
} else {
Ok(())
Expand All @@ -106,14 +126,9 @@ pub fn run(run_options: RunOpt) -> anyhow::Result<()> {
.map_err(|e| RunError::CannotRegenLockfile(command_name.to_string(), e))?,
}

let find_command_result::Command {
source: source_path_buf,
manifest_dir,
args: _,
module_name,
is_global,
prehashed_cache_key,
} = match get_command_from_anywhere(command_name) {
let found_command = get_command_from_anywhere(command_name);

let command = match found_command {
Err(find_command_result::Error::CommandNotFound(command)) => {
let package_info = find_command_result::PackageInfoFromCommand::get(command)?;
return Err(anyhow!("Command {} not found, but package {} version {} has this command. You can install it with `wapm install {}@{}`",
Expand All @@ -123,28 +138,45 @@ pub fn run(run_options: RunOpt) -> anyhow::Result<()> {
&package_info.namespaced_package_name,
&package_info.version,
));
}
otherwise => otherwise?,
},
Err(e) => { return Err(e.into()); },
Ok(o) => o,
};

let run_dir = if is_global {
Config::get_globals_directory().unwrap()
} else {
current_dir.clone()
};

let manifest_dir = run_dir.join(manifest_dir);

do_run(
run_dir,
source_path_buf,
manifest_dir,
command_name,
&module_name,
&run_options.pre_opened_directories,
&args,
prehashed_cache_key,
)
match command {
find_command_result::Command::TarGz(find_command_result::TarGzCommand {
source: source_path_buf,
manifest_dir,
args: _,
module_name,
is_global,
prehashed_cache_key,
}) => {
let run_dir = if is_global {
Config::get_globals_directory().unwrap()
} else {
current_dir.clone()
};

let manifest_dir = run_dir.join(manifest_dir);

do_run(
run_dir,
source_path_buf,
manifest_dir,
command_name,
&module_name,
&run_options.pre_opened_directories,
&args,
prehashed_cache_key,
)
},
find_command_result::Command::Pirita(find_command_result::PiritaCommand {
cmd
}) => {
crate::commands::run::try_run_pirita_cmd(&cmd, command_name, args)
}
}
}

pub(crate) fn do_run(
Expand Down
44 changes: 36 additions & 8 deletions src/dataflow/find_command_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::data::lock::lockfile::{Lockfile, LockfileError};
use crate::data::manifest::Manifest;
use crate::dataflow::lockfile_packages::LockfileResult;
use crate::dataflow::manifest_packages::ManifestResult;
use crate::dataflow::pirita_packages::PiritaResult;
use std::path::{Path, PathBuf};
use thiserror::Error;

Expand Down Expand Up @@ -81,6 +82,7 @@ pub enum FindCommandResult {
module_name: String,
prehashed_cache_key: Option<String>,
},
CommandFoundPirita(String),
Error(anyhow::Error),
}

Expand Down Expand Up @@ -197,8 +199,17 @@ impl FindCommandResult {
}

pub fn find_command_in_directory<S: AsRef<str>>(directory: &Path, command_name: S) -> Self {

let command_name = command_name.as_ref();

let pirita_result = PiritaResult::find_in_directory(&directory, command_name);
if let PiritaResult::Ok(o) = pirita_result {
return FindCommandResult::CommandFoundPirita(o);
}

let manifest_result = ManifestResult::find_in_directory(&directory);
let lockfile_result = LockfileResult::find_in_directory(&directory);

match (manifest_result, lockfile_result) {
(ManifestResult::ManifestError(e), _) => return FindCommandResult::Error(e.into()),
(_, LockfileResult::LockfileError(e)) => return FindCommandResult::Error(e.into()),
Expand All @@ -219,12 +230,23 @@ impl FindCommandResult {
return Self::find_command_in_manifest_and_lockfile(command_name, m, l, directory);
}
};
FindCommandResult::CommandNotFound(command_name.as_ref().to_string())
FindCommandResult::CommandNotFound(command_name.to_string())
}
}

#[derive(Debug)]
pub struct Command {
pub enum Command {
Pirita(PiritaCommand),
TarGz(TarGzCommand)
}

#[derive(Debug)]
pub struct PiritaCommand {
pub cmd: String,
}

#[derive(Debug)]
pub struct TarGzCommand {
// PathBuf, Option<String>, String, bool
pub source: PathBuf,
pub manifest_dir: PathBuf,
Expand Down Expand Up @@ -253,15 +275,18 @@ pub fn get_command_from_anywhere<S: AsRef<str>>(command_name: S) -> Result<Comma
module_name,
prehashed_cache_key,
} => {
return Ok(Command {
return Ok(Command::TarGz(TarGzCommand {
source,
manifest_dir,
args,
module_name,
is_global: false,
prehashed_cache_key,
});
}
}));
},
FindCommandResult::CommandFoundPirita(cmd) => {
return Ok(Command::Pirita(PiritaCommand { cmd }));
},
FindCommandResult::Error(e) => {
return Err(Error::ErrorReadingLocalDirectory(
command_name.as_ref().to_string(),
Expand All @@ -287,15 +312,18 @@ pub fn get_command_from_anywhere<S: AsRef<str>>(command_name: S) -> Result<Comma
module_name,
prehashed_cache_key,
} => {
return Ok(Command {
return Ok(Command::TarGz(TarGzCommand {
source,
manifest_dir,
args,
module_name,
is_global: true,
prehashed_cache_key,
});
}
}));
},
FindCommandResult::CommandFoundPirita(cmd) => {
return Ok(Command::Pirita(PiritaCommand { cmd }));
},
FindCommandResult::Error(e) => {
return Err(
Error::CommandNotFoundInLocalDirectoryAndErrorReadingGlobalDirectory(
Expand Down
1 change: 1 addition & 0 deletions src/dataflow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod local_package;
pub mod lockfile_packages;
pub mod manifest_packages;
pub mod merged_lockfile_packages;
pub mod pirita_packages;
pub mod removed_lockfile_packages;
pub mod removed_packages;
pub mod resolved_packages;
Expand Down
19 changes: 19 additions & 0 deletions src/dataflow/pirita_packages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::path::Path;
use std::io::Error as IoError;

/// A ternary for a manifest: Some, None, Error.
#[derive(Debug)]
pub enum PiritaResult {
Ok(String),
Error(IoError)
}

impl PiritaResult {
pub fn find_in_directory<P: AsRef<Path>>(directory: P, command: &str) -> Self {
let directory = directory.as_ref();
match std::fs::read_to_string(directory.join("wapm_packages").join(".bin").join(command)) {
Ok(o) => Self::Ok(o),
Err(e) => Self::Error(e),
}
}
}

0 comments on commit 0262325

Please sign in to comment.