Skip to content

Commit

Permalink
refactor: introduce tools
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe committed Feb 22, 2020
1 parent 4e54018 commit 8b5ecd2
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 197 deletions.
14 changes: 6 additions & 8 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use manifest::CrateData;
use semver;
use std::path::{Path, PathBuf};
use std::process::Command;
use tool::{self, Tool};
use tool::{self, Kind};

/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
/// `.wasm`.
pub fn wasm_bindgen_build(
data: &CrateData,
install_status: &tool::Status,
bindgen_path: &Path,
out_dir: &Path,
out_name: &Option<String>,
disable_dts: bool,
Expand All @@ -39,16 +39,14 @@ pub fn wasm_bindgen_build(
} else {
"--typescript"
};
let bindgen_path = tool::get_tool_path(install_status, Tool::WasmBindgen)?
.binary(&Tool::WasmBindgen.to_string())?;

let mut cmd = Command::new(&bindgen_path);
cmd.arg(&wasm_path)
.arg("--out-dir")
.arg(out_dir)
.arg(dts_arg);

let target_arg = build_target_arg(target, &bindgen_path)?;
let target_arg = build_target_arg(target, &bindgen_path.to_path_buf())?;
if supports_dash_dash_target(bindgen_path.to_path_buf())? {
cmd.arg("--target").arg(target_arg);
} else {
Expand All @@ -75,17 +73,17 @@ pub fn wasm_bindgen_build(
}

/// Check if the `wasm-bindgen` dependency is locally satisfied for the web target
fn supports_web_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
fn supports_web_target(cli_path: &Path) -> Result<bool, failure::Error> {
let cli_version =
semver::Version::parse(&tool::get_cli_version(&tool::Tool::WasmBindgen, cli_path)?)?;
semver::Version::parse(&tool::get_cli_version(tool::Kind::WasmBindgen, cli_path)?)?;
let expected_version = semver::Version::parse("0.2.39")?;
Ok(cli_version >= expected_version)
}

/// Check if the `wasm-bindgen` dependency is locally satisfied for the --target flag
fn supports_dash_dash_target(cli_path: PathBuf) -> Result<bool, failure::Error> {
let cli_version =
semver::Version::parse(&tool::get_cli_version(&tool::Tool::WasmBindgen, &cli_path)?)?;
semver::Version::parse(&tool::get_cli_version(Kind::WasmBindgen, &cli_path)?)?;
let expected_version = semver::Version::parse("0.2.40")?;
Ok(cli_version >= expected_version)
}
Expand Down
3 changes: 1 addition & 2 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use failure::Error;
use log::info;
use std::process::{Command, Stdio};
use tool::Tool;

/// Return a new Command object
pub fn new_command(program: &str) -> Command {
Expand Down Expand Up @@ -43,7 +42,7 @@ pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
}

/// Run the given command and return its stdout.
pub fn run_capture_stdout(mut command: Command, command_name: &Tool) -> Result<String, Error> {
pub fn run_capture_stdout(mut command: Command, command_name: &str) -> Result<String, Error> {
info!("Running {:?}", command);

let output = command
Expand Down
75 changes: 39 additions & 36 deletions src/command/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Implementation of the `wasm-pack build` command.
use crate::tool::wasm_opt;
use crate::child;
use binary_install::Cache;
use bindgen;
use build;
Expand All @@ -14,10 +14,11 @@ use log::info;
use manifest;
use readme;
use std::fmt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::time::Instant;
use tool::{self, InstallMode, Status, Tool};
use tool::{InstallMode, Kind, Status, Tool};
use PBAR;

/// Everything required to configure and run the `wasm-pack build` command.
Expand Down Expand Up @@ -266,7 +267,6 @@ impl Build {
step_create_dir,
step_copy_readme,
step_copy_license,
step_install_wasm_bindgen,
step_run_wasm_bindgen,
step_run_wasm_opt,
step_create_json,
Expand Down Expand Up @@ -346,34 +346,24 @@ impl Build {
Ok(())
}

fn step_install_wasm_bindgen(&mut self) -> Result<(), failure::Error> {
fn step_run_wasm_bindgen(&mut self) -> Result<(), failure::Error> {
info!("Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
info!("Installing wasm-bindgen-cli...");
let bindgen = tool::download_prebuilt_or_cargo_install(
Tool::WasmBindgen,
&self.cache,
&bindgen_version,
self.mode.install_permitted(),
)?;
self.bindgen = Some(bindgen);
info!("Installing wasm-bindgen-cli was successful.");
Ok(())
}

fn step_run_wasm_bindgen(&mut self) -> Result<(), Error> {
info!("Building the wasm bindings...");
bindgen::wasm_bindgen_build(
&self.crate_data,
&self.bindgen.as_ref().unwrap(),
&self.out_dir,
&self.out_name,
self.disable_dts,
self.target,
self.profile,
)?;
info!("wasm bindings were built at {:#?}.", &self.out_dir);
let bindgen = Tool::new(Kind::WasmBindgen, bindgen_version.to_string());
bindgen.run(&self.cache, self.mode.install_permitted(), |exec: &Path| {
bindgen::wasm_bindgen_build(
&self.crate_data,
exec,
&self.out_dir,
&self.out_name,
self.disable_dts,
self.target,
self.profile,
)?;
info!("wasm bindings were built at {:#?}.", &self.out_dir);
Ok(())
})?;
Ok(())
}

Expand All @@ -387,15 +377,28 @@ impl Build {
None => return Ok(()),
};
info!("executing wasm-opt with {:?}", args);
wasm_opt::run(
&self.cache,
&self.out_dir,
&args,
self.mode.install_permitted(),
).map_err(|e| {
let version = String::from("version_78");
let wasm_opt = Tool::new(Kind::WasmOpt, version);
wasm_opt.run(&self.cache,self.mode.install_permitted(), |exec: &Path| {
for file in self.out_dir.read_dir()? {
let file = file?;
let path = file.path();
if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
continue;
}
let tmp = path.with_extension("wasm-opt.wasm");
let mut cmd = Command::new(exec);
cmd.arg(&path).arg("-o").arg(&tmp).args(&args);
child::run(cmd, "wasm-opt")?;
std::fs::rename(&tmp, &path)?;
}
Ok(())
})
.map_err(|e| {
format_err!(
"{}\nTo disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.", e
)
})
})?;
Ok(())
}
}
17 changes: 3 additions & 14 deletions src/command/generate.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
use cache;
use failure::Error;
use generate;
use log::info;
use std::path::Path;
use std::result;
use tool::{self, Tool};
use PBAR;

/// Executes the 'cargo-generate' command in the current directory
/// which generates a new rustwasm project from a template.
pub fn generate(
template: String,
name: String,
install_permitted: bool,
) -> result::Result<(), Error> {
pub fn generate(template: String, name: &str, exec_path: &Path) -> result::Result<(), Error> {
info!("Generating a new rustwasm project...");
let download = tool::download_prebuilt_or_cargo_install(
Tool::CargoGenerate,
&cache::get_wasm_pack_cache()?,
"latest",
install_permitted,
)?;
generate::generate(&template, &name, &download)?;
generate::generate(&template, &name, exec_path)?;

let msg = format!("🐑 Generated new project at /{}", name);
PBAR.info(&msg);
Expand Down
14 changes: 11 additions & 3 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ use self::login::login;
use self::pack::pack;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use crate::tool::InstallMode;
use crate::cache;
use crate::tool::{InstallMode, Kind, Tool};
use failure::Error;
use log::info;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::result;

/// The various kinds of commands that `wasm-pack` can execute.
Expand Down Expand Up @@ -134,7 +135,14 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
info!("Running generate command...");
info!("Template: {:?}", &template);
info!("Name: {:?}", &name);
generate(template, name, mode.install_permitted())
let version = String::from("latest");
let cargo_gen = Tool::new(Kind::CargoGenerate, version);
let cache = cache::get_wasm_pack_cache()?;
cargo_gen.run(&cache, mode.install_permitted(), |exec: &Path| {
generate(template, &name, exec)?;
Ok(())
})?;
Ok(())
}
Command::Publish {
target,
Expand Down
12 changes: 4 additions & 8 deletions src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use manifest;
use std::path::PathBuf;
use std::time::Instant;
use test::{self, webdriver};
use tool::{self, InstallMode, Tool};
use tool::{self, InstallMode};

#[derive(Debug, Default, StructOpt)]
/// Everything required to configure the `wasm-pack test` command.
Expand Down Expand Up @@ -268,14 +268,10 @@ impl Test {
)
}

let status = tool::download_prebuilt_or_cargo_install(
Tool::WasmBindgen,
&self.cache,
&bindgen_version,
self.mode.install_permitted(),
)?;
let wasm_bindgen = tool::Tool::new(tool::Kind::WasmBindgen, bindgen_version.to_string())
.install(&self.cache, self.mode.install_permitted())?;

self.test_runner_path = match status {
self.test_runner_path = match wasm_bindgen {
tool::Status::Found(dl) => Some(dl.binary("wasm-bindgen-test-runner")?),
_ => bail!("Could not find 'wasm-bindgen-test-runner'."),
};
Expand Down
12 changes: 3 additions & 9 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
use child;
use emoji;
use failure::{self, ResultExt};
use std::path::Path;
use std::process::Command;
use tool::{self, Tool};

/// Run `cargo generate` in the current directory to create a new
/// project from a template
pub fn generate(
template: &str,
name: &str,
install_status: &tool::Status,
) -> Result<(), failure::Error> {
let bin_path = tool::get_tool_path(install_status, Tool::CargoGenerate)?
.binary(&Tool::CargoGenerate.to_string())?;
let mut cmd = Command::new(&bin_path);
pub fn generate(template: &str, name: &str, exec: &Path) -> Result<(), failure::Error> {
let mut cmd = Command::new(&exec);
cmd.arg("generate");
cmd.arg("--git").arg(&template);
cmd.arg("--name").arg(&name);
Expand Down
2 changes: 1 addition & 1 deletion src/tool/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct KrateResponse {
}

impl Krate {
pub fn new(name: &tool::Tool) -> Result<Krate, failure::Error> {
pub fn new(name: tool::Kind) -> Result<Krate, failure::Error> {
let krate_address = format!("https://crates.io/api/v1/crates/{}", name);
let client = reqwest::Client::new();
let mut res = client.get(&krate_address).send()?;
Expand Down
Loading

0 comments on commit 8b5ecd2

Please sign in to comment.