Skip to content

Commit 7ce7a2a

Browse files
committed
rebase
1 parent 448a048 commit 7ce7a2a

File tree

4 files changed

+53
-194
lines changed

4 files changed

+53
-194
lines changed

src/command/build.rs

+23-44
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use manifest;
88
use progressbar::Step;
99
use readme;
1010
use slog::Logger;
11+
use std::path::PathBuf;
1112
use std::time::Instant;
1213
use PBAR;
1314

1415
/// Everything required to configure and run the `wasm-pack init` command.
1516
pub(crate) struct Build {
16-
pub crate_path: String,
17+
pub crate_path: PathBuf,
1718
pub scope: Option<String>,
1819
pub disable_dts: bool,
1920
pub target: String,
@@ -36,7 +37,8 @@ pub enum BuildMode {
3637
#[derive(Debug, StructOpt)]
3738
pub struct BuildOptions {
3839
/// The path to the Rust crate.
39-
pub path: Option<String>,
40+
#[structopt(parse(from_os_str))]
41+
pub path: Option<PathBuf>,
4042

4143
/// The npm scope to use in package.json, if any.
4244
#[structopt(long = "scope", short = "s")]
@@ -99,15 +101,16 @@ impl Build {
99101
info!(&log, "Done in {}.", &duration);
100102
info!(
101103
&log,
102-
"Your WASM pkg is ready to publish at {}/pkg.", &self.crate_path
104+
"Your WASM pkg is ready to publish at {:#?}.",
105+
&self.crate_path.join("pkg")
103106
);
104107

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

107110
PBAR.message(&format!(
108-
"{} Your WASM pkg is ready to publish at {}/pkg.",
111+
"{} Your WASM pkg is ready to publish at {:#?}.",
109112
emoji::PACKAGE,
110-
&self.crate_path
113+
&self.crate_path.join("pkg")
111114
));
112115
Ok(())
113116
}
@@ -163,23 +166,22 @@ impl Build {
163166
info!(&log, "Building wasm...");
164167
build::cargo_build_wasm(&self.crate_path, self.debug, step)?;
165168

166-
#[cfg(not(target_os = "windows"))]
167169
info!(
168170
&log,
169-
"wasm built at {}/target/wasm32-unknown-unknown/release.", &self.crate_path
170-
);
171-
#[cfg(target_os = "windows")]
172-
info!(
173-
&log,
174-
"wasm built at {}\\target\\wasm32-unknown-unknown\\release.", &self.crate_path
171+
"wasm built at {:#?}.",
172+
&self
173+
.crate_path
174+
.join("target")
175+
.join("wasm32-unknown-unknown")
176+
.join("release")
175177
);
176178
Ok(())
177179
}
178180

179181
fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
180182
info!(&log, "Creating a pkg directory...");
181183
create_pkg_dir(&self.crate_path, step)?;
182-
info!(&log, "Created a pkg directory at {}.", &self.crate_path);
184+
info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path);
183185
Ok(())
184186
}
185187

@@ -192,31 +194,21 @@ impl Build {
192194
&self.target,
193195
step,
194196
)?;
195-
#[cfg(not(target_os = "windows"))]
196-
info!(
197-
&log,
198-
"Wrote a package.json at {}/pkg/package.json.", &self.crate_path
199-
);
200-
#[cfg(target_os = "windows")]
201197
info!(
202198
&log,
203-
"Wrote a package.json at {}\\pkg\\package.json.", &self.crate_path
199+
"Wrote a package.json at {:#?}.",
200+
&self.crate_path.join("pkg").join("package.json")
204201
);
205202
Ok(())
206203
}
207204

