Fix pip install command to account for python2 / python3

This commit is contained in:
Felix Schütt
2022-11-20 10:49:00 +01:00
parent 5fcb1e5fed
commit c5af4657e8

View File

@@ -40,7 +40,7 @@ impl Add {
let bindings = self.lookup_bindings(&registry)?;
let mut cmd = self.target()?.command(&bindings);
let mut cmd = self.target()?.command(&bindings)?;
#[cfg(feature = "debug")]
log::debug!("Running {cmd:?}");
@@ -156,13 +156,43 @@ impl Target {
/// `npm.cmd` or `yarn.ps1`).
///
/// See <https://github.com/wasmerio/wapm-cli/issues/291> for more.
fn command(self, packages: &[Bindings]) -> Command {
fn command(self, packages: &[Bindings]) -> Result<Command, Error> {
let command_line = match self {
Target::Pip => "pip install",
Target::Yarn { dev: true } => "yarn add --dev",
Target::Yarn { dev: false } => "yarn add",
Target::Npm { dev: true } => "npm install --dev",
Target::Npm { dev: false } => "npm install",
Target::Pip => {
if Command::new("pip").arg("--version").output().is_ok() {
"pip install"
} else if Command::new("pip3").arg("--version").output().is_ok() {
"pip3 install"
} else if Command::new("python").arg("--version").output().is_ok() {
"python -m pip install"
} else if Command::new("python3").arg("--version").output().is_ok() {
"python3 -m pip install"
} else {
return Err(anyhow::anyhow!(
"neither pip, pip3, python or python3 installed"
));
}
}
Target::Yarn { dev } => {
if Command::new("yarn").arg("--version").output().is_err() {
return Err(anyhow::anyhow!("yarn not installed"));
}
if dev {
"yarn add --dev"
} else {
"yarn add"
}
}
Target::Npm { dev } => {
if Command::new("npm").arg("--version").output().is_err() {
return Err(anyhow::anyhow!("yarn not installed"));
}
if dev {
"npm install --dev"
} else {
"npm install"
}
}
};
let mut command_line = command_line.to_string();
@@ -174,11 +204,11 @@ impl Target {
if cfg!(windows) {
let mut cmd = Command::new("cmd");
cmd.arg("/C").arg(command_line);
cmd
Ok(cmd)
} else {
let mut cmd = Command::new("sh");
cmd.arg("-c").arg(command_line);
cmd
Ok(cmd)
}
}
}