2015-09-18 18:05:55 +08:00
|
|
|
// build.rs
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
2017-05-22 10:10:51 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate error_chain;
|
2015-09-18 18:05:55 +08:00
|
|
|
|
2017-05-22 22:35:39 +08:00
|
|
|
#[cfg(windows)]
|
|
|
|
mod execs {
|
2017-05-22 23:06:38 +08:00
|
|
|
pub const NPM: &'static str = "npm.cmd";
|
|
|
|
pub const STYLUS: &'static str = "stylus.cmd";
|
2017-05-22 22:35:39 +08:00
|
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
mod execs {
|
2017-05-22 23:06:38 +08:00
|
|
|
pub const NPM: &'static str = "npm";
|
|
|
|
pub const STYLUS: &'static str = "stylus";
|
2017-05-22 22:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-22 10:10:51 +08:00
|
|
|
error_chain!{
|
|
|
|
foreign_links {
|
|
|
|
Io(std::io::Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn program_exists(program: &str) -> Result<()> {
|
|
|
|
Command::new(program)
|
|
|
|
.arg("-v")
|
|
|
|
.output()
|
|
|
|
.chain_err(|| format!("Please install '{}'!", program))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn npm_package_exists(package: &str) -> Result<()> {
|
2017-05-22 22:35:39 +08:00
|
|
|
let status = Command::new(execs::NPM)
|
2017-05-22 10:10:51 +08:00
|
|
|
.args(&["list", "-g"])
|
|
|
|
.arg(package)
|
|
|
|
.output();
|
|
|
|
|
|
|
|
match status {
|
|
|
|
Ok(ref out) if out.status.success() => Ok(()),
|
|
|
|
_ => {
|
|
|
|
bail!("Missing npm package '{0}' \
|
|
|
|
install with: 'npm -g install {0}'",
|
|
|
|
package)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Resource<'a> {
|
|
|
|
Program(&'a str),
|
|
|
|
Package(&'a str),
|
|
|
|
}
|
|
|
|
use Resource::{Program, Package};
|
|
|
|
|
|
|
|
impl<'a> Resource<'a> {
|
|
|
|
pub fn exists(&self) -> Result<()> {
|
|
|
|
match *self {
|
|
|
|
Program(name) => program_exists(name),
|
|
|
|
Package(name) => npm_package_exists(name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run() -> Result<()> {
|
2015-09-18 18:05:55 +08:00
|
|
|
|
2015-09-24 21:37:20 +08:00
|
|
|
if let Ok(_) = env::var("CARGO_FEATURE_REGENERATE_CSS") {
|
2017-05-22 10:10:51 +08:00
|
|
|
// Check dependencies
|
2017-05-22 22:35:39 +08:00
|
|
|
Program(execs::NPM).exists()?;
|
2017-05-22 23:06:38 +08:00
|
|
|
Program("node").exists().or(Program("nodejs").exists())?;
|
2017-05-22 10:10:51 +08:00
|
|
|
Package("nib").exists()?;
|
|
|
|
Package("stylus").exists()?;
|
2015-09-18 18:05:55 +08:00
|
|
|
|
2015-09-24 21:37:20 +08:00
|
|
|
// Compile stylus stylesheet to css
|
2017-05-22 10:10:51 +08:00
|
|
|
let manifest_dir = env::var("CARGO_MANIFEST_DIR")
|
|
|
|
.chain_err(|| "Please run the script with: 'cargo build'!")?;
|
2015-09-24 21:37:20 +08:00
|
|
|
let theme_dir = Path::new(&manifest_dir).join("src/theme/");
|
|
|
|
let stylus_dir = theme_dir.join("stylus/book.styl");
|
|
|
|
|
2017-05-22 22:35:39 +08:00
|
|
|
if !Command::new(execs::STYLUS)
|
2017-05-22 10:10:51 +08:00
|
|
|
.arg(stylus_dir)
|
2015-09-24 21:37:20 +08:00
|
|
|
.arg("--out")
|
2017-05-22 10:10:51 +08:00
|
|
|
.arg(theme_dir)
|
2015-09-24 21:37:20 +08:00
|
|
|
.arg("--use")
|
|
|
|
.arg("nib")
|
2017-05-22 10:10:51 +08:00
|
|
|
.status()?
|
2015-09-24 21:37:20 +08:00
|
|
|
.success() {
|
2017-05-22 10:10:51 +08:00
|
|
|
bail!("Stylus encoutered an error");
|
2015-09-24 21:37:20 +08:00
|
|
|
}
|
2015-09-19 04:13:55 +08:00
|
|
|
}
|
2017-05-22 10:10:51 +08:00
|
|
|
Ok(())
|
2015-09-18 18:05:55 +08:00
|
|
|
}
|
2017-05-22 10:10:51 +08:00
|
|
|
|
|
|
|
quick_main!(run);
|