Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into refactor-binary-installation
Browse files Browse the repository at this point in the history
  • Loading branch information
drager committed Dec 28, 2018
2 parents 7f38ccf + cab9e6a commit a2235e5
Show file tree
Hide file tree
Showing 14 changed files with 763 additions and 216 deletions.
6 changes: 2 additions & 4 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
Make sure these boxes are checked! 📦✅

- [ ] You have the latest version of `rustfmt` installed and have your
cloned directory set to nightly
- [ ] You have the latest version of `rustfmt` installed
```bash
$ rustup override set nightly
$ rustup component add rustfmt-preview --toolchain nightly
```
- [ ] You ran `rustfmt` on the code base before submitting
- [ ] You ran `cargo fmt` on the code base before submitting
- [ ] You reference which issue is being closed in the PR text

✨✨ 😄 Thanks so much for contributing to wasm-pack! 😄 ✨✨
561 changes: 364 additions & 197 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ cargo_metadata = "0.6.0"
console = "0.6.1"
failure = "0.1.2"
human-panic = "1.0.1"
glob = "0.2"
indicatif = "0.9.0"
lazy_static = "1.1.0"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6"
serde = "1.0.74"
serde_derive = "1.0.74"
serde_ignored = "0.0.4"
serde_json = "1.0.26"
slog = "2.3"
slog-term = "2.4"
slog-async = "2.3"
strsim = "0.8.0"
structopt = "0.2"
toml = "0.4"
which = "2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion binary-install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ flate2 = "1.0.2"
hex = "0.3"
siphasher = "0.2.3"
tar = "0.4.16"
zip = "0.4.2"
zip = "0.5.0"
2 changes: 1 addition & 1 deletion docs/src/tutorial/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ further in this guide.
⚠️ If you'd rather not use a template, or are having trouble with the template, you can
do a manual setup by following [these instructions].

[these instructions]: ../project-setup/manual-setup/index.html
[these instructions]: ../project-setup/manual-setup.html
17 changes: 16 additions & 1 deletion src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use command::utils::{create_pkg_dir, set_crate_path};
use emoji;
use failure::Error;
use indicatif::HumanDuration;
use license;
use lockfile::Lockfile;
use manifest;
use progressbar::Step;
Expand Down Expand Up @@ -209,6 +210,7 @@ impl Build {
step_create_dir,
step_create_json,
step_copy_readme,
step_copy_license,
step_install_wasm_bindgen,
step_run_wasm_bindgen,
],
Expand All @@ -219,13 +221,15 @@ impl Build {
step_create_dir,
step_create_json,
step_copy_readme,
step_copy_license,
step_run_wasm_bindgen
],
BuildMode::Force => steps![
step_build_wasm,
step_create_dir,
step_create_json,
step_copy_readme,
step_copy_license,
step_run_wasm_bindgen
],
}
Expand Down Expand Up @@ -300,7 +304,18 @@ impl Build {
Ok(())
}

fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
fn step_copy_license(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
info!(&log, "Copying license from crate...");
license::copy_from_crate(&self.crate_data, &self.crate_path, &self.out_dir, step)?;
info!(&log, "Copied license from crate to {:#?}.", &self.out_dir);
Ok(())
}

fn step_install_wasm_bindgen(
&mut self,
step: &Step,
log: &Logger,
) -> Result<(), failure::Error> {
info!(&log, "Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
Expand Down
1 change: 1 addition & 0 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), failure::Error>
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg);
fs::create_dir_all(&out_dir)?;
fs::write(out_dir.join(".gitignore"), "*")?;
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

extern crate cargo_metadata;
extern crate console;
extern crate strsim;
#[macro_use]
extern crate failure;
extern crate glob;
extern crate indicatif;
extern crate which;
#[macro_use]
Expand All @@ -14,6 +16,7 @@ extern crate parking_lot;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_ignored;
extern crate serde_json;
#[macro_use]
extern crate structopt;
Expand All @@ -29,6 +32,7 @@ pub mod build;
pub mod child;
pub mod command;
pub mod emoji;
pub mod license;
pub mod lockfile;
pub mod logger;
pub mod manifest;
Expand Down
74 changes: 74 additions & 0 deletions src/license.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! Copy `LICENSE` file(s) for the packaged wasm.
use failure;
use std::fs;
use std::path::Path;

use emoji;
use glob::glob;
use manifest::CrateData;
use progressbar::Step;
use PBAR;

fn glob_license_files(path: &Path) -> Result<Vec<String>, failure::Error> {
let mut license_files: Vec<String> = Vec::new();
for entry in glob(path.join("LICENSE*").to_str().unwrap())? {
match entry {
Ok(globed_path) => {
license_files.push(String::from(
globed_path.file_name().unwrap().to_str().unwrap(),
));
}
Err(e) => println!("{:?}", e),
}
}
Ok(license_files)
}

