#![experimental]
extern crate buildable;
extern crate docopt;
extern crate serialize;
use buildable::{Buildable,BuildConfig,to_opt};
use buildable::command::{CommandExt,to_res,usable_cores};
use buildable::scm::git::GitCommand;
use docopt::Docopt;
use std::default::Default;
use std::io::fs::PathExtensions;
static USAGE: &'static str = "cargo - Rust Builder
Usage:
rub cargo [options] [<lifecycle>...]
rub cargo (-h | --help)
rub cargo --version
Options:
-d --dir <dir> Set the projects directory.
-b --branch <branch> Set the build branch. [default: master]
-t --enable-test Enable tests.
-p --prefix <prefix> Set the installation prefix.
-u --url <url> Set the SCM URL.
-h --help Show this usage.
--force-configure Force the configure lifecycle to run.
--disable-optimize Disable compile-time optimization.
--version Show cargo-rub version.";
#[deriving(Decodable)]
struct Args {
flag_dir: String,
flag_branch: String,
flag_enable_test: bool,
flag_prefix: String,
flag_url: String,
flag_force_configure: bool,
flag_disable_optimize: bool,
flag_version: bool,
flag_help: bool,
arg_lifecycle: Vec<String>,
}
fn install_deps(wd: &Path) -> Result<u8,u8> {
let mut cmd = CommandExt::new(".travis.install.deps.sh");
cmd.wd(wd);
cmd.header(true);
if cfg!(target_word_size = "64") {
cmd.env("BITS","64");
} else if cfg!(target_word_size = "32") {
cmd.env("BITS","32");
}
cmd.exec(to_res())
}
#[cfg(target_os = "linux")]
fn os_clean(wd: &Path) -> Result<u8,u8> {
let mut cmd = CommandExt::new("sudo");
cmd.wd(wd);
cmd.header(true);
cmd.args(&["make", "clean"]);
cmd.exec(to_res())
}
#[cfg(not(target_os = "linux"))]
fn os_clean(wd: &Path) -> Result<u8,u8> {
let mut cmd = CommandExt::new("make");
cmd.wd(wd);
cmd.header(true);
cmd.args(&["clean"]);
cmd.exec(to_res())
}
#[cfg(target_os = "linux")]
fn os_install(wd: &Path) -> Result<u8,u8> {
let mut jobs = String::from_str("-j");
jobs.push_str(usable_cores().to_string().as_slice());
let mut cmd = CommandExt::new("sudo");
cmd.wd(wd);
cmd.header(true);
cmd.args(&["make", jobs.as_slice(), "install"]);
cmd.exec(to_res())
}
#[cfg(not(target_os = "linux"))]
fn os_install(wd: &Path) -> Result<u8,u8> {
let mut jobs = String::from_str("-j");
jobs.push_str(usable_cores().to_string().as_slice());
let mut cmd = CommandExt::new("make");
cmd.wd(wd);
cmd.header(true);
cmd.args(&[jobs.as_slice(), "install"]);
cmd.exec(to_res())
}
#[experimental]
#[deriving(Clone,Default)]
pub struct CargoRub {
config: BuildConfig,
prefix: String,
url: String,
force_config: bool,
disable_optimize: bool,
}
impl CargoRub {
pub fn new() -> CargoRub {
Default::default()
}
}
impl Buildable for CargoRub {
fn new(&mut self, args: &Vec<String>) -> &mut CargoRub {
let dargs: Args = Docopt::new(USAGE)
.and_then(|d| Ok(d.help(false)))
.and_then(|d| d.argv(args.clone().into_iter()).decode())
.unwrap_or_else( |e| e.exit());
self.prefix = dargs.flag_prefix;
self.url = dargs.flag_url;
self.force_config = dargs.flag_force_configure;
self.disable_optimize = dargs.flag_disable_optimize;
if dargs.flag_version {
let mut cfg = BuildConfig::new();
cfg.lifecycle(vec!["version"]);
self.config = cfg;
} else if dargs.flag_help {
let mut cfg = BuildConfig::new();
cfg.lifecycle(vec!["help"]);
self.config = cfg;
} else {
let mut cfg = BuildConfig::new();
if to_opt(dargs.flag_dir.as_slice()).is_some() {
cfg.dir(Path::new(dargs.flag_dir.as_slice()));
}
cfg.project("cargo");
if to_opt(dargs.flag_branch.as_slice()).is_some() {
cfg.branch(dargs.flag_branch.as_slice());
}
cfg.test(dargs.flag_enable_test);
let lc = dargs.arg_lifecycle;
if to_opt(lc.clone()).is_some() {
let mut mylc = Vec::new();
for lc in lc.iter() {
mylc.push(lc.as_slice());
}
cfg.lifecycle(mylc);
}
self.config = cfg;
}
self
}
fn get_bc(&self) -> &BuildConfig {
&self.config
}
fn scm(&self) -> Result<u8,u8> {
let cfg = &self.config;
let base = Path::new(cfg.get_dir());
let u = if self.url.is_empty() {
"git@github.com:rust-lang/cargo.git"
} else {
self.url.as_slice()
};
let mut res: Result<u8,u8> = if !base.join(cfg.get_project()).exists() {
GitCommand::new()
.wd(base.clone())
.verbose(true)
.clone(Some(vec!["--recursive", u]), to_res())
} else {
Ok(0)
};
res = if res.is_ok() {
GitCommand::new()
.wd(base.join(cfg.get_project()))
.verbose(true)
.update_branch(self.config.get_branch())
} else {
res
};
res
}
fn clean(&self) -> Result<u8,u8> {
let cfg = &self.config;
os_clean(&Path::new(cfg.get_dir()).join(cfg.get_project()))
}
fn configure(&self) -> Result<u8,u8> {
let cfg = &self.config;
let proj_path = Path::new(cfg.get_dir()).join(cfg.get_project());
let res = if !proj_path.join("rustc").exists() || self.force_config {
install_deps(&proj_path)
} else {
Ok(0)
};
if res.is_ok() && (!proj_path.join("Makefile").exists()
|| self.force_config) {
let wd = Path::new(self.config.get_dir())
.join(self.config.get_project());
let mut cmd = CommandExt::new("configure");
cmd.wd(&wd);
cmd.header(true);
let mut prefix = String::from_str(" --prefix=");
if self.prefix.is_empty() {
prefix.push_str("/opt/cargo-");
prefix.push_str(self.config.get_branch());
} else {
prefix.push_str(self.prefix.as_slice());
}
cmd.arg(prefix.as_slice());
cmd.arg("--local-rust-root=`pwd`/rustc");
if !self.disable_optimize {
cmd.arg("--enable-optimize");
}
cmd.exec(to_res())
} else {
res
}
}
fn make(&self) -> Result<u8,u8> {
let cfg = &self.config;
let mut jobs = String::from_str("-j");
jobs.push_str(usable_cores().to_string().as_slice());
let wd = Path::new(cfg.get_dir()).join(cfg.get_project());
let mut cmd = CommandExt::new("make");
cmd.wd(&wd);
cmd.arg(jobs.as_slice());
cmd.header(true);
cmd.exec(to_res())
}
fn test(&self) -> Result<u8,u8> {
if self.config.get_test() {
let cfg = &self.config;
let mut jobs = String::from_str("-j");
jobs.push_str(usable_cores().to_string().as_slice());
let wd = Path::new(cfg.get_dir()).join(cfg.get_project());
let mut cmd = CommandExt::new("make");
cmd.wd(&wd);
cmd.args(&[jobs.as_slice(), "test"]);
cmd.header(true);
cmd.exec(to_res())
} else {
Ok(0)
}
}
fn install(&self) -> Result<u8,u8> {
let cfg = &self.config;
os_install(&Path::new(cfg.get_dir()).join(cfg.get_project()))
}
fn cleanup(&self) -> Result<u8,u8> {
Ok(0)
}
fn help(&self) -> Result<u8,u8> {
println!("{}", USAGE);
Ok(0)
}
fn version(&self) -> Result<u8,u8> {
println!("cargo_rub: 0.0.1");
Ok(0)
}
}
#[cfg(test)]
mod test {
use buildable::{Buildable,BuildConfig};
use super::CargoRub;
fn check_cr(cr: &CargoRub) {
assert_eq!(cr.prefix, "");
assert_eq!(cr.url, "");
assert!(!cr.force_config);
assert!(!cr.disable_optimize);
}
fn check_bc(bc: &BuildConfig, lc: &Vec<&str>) {
let mut tdir = env!("HOME").into_string();
tdir.push_str("/projects");
assert_eq!(bc.get_lifecycle(), lc);
assert_eq!(bc.get_dir().as_str().unwrap(), tdir.as_slice());
assert_eq!(bc.get_project(), "cargo");
assert_eq!(bc.get_branch(), "master");
assert!(!bc.get_test());
}
#[test]
fn test_new() {
let cr = CargoRub::new();
check_cr(&cr);
}
#[test]
fn test_version() {
let args = vec!["rub".into_string(),
"cargo".into_string(),
"--version".into_string()];
let mut cr = CargoRub::new();
check_cr(&cr);
let b = Buildable::new(&mut cr, &args);
let bc = b.get_bc();
assert_eq!(bc.get_lifecycle(), &vec!["version"]);
assert_eq!(b.version(), Ok(0))
}
#[test]
fn test_help() {
let args = vec!["rub".into_string(),
"cargo".into_string(),
"-h".into_string()];
let mut cr = CargoRub::new();
check_cr(&cr);
let b = Buildable::new(&mut cr, &args);
let bc = b.get_bc();
assert_eq!(bc.get_lifecycle(), &vec!["help"]);
assert_eq!(b.help(), Ok(0))
}
#[test]
fn test_base() {
let args = vec!["rub".into_string(),
"cargo".into_string()];
let mut cr = CargoRub::new();
check_cr(&cr);
let b = Buildable::new(&mut cr, &args);
let bc = b.get_bc();
check_bc(bc, &vec!["most"]);
assert_eq!(b.version(), Ok(0));
}
#[test]
fn test_scm() {
let args = vec!["rub".into_string(),
"cargo".into_string(),
"scm".into_string()];
let mut cr = CargoRub::new();
check_cr(&cr);
let b = Buildable::new(&mut cr, &args);
let bc = b.get_bc();
check_bc(bc, &vec!["scm"]);
assert_eq!(b.version(), Ok(0));
}
#[test]
fn test_all() {
let args = vec!["rub".into_string(),
"cargo".into_string(),
"all".into_string()];
let mut cr = CargoRub::new();
check_cr(&cr);
let b = Buildable::new(&mut cr, &args);
let bc = b.get_bc();
check_bc(bc, &vec!["all"]);
assert_eq!(b.version(), Ok(0));
}
}