Skip to content

Commit 42b8fec

Browse files
Merge pull request #220 from Mackiovello/issue/213
Use PathBuf instead of String to handle paths
2 parents cc4016a + 37fb0bc commit 42b8fec

File tree

13 files changed

+110
-110
lines changed

13 files changed

+110
-110
lines changed

src/bindgen.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use emoji;
44
use error::Error;
55
use progressbar::Step;
6+
use std::path::Path;
67
use std::process::Command;
78
use PBAR;
89

@@ -30,7 +31,7 @@ pub fn cargo_install_wasm_bindgen(step: &Step) -> Result<(), Error> {
3031
/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
3132
/// `.wasm`.
3233
pub fn wasm_bindgen_build(
33-
path: &str,
34+
path: &Path,
3435
name: &str,
3536
disable_dts: bool,
3637
target: &str,

src/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use emoji;
44
use error::Error;
55
use progressbar::Step;
6+
use std::path::Path;
67
use std::process::Command;
78
use PBAR;
89

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

4748
/// Run `cargo build` with the `nightly` toolchain and targetting
4849
/// `wasm32-unknown-unknown`.
49-
pub fn cargo_build_wasm(path: &str, debug: bool, step: &Step) -> Result<(), Error> {
50+
pub fn cargo_build_wasm(path: &Path, debug: bool, step: &Step) -> Result<(), Error> {
5051
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
5152
PBAR.step(step, &msg);
5253
let output = {

src/command/init.rs

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ use progressbar::Step;
1111
use readme;
1212
use slog::Logger;
1313
use std::fs;
14+
use std::path::{Path, PathBuf};
1415
use std::time::Instant;
1516
use PBAR;
1617

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

3940
/// Everything required to configure and run the `wasm-pack init` command.
4041
pub struct Init {
41-
crate_path: String,
42+
crate_path: PathBuf,
4243
scope: Option<String>,
4344
disable_dts: bool,
4445
target: String,
@@ -51,7 +52,7 @@ type InitStep = fn(&mut Init, &Step, &Logger) -> Result<(), Error>;
5152
impl Init {
5253
/// Construct a new `Init` command.
5354
pub fn new(
54-
path: Option<String>,
55+
path: Option<PathBuf>,
5556
scope: Option<String>,
5657
disable_dts: bool,
5758
target: String,
@@ -121,15 +122,16 @@ impl Init {
121122
info!(&log, "Done in {}.", &duration);
122123
info!(
123124
&log,
124-
"Your WASM pkg is ready to publish at {}/pkg.", &self.crate_path
125+
"Your WASM pkg is ready to publish at {:#?}.",
126+
&self.crate_path.join("pkg")
125127
);
126128

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

129131
PBAR.message(&format!(
130-
"{} Your WASM pkg is ready to publish at {}/pkg.",
132+
"{} Your WASM pkg is ready to publish at {:#?}.",
131133
emoji::PACKAGE,
132-
&self.crate_path
134+
&self.crate_path.join("pkg")
133135
));
134136
Ok(())
135137
}
@@ -152,23 +154,22 @@ impl Init {
152154
info!(&log, "Building wasm...");
153155
build::cargo_build_wasm(&self.crate_path, self.debug, step)?;
154156

155-
#[cfg(not(target_os = "windows"))]
156157
info!(
157158
&log,
158-
"wasm built at {}/target/wasm32-unknown-unknown/release.", &self.crate_path
159-
);
160-
#[cfg(target_os = "windows")]
161-
info!(
162-
&log,
163-
"wasm built at {}\\target\\wasm32-unknown-unknown\\release.", &self.crate_path
159+
"wasm built at {:#?}.",
160+
&self
161+
.crate_path
162+
.join("target")
163+
.join("wasm32-unknown-unknown")
164+
.join("release")
164165
);
165166
Ok(())
166167
}
167168

168169
fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
169170
info!(&log, "Creating a pkg directory...");
170171
create_pkg_dir(&self.crate_path, step)?;
171-
info!(&log, "Created a pkg directory at {}.", &self.crate_path);
172+
info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path);
172173
Ok(())
173174
}
174175

@@ -181,31 +182,21 @@ impl Init {
181182
&self.target,
182183
step,
183184
)?;
184-
#[cfg(not(target_os = "windows"))]
185-
info!(
186-
&log,
187-
"Wrote a package.json at {}/pkg/package.json.", &self.crate_path
188-
);
189-
#[cfg(target_os = "windows")]
190185
info!(
191186
&log,
192-
"Wrote a package.json at {}\\pkg\\package.json.", &self.crate_path
187+
"Wrote a package.json at {:#?}.",
188+
&self.crate_path.join("pkg").join("package.json")
193189
);
194190
Ok(())
195191
}
196192

197193
fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
198194
info!(&log, "Copying readme from crate...");
199195
readme::copy_from_crate(&self.crate_path, step)?;
200-
#[cfg(not(target_os = "windows"))]
201-
info!(
202-
&log,
203-
"Copied readme from crate to {}/pkg.", &self.crate_path
204-
);
205-
#[cfg(target_os = "windows")]
206196
info!(
207197
&log,
208-
"Copied readme from crate to {}\\pkg.", &self.crate_path
198+
"Copied readme from crate to {:#?}.",
199+
&self.crate_path.join("pkg")
209200
);
210201
Ok(())
211202
}
@@ -217,19 +208,11 @@ impl Init {
217208

218209
info!(&log, "Getting the crate name from the manifest...");
219210
self.crate_name = manifest::get_crate_name(&self.crate_path)?;
220-
#[cfg(not(target_os = "windows"))]
221-
info!(
222-
&log,
223-
"Got crate name {} from the manifest at {}/Cargo.toml.",
224-
&self.crate_name,
225-
&self.crate_path
226-
);
227-
#[cfg(target_os = "windows")]
228211
info!(
229212
&log,
230-
"Got crate name {} from the manifest at {}\\Cargo.toml.",
213+
"Got crate name {:#?} from the manifest at {:#?}.",
231214
&self.crate_name,
232-
&self.crate_path
215+
&self.crate_path.join("Cargo.toml")
233216
);
234217
Ok(())
235218
}
@@ -244,15 +227,10 @@ impl Init {
244227
self.debug,
245228
step,
246229
)?;
247-
#[cfg(not(target_os = "windows"))]
248-
info!(
249-
&log,
250-
"wasm bindings were built at {}/pkg.", &self.crate_path
251-
);
252-
#[cfg(target_os = "windows")]
253230
info!(
254231
&log,
255-
"wasm bindings were built at {}\\pkg.", &self.crate_path
232+
"wasm bindings were built at {:#?}.",
233+
&self.crate_path.join("pkg")
256234
);
257235
Ok(())
258236
}

src/command/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use self::pack::pack;
1212
use self::publish::publish;
1313
use error::Error;
1414
use slog::Logger;
15+
use std::path::PathBuf;
1516
use std::result;
1617
use PBAR;
1718

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

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

5659
#[structopt(name = "publish")]
5760
/// 🎆 pack up your npm package and publish!
5861
Publish {
5962
/// The path to the Rust crate.
60-
path: Option<String>,
63+
#[structopt(parse(from_os_str))]
64+
path: Option<PathBuf>,
6165
},
6266

6367
#[structopt(name = "login", alias = "adduser", alias = "add-user")]

src/command/pack.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@ use command::utils::{find_pkg_directory, set_crate_path};
22
use error::Error;
33
use npm;
44
use slog::Logger;
5+
use std::path::PathBuf;
56
use std::result;
67
use PBAR;
78

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

1314
info!(&log, "Packing up the npm package...");
1415
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
1516
message: format!(
16-
"Unable to find the pkg directory at path '{}', or in a child directory of '{}'",
17+
"Unable to find the pkg directory at path {:#?}, or in a child directory of {:#?}",
1718
&crate_path, &crate_path
1819
),
1920
})?;
2021
npm::npm_pack(&pkg_directory.to_string_lossy())?;
21-
#[cfg(not(target_os = "windows"))]
22-
info!(&log, "Your package is located at {}/pkg", &crate_path);
23-
#[cfg(target_os = "windows")]
24-
info!(&log, "Your package is located at {}\\pkg", &crate_path);
22+
info!(
23+
&log,
24+
"Your package is located at {:#?}",
25+
crate_path.join("pkg")
26+
);
2527

2628
PBAR.message("🎒 packed up your package!");
2729
Ok(())

src/command/publish.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ use command::utils::{find_pkg_directory, set_crate_path};
22
use error::Error;
33
use npm;
44
use slog::Logger;
5+
use std::path::PathBuf;
56
use std::result;
67
use PBAR;
78

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

1314
info!(&log, "Publishing the npm package...");
1415
info!(&log, "npm info located in the npm debug log");
1516
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
1617
message: format!(
17-
"Unable to find the pkg directory at path '{}', or in a child directory of '{}'",
18+
"Unable to find the pkg directory at path '{:#?}', or in a child directory of '{:#?}'",
1819
&crate_path, &crate_path
1920
),
2021
})?;

