Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo opts #924

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,059 changes: 1,041 additions & 1,018 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ human-panic = "1.0.1"
glob = "0.2"
log = "0.4.6"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6"
parking_lot = "0.10"
reqwest = "0.9.14"
semver = "0.9.0"
serde = "1.0.74"
Expand Down
5 changes: 3 additions & 2 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use child;
use command::build::BuildProfile;
use command::global_opts;
use emoji;
use failure::{Error, ResultExt};
use manifest::Crate;
Expand Down Expand Up @@ -81,7 +82,7 @@ pub fn cargo_build_wasm(
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
PBAR.info(&msg);

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

if PBAR.quiet() {
Expand Down Expand Up @@ -117,7 +118,7 @@ pub fn cargo_build_wasm(
/// This generates the `Cargo.lock` file that we use in order to know which version of
/// wasm-bindgen-cli to use when running tests.
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
let mut cmd = Command::new("cargo");
let mut cmd = global_opts().cargo_cmd();

cmd.current_dir(path).arg("build").arg("--tests");

Expand Down
30 changes: 30 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use crate::install::InstallMode;
use failure::Error;
use parking_lot::{const_rwlock, MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use GlobalOpts;
use log::info;
use std::path::PathBuf;
use std::result;
Expand Down Expand Up @@ -112,6 +114,34 @@ pub enum Command {
Test(TestOptions),
}

type GlobalOptsContainer = RwLock<Option<GlobalOpts>>;
static GLOBAL_OPTS_CONTAINER: GlobalOptsContainer = const_rwlock(None);

/// Retrieves the global options for use by all wasm-pack subcommands
/// Panics if `set_global_opts` has not yet been called.
pub fn global_opts() -> MappedRwLockReadGuard<'static, GlobalOpts> {
RwLockReadGuard::map(GLOBAL_OPTS_CONTAINER.read(), |g| {
g.as_ref()
.expect("global_opts() permitted only after run_wasm_pack entered")
})
}

/// Sets the global options for use by all wasm-pack subcommands.
pub fn set_global_opts(g: GlobalOpts) {
*GLOBAL_OPTS_CONTAINER.write() = Some(g);
}

impl GlobalOpts {
/// Starts building a cargo command. By default, just `cargo`.
/// The caller should use `.args` to specify the cargo subcommand,
/// and set the current directory, and so on.
pub fn cargo_cmd(&self) -> std::process::Command {
let mut cmd = std::process::Command::new(&self.cargo_path);
cmd.args(&self.cargo_opt);
cmd
}
}

/// Run a command with the given logger!
pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
// Run the correct command based off input and store the result of it so that we can clear
Expand Down
3 changes: 2 additions & 1 deletion src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use self::krate::Krate;
use binary_install::{Cache, Download};
use child;
use command::global_opts;
use emoji;
use failure::{self, ResultExt};
use install;
Expand Down Expand Up @@ -256,7 +257,7 @@ pub fn cargo_install(
Tool::WasmBindgen => "wasm-bindgen-cli".to_string(),
_ => tool.to_string(),
};
let mut cmd = Command::new("cargo");
let mut cmd = global_opts().cargo_cmd();
cmd.arg("install")
.arg("--force")
.arg(crate_name)
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@ pub struct Cli {
#[structopt(long = "log-level", default_value = "info")]
/// The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error]
pub log_level: LogLevel,

#[allow(missing_docs)] // avoids "methods and doc comments are not allowed for flattened entry"
#[structopt(flatten)]
pub global_opts: GlobalOpts,
}

#[derive(Debug, StructOpt)]
/// Global options, passed to every subcommand.
pub struct GlobalOpts {
/// Additional global argument to pass to `cargo`, before the cargo subcommand.
/// For example, `--cargo-opt=--locked`.
/// May be repeated to pass multiple options.
#[structopt(long = "cargo-opt", number_of_values = 1)]
pub cargo_opt: Vec<String>,

/// Path to cargo. `cargo`, by default.
#[structopt(long = "cargo-path", default_value = "cargo")]
pub cargo_path: String,
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use structopt::StructOpt;
use wasm_pack::{
build::{self, WasmPackVersion},
command::run_wasm_pack,
command::set_global_opts,
Cli, PBAR,
};

Expand Down Expand Up @@ -82,6 +83,7 @@ fn run() -> Result<(), failure::Error> {
let args = Cli::from_args();

PBAR.set_log_level(args.log_level);
set_global_opts(args.global_opts);

if args.quiet {
PBAR.set_quiet(true);
Expand Down
4 changes: 2 additions & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ pub mod webdriver;

use crate::PBAR;
use child;
use command::global_opts;
use failure::{self, ResultExt};
use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;

/// Run `cargo test` with the `nightly` toolchain and targeting
/// `wasm32-unknown-unknown`.
Expand All @@ -22,7 +22,7 @@ where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
let mut cmd = Command::new("cargo");
let mut cmd = global_opts().cargo_cmd();

cmd.envs(envs);
cmd.current_dir(path).arg("test");
Expand Down