208205
fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
209206
info!(&log, "Copying readme from crate...");
210207
readme::copy_from_crate(&self.crate_path, step)?;
211-
#[cfg(not(target_os = "windows"))]
212-
info!(
213-
&log,
214-
"Copied readme from crate to {}/pkg.", &self.crate_path
215-
);
216-
#[cfg(target_os = "windows")]
217208
info!(
218209
&log,
219-
"Copied readme from crate to {}\\pkg.", &self.crate_path
210+
"Copied readme from crate to {:#?}.",
211+
&self.crate_path.join("pkg")
220212
);
221213
Ok(())
222214
}
@@ -228,19 +220,11 @@ impl Build {
228220

229221
info!(&log, "Getting the crate name from the manifest...");
230222
self.crate_name = manifest::get_crate_name(&self.crate_path)?;
231-
#[cfg(not(target_os = "windows"))]
232-
info!(
233-
&log,
234-
"Got crate name {} from the manifest at {}/Cargo.toml.",
235-
&self.crate_name,
236-
&self.crate_path
237-
);
238-
#[cfg(target_os = "windows")]
239223
info!(
240224
&log,
241-
"Got crate name {} from the manifest at {}\\Cargo.toml.",
225+
"Got crate name {:#?} from the manifest at {:#?}.",
242226
&self.crate_name,
243-
&self.crate_path
227+
&self.crate_path.join("Cargo.toml")
244228
);
245229
Ok(())
246230
}
@@ -255,15 +239,10 @@ impl Build {
255239
self.debug,
256240
step,
257241
)?;
258-
#[cfg(not(target_os = "windows"))]
259-
info!(
260-
&log,
261-
"wasm bindings were built at {}/pkg.", &self.crate_path
262-
);
263-
#[cfg(target_os = "windows")]
264242
info!(
265243
&log,
266-
"wasm bindings were built at {}\\pkg.", &self.crate_path
244+
"wasm bindings were built at {:#?}.",
245+
&self.crate_path.join("pkg")
267246
);
268247
Ok(())
269248
}

src/command/init.rs

+15-144
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,10 @@ use manifest;
88
use progressbar::Step;
99
use readme;
1010
use slog::Logger;
11-
use std::fs;
12-
use std::path::{Path, PathBuf};
11+
use std::path::PathBuf;
1312
use std::time::Instant;
1413
use PBAR;
1514