src/command/utils.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ use std::path::{Path, PathBuf};
44

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

1313
crate_path
1414
}
1515

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

2423
path.read_dir().ok().and_then(|entries| {

src/manifest.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::fs::File;
44
use std::io::prelude::*;
5+
use std::path::Path;
56

67
use console::style;
78
use emoji;
@@ -60,8 +61,8 @@ struct Repository {
6061
url: String,
6162
}
6263

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

126127
/// Generate a package.json file inside in `./pkg`.
127128
pub fn write_package_json(
128-
path: &str,
129+
path: &Path,
129130
scope: &Option<String>,
130131
disable_dts: bool,
131132
target: &str,
@@ -141,7 +142,7 @@ pub fn write_package_json(
141142
};
142143

143144
PBAR.step(step, &msg);
144-
let pkg_file_path = format!("{}/pkg/package.json", path);
145+
let pkg_file_path = path.join("pkg").join("package.json");
145146
let mut pkg_file = File::create(pkg_file_path)?;
146147
let crate_data = read_cargo_toml(path)?;
147148
let npm_data = crate_data.into_npm(scope, disable_dts, target);
@@ -162,20 +163,20 @@ pub fn write_package_json(
162163
}
163164

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

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

178-
fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
179+
fn check_wasm_bindgen(path: &Path) -> Result<(), Error> {
179180
if read_cargo_toml(path)?.dependencies.map_or(false, |x| {
180181
!x.wasm_bindgen.unwrap_or("".to_string()).is_empty()
181182
}) {
@@ -187,7 +188,7 @@ fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
187188
))
188189
}
189190

190-
fn check_crate_type(path: &str) -> Result<(), Error> {
191+
fn check_crate_type(path: &Path) -> Result<(), Error> {
191192
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
192193
lib.crate_type
193194
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))

0 commit comments

Comments
 (0)