Merge pull request #32 from ashleygwilliams/copy-readme

feat(readme): copy from crate
master
ashley williams 7 years ago committed by GitHub
commit 23369ce4d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 53
      Cargo.lock
  2. 3
      Cargo.toml
  3. 2
      examples/js-hello-world/README.md
  4. 86
      src/lib.rs
  5. 10
      src/main.rs
  6. 82
      src/manifest.rs
  7. 10
      src/readme.rs
  8. 1
      tests/fixtures/package-json/package.json
  9. 38
      tests/manifest.rs
  10. 17
      tests/readme.rs
  11. 32
      tests/wasm-pack.rs

53
Cargo.lock generated

@ -89,14 +89,6 @@ dependencies = [
"scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dir-diff"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dtoa"
version = "0.4.2"
@ -162,15 +154,6 @@ name = "itoa"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.11"
@ -364,15 +347,6 @@ name = "rustc-demangle"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "same-file"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scopeguard"
version = "0.3.3"
@ -565,21 +539,10 @@ name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "walkdir"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wasm-pack"
version = "0.1.0"
dependencies = [
"dir-diff 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quicli 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
@ -588,11 +551,6 @@ dependencies = [
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.4"
@ -602,11 +560,6 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
@ -637,7 +590,6 @@ dependencies = [
"checksum chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c20ebe0b2b08b0aeddba49c609fe7957ba2e33449882cb186a180bc60682fa9"
"checksum clap 2.29.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7b8f59bcebcfe4269b09f71dab0da15b355c75916a8f975d3876ce81561893ee"
"checksum coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c06169f5beb7e31c7c67ebf5540b8b472d23e3eade3b2ec7d1f5b504a85f91bd"
"checksum dir-diff 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "86ea5bba0129e6a7ff7fc710a49f3234c2f940f9baa26e87cf9a837ca62e7b82"
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
"checksum either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "740178ddf48b1a9e878e6d6509a1442a2d42fd2928aae8e7a6f8a36fb01981b3"
"checksum env_logger 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f15f0b172cb4f52ed5dbf47f774a387cd2315d1bf7894ab5af9b083ae27efa5a"
@ -647,7 +599,6 @@ dependencies = [
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121"
@ -672,7 +623,6 @@ dependencies = [
"checksum regex 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "5be5347bde0c48cfd8c3fdc0766cdfe9d8a755ef84d620d6794c778c91de8b2b"
"checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e"
"checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e"
"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526"
"checksum serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ba7591cfe93755e89eeecdbcc668885624829b020050e6aec99c2a03bd3fd0"
@ -698,10 +648,7 @@ dependencies = [
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767"

@ -14,6 +14,3 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.4"
[dev-dependencies]
dir-diff = "0.2.0"

@ -0,0 +1,2 @@
# js-hello-world
> an example rust -> wasm project

@ -4,91 +4,17 @@ extern crate serde_derive;
extern crate serde_json;
extern crate toml;
pub mod build;
pub mod bindgen;
pub mod readme;
pub mod manifest;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use failure::Error;
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
}
#[derive(Deserialize)]
struct CargoPackage {
name: String,
description: String,
version: String,
license: String,
repository: String,
}
#[derive(Serialize)]
struct NpmPackage {
name: String,
description: String,
version: String,
license: String,
repository: Repository,
files: Vec<String>,
}
#[derive(Serialize)]
struct Repository {
#[serde(rename = "type")]
ty: String,
url: String,
}
fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
let path_to_manifest = format!("{}/Cargo.toml", path);
println!("👩🍳 reading {}", path_to_manifest);
let mut cargo_file = File::open(path_to_manifest)?;
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;
Ok(toml::from_str(&cargo_contents)?)
}
impl CargoManifest {
fn into_npm(self) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let js_file = format!("{}.js", filename);
let wasm_file = format!("{}_bg.wasm", filename);
NpmPackage {
name: self.package.name,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: Repository {
ty: "git".to_string(),
url: self.package.repository,
},
files: vec![js_file, wasm_file],
}
}
}
fn create_pkg_dir(path: &str) -> Result<(), Error> {
pub fn create_pkg_dir(path: &str) -> Result<(), Error> {
let path_to_pkg_dir = format!("{}/pkg", path);
fs::create_dir_all(path_to_pkg_dir)?;
Ok(())
}
/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(path: &str) -> Result<(), Error> {
create_pkg_dir(path)?;
let path_to_pkg_file = format!("{}/pkg/package.json", path);
let mut pkg_file = File::create(path_to_pkg_file)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm();
let npm_json = serde_json::to_string(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
println!("✍ wrote a package.json!");
Ok(())
}
pub fn get_crate_name(path: &str) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}

@ -1,9 +1,9 @@
extern crate failure;
#[macro_use]
extern crate quicli;
extern crate wasm_pack;
mod build;
mod bindgen;
use wasm_pack::{bindgen, build, manifest, readme};
use quicli::prelude::*;
@ -38,9 +38,11 @@ main!(|args: Cli, log_level: verbosity| match args.cmd {
};
build::rustup_add_wasm_target();
build::cargo_build_wasm(&crate_path);
wasm_pack::write_package_json(&crate_path)?;
wasm_pack::create_pkg_dir(&crate_path)?;
manifest::write_package_json(&crate_path)?;
readme::copy_from_crate(&crate_path)?;
bindgen::cargo_install_wasm_bindgen();
let name = wasm_pack::get_crate_name(&crate_path)?;
let name = manifest::get_crate_name(&crate_path)?;
bindgen::wasm_bindgen_build(&crate_path, &name);
}
Command::Pack { .. } => {

@ -0,0 +1,82 @@
use std::fs::File;
use std::io::prelude::*;
use failure::Error;
use serde_json;
use toml;
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
}
#[derive(Deserialize)]
struct CargoPackage {
name: String,
description: String,
version: String,
license: String,
repository: String,
}
#[derive(Serialize)]
struct NpmPackage {
name: String,
description: String,
version: String,
license: String,
repository: Repository,
files: Vec<String>,
}
#[derive(Serialize)]
struct Repository {
#[serde(rename = "type")]
ty: String,
url: String,
}
fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
let path_to_manifest = format!("{}/Cargo.toml", path);
println!("👩<200d>🍳 reading {}", path_to_manifest);
let mut cargo_file = File::open(path_to_manifest)?;
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;
Ok(toml::from_str(&cargo_contents)?)
}
impl CargoManifest {
fn into_npm(self) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let js_file = format!("{}.js", filename);
let wasm_file = format!("{}_bg.wasm", filename);
NpmPackage {
name: self.package.name,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: Repository {
ty: "git".to_string(),
url: self.package.repository,
},
files: vec![js_file, wasm_file],
}
}
}
/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(path: &str) -> Result<(), Error> {
let path_to_pkg_file = format!("{}/pkg/package.json", path);
let mut pkg_file = File::create(path_to_pkg_file)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm();
let npm_json = serde_json::to_string(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
println!("✍ wrote a package.json!");
Ok(())
}
pub fn get_crate_name(path: &str) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}

@ -0,0 +1,10 @@
use std::fs;
use std::fs::File;
use failure::Error;
pub fn copy_from_crate(path: &str) -> Result<(), Error> {
let crate_readme_path = format!("{}/README.md", path);
let new_readme_path = format!("{}/pkg/README.md", path);
fs::copy(&crate_readme_path, &new_readme_path)?;
Ok(())
}

@ -1 +0,0 @@
{"name":"wasm-pack","description":"pack up the wasm and publish it to npm!","version":"0.1.0","license":"MIT/Apache-2.0","repository":{"type":"git","url":"https://github.com/ashleygwilliams/wasm-pack.git"},"files":["wasm_pack.js","wasm_pack_bg.wasm"]}

@ -0,0 +1,38 @@
extern crate wasm_pack;
use std::fs;
use wasm_pack::manifest;
#[test]
fn it_gets_the_crate_name_default_path() {
assert!(manifest::get_crate_name(".").is_ok());
assert_eq!(manifest::get_crate_name(".").unwrap(), "wasm-pack");
}
#[test]
fn it_gets_the_crate_name_provided_path() {
assert!(manifest::get_crate_name("./examples/js-hello-world").is_ok());
assert_eq!(
manifest::get_crate_name("./examples/js-hello-world").unwrap(),
"js-hello-world"
);
}
#[test]
fn it_creates_a_package_json_default_path() {
let path = ".".to_string();
wasm_pack::create_pkg_dir(&path).unwrap();
assert!(manifest::write_package_json(&path).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
}
#[test]
fn it_creates_a_package_json_provided_path() {
let path = "./examples/js-hello-world".to_string();
wasm_pack::create_pkg_dir(&path).unwrap();
assert!(manifest::write_package_json(&path).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
}

@ -0,0 +1,17 @@
extern crate wasm_pack;
use std::fs;
use wasm_pack::readme;
#[test]
fn it_copies_a_readme_default_path() {
assert!(readme::copy_from_crate(".").is_ok());
assert!(fs::metadata("./pkg/README.md").is_ok());
}
#[test]
fn it_creates_a_package_json_provided_path() {
assert!(readme::copy_from_crate("./examples/js-hello-world").is_ok());
assert!(fs::metadata("./examples/js-hello-world/pkg/README.md").is_ok());
}

@ -1,32 +0,0 @@
extern crate dir_diff;
extern crate wasm_pack;
#[test]
fn it_gets_the_crate_name_default_path() {
assert!(wasm_pack::get_crate_name(".").is_ok());
assert_eq!(wasm_pack::get_crate_name(".").unwrap(), "wasm-pack");
}
#[test]
fn it_gets_the_crate_name_provided_path() {
assert!(wasm_pack::get_crate_name("./examples/js-hello-world").is_ok());
assert_eq!(
wasm_pack::get_crate_name("./examples/js-hello-world").unwrap(),
"js-hello-world"
);
}
#[test]
fn it_creates_a_package_json_default_path() {
assert!(wasm_pack::write_package_json(".").is_ok());
assert!(!dir_diff::is_different("./pkg", "./test/fixtures/package-json").unwrap());
}
#[test]
fn it_creates_a_package_json_provided_path() {
assert!(wasm_pack::write_package_json("./examples/js-hello-world").is_ok());
assert!(!dir_diff::is_different(
"./examples/js-hello-world/pkg",
"./test/fixtures/package-json"
).unwrap());
}
Loading…
Cancel
Save