2018-09-25 14:51:32 +02:00
|
|
|
extern crate dirs;
|
|
|
|
extern crate notify_rust;
|
2018-09-24 14:32:12 +02:00
|
|
|
extern crate hyper;
|
2018-09-24 15:45:32 +02:00
|
|
|
|
|
|
|
use std::io;
|
|
|
|
use std::process::{Command, ExitStatus};
|
|
|
|
use std::path::PathBuf;
|
2018-09-25 14:51:32 +02:00
|
|
|
use std::fs::{File, create_dir_all};
|
|
|
|
|
|
|
|
use notify_rust::Notification;
|
|
|
|
|
|
|
|
/// The different types of error that can occur.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// No builder can build this project.
|
|
|
|
NoBuilderFound,
|
|
|
|
|
|
|
|
/// An io::Error happened while running a build command.
|
|
|
|
IoError(io::Error),
|
|
|
|
|
|
|
|
/// A command exited with non-zero exit code.
|
|
|
|
CommandError(String, Vec<String>, ExitStatus),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(e: io::Error) -> Error {
|
|
|
|
Error::IoError(e)
|
2018-09-25 12:08:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
|
|
|
|
/// Tests if a file is in a directory.
|
|
|
|
pub fn contains_file(path: &str, file: &str) -> bool {
|
|
|
|
let mut path = PathBuf::from(path);
|
|
|
|
path.push(file);
|
|
|
|
path.exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to build a certain directory using the specified builders.
|
2018-09-25 14:51:32 +02:00
|
|
|
pub fn run_command(command: &str, path: &str) -> Result<(), Error> {
|
|
|
|
run_command_with_args(command, path, vec![])
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run a build commands, and wait untils its finished, returning its result.
|
2018-09-25 14:51:32 +02:00
|
|
|
pub fn run_command_with_args(command: &str, path: &str, args: Vec<&str>) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let new_args: Vec<String> = args.iter().map(|x| String::from(*x)).collect();
|
2018-09-25 12:08:37 +02:00
|
|
|
|
|
|
|
let mut child = Command::new(command)
|
2018-09-24 15:45:32 +02:00
|
|
|
.current_dir(path)
|
|
|
|
.args(args)
|
2018-09-25 12:08:37 +02:00
|
|
|
.spawn()?;
|
2018-09-24 15:45:32 +02:00
|
|
|
|
2018-09-25 14:51:32 +02:00
|
|
|
let exit = child.wait()?;
|
|
|
|
|
|
|
|
if exit.success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::CommandError(command.to_owned(), new_args, exit))
|
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to build a certain directory using the specified builders.
|
2018-09-25 14:51:32 +02:00
|
|
|
pub fn build(path: &PathBuf, builders: &Vec<Box<Builder>>) -> Result<(), Error> {
|
2018-09-24 15:45:32 +02:00
|
|
|
|
|
|
|
let mut path = path.clone();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
|
|
|
if path.to_str().unwrap() == "/" {
|
|
|
|
// Couldn't find a buildable directory
|
2018-09-25 14:51:32 +02:00
|
|
|
return Err(Error::NoBuilderFound);
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:55:05 +02:00
|
|
|
for builder in builders {
|
2018-09-24 15:45:32 +02:00
|
|
|
|
|
|
|
if builder.is_buildable(path.to_str().unwrap()) {
|
|
|
|
builder.build(path.to_str().unwrap())?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
path.pop();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destucture a Result to return a Result<(), ()>
|
|
|
|
pub fn destroy<T, E>(result: Result<T, E>) -> Result<(), ()> {
|
|
|
|
match result {
|
|
|
|
Err(_) => Err(()),
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:55:05 +02:00
|
|
|
/// A general builders that contains many builders and can build any type of code.
|
|
|
|
pub struct GeneralBuilder {
|
2018-09-25 14:51:32 +02:00
|
|
|
/// The path to the success icon.
|
|
|
|
success: PathBuf,
|
|
|
|
|
|
|
|
/// The path to the failure icon.
|
|
|
|
failure: PathBuf,
|
|
|
|
|
2018-09-24 17:55:05 +02:00
|
|
|
/// The builders contained.
|
2018-09-25 12:08:37 +02:00
|
|
|
builders: Vec<Box<Builder>>,
|
2018-09-24 17:55:05 +02:00
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
|
2018-09-24 17:55:05 +02:00
|
|
|
impl GeneralBuilder {
|
|
|
|
/// Creates a new general builder with the defaults builders.
|
|
|
|
pub fn new() -> GeneralBuilder {
|
2018-09-25 14:51:32 +02:00
|
|
|
|
|
|
|
let mut config = PathBuf::from(dirs::config_dir().unwrap());
|
|
|
|
config.push("mars");
|
|
|
|
|
|
|
|
let mut success = config.clone();
|
|
|
|
success.push("success.png");
|
|
|
|
|
|
|
|
let mut failure = config.clone();
|
|
|
|
failure.push("failure.png");
|
|
|
|
|
|
|
|
if ! success.exists() || ! failure.exists() {
|
|
|
|
|
|
|
|
create_dir_all(&config).unwrap();
|
|
|
|
|
|
|
|
const SUCCESS_BYTES: &[u8] = include_bytes!("../assets/success.png");
|
|
|
|
const FAILURE_BYTES: &[u8] = include_bytes!("../assets/failure.png");
|
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
let mut path = config.clone();
|
|
|
|
path.push("success.png");
|
|
|
|
|
|
|
|
let mut success_file = File::create(&success).unwrap();
|
|
|
|
success_file.write_all(SUCCESS_BYTES).unwrap();
|
|
|
|
|
|
|
|
let mut path = config.clone();
|
|
|
|
path.push("success.png");
|
|
|
|
|
|
|
|
let mut failure_file = File::create(&failure).unwrap();
|
|
|
|
failure_file.write_all(FAILURE_BYTES).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:55:05 +02:00
|
|
|
GeneralBuilder {
|
2018-09-25 14:51:32 +02:00
|
|
|
success: success,
|
|
|
|
failure: failure,
|
2018-09-24 17:55:05 +02:00
|
|
|
builders: vec![
|
2018-09-25 12:08:37 +02:00
|
|
|
Box::new(MakeBuilder::new()),
|
|
|
|
Box::new(CMakeBuilder::new()),
|
|
|
|
Box::new(CargoBuilder::new()),
|
2018-09-24 17:55:05 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Triggers a build.
|
2018-09-25 14:51:32 +02:00
|
|
|
pub fn build(&self, path: &str) -> Result<(), Error> {
|
|
|
|
let result = build(&PathBuf::from(path), &self.builders);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => self.notify_success(),
|
|
|
|
Err(ref e) => self.notify_error(&e),
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2018-09-24 17:55:05 +02:00
|
|
|
}
|
2018-09-25 14:51:32 +02:00
|
|
|
|
|
|
|
/// Sends a notification of a successful build.
|
|
|
|
pub fn notify_success(&self) {
|
|
|
|
|
|
|
|
let _ = Notification::new()
|
|
|
|
.appname("Mars")
|
|
|
|
.summary("Success")
|
|
|
|
.body("Mars finished successfully")
|
|
|
|
.icon(self.success.to_str().unwrap())
|
|
|
|
.show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a notification of an error.
|
|
|
|
pub fn notify_error(&self, e: &Error) {
|
|
|
|
|
|
|
|
let body = match e {
|
|
|
|
Error::NoBuilderFound => "No builder was found for this directory".to_string(),
|
|
|
|
Error::IoError(ref e) => format!("Error while running command: {:?}", e).to_string(),
|
|
|
|
Error::CommandError(ref command, ref args, ref status) => {
|
|
|
|
let status = match status.code() {
|
|
|
|
None => "None".to_owned(),
|
|
|
|
Some(e) => e.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
format!("Command \"{} {}\" failed: {}",
|
|
|
|
command, args.join(" "), status).to_string()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = Notification::new()
|
|
|
|
.appname("Mars")
|
|
|
|
.summary("Failure")
|
|
|
|
.body(&body)
|
|
|
|
.icon(self.failure.to_str().unwrap())
|
|
|
|
.show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 12:08:37 +02:00
|
|
|
/// A generic builder. It can build some projects.
|
|
|
|
pub trait Builder {
|
|
|
|
/// Checks if a directory has corresponding files so it can build it.
|
|
|
|
/// e.g.: if there is a Makefile
|
|
|
|
fn is_buildable(&self, path: &str) -> bool;
|
|
|
|
|
|
|
|
/// Trigger all the commands to build the project.
|
2018-09-25 14:51:32 +02:00
|
|
|
fn build(&self, path: &str) -> Result<(), Error>;
|
2018-09-25 12:08:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 15:45:32 +02:00
|
|
|
/// The builder that looks for makefiles.
|
2018-09-25 12:08:37 +02:00
|
|
|
pub struct MakeBuilder;
|
|
|
|
|
|
|
|
impl MakeBuilder {
|
|
|
|
/// Creates a new make builder.
|
|
|
|
pub fn new() -> MakeBuilder {
|
|
|
|
MakeBuilder
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
|
2018-09-25 12:08:37 +02:00
|
|
|
impl Builder for MakeBuilder {
|
|
|
|
fn is_buildable(&self, path: &str) -> bool {
|
|
|
|
contains_file(path, "Makefile")
|
|
|
|
}
|
2018-09-24 17:55:05 +02:00
|
|
|
|
2018-09-25 14:51:32 +02:00
|
|
|
fn build(&self, path: &str) -> Result<(), Error> {
|
|
|
|
run_command("make", path)?;
|
2018-09-25 12:08:37 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 12:08:37 +02:00
|
|
|
/// The builder that builds cmake projects.
|
|
|
|
pub struct CMakeBuilder;
|
|
|
|
|
|
|
|
impl CMakeBuilder {
|
|
|
|
/// Creates a new cmake builder.
|
|
|
|
pub fn new() -> CMakeBuilder {
|
|
|
|
CMakeBuilder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Builder for CMakeBuilder {
|
|
|
|
fn is_buildable(&self, path: &str) -> bool {
|
|
|
|
contains_file(path, "CMakeLists.txt")
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:51:32 +02:00
|
|
|
fn build(&self, path: &str) -> Result<(), Error> {
|
2018-09-25 12:08:37 +02:00
|
|
|
|
|
|
|
let mut build = PathBuf::from(path);
|
|
|
|
build.push("build");
|
|
|
|
|
|
|
|
// If there's a build/Makefile, we'll only make
|
|
|
|
if ! contains_file(path, "build/Makefile") {
|
|
|
|
|
|
|
|
// Create build directory
|
2018-09-25 14:51:32 +02:00
|
|
|
create_dir_all(&build)?;
|
2018-09-25 12:08:37 +02:00
|
|
|
|
|
|
|
// Run cmake .. in build directory
|
2018-09-25 14:51:32 +02:00
|
|
|
run_command_with_args("cmake", build.to_str().unwrap(), vec![".."])?;
|
2018-09-24 17:55:05 +02:00
|
|
|
}
|
2018-09-25 12:08:37 +02:00
|
|
|
|
|
|
|
// Run make in build directory
|
2018-09-25 14:51:32 +02:00
|
|
|
run_command("make", build.to_str().unwrap())?;
|
2018-09-25 12:08:37 +02:00
|
|
|
Ok(())
|
2018-09-24 17:55:05 +02:00
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
|
2018-09-25 12:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The builder that looks for Cargo.toml.
|
|
|
|
pub struct CargoBuilder;
|
|
|
|
|
|
|
|
impl CargoBuilder {
|
2018-09-24 17:55:05 +02:00
|
|
|
/// Creates a new cargo builder.
|
2018-09-25 12:08:37 +02:00
|
|
|
pub fn new() -> CargoBuilder {
|
|
|
|
CargoBuilder
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
2018-09-25 12:08:37 +02:00
|
|
|
}
|
2018-09-24 15:45:32 +02:00
|
|
|
|
2018-09-25 12:08:37 +02:00
|
|
|
impl Builder for CargoBuilder {
|
2018-09-24 15:45:32 +02:00
|
|
|
fn is_buildable(&self, path: &str) -> bool {
|
2018-09-25 12:08:37 +02:00
|
|
|
contains_file(path, "Cargo.toml")
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 14:51:32 +02:00
|
|
|
fn build(&self, path: &str) -> Result<(), Error> {
|
|
|
|
run_command_with_args("cargo", path, vec!["build"])
|
2018-09-24 15:45:32 +02:00
|
|
|
}
|
|
|
|
}
|