16-
/// Construct our `pkg` directory in the crate.
17-
pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> {
18-
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
19-
PBAR.step(step, &msg);
20-
let pkg_dir_path = path.join("pkg");
21-
fs::create_dir_all(pkg_dir_path)?;
22-
Ok(())
23-
}
24-
25-
/// The `InitMode` determines which mode of initialization we are running, and
26-
/// what build and install steps we perform.
27-
pub enum InitMode {
28-
/// Perform all the build and install steps.
29-
Normal,
30-
/// Don't build the crate as a `.wasm` but do install tools and create
31-
/// meta-data.
32-
Nobuild,
33-
/// Don't install tools like `wasm-bindgen`, just use the global
34-
/// environment's existing versions to do builds.
35-
Noinstall,
36-
}
37-
3815
/// Everything required to configure and run the `wasm-pack init` command.
3916
pub struct Init {
4017
crate_path: PathBuf,
@@ -47,7 +24,8 @@ pub struct Init {
4724
#[derive(Debug, StructOpt)]
4825
pub struct InitOptions {
4926
/// The path to the Rust crate.
50-
pub path: Option<String>,
27+
#[structopt(parse(from_os_str))]
28+
pub path: Option<PathBuf>,
5129

5230
/// The npm scope to use in package.json, if any.
5331
#[structopt(long = "scope", short = "s")]
@@ -109,34 +87,18 @@ impl Init {
10987
Ok(())
11088
}
11189

112-
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
113-
info!(&log, "Checking crate configuration...");
114-
manifest::check_crate_config(&self.crate_path, step)?;
115-
info!(&log, "Crate is correctly configured.");
116-
Ok(())
117-
}
118-
119-
fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
120-
info!(&log, "Adding wasm-target...");
121-
build::rustup_add_wasm_target(step)?;
122-
info!(&log, "Adding wasm-target was successful.");
123-
Ok(())
124-
}
125-
126-
fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
127-
info!(&log, "Building wasm...");
128-
build::cargo_build_wasm(&self.crate_path, self.debug, step)?;
129-
130-
info!(
131-
&log,
132-
"wasm built at {:#?}.",
133-
&self
134-
.crate_path
135-
.join("target")
136-
.join("wasm32-unknown-unknown")
137-
.join("release")
138-
);
139-
Ok(())
90+
fn set_process_steps() -> Vec<(&'static str, InitStep)> {
91+
macro_rules! steps {
92+
($($name:ident),+) => {
93+
{
94+
let mut steps: Vec<(&'static str, InitStep)> = Vec::new();
95+
$(steps.push((stringify!($name), Init::$name));)*
96+
steps
97+
}
98+
};
99+
($($name:ident,)*) => (steps![$($name),*])
100+
}
101+
steps![step_create_dir, step_create_json, step_copy_readme,]
140102
}
141103

142104
fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
@@ -173,95 +135,4 @@ impl Init {
173135
);
174136
Ok(())
175137
}
176-
177-
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
178-
info!(&log, "Installing wasm-bindgen-cli...");
179-
bindgen::cargo_install_wasm_bindgen(step)?;
180-
info!(&log, "Installing wasm-bindgen-cli was successful.");
181-
182-
info!(&log, "Getting the crate name from the manifest...");
183-
self.crate_name = manifest::get_crate_name(&self.crate_path)?;
184-
info!(
185-
&log,
186-
"Got crate name {:#?} from the manifest at {:#?}.",
187-
&self.crate_name,
188-
&self.crate_path.join("Cargo.toml")
189-
);
190-
Ok(())
191-
}
192-
193-
fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
194-
info!(&log, "Building the wasm bindings...");
195-
bindgen::wasm_bindgen_build(
196-
&self.crate_path,
197-
&self.crate_name,
198-
self.disable_dts,
199-
&self.target,
200-
self.debug,
201-
step,
202-
)?;
203-
info!(
204-
&log,
205-
"wasm bindings were built at {:#?}.",
206-
&self.crate_path.join("pkg")
207-
);
208-
Ok(())
209-
}
210-
}
211-
212-
#[cfg(test)]
213-
mod test {
214-
use super::*;
215-
216-
#[test]
217-
fn init_normal_build() {
218-
let steps: Vec<&str> = Init::get_process_steps(InitMode::Normal)
219-
.into_iter()
220-
.map(|(n, _)| n)
221-
.collect();
222-
assert_eq!(
223-
steps,
224-
[
225-
"step_check_crate_config",
226-
"step_add_wasm_target",
227-
"step_build_wasm",
228-
"step_create_dir",
229-
"step_create_json",
230-
"step_copy_readme",
231-
"step_install_wasm_bindgen",
232-
"step_run_wasm_bindgen"
233-
]
234-
);
235-
}
236-
237-
#[test]
238-
fn init_skip_build() {
239-
let steps: Vec<&str> = Init::get_process_steps(InitMode::Nobuild)
240-
.into_iter()
241-
.map(|(n, _)| n)
242-
.collect();
243-
assert_eq!(
244-
steps,
245-
["step_create_dir", "step_create_json", "step_copy_readme"]
246-
);
247-
}
248-
249-
#[test]
250-
fn init_skip_install() {
251-
let steps: Vec<&str> = Init::get_process_steps(InitMode::Noinstall)
252-
.into_iter()
253-
.map(|(n, _)| n)
254-
.collect();
255-
assert_eq!(
256-
steps,
257-
[
258-
"step_check_crate_config",
259-
"step_build_wasm",
260-
"step_create_dir",
261-
"step_create_json",
262-
"step_copy_readme",
263-
"step_run_wasm_bindgen"
264-
]
265-
);
266-
}
267138
}

src/command/utils.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Utility functions for commands.
22
3-
use std::path::{Path, PathBuf};
43
use emoji;
54
use error::Error;
65
use progressbar::Step;
76
use std::fs;
7+
use std::path::{Path, PathBuf};
88
use PBAR;
99

1010
/// If an explicit path is given, then use it, otherwise assume the current
@@ -18,6 +18,15 @@ pub fn set_crate_path(path: Option<PathBuf>) -> PathBuf {
1818
crate_path
1919
}
2020

21+
/// Construct our `pkg` directory in the crate.
22+
pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> {
23+
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
24+
PBAR.step(step, &msg);
25+
let pkg_dir_path = path.join("pkg");
26+
fs::create_dir_all(pkg_dir_path)?;
27+
Ok(())
28+
}
29+
2130
/// Locates the pkg directory from a specific path
2231
/// Returns None if unable to find the 'pkg' directory
2332
pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> {

0 commit comments

Comments
 (0)