|
|
@ -1,7 +1,6 @@ |
|
|
|
//! Reading Cargo.lock lock file.
|
|
|
|
//! Reading Cargo.lock lock file.
|
|
|
|
|
|
|
|
|
|
|
|
use std::fs::File; |
|
|
|
use std::fs; |
|
|
|
use std::io::Read; |
|
|
|
|
|
|
|
use std::path::{Path, PathBuf}; |
|
|
|
use std::path::{Path, PathBuf}; |
|
|
|
|
|
|
|
|
|
|
|
use cargo_metadata; |
|
|
|
use cargo_metadata; |
|
|
@ -11,7 +10,7 @@ use toml; |
|
|
|
|
|
|
|
|
|
|
|
/// This struct represents the contents of `Cargo.lock`.
|
|
|
|
/// This struct represents the contents of `Cargo.lock`.
|
|
|
|
#[derive(Clone, Debug, Deserialize)] |
|
|
|
#[derive(Clone, Debug, Deserialize)] |
|
|
|
struct Lockfile { |
|
|
|
pub struct Lockfile { |
|
|
|
package: Vec<Package>, |
|
|
|
package: Vec<Package>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -23,50 +22,43 @@ struct Package { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Lockfile { |
|
|
|
impl Lockfile { |
|
|
|
fn get_package_version(&self, package: &str) -> Option<String> { |
|
|
|
/// Read the `Cargo.lock` file for the crate at the given path.
|
|
|
|
self.package |
|
|
|
pub fn new(crate_path: &Path) -> Result<Lockfile, Error> { |
|
|
|
.iter() |
|
|
|
let lock_path = get_lockfile_path(crate_path)?; |
|
|
|
.find(|p| p.name == package) |
|
|
|
let lockfile = fs::read_to_string(lock_path)?; |
|
|
|
.map(|p| p.version.clone()) |
|
|
|
toml::from_str(&lockfile).map_err(Error::from) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
|
|
|
|
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
|
|
|
|
pub fn get_wasm_bindgen_version(path: &Path) -> Result<String, Error> { |
|
|
|
pub fn wasm_bindgen_version(&self) -> Option<&str> { |
|
|
|
let lockfile = read_cargo_lock(&path)?; |
|
|
|
self.get_package_version("wasm-bindgen") |
|
|
|
lockfile.get_package_version("wasm-bindgen").ok_or_else(|| { |
|
|
|
} |
|
|
|
let message = format!( |
|
|
|
|
|
|
|
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ |
|
|
|
|
|
|
|
[dependencies]\n\ |
|
|
|
|
|
|
|
wasm-bindgen = \"0.2\"", |
|
|
|
|
|
|
|
style("wasm-bindgen").bold().dim(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
Error::CrateConfig { message } |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
|
|
|
|
/// Like `wasm_bindgen_version`, except it returns an error instead of
|
|
|
|
pub fn get_wasm_bindgen_test_version(path: &Path) -> Result<String, Error> { |
|
|
|
/// `None`.
|
|
|
|
let lockfile = read_cargo_lock(&path)?; |
|
|
|
pub fn require_wasm_bindgen(&self) -> Result<&str, Error> { |
|
|
|
lockfile |
|
|
|
self.wasm_bindgen_version().ok_or_else(|| { |
|
|
|
.get_package_version("wasm-bindgen-test") |
|
|
|
|
|
|
|
.ok_or_else(|| { |
|
|
|
|
|
|
|
let message = format!( |
|
|
|
let message = format!( |
|
|
|
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ |
|
|
|
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ |
|
|
|
[dev-dependencies]\n\ |
|
|
|
[dependencies]\n\ |
|
|
|
wasm-bindgen-test = \"0.2\"", |
|
|
|
wasm-bindgen = \"0.2\"", |
|
|
|
style("wasm-bindgen").bold().dim(), |
|
|
|
style("wasm-bindgen").bold().dim(), |
|
|
|
); |
|
|
|
); |
|
|
|
Error::CrateConfig { message } |
|
|
|
Error::CrateConfig { message } |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
|
|
|
|
|
|
|
|
pub fn wasm_bindgen_test_version(&self) -> Option<&str> { |
|
|
|
|
|
|
|
self.get_package_version("wasm-bindgen-test") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Read the `Cargo.lock` file for the crate at the given path.
|
|
|
|
fn get_package_version(&self, package: &str) -> Option<&str> { |
|
|
|
fn read_cargo_lock(crate_path: &Path) -> Result<Lockfile, Error> { |
|
|
|
self.package |
|
|
|
let lock_path = get_lockfile_path(crate_path)?; |
|
|
|
.iter() |
|
|
|
let mut lockfile = String::new(); |
|
|
|
.find(|p| p.name == package) |
|
|
|
File::open(lock_path)?.read_to_string(&mut lockfile)?; |
|
|
|
.map(|p| &p.version[..]) |
|
|
|
toml::from_str(&lockfile).map_err(Error::from) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Given the path to the crate that we are buliding, return a `PathBuf`
|
|
|
|
/// Given the path to the crate that we are buliding, return a `PathBuf`
|
|
|
|