Skip to content

Commit e41850c

Browse files
feat(build): add rustc check
1 parent 3dbd058 commit e41850c

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ indicatif = "0.9.0"
1919
lazy_static = "1.1.0"
2020
openssl = { version = '0.10.11', optional = true }
2121
parking_lot = "0.6"
22+
rustc_version = "0.2.3"
2223
serde = "1.0.74"
2324
serde_derive = "1.0.74"
2425
serde_json = "1.0.26"

src/build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
use emoji;
44
use error::Error;
55
use progressbar::Step;
6+
use rustc_version::{version, Version};
67
use std::path::Path;
78
use std::process::Command;
89
use PBAR;
910

11+
/// Ensure that `rustc` is present and that it is >= 1.30.0
12+
pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
13+
let msg = format!("{}Checking `rustc` version...", emoji::TARGET);
14+
PBAR.step(step, &msg);
15+
let local_version = version()?;
16+
let minimum_version = Version::parse("1.30.0").unwrap();
17+
if local_version < minimum_version {
18+
return Err(Error::RustcVersion {
19+
message: format!("Your version of Rust, '{}', is not supported.", local_version.to_string()),
20+
local_version: local_version.to_string(),
21+
});
22+
}
23+
Ok(local_version.to_string())
24+
}
25+
1026
/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
1127
/// the `nightly` toolchain.
1228
pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> {

src/command/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl Build {
155155
}
156156
match &mode {
157157
BuildMode::Normal => steps![
158+
step_check_rustc_version,
158159
step_check_crate_config,
159160
step_add_wasm_target,
160161
step_build_wasm,
@@ -165,6 +166,7 @@ impl Build {
165166
step_run_wasm_bindgen,
166167
],
167168
BuildMode::Noinstall => steps![
169+
step_check_rustc_version,
168170
step_check_crate_config,
169171
step_build_wasm,
170172
step_create_dir,
@@ -175,6 +177,14 @@ impl Build {
175177
}
176178
}
177179

180+
fn step_check_rustc_version(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
181+
info!(&log, "Checking rustc version...");
182+
let version = build::check_rustc_version(step)?;
183+
let msg = format!("rustc version is {}.", version);
184+
info!(&log, "{}", &msg);
185+
Ok(())
186+
}
187+
178188
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
179189
info!(&log, "Checking crate configuration...");
180190
manifest::check_crate_config(&self.crate_path, step)?;

src/error.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Code related to error handling for wasm-pack
22
use curl;
3+
use rustc_version;
34
use serde_json;
45
use std::borrow::Cow;
56
use std::io;
@@ -29,6 +30,19 @@ pub enum Error {
2930
/// An error handling zip archives.
3031
Zip(#[cause] zip::result::ZipError),
3132

33+
#[fail(display = "{}", _0)]
34+
/// An error in parsing your rustc version.
35+
Rustc(#[cause] rustc_version::Error),
36+
37+
#[fail(display = "{}", _0)]
38+
/// An error from having an unsupported rustc version.
39+
RustcVersion {
40+
/// Error message
41+
message: String,
42+
/// The version of the local rust
43+
local_version: String,
44+
},
45+
3246
/// An error invoking another CLI tool.
3347
#[fail(display = "{}. stderr:\n\n{}", message, stderr)]
3448
Cli {
@@ -112,13 +126,26 @@ impl Error {
112126
}
113127
}
114128

129+
/// Construct a rustc version error.
130+
pub fn rustc_version_error(message: &str, local_version: &str) -> Self {
131+
Error::RustcVersion {
132+
message: message.to_string(),
133+
local_version: local_version.to_string(),
134+
}
135+
}
136+
115137
/// Get a string description of this error's type.
116138
pub fn error_type(&self) -> String {
117139
match self {
118140
Error::Io(_) => "There was an I/O error. Details:\n\n",
119141
Error::SerdeJson(_) => "There was an JSON error. Details:\n\n",
120142
Error::SerdeToml(_) => "There was an TOML error. Details:\n\n",
121143
Error::Zip(_) => "There was an error handling zip files. Details:\n\n",
144+
Error::Rustc(_) => "Your rustc version isn't parsable- which means you might not have Rust installed. Please install Rust version 1.30.0.",
145+
Error::RustcVersion {
146+
message: _,
147+
local_version: _,
148+
} => "Your rustc version is not supported. Please install version 1.30.0 or higher.",
122149
Error::Cli {
123150
message: _,
124151
stderr: _,
@@ -161,6 +188,12 @@ impl From<zip::result::ZipError> for Error {
161188
}
162189
}
163190

191+
impl From<rustc_version::Error> for Error {
192+
fn from(e: rustc_version::Error) -> Self {
193+
Error::Rustc(e)
194+
}
195+
}
196+
164197
impl From<toml::de::Error> for Error {
165198
fn from(e: toml::de::Error) -> Self {
166199
Error::SerdeToml(e)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern crate indicatif;
1111
#[macro_use]
1212
extern crate lazy_static;
1313
extern crate parking_lot;
14+
extern crate rustc_version;
1415
#[macro_use]
1516
extern crate serde_derive;
1617
extern crate serde_json;

0 commit comments

Comments
 (0)