Working on makefile and cargo builders

This commit is contained in:
Thomas Forgione 2018-09-24 15:45:32 +02:00
parent b7bb4f5964
commit 65dd9b5ead
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
2 changed files with 145 additions and 1 deletions

View File

@ -1,3 +1,13 @@
extern crate mars;
use std::env::current_dir;
use std::process::exit;
use mars::{Builder, MakeBuilder, CargoBuilder};
fn main() {
println!("Hello, world!");
let mut current_dir = current_dir()
.expect("Cannot read current directory");
}

View File

@ -1 +1,135 @@
extern crate hyper;
use std::io;
use std::process::{Command, ExitStatus};
use std::path::PathBuf;
use std::ffi::OsStr;
/// 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.
pub fn run_command(command: &str, path: &str) -> Result<ExitStatus, io::Error> {
let mut child = match Command::new(command)
.current_dir(path)
.spawn() {
Err(x) => return Err(x),
Ok(child) => child,
};
child.wait()
}
/// Run a build commands, and wait untils its finished, returning its result.
pub fn run_command_with_args<I, S>(command: &str, path: &str, args: I) -> Result<ExitStatus, io::Error> where
I: IntoIterator<Item=S>,
S: AsRef<OsStr> {
let mut child = match Command::new(command)
.current_dir(path)
.args(args)
.spawn() {
Err(x) => return Err(x),
Ok(child) => child,
};
child.wait()
}
/// Tries to build a certain directory using the specified builders.
pub fn build(path: &PathBuf, builders: Vec<Box<Builder>>) -> Result<(), ()> {
let mut path = path.clone();
loop {
if path.to_str().unwrap() == "/" {
// Couldn't find a buildable directory
return Err(());
}
for builder in &builders {
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(()),
}
}
/// 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.
fn build(&self, path: &str) -> Result<(), ()>;
}
/// The builder that looks for makefiles.
pub struct MakeBuilder {
}
impl MakeBuilder {
/// Creates a new make builder.
pub fn new() -> MakeBuilder {
MakeBuilder {
}
}
}
impl Builder for MakeBuilder {
fn is_buildable(&self, path: &str) -> bool {
contains_file(path, "Makefile")
}
fn build(&self, path: &str) -> Result<(), ()> {
match run_command("make", path) {
Ok(_) => Ok(()),
Err(_) => Err(()),
}
}
}
/// The builder that looks for Cargo.toml.
pub struct CargoBuilder {
}
impl CargoBuilder {
/// Creates a new cargo builder.
pub fn new() -> CargoBuilder {
CargoBuilder {
}
}
}
impl Builder for CargoBuilder {
fn is_buildable(&self, path: &str) -> bool {
contains_file(path, "Cargo.toml")
}
fn build(&self, path: &str) -> Result<(), ()> {
destroy(run_command_with_args("cargo", path, ["build"].iter()))
}
}