Skip to content

Commit

Permalink
Revert "Use Command::args with slices (partial)"
Browse files Browse the repository at this point in the history
This reverts commit 592be5b.
  • Loading branch information
dullbananas committed Jun 5, 2023
1 parent 592be5b commit 04fb5a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
47 changes: 23 additions & 24 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,32 @@ pub fn cargo_build_wasm(
PBAR.info(&msg);

let mut cmd = Command::new("cargo");
cmd.current_dir(path);
cmd.current_dir(path).arg("build").arg("--lib");

let [a1, a2, a3, a4] = ["build", "--lib", "--target", "wasm32-unknown-unknown"];
let quiet_arg = PBAR.quiet().then_some("--quiet");
let release_arg = match profile {
// Once there are DWARF debug info consumers, force enable debug
// info, because builds that use the release cargo profile disables
// debug info.
//
// cmd.env("RUSTFLAGS", "-g");
BuildProfile::Profiling => Some("--release"),

BuildProfile::Release => Some("--release"),

// Plain cargo builds use the dev cargo profile, which includes
// debug info by default.
BuildProfile::Dev => None,
};

let options = extra_options.iter().map(|s|s.as_str());
if PBAR.quiet() {
cmd.arg("--quiet");
}

match (quiet_arg, release_arg) {
(None, None) => cmd.args([a1, a2, a3, a4].iter().copied().chain(options)),
(Some(a5), None) | (None, Some(a5)) => cmd.args([a1, a2, a3, a4, a5].iter().copied().chain(options)),
(Some(a5), Some(a6)) => cmd.args([a1, a2, a3, a4, a5, a6].iter().copied().chain(options)),
};
match profile {
BuildProfile::Profiling => {
// Once there are DWARF debug info consumers, force enable debug
// info, because builds that use the release cargo profile disables
// debug info.
//
// cmd.env("RUSTFLAGS", "-g");
cmd.arg("--release");
}
BuildProfile::Release => {
cmd.arg("--release");
}
BuildProfile::Dev => {
// Plain cargo builds use the dev cargo profile, which includes
// debug info by default.
}
}

cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options);
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub fn generate(template: &str, name: &str, install_status: &install::Status) ->
let bin_path = install::get_tool_path(install_status, Tool::CargoGenerate)?
.binary(&Tool::CargoGenerate.to_string())?;
let mut cmd = Command::new(&bin_path);
cmd.args(["generate", "--git", template, "--name", name].as_slice());
cmd.arg("generate");
cmd.arg("--git").arg(&template);
cmd.arg("--name").arg(&name);

println!(
"{} Generating a new rustwasm project with name '{}'...",
Expand Down
30 changes: 9 additions & 21 deletions src/npm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Functionality related to publishing to npm.
use std::ffi::{OsStr, OsString};

use crate::child;
use crate::command::publish::access::Access;
use anyhow::{bail, Context, Result};
Expand All @@ -21,14 +19,12 @@ pub fn npm_pack(path: &str) -> Result<()> {
/// Run the `npm publish` command.
pub fn npm_publish(path: &str, access: Option<Access>, tag: Option<String>) -> Result<()> {
let mut cmd = child::new_command("npm");
cmd.current_dir(path);
let arg1 = OsStr::new("publish");
let access_string = access.map(|a| a.to_string());
// Using cmd.args(match ...) would cause a "temporary value dropped while borrowed" error on each array
match (access_string, tag) {
(None, None) => cmd.args([arg1].as_slice()),
(Some(arg2), None) | (None, Some(arg2)) => cmd.args([arg1, arg2.as_ref()].as_slice()),
(Some(arg2), Some(arg3)) => cmd.args([arg1, arg2.as_ref(), arg3.as_ref()].as_slice()),
match access {
Some(a) => cmd.current_dir(path).arg("publish").arg(&a.to_string()),
None => cmd.current_dir(path).arg("publish"),
};
if let Some(tag) = tag {
cmd.arg("--tag").arg(tag);
};

child::run(cmd, "npm publish").context("Publishing to npm failed")?;
Expand All @@ -37,28 +33,20 @@ pub fn npm_publish(path: &str, access: Option<Access>, tag: Option<String>) -> R

/// Run the `npm login` command.
pub fn npm_login(registry: &str, scope: &Option<String>, auth_type: &Option<String>) -> Result<()> {
let arg1 = OsStr::new("login");
let arg2 = OsString::from(format!("--registry={}", registry));
let scope_arg = scope.as_deref().map(|s| OsString::from(format!("--scope={}", s)));
let auth_type_arg = auth_type.as_deref().map(|s| OsString::from(format!("--auth_type={}", s)));
/*let mut args = vec!["login".to_string(), format!("--registry={}", registry)];
let mut args = vec!["login".to_string(), format!("--registry={}", registry)];

if let Some(scope) = scope {
args.push(format!("--scope={}", scope));
}

if let Some(auth_type) = auth_type {
args.push(format!("--auth_type={}", auth_type));
}*/
}

// Interactively ask user for npm login info.
// (child::run does not support interactive input)
let mut cmd = child::new_command("npm");
match (scope_arg, auth_type_arg) {
(None, None) => cmd.args([arg1, arg2.as_ref()].as_slice()),
(Some(arg3), None) | (None, Some(arg3)) => cmd.args([arg1, &arg2, &arg3].as_slice()),
(Some(arg3), Some(arg4)) => cmd.args([arg1, &arg2, &arg3, &arg4].as_slice()),
};
cmd.args(args);

info!("Running {:?}", cmd);
if cmd.status()?.success() {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn run(cache: &Cache, out_dir: &Path, args: &[String], install_permitted: bo

let tmp = path.with_extension("wasm-opt.wasm");
let mut cmd = Command::new(&wasm_opt_path);
cmd.args([path.as_os_str(), "-o".as_ref(), tmp.as_os_str()].as_slice().iter().copied().chain(args.iter().map(|s|s.as_ref())));
cmd.arg(&path).arg("-o").arg(&tmp).args(args);
child::run(cmd, "wasm-opt")?;
std::fs::rename(&tmp, &path)?;
}
Expand Down

0 comments on commit 04fb5a1

Please sign in to comment.