Skip to content

Commit 9b24dcb

Browse files
committed
Execute cargo metadata at most once
This can be somewhat expensive, so make sure we don't have to chew through too much!
1 parent 3d1f528 commit 9b24dcb

File tree

5 files changed

+69
-60
lines changed

5 files changed

+69
-60
lines changed

src/command/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use command::utils::{create_pkg_dir, set_crate_path};
66
use emoji;
77
use error::Error;
88
use indicatif::HumanDuration;
9-
use lockfile;
9+
use lockfile::Lockfile;
1010
use manifest;
1111
use progressbar::Step;
1212
use readme;
@@ -240,7 +240,8 @@ impl Build {
240240

241241
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
242242
info!(&log, "Identifying wasm-bindgen dependency...");
243-
let bindgen_version = lockfile::get_wasm_bindgen_version(&self.crate_path)?;
243+
let lockfile = Lockfile::new(&self.crate_path)?;
244+
let bindgen_version = lockfile.require_wasm_bindgen()?;
244245
info!(&log, "Installing wasm-bindgen-cli...");
245246
let install_permitted = match self.mode {
246247
BuildMode::Normal => true,

src/command/test.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use super::build::BuildMode;
44
use bindgen;
55
use build;
66
use command::utils::set_crate_path;
7+
use console::style;
78
use emoji;
89
use error::Error;
910
use indicatif::HumanDuration;
10-
use lockfile;
11+
use lockfile::Lockfile;
1112
use manifest;
1213
use progressbar::Step;
1314
use slog::Logger;
@@ -239,14 +240,23 @@ impl Test {
239240

240241
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
241242
info!(&log, "Identifying wasm-bindgen dependency...");
242-
let bindgen_version = lockfile::get_wasm_bindgen_version(&self.crate_path)?;
243+
let lockfile = Lockfile::new(&self.crate_path)?;
244+
let bindgen_version = lockfile.require_wasm_bindgen()?;
243245

244246
// Unlike `wasm-bindgen` and `wasm-bindgen-cli`, `wasm-bindgen-test`
245247
// will work with any semver compatible `wasm-bindgen-cli`, so just make
246248
// sure that it is depended upon, so we can run tests on
247249
// `wasm32-unkown-unknown`. Don't enforce that it is the same version as
248250
// `wasm-bindgen`.
249-
let _bindgen_test_version = lockfile::get_wasm_bindgen_test_version(&self.crate_path)?;
251+
if lockfile.wasm_bindgen_test_version().is_none() {
252+
let message = format!(
253+
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
254+
[dev-dependencies]\n\
255+
wasm-bindgen-test = \"0.2\"",
256+
style("wasm-bindgen").bold().dim(),
257+
);
258+
return Err(Error::CrateConfig { message })
259+
}
250260

251261
let install_permitted = match self.mode {
252262
BuildMode::Normal => {

src/lockfile.rs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Reading Cargo.lock lock file.
22
3-
use std::fs::File;
4-
use std::io::Read;
3+
use std::fs;
54
use std::path::{Path, PathBuf};
65

76
use cargo_metadata;
@@ -11,7 +10,7 @@ use toml;
1110

1211
/// This struct represents the contents of `Cargo.lock`.
1312
#[derive(Clone, Debug, Deserialize)]
14-
struct Lockfile {
13+
pub struct Lockfile {
1514
package: Vec<Package>,
1615
}
1716

@@ -23,50 +22,43 @@ struct Package {
2322
}
2423

2524
impl Lockfile {
26-
fn get_package_version(&self, package: &str) -> Option<String> {
27-
self.package
28-
.iter()
29-
.find(|p| p.name == package)
30-
.map(|p| p.version.clone())
25+
/// Read the `Cargo.lock` file for the crate at the given path.
26+
pub fn new(crate_path: &Path) -> Result<Lockfile, Error> {
27+
let lock_path = get_lockfile_path(crate_path)?;
28+
let lockfile = fs::read_to_string(lock_path)?;
29+
toml::from_str(&lockfile).map_err(Error::from)
3130
}
32-
}
3331

34-
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
35-
pub fn get_wasm_bindgen_version(path: &Path) -> Result<String, Error> {
36-
let lockfile = read_cargo_lock(&path)?;
37-
lockfile.get_package_version("wasm-bindgen").ok_or_else(|| {
38-
let message = format!(
39-
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
40-
[dependencies]\n\
41-
wasm-bindgen = \"0.2\"",
42-
style("wasm-bindgen").bold().dim(),
43-
);
44-
Error::CrateConfig { message }
45-
})
46-
}
32+
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
33+
pub fn wasm_bindgen_version(&self) -> Option<&str> {
34+
self.get_package_version("wasm-bindgen")
35+
}
4736

48-
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
49-
pub fn get_wasm_bindgen_test_version(path: &Path) -> Result<String, Error> {
50-
let lockfile = read_cargo_lock(&path)?;
51-
lockfile
52-
.get_package_version("wasm-bindgen-test")
53-
.ok_or_else(|| {
37+
/// Like `wasm_bindgen_version`, except it returns an error instead of
38+
/// `None`.
39+
pub fn require_wasm_bindgen(&self) -> Result<&str, Error> {
40+
self.wasm_bindgen_version().ok_or_else(|| {
5441
let message = format!(
5542
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
56-
[dev-dependencies]\n\
57-
wasm-bindgen-test = \"0.2\"",
43+
[dependencies]\n\
44+
wasm-bindgen = \"0.2\"",
5845
style("wasm-bindgen").bold().dim(),
5946
);
6047
Error::CrateConfig { message }
6148
})
62-
}
49+
}
50+
51+
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
52+
pub fn wasm_bindgen_test_version(&self) -> Option<&str> {
53+
self.get_package_version("wasm-bindgen-test")
54+
}
6355

64-
/// Read the `Cargo.lock` file for the crate at the given path.
65-
fn read_cargo_lock(crate_path: &Path) -> Result<Lockfile, Error> {
66-
let lock_path = get_lockfile_path(crate_path)?;
67-
let mut lockfile = String::new();
68-
File::open(lock_path)?.read_to_string(&mut lockfile)?;
69-
toml::from_str(&lockfile).map_err(Error::from)
56+
fn get_package_version(&self, package: &str) -> Option<&str> {
57+
self.package
58+
.iter()
59+
.find(|p| p.name == package)
60+
.map(|p| &p.version[..])
61+
}
7062
}
7163

7264
/// Given the path to the crate that we are buliding, return a `PathBuf`

tests/all/lockfile.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
use utils::fixture;
2-
use wasm_pack::lockfile;
2+
use wasm_pack::lockfile::Lockfile;
33

44
#[test]
55
fn it_gets_wasm_bindgen_version() {
66
let fixture = fixture::js_hello_world();
77
fixture.cargo_check();
8+
let lock = Lockfile::new(&fixture.path).unwrap();
89
assert_eq!(
9-
lockfile::get_wasm_bindgen_version(&fixture.path).unwrap(),
10-
"0.2.21"
10+
lock.wasm_bindgen_version(),
11+
Some("0.2.21"),
1112
);
1213
}
1314

1415
#[test]
1516
fn it_gets_wasm_bindgen_test_version() {
1617
let fixture = fixture::wbg_test_node();
1718
fixture.cargo_check();
19+
let lock = Lockfile::new(&fixture.path).unwrap();
1820
assert_eq!(
19-
lockfile::get_wasm_bindgen_test_version(&fixture.path).unwrap(),
20-
"0.2.21"
21+
lock.wasm_bindgen_test_version(),
22+
Some("0.2.21"),
2123
);
2224
}
2325

@@ -59,9 +61,10 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
5961
"#,
6062
);
6163
fixture.cargo_check();
64+
let lock = Lockfile::new(&fixture.path.join("blah")).unwrap();
6265
assert_eq!(
63-
lockfile::get_wasm_bindgen_version(&fixture.path.join("blah")).unwrap(),
64-
"0.2.21"
66+
lock.wasm_bindgen_version(),
67+
Some("0.2.21"),
6568
);
6669
}
6770

@@ -124,8 +127,9 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
124127
"#,
125128
);
126129
fixture.cargo_check();
130+
let lock = Lockfile::new(&fixture.path.join("parent")).unwrap();
127131
assert_eq!(
128-
lockfile::get_wasm_bindgen_version(&fixture.path.join("parent")).unwrap(),
129-
"0.2.21"
132+
lock.wasm_bindgen_version(),
133+
Some("0.2.21"),
130134
);
131135
}

tests/all/utils/fixture.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::{Once, ONCE_INIT};
88
use std::thread;
99
use wasm_pack;
1010

11-
use tempfile;
11+
use tempfile::TempDir;
1212

1313
fn hard_link_or_copy<P1: AsRef<Path>, P2: AsRef<Path>>(from: P1, to: P2) -> io::Result<()> {
1414
let from = from.as_ref();
@@ -21,7 +21,7 @@ pub struct Fixture {
2121
// NB: we wrap the fixture's tempdir in a `ManuallyDrop` so that if a test
2222
// fails, its directory isn't deleted, and we have a chance to manually
2323
// inspect its state and figure out what is going on.
24-
pub dir: ManuallyDrop<tempfile::TempDir>,
24+
pub dir: ManuallyDrop<TempDir>,
2525
pub path: PathBuf,
2626
}
2727

@@ -31,18 +31,20 @@ impl Fixture {
3131
// Make sure that all fixtures end up sharing a target dir, and we don't
3232
// recompile wasm-bindgen and friends many times over.
3333
static SET_TARGET_DIR: Once = ONCE_INIT;
34+
let target_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("target");
3435
SET_TARGET_DIR.call_once(|| {
35-
env::set_var(
36-
"CARGO_TARGET_DIR",
37-
Path::new(env!("CARGO_MANIFEST_DIR")).join("target"),
38-
);
36+
env::set_var("CARGO_TARGET_DIR", &target_dir);
3937
});
4038

41-
let dir =
42-
ManuallyDrop::new(tempfile::tempdir().expect("should create temporary directory OK"));
39+
let root = target_dir.join("t");
40+
fs::create_dir_all(&root).unwrap();
41+
let dir = TempDir::new_in(&root).unwrap();
4342
let path = dir.path().join("wasm-pack");
4443
eprintln!("Created fixture at {}", path.display());
45-
Fixture { dir, path }
44+
Fixture {
45+
dir: ManuallyDrop::new(dir),
46+
path,
47+
}
4648
}
4749

4850
/// Create a file within this fixture.

0 commit comments

Comments
 (0)