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

Commit 0262325

Browse files
author
Felix Schütt
committed
Add support for "wapm execute [pirita-cmd]" and test "wapm run"
1 parent 46739a7 commit 0262325

File tree

6 files changed

+136
-51
lines changed

6 files changed

+136
-51
lines changed

src/commands/execute.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ pub fn execute(opt: ExecuteOpt) -> anyhow::Result<()> {
266266

267267
// first search for locally installed command
268268
match FindCommandResult::find_command_in_directory(&current_dir, &command_name) {
269+
FindCommandResult::CommandFoundPirita(cmd) => {
270+
crate::commands::run::try_run_pirita_cmd(&cmd, command_name, &opt.args.as_ref())?;
271+
return Ok(());
272+
},
269273
FindCommandResult::CommandNotFound(_) => {
270274
// go to normal wax flow
271275
debug!(
@@ -549,6 +553,10 @@ fn run(
549553
prehashed_cache_key,
550554
);
551555
}
556+
FindCommandResult::CommandFoundPirita(cmd) => {
557+
crate::commands::run::try_run_pirita_cmd(&cmd, command_name, args)?;
558+
return Ok(());
559+
},
552560
FindCommandResult::Error(e) => return Err(e),
553561
};
554562
}

src/commands/install.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ pub struct InstallOpt {
2323
/// Install the package(s) globally
2424
#[structopt(short = "g", long = "global")]
2525
global: bool,
26-
/// Expect the file to be a PiritaFile (experimental flag)
27-
#[structopt(long = "pirita")]
28-
pirita: bool,
2926
/// If packages already exist, the CLI will throw a prompt whether you'd like to
3027
/// re-download the package. This flag disables the prompt and will re-download
3128
/// the file even if it already exists.
@@ -81,7 +78,7 @@ mod package_args {
8178

8279
/// Run the install command
8380
pub fn install(options: InstallOpt) -> anyhow::Result<()> {
84-
if options.pirita {
81+
if std::env::var("USE_PIRITA").ok() == Some("1".to_string()) {
8582
return install_pirita(options);
8683
}
8784
let current_directory = crate::config::Config::get_current_dir()?;

src/commands/run.rs

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,57 @@ pub fn try_run_pirita(run_options: &RunOpt) -> Result<(), PiritaRunError> {
4949
let cmd = std::fs::read_to_string(current_dir.join("wapm_packages").join(".bin").join(command_name))
5050
.map_err(|e| PiritaRunError::Initialize(PiritaInitializeError::CouldNotFindCommandInDotBin(e)))?;
5151

52-
let mut sw = shellwords::split(&cmd)
53-
.map_err(|e| PiritaRunError::Run(e.into()))?;
52+
try_run_pirita_cmd(&cmd, command_name, args.as_ref())
53+
.map_err(|e| PiritaRunError::Run(e))
54+
}
55+
56+
pub(crate) fn try_run_pirita_cmd(cmd: &str, command_name: &str, args: &[OsString]) -> Result<(), anyhow::Error> {
57+
58+
println!("try_run_pirita_cmd");
59+
60+
let mut sw = shellwords::split(&cmd)?;
5461

5562
if sw.get(0).map(|s| s.as_str()) != Some("wasmer") || sw.get(1).map(|s| s.as_str()) != Some("run") {
56-
return Err(PiritaRunError::Run(anyhow!(
63+
return Err(anyhow!(
5764
"Expected \"wasmer run\" command in command for {command_name:?}, got: {sw:?}"
58-
)));
65+
));
5966
}
6067

6168
sw.remove(0);
6269
sw.remove(0);
6370

64-
run_pirita(&sw)
65-
.map_err(|e| PiritaRunError::Run(e))
71+
run_pirita(&sw, args)
6672
}
6773

68-
fn run_pirita(args: &[String]) -> Result<(), anyhow::Error> {
69-
70-
let mut command = std::process::Command::new("wasmer");
74+
fn run_pirita(args: &[String], rt_args: &[OsString]) -> Result<(), anyhow::Error> {
75+
76+
let (runtime, runtime_args) = get_runtime_with_args();
77+
let mut command = std::process::Command::new(runtime);
7178

79+
for arg in runtime_args {
80+
command.arg(arg);
81+
}
82+
7283
command.arg("run");
7384

7485
for arg in args {
7586
command.arg(arg);
7687
}
7788

78-
let output = command.output()?;
89+
for arg in rt_args {
90+
command.arg(arg);
91+
}
92+
93+
let output = command.spawn()?;
94+
95+
let output = output
96+
.wait_with_output()
97+
.expect("failed to wait on child");
98+
7999
if !output.stderr.is_empty() {
80100
Err(anyhow!("{}", String::from_utf8_lossy(&output.stderr)))
81101
} else if !output.stdout.is_empty() {
82-
println!("{}", String::from_utf8_lossy(&output.stdout));
102+
println!("{}", String::from_utf8_lossy(&output.stderr));
83103
Ok(())
84104
} else {
85105
Ok(())
@@ -106,14 +126,9 @@ pub fn run(run_options: RunOpt) -> anyhow::Result<()> {
106126
.map_err(|e| RunError::CannotRegenLockfile(command_name.to_string(), e))?,
107127
}
108128

109-
let find_command_result::Command {
110-
source: source_path_buf,
111-
manifest_dir,
112-
args: _,
113-
module_name,
114-
is_global,
115-
prehashed_cache_key,
116-
} = match get_command_from_anywhere(command_name) {
129+
let found_command = get_command_from_anywhere(command_name);
130+
131+
let command = match found_command {
117132
Err(find_command_result::Error::CommandNotFound(command)) => {
118133
let package_info = find_command_result::PackageInfoFromCommand::get(command)?;
119134
return Err(anyhow!("Command {} not found, but package {} version {} has this command. You can install it with `wapm install {}@{}`",
@@ -123,28 +138,45 @@ pub fn run(run_options: RunOpt) -> anyhow::Result<()> {
123138
&package_info.namespaced_package_name,
124139
&package_info.version,
125140
));
126-
}
127-
otherwise => otherwise?,
141+
},
142+
Err(e) => { return Err(e.into()); },
143+
Ok(o) => o,
128144
};
129145

130-
let run_dir = if is_global {
131-
Config::get_globals_directory().unwrap()
132-
} else {
133-
current_dir.clone()
134-
};
135-
136-
let manifest_dir = run_dir.join(manifest_dir);
137-
138-
do_run(
139-
run_dir,
140-
source_path_buf,
141-
manifest_dir,
142-
command_name,
143-
&module_name,
144-
&run_options.pre_opened_directories,
145-
&args,
146-
prehashed_cache_key,
147-
)
146+
match command {
147+
find_command_result::Command::TarGz(find_command_result::TarGzCommand {
148+
source: source_path_buf,
149+
manifest_dir,
150+
args: _,
151+
module_name,
152+
is_global,
153+
prehashed_cache_key,
154+
}) => {
155+
let run_dir = if is_global {
156+
Config::get_globals_directory().unwrap()
157+
} else {
158+
current_dir.clone()
159+
};
160+
161+
let manifest_dir = run_dir.join(manifest_dir);
162+
163+
do_run(
164+
run_dir,
165+
source_path_buf,
166+
manifest_dir,
167+
command_name,
168+
&module_name,
169+
&run_options.pre_opened_directories,
170+
&args,
171+
prehashed_cache_key,
172+
)
173+
},
174+
find_command_result::Command::Pirita(find_command_result::PiritaCommand {
175+
cmd
176+
}) => {
177+
crate::commands::run::try_run_pirita_cmd(&cmd, command_name, args)
178+
}
179+
}
148180
}
149181

150182
pub(crate) fn do_run(

src/dataflow/find_command_result.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::data::lock::lockfile::{Lockfile, LockfileError};
33
use crate::data::manifest::Manifest;
44
use crate::dataflow::lockfile_packages::LockfileResult;
55
use crate::dataflow::manifest_packages::ManifestResult;
6+
use crate::dataflow::pirita_packages::PiritaResult;
67
use std::path::{Path, PathBuf};
78
use thiserror::Error;
89

@@ -81,6 +82,7 @@ pub enum FindCommandResult {
8182
module_name: String,
8283
prehashed_cache_key: Option<String>,
8384
},
85+
CommandFoundPirita(String),
8486
Error(anyhow::Error),
8587
}
8688

@@ -197,8 +199,17 @@ impl FindCommandResult {
197199
}
198200

199201
pub fn find_command_in_directory<S: AsRef<str>>(directory: &Path, command_name: S) -> Self {
202+
203+
let command_name = command_name.as_ref();
204+
205+
let pirita_result = PiritaResult::find_in_directory(&directory, command_name);
206+
if let PiritaResult::Ok(o) = pirita_result {
207+
return FindCommandResult::CommandFoundPirita(o);
208+
}
209+
200210
let manifest_result = ManifestResult::find_in_directory(&directory);
201211
let lockfile_result = LockfileResult::find_in_directory(&directory);
212+
202213
match (manifest_result, lockfile_result) {
203214
(ManifestResult::ManifestError(e), _) => return FindCommandResult::Error(e.into()),
204215
(_, LockfileResult::LockfileError(e)) => return FindCommandResult::Error(e.into()),
@@ -219,12 +230,23 @@ impl FindCommandResult {
219230
return Self::find_command_in_manifest_and_lockfile(command_name, m, l, directory);
220231
}
221232
};
222-
FindCommandResult::CommandNotFound(command_name.as_ref().to_string())
233+
FindCommandResult::CommandNotFound(command_name.to_string())
223234
}
224235
}
225236

226237
#[derive(Debug)]
227-
pub struct Command {
238+
pub enum Command {
239+
Pirita(PiritaCommand),
240+
TarGz(TarGzCommand)
241+
}
242+
243+
#[derive(Debug)]
244+
pub struct PiritaCommand {
245+
pub cmd: String,
246+
}
247+
248+
#[derive(Debug)]
249+
pub struct TarGzCommand {
228250
// PathBuf, Option<String>, String, bool
229251
pub source: PathBuf,
230252
pub manifest_dir: PathBuf,
@@ -253,15 +275,18 @@ pub fn get_command_from_anywhere<S: AsRef<str>>(command_name: S) -> Result<Comma
253275
module_name,
254276
prehashed_cache_key,
255277
} => {
256-
return Ok(Command {
278+
return Ok(Command::TarGz(TarGzCommand {
257279
source,
258280
manifest_dir,
259281
args,
260282
module_name,
261283
is_global: false,
262284
prehashed_cache_key,
263-
});
264-
}
285+
}));
286+
},
287+
FindCommandResult::CommandFoundPirita(cmd) => {
288+
return Ok(Command::Pirita(PiritaCommand { cmd }));
289+
},
265290
FindCommandResult::Error(e) => {
266291
return Err(Error::ErrorReadingLocalDirectory(
267292
command_name.as_ref().to_string(),
@@ -287,15 +312,18 @@ pub fn get_command_from_anywhere<S: AsRef<str>>(command_name: S) -> Result<Comma
287312
module_name,
288313
prehashed_cache_key,
289314
} => {
290-
return Ok(Command {
315+
return Ok(Command::TarGz(TarGzCommand {
291316
source,
292317
manifest_dir,
293318
args,
294319
module_name,
295320
is_global: true,
296321
prehashed_cache_key,
297-
});
298-
}
322+
}));
323+
},
324+
FindCommandResult::CommandFoundPirita(cmd) => {
325+
return Ok(Command::Pirita(PiritaCommand { cmd }));
326+
},
299327
FindCommandResult::Error(e) => {
300328
return Err(
301329
Error::CommandNotFoundInLocalDirectoryAndErrorReadingGlobalDirectory(

src/dataflow/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod local_package;
2626
pub mod lockfile_packages;
2727
pub mod manifest_packages;
2828
pub mod merged_lockfile_packages;
29+
pub mod pirita_packages;
2930
pub mod removed_lockfile_packages;
3031
pub mod removed_packages;
3132
pub mod resolved_packages;

src/dataflow/pirita_packages.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::path::Path;
2+
use std::io::Error as IoError;
3+
4+
/// A ternary for a manifest: Some, None, Error.
5+
#[derive(Debug)]
6+
pub enum PiritaResult {
7+
Ok(String),
8+
Error(IoError)
9+
}
10+
11+
impl PiritaResult {
12+
pub fn find_in_directory<P: AsRef<Path>>(directory: P, command: &str) -> Self {
13+
let directory = directory.as_ref();
14+
match std::fs::read_to_string(directory.join("wapm_packages").join(".bin").join(command)) {
15+
Ok(o) => Self::Ok(o),
16+
Err(e) => Self::Error(e),
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)