From 40b0291715481c8946b562d1a14a0ef97a985401 Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Sun, 2 Sep 2018 11:30:17 -0700 Subject: [PATCH] add out dir cli parameter --- .travis.yml | 1 - src/bindgen.rs | 5 ++- src/command/build.rs | 31 +++++++------ src/command/utils.rs | 5 +-- src/manifest.rs | 3 +- src/readme.rs | 8 ++-- tests/all/manifest.rs | 86 +++++++++++++++++++++++++++---------- tests/all/readme.rs | 14 +++--- tests/all/utils/manifest.rs | 4 +- 9 files changed, 99 insertions(+), 58 deletions(-) diff --git a/.travis.yml b/.travis.yml index 699a89d..963d760 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: rust sudo: false -cache: cargo DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB before_deploy: diff --git a/src/bindgen.rs b/src/bindgen.rs index 55d2356..34547f2 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -59,6 +59,7 @@ pub fn cargo_install_wasm_bindgen( /// `.wasm`. pub fn wasm_bindgen_build( path: &Path, + out_dir: &Path, name: &str, disable_dts: bool, target: &str, @@ -70,6 +71,8 @@ pub fn wasm_bindgen_build( let binary_name = name.replace("-", "_"); let release_or_debug = if debug { "debug" } else { "release" }; + let out_dir = out_dir.to_str().unwrap(); + if let Some(wasm_bindgen_path) = wasm_bindgen_path(path) { let wasm_path = format!( "target/wasm32-unknown-unknown/{}/{}.wasm", @@ -89,7 +92,7 @@ pub fn wasm_bindgen_build( .current_dir(path) .arg(&wasm_path) .arg("--out-dir") - .arg("./pkg") + .arg(out_dir) .arg(dts_arg) .arg(target_arg) .output()?; diff --git a/src/command/build.rs b/src/command/build.rs index ae26938..dbf1a19 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -22,6 +22,7 @@ pub(crate) struct Build { pub mode: BuildMode, // build_config: Option, pub crate_name: String, + pub out_dir: PathBuf, } /// The `BuildMode` determines which mode of initialization we are running, and @@ -63,6 +64,9 @@ pub struct BuildOptions { debug: bool, // build config from manifest // build_config: Option, + #[structopt(long = "out-dir", short = "d", default_value = "pkg")] + /// Sets the output directory with a relative path. + pub out_dir: String, } type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>; @@ -72,6 +76,7 @@ impl Build { pub fn try_from_opts(build_opts: BuildOptions) -> Result { let crate_path = set_crate_path(build_opts.path); let crate_name = manifest::get_crate_name(&crate_path)?; + let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)); let mode = match build_opts.mode.as_str() { "no-install" => BuildMode::Noinstall, _ => BuildMode::Normal, @@ -86,6 +91,7 @@ impl Build { mode, // build_config, crate_name, + out_dir, }) } @@ -106,8 +112,7 @@ impl Build { info!(&log, "Done in {}.", &duration); info!( &log, - "Your wasm pkg is ready to publish at {:#?}.", - &self.crate_path.join("pkg") + "Your wasm pkg is ready to publish at {:#?}.", &self.out_dir ); PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); @@ -115,7 +120,7 @@ impl Build { PBAR.message(&format!( "{} Your wasm pkg is ready to publish at {:#?}.", emoji::PACKAGE, - &self.crate_path.join("pkg") + self.out_dir.canonicalize().unwrap_or(self.out_dir.clone()) )); Ok(()) } @@ -185,7 +190,7 @@ impl Build { fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { info!(&log, "Creating a pkg directory..."); - create_pkg_dir(&self.crate_path, step)?; + create_pkg_dir(&self.out_dir, step)?; info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path); Ok(()) } @@ -194,6 +199,7 @@ impl Build { info!(&log, "Writing a package.json..."); manifest::write_package_json( &self.crate_path, + &self.out_dir, &self.scope, self.disable_dts, &self.target, @@ -202,19 +208,15 @@ impl Build { info!( &log, "Wrote a package.json at {:#?}.", - &self.crate_path.join("pkg").join("package.json") + &self.out_dir.join("package.json") ); Ok(()) } fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { info!(&log, "Copying readme from crate..."); - readme::copy_from_crate(&self.crate_path, step)?; - info!( - &log, - "Copied readme from crate to {:#?}.", - &self.crate_path.join("pkg") - ); + readme::copy_from_crate(&self.crate_path, &self.out_dir, step)?; + info!(&log, "Copied readme from crate to {:#?}.", &self.out_dir); Ok(()) } @@ -249,17 +251,14 @@ impl Build { info!(&log, "Building the wasm bindings..."); bindgen::wasm_bindgen_build( &self.crate_path, + &self.out_dir, &self.crate_name, self.disable_dts, &self.target, self.debug, step, )?; - info!( - &log, - "wasm bindings were built at {:#?}.", - &self.crate_path.join("pkg") - ); + info!(&log, "wasm bindings were built at {:#?}.", &self.out_dir); Ok(()) } } diff --git a/src/command/utils.rs b/src/command/utils.rs index c66b089..4ab70f3 100644 --- a/src/command/utils.rs +++ b/src/command/utils.rs @@ -19,11 +19,10 @@ pub fn set_crate_path(path: Option) -> PathBuf { } /// Construct our `pkg` directory in the crate. -pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> { +pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), Error> { let msg = format!("{}Creating a pkg directory...", emoji::FOLDER); PBAR.step(step, &msg); - let pkg_dir_path = path.join("pkg"); - fs::create_dir_all(pkg_dir_path)?; + fs::create_dir_all(&out_dir)?; Ok(()) } diff --git a/src/manifest.rs b/src/manifest.rs index 70c92fa..e62d1fb 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -146,6 +146,7 @@ impl CargoManifest { /// Generate a package.json file inside in `./pkg`. pub fn write_package_json( path: &Path, + out_dir: &Path, scope: &Option, disable_dts: bool, target: &str, @@ -161,7 +162,7 @@ pub fn write_package_json( }; PBAR.step(step, &msg); - let pkg_file_path = path.join("pkg").join("package.json"); + let pkg_file_path = out_dir.join("package.json"); let mut pkg_file = File::create(pkg_file_path)?; let crate_data = read_cargo_toml(path)?; let npm_data = crate_data.into_npm(scope, disable_dts, target); diff --git a/src/readme.rs b/src/readme.rs index 3af95b2..2c34a2e 100644 --- a/src/readme.rs +++ b/src/readme.rs @@ -9,22 +9,20 @@ use progressbar::Step; use PBAR; /// Copy the crate's README into the `pkg` directory. -pub fn copy_from_crate(path: &Path, step: &Step) -> Result<(), Error> { +pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), Error> { assert!( fs::metadata(path).ok().map_or(false, |m| m.is_dir()), "crate directory should exist" ); assert!( - fs::metadata(path.join("pkg")) - .ok() - .map_or(false, |m| m.is_dir()), + fs::metadata(&out_dir).ok().map_or(false, |m| m.is_dir()), "crate's pkg directory should exist" ); let msg = format!("{}Copying over your README...", emoji::DANCERS); PBAR.step(step, &msg); let crate_readme_path = path.join("README.md"); - let new_readme_path = path.join("pkg").join("README.md"); + let new_readme_path = out_dir.join("README.md"); if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) { PBAR.warn("origin crate has no README"); }; diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index a709b42..668e112 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -54,13 +54,14 @@ fn it_recognizes_a_map_during_depcheck() { #[test] fn it_creates_a_package_json_default_path() { let fixture = fixture::fixture("."); + let out_dir = fixture.path.join("pkg"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok()); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); - assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); - let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); + assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); assert_eq!(pkg.name, "wasm-pack"); assert_eq!(pkg.repository.ty, "git"); assert_eq!( @@ -82,13 +83,14 @@ fn it_creates_a_package_json_default_path() { #[test] fn it_creates_a_package_json_provided_path() { let fixture = fixture::fixture("tests/fixtures/js-hello-world"); + let out_dir = fixture.path.join("pkg"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok()); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); - assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); - let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); + assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); assert_eq!(pkg.name, "js-hello-world"); assert_eq!(pkg.main, "js_hello_world.js"); @@ -103,16 +105,23 @@ fn it_creates_a_package_json_provided_path() { #[test] fn it_creates_a_package_json_provided_path_with_scope() { let fixture = fixture::fixture("tests/fixtures/scopes"); + let out_dir = fixture.path.join("pkg"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( - manifest::write_package_json(&fixture.path, &Some("test".to_string()), false, "", &step) - .is_ok() + manifest::write_package_json( + &fixture.path, + &out_dir, + &Some("test".to_string()), + false, + "", + &step + ).is_ok() ); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); - assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); - let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); + assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); assert_eq!(pkg.name, "@test/scopes-hello-world"); assert_eq!(pkg.main, "scopes_hello_world.js"); @@ -127,13 +136,17 @@ fn it_creates_a_package_json_provided_path_with_scope() { #[test] fn it_creates_a_pkg_json_with_correct_files_on_node() { let fixture = fixture::fixture("."); + let out_dir = fixture.path.join("pkg"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &None, false, "nodejs", &step).is_ok()); - let package_json_path = &fixture.path.join("pkg").join("package.json"); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + assert!( + manifest::write_package_json(&fixture.path, &out_dir, &None, false, "nodejs", &step) + .is_ok() + ); + let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); - assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); - let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); + assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); assert_eq!(pkg.name, "wasm-pack"); assert_eq!(pkg.repository.ty, "git"); assert_eq!( @@ -153,16 +166,43 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { assert_eq!(actual_files, expected_files); } +#[test] +fn it_creates_a_pkg_json_in_out_dir() { + let fixture = fixture::fixture("tests/fixtures/js-hello-world"); + let out_dir = fixture.path.join("./custom/out"); + let step = wasm_pack::progressbar::Step::new(1); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok()); + + let package_json_path = &fixture.path.join(&out_dir).join("package.json"); + assert!(fs::metadata(package_json_path).is_ok()); + assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); + + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + assert_eq!(pkg.name, "js-hello-world"); + assert_eq!(pkg.main, "js_hello_world.js"); + + let actual_files: HashSet = pkg.files.into_iter().collect(); + + let expected_files: HashSet = ["js_hello_world_bg.wasm", "js_hello_world.d.ts"] + .iter() + .map(|&s| String::from(s)) + .collect(); + + assert_eq!(actual_files, expected_files); +} + #[test] fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let fixture = fixture::fixture("."); + let out_dir = fixture.path.join("pkg"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &None, true, "", &step).is_ok()); - let package_json_path = &fixture.path.join("pkg").join("package.json"); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, true, "", &step).is_ok()); + let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); - assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); - let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); + assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); assert_eq!(pkg.name, "wasm-pack"); assert_eq!(pkg.repository.ty, "git"); assert_eq!( diff --git a/tests/all/readme.rs b/tests/all/readme.rs index 9bb88b1..e8d124f 100644 --- a/tests/all/readme.rs +++ b/tests/all/readme.rs @@ -9,13 +9,14 @@ use wasm_pack::readme; #[test] fn it_copies_a_readme_default_path() { let fixture = fixture::fixture("."); - fs::create_dir(fixture.path.join("pkg")).expect("should create pkg directory OK"); + let out_dir = fixture.path.join("pkg"); + fs::create_dir(&out_dir).expect("should create pkg directory OK"); let step = wasm_pack::progressbar::Step::new(1); - assert!(readme::copy_from_crate(&fixture.path, &step).is_ok()); + assert!(readme::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); let crate_readme_path = fixture.path.join("README.md"); - let pkg_readme_path = fixture.path.join("pkg").join("README.md"); + let pkg_readme_path = out_dir.join("README.md"); println!( "wasm-pack: should have copied README.md from '{}' to '{}'", crate_readme_path.display(), @@ -33,12 +34,13 @@ fn it_copies_a_readme_default_path() { #[test] fn it_creates_a_package_json_provided_path() { let fixture = fixture::fixture("tests/fixtures/js-hello-world"); - fs::create_dir(fixture.path.join("pkg")).expect("should create pkg directory OK"); + let out_dir = fixture.path.join("pkg"); + fs::create_dir(&out_dir).expect("should create pkg directory OK"); let step = wasm_pack::progressbar::Step::new(1); - assert!(readme::copy_from_crate(&fixture.path, &step).is_ok()); + assert!(readme::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); let crate_readme_path = fixture.path.join("README.md"); - let pkg_readme_path = fixture.path.join("pkg").join("README.md"); + let pkg_readme_path = out_dir.join("README.md"); println!( "wasm-pack: should have copied README.md from '{}' to '{}'", crate_readme_path.display(), diff --git a/tests/all/utils/manifest.rs b/tests/all/utils/manifest.rs index cc3d609..8c8b008 100644 --- a/tests/all/utils/manifest.rs +++ b/tests/all/utils/manifest.rs @@ -24,8 +24,8 @@ pub struct Repository { pub url: String, } -pub fn read_package_json(path: &Path) -> Result { - let manifest_path = path.join("pkg").join("package.json"); +pub fn read_package_json(path: &Path, out_dir: &Path) -> Result { + let manifest_path = path.join(out_dir).join("package.json"); let mut pkg_file = File::open(manifest_path)?; let mut pkg_contents = String::new(); pkg_file.read_to_string(&mut pkg_contents)?;