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

Use PathBuf instead of String to handle paths #220

Merged
merged 3 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use emoji;
use error::Error;
use progressbar::Step;
use std::path::Path;
use std::process::Command;
use PBAR;

Expand Down Expand Up @@ -30,7 +31,7 @@ pub fn cargo_install_wasm_bindgen(step: &Step) -> Result<(), Error> {
/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
/// `.wasm`.
pub fn wasm_bindgen_build(
path: &str,
path: &Path,
name: &str,
disable_dts: bool,
target: &str,
Expand Down
3 changes: 2 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use emoji;
use error::Error;
use progressbar::Step;
use std::path::Path;
use std::process::Command;
use PBAR;

Expand Down Expand Up @@ -46,7 +47,7 @@ fn ensure_nightly() -> Result<(), Error> {

/// Run `cargo build` with the `nightly` toolchain and targetting
/// `wasm32-unknown-unknown`.
pub fn cargo_build_wasm(path: &str, debug: bool, step: &Step) -> Result<(), Error> {
pub fn cargo_build_wasm(path: &Path, debug: bool, step: &Step) -> Result<(), Error> {
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
let output = {
Expand Down
70 changes: 24 additions & 46 deletions src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use progressbar::Step;
use readme;
use slog::Logger;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;
use PBAR;

/// Construct our `pkg` directory in the crate.
pub fn create_pkg_dir(path: &str, step: &Step) -> Result<(), Error> {
pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> {
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg);
let pkg_dir_path = format!("{}/pkg", path);
let pkg_dir_path = path.join("pkg");
fs::create_dir_all(pkg_dir_path)?;
Ok(())
}
Expand All @@ -38,7 +39,7 @@ pub enum InitMode {

/// Everything required to configure and run the `wasm-pack init` command.
pub struct Init {
crate_path: String,
crate_path: PathBuf,
scope: Option<String>,
disable_dts: bool,
target: String,
Expand All @@ -51,7 +52,7 @@ type InitStep = fn(&mut Init, &Step, &Logger) -> Result<(), Error>;
impl Init {
/// Construct a new `Init` command.
pub fn new(
path: Option<String>,
path: Option<PathBuf>,
scope: Option<String>,
disable_dts: bool,
target: String,
Expand Down Expand Up @@ -121,15 +122,16 @@ impl Init {
info!(&log, "Done in {}.", &duration);
info!(
&log,
"Your WASM pkg is ready to publish at {}/pkg.", &self.crate_path
"Your WASM pkg is ready to publish at {:#?}.",
&self.crate_path.join("pkg")
);

PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration));

PBAR.message(&format!(
"{} Your WASM pkg is ready to publish at {}/pkg.",
"{} Your WASM pkg is ready to publish at {:#?}.",
emoji::PACKAGE,
&self.crate_path
&self.crate_path.join("pkg")
));
Ok(())
}
Expand All @@ -152,23 +154,22 @@ impl Init {
info!(&log, "Building wasm...");
build::cargo_build_wasm(&self.crate_path, self.debug, step)?;

#[cfg(not(target_os = "windows"))]
info!(
&log,
"wasm built at {}/target/wasm32-unknown-unknown/release.", &self.crate_path
);
#[cfg(target_os = "windows")]
info!(
&log,
"wasm built at {}\\target\\wasm32-unknown-unknown\\release.", &self.crate_path
"wasm built at {:#?}.",
&self
.crate_path
.join("target")
.join("wasm32-unknown-unknown")
.join("release")
);
Ok(())
}

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)?;
info!(&log, "Created a pkg directory at {}.", &self.crate_path);
info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path);
Ok(())
}

Expand All @@ -181,31 +182,21 @@ impl Init {
&self.target,
step,
)?;
#[cfg(not(target_os = "windows"))]
info!(
&log,
"Wrote a package.json at {}/pkg/package.json.", &self.crate_path
);
#[cfg(target_os = "windows")]
info!(
&log,
"Wrote a package.json at {}\\pkg\\package.json.", &self.crate_path
"Wrote a package.json at {:#?}.",
&self.crate_path.join("pkg").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)?;
#[cfg(not(target_os = "windows"))]
info!(
&log,
"Copied readme from crate to {}/pkg.", &self.crate_path
);
#[cfg(target_os = "windows")]
info!(
&log,
"Copied readme from crate to {}\\pkg.", &self.crate_path
"Copied readme from crate to {:#?}.",
&self.crate_path.join("pkg")
);
Ok(())
}
Expand All @@ -217,19 +208,11 @@ impl Init {

info!(&log, "Getting the crate name from the manifest...");
self.crate_name = manifest::get_crate_name(&self.crate_path)?;
#[cfg(not(target_os = "windows"))]
info!(
&log,
"Got crate name {} from the manifest at {}/Cargo.toml.",
&self.crate_name,
&self.crate_path
);
#[cfg(target_os = "windows")]
info!(
&log,
"Got crate name {} from the manifest at {}\\Cargo.toml.",
"Got crate name {:#?} from the manifest at {:#?}.",
&self.crate_name,
&self.crate_path
&self.crate_path.join("Cargo.toml")
);
Ok(())
}
Expand All @@ -244,15 +227,10 @@ impl Init {
self.debug,
step,
)?;
#[cfg(not(target_os = "windows"))]
info!(
&log,
"wasm bindings were built at {}/pkg.", &self.crate_path
);
#[cfg(target_os = "windows")]
info!(
&log,
"wasm bindings were built at {}\\pkg.", &self.crate_path
"wasm bindings were built at {:#?}.",
&self.crate_path.join("pkg")
);
Ok(())
}
Expand Down
12 changes: 8 additions & 4 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use self::pack::pack;
use self::publish::publish;
use error::Error;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use PBAR;

Expand All @@ -22,7 +23,8 @@ pub enum Command {
/// 🐣 initialize a package.json based on your compiled wasm!
Init {
/// The path to the Rust crate.
path: Option<String>,
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,

/// The npm scope to use in package.json, if any.
#[structopt(long = "scope", short = "s")]
Expand All @@ -49,15 +51,17 @@ pub enum Command {
#[structopt(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish!
Pack {
/// The path to the Rust crate.
path: Option<String>,
/// The path to the Rust crate.
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
},

#[structopt(name = "publish")]
/// πŸŽ† pack up your npm package and publish!
Publish {
/// The path to the Rust crate.
path: Option<String>,
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
},

#[structopt(name = "login", alias = "adduser", alias = "add-user")]
Expand Down
14 changes: 8 additions & 6 deletions src/command/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ use command::utils::{find_pkg_directory, set_crate_path};
use error::Error;
use npm;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use PBAR;

/// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<String>, log: &Logger) -> result::Result<(), Error> {
pub fn pack(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path);

info!(&log, "Packing up the npm package...");
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
message: format!(
"Unable to find the pkg directory at path '{}', or in a child directory of '{}'",
"Unable to find the pkg directory at path {:?}, or in a child directory of {:?}",
&crate_path, &crate_path
),
})?;
npm::npm_pack(&pkg_directory.to_string_lossy())?;
#[cfg(not(target_os = "windows"))]
info!(&log, "Your package is located at {}/pkg", &crate_path);
#[cfg(target_os = "windows")]
info!(&log, "Your package is located at {}\\pkg", &crate_path);
info!(
&log,
"Your package is located at {:#?}",
crate_path.join("pkg")
);

PBAR.message("πŸŽ’ packed up your package!");
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions src/command/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ use command::utils::{find_pkg_directory, set_crate_path};
use error::Error;
use npm;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use PBAR;

/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(path: Option<String>, log: &Logger) -> result::Result<(), Error> {
pub fn publish(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path);

info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log");
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
message: format!(
"Unable to find the pkg directory at path '{}', or in a child directory of '{}'",
"Unable to find the pkg directory at path '{:#?}', or in a child directory of '{:#?}'",
&crate_path, &crate_path
),
})?;
Expand Down
11 changes: 5 additions & 6 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ use std::path::{Path, PathBuf};

/// If an explicit path is given, then use it, otherwise assume the current
/// directory is the crate path.
pub fn set_crate_path(path: Option<String>) -> String {
pub fn set_crate_path(path: Option<PathBuf>) -> PathBuf {
let crate_path = match path {
Some(p) => p,
None => ".".to_string(),
None => PathBuf::from("."),
};

crate_path
}

/// Locates the pkg directory from a specific path
/// Returns None if unable to find the 'pkg' directory
pub fn find_pkg_directory(guess_path: &str) -> Option<PathBuf> {
let path = PathBuf::from(guess_path);
if is_pkg_directory(&path) {
return Some(path);
pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> {
if is_pkg_directory(path) {
return Some(path.to_owned());
}

path.read_dir().ok().and_then(|entries| {
Expand Down
17 changes: 9 additions & 8 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

use console::style;
use emoji;
Expand Down Expand Up @@ -60,8 +61,8 @@ struct Repository {
url: String,
}

fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
let manifest_path = format!("{}/Cargo.toml", path);
fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
let manifest_path = path.join("Cargo.toml");
let mut cargo_file = File::open(manifest_path)?;
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;
Expand Down Expand Up @@ -125,7 +126,7 @@ impl CargoManifest {

/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(
path: &str,
path: &Path,
scope: &Option<String>,
disable_dts: bool,
target: &str,
Expand All @@ -141,7 +142,7 @@ pub fn write_package_json(
};

PBAR.step(step, &msg);
let pkg_file_path = format!("{}/pkg/package.json", path);
let pkg_file_path = path.join("pkg").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);
Expand All @@ -162,20 +163,20 @@ pub fn write_package_json(
}

/// Get the crate name for the crate at the given path.
pub fn get_crate_name(path: &str) -> Result<String, Error> {
pub fn get_crate_name(path: &Path) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}

/// Check that the crate the given path is properly configured.
pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> {
pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
check_wasm_bindgen(path)?;
check_crate_type(path)?;
Ok(())
}

fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
fn check_wasm_bindgen(path: &Path) -> Result<(), Error> {
if read_cargo_toml(path)?.dependencies.map_or(false, |x| {
!x.wasm_bindgen.unwrap_or("".to_string()).is_empty()
}) {
Expand All @@ -187,7 +188,7 @@ fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
))
}

fn check_crate_type(path: &str) -> Result<(), Error> {
fn check_crate_type(path: &Path) -> Result<(), Error> {
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
Expand Down
Loading