/// Copy the crate's license into the `pkg` directory.
pub fn copy_from_crate(
crate_data: &CrateData,
path: &Path,
out_dir: &Path,
step: &Step,
) -> Result<(), failure::Error> {
assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist"
);

assert!(
fs::metadata(&out_dir).ok().map_or(false, |m| m.is_dir()),
"crate's pkg directory should exist"
);

match crate_data.crate_license() {
Some(_) => {
let msg = format!("{}Copying over your LICENSE...", emoji::DANCERS);
PBAR.step(step, &msg);
let license_files = glob_license_files(path);

match license_files {
Ok(files) => {
if files.len() == 0 {
PBAR.info("License key is set in Cargo.toml but no LICENSE file(s) were found; Please add the LICENSE file(s) to your project directory");
return Ok(());
}
for license_file in files {
let crate_license_path = path.join(&license_file);
let new_license_path = out_dir.join(&license_file);
if let Err(_) = fs::copy(&crate_license_path, &new_license_path) {
PBAR.info("origin crate has no LICENSE");
}
}
}
Err(_) => PBAR.info("origin crate has no LICENSE"),
}
}
None => {
PBAR.step(step, "No LICENSE found in Cargo.toml, skipping...");
}
};

Ok(())
}
70 changes: 65 additions & 5 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ use failure::{Error, ResultExt};
use progressbar::Step;
use serde::{self, Deserialize};
use serde_json;
use std::collections::BTreeSet;
use strsim::levenshtein;
use toml;
use PBAR;

const WASM_PACK_METADATA_KEY: &'static str = "package.metadata.wasm-pack";

/// Store for metadata learned about a crate
pub struct CrateData {
data: Metadata,
current_idx: usize,
manifest: CargoManifest,
}

#[doc(hidden)]
#[derive(Deserialize)]
struct CargoManifest {
pub struct CargoManifest {
package: CargoPackage,
}

Expand Down Expand Up @@ -196,6 +201,12 @@ struct NpmData {
main: String,
}

#[doc(hidden)]
pub struct ManifestAndUnsedKeys {
pub manifest: CargoManifest,
pub unused_keys: BTreeSet<String>,
}

impl CrateData {
/// Reads all metadata for the crate whose manifest is inside the directory
/// specified by `path`.
Expand All @@ -208,14 +219,14 @@ impl CrateData {
crate_path.display()
)
}
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest: CargoManifest = toml::from_str(&manifest)
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;

let data =
cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?;

let manifest_and_keys = CrateData::parse_crate_data(&manifest_path)?;
CrateData::warn_for_unused_keys(&manifest_and_keys);

let manifest = manifest_and_keys.manifest;
let current_idx = data
.packages
.iter()
Expand All @@ -241,6 +252,50 @@ impl CrateData {
}
}

/// Read the `manifest_path` file and deserializes it using the toml Deserializer.
/// Returns a Result containing `ManifestAndUnsedKeys` which contains `CargoManifest`
/// and a `BTreeSet<String>` containing the unused keys from the parsed file.
///
/// # Errors
/// Will return Err if the file (manifest_path) couldn't be read or
/// if deserialize to `CargoManifest` fails.
pub fn parse_crate_data(manifest_path: &Path) -> Result<ManifestAndUnsedKeys, Error> {
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest = &mut toml::Deserializer::new(&manifest);

let mut unused_keys = BTreeSet::new();
let levenshtein_threshold = 1;

let manifest: CargoManifest = serde_ignored::deserialize(manifest, |path| {
let path_string = path.to_string();

if path_string.starts_with("package.metadata")
&& (path_string.contains("wasm-pack")
|| levenshtein(WASM_PACK_METADATA_KEY, &path_string) <= levenshtein_threshold)
{
unused_keys.insert(path_string);
}
})
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;

Ok(ManifestAndUnsedKeys {
manifest,
unused_keys,
})
}

/// Iterating through all the passed `unused_keys` and output
/// a warning for each unknown key.
pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) {
manifest_and_keys.unused_keys.iter().for_each(|path| {
PBAR.warn(&format!(
"\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.",
path
));
});
}

/// Get the configured profile.
pub fn configured_profile(&self, profile: BuildProfile) -> &CargoWasmPackProfile {
match profile {
Expand Down Expand Up @@ -289,6 +344,11 @@ impl CrateData {
}
}

/// Get the license for the crate at the given path.
pub fn crate_license(&self) -> &Option<String> {
&self.manifest.package.license
}

/// Returns the path to this project's target directory where artifacts are
/// located after a cargo build.
pub fn target_directory(&self) -> &Path {
Expand Down
Loading

0 comments on commit a2235e5

Please sign in to comment.