Used the clean version with boxes

This commit is contained in:
Thomas Forgione 2018-09-25 12:08:37 +02:00
parent 7a216fabaf
commit 6b3bab306d
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
2 changed files with 101 additions and 51 deletions

View File

@ -20,7 +20,6 @@ fn main() {
};
let url = format!("http://localhost:1500{}", current_dir);
println!("{:?}", url);
let url = url.parse::<hyper::Uri>().unwrap();
rt::run(fetch_url(url));

View File

@ -4,6 +4,19 @@ use std::io;
use std::process::{Command, ExitStatus};
use std::path::PathBuf;
use std::ffi::OsStr;
use std::fs::create_dir_all;
macro_rules! destroy {
($e: expr) => {
match $e {
Err(_) => {
let err: Result<(), ()> = Err(());
return err;
},
_ => (),
}
}
}
/// Tests if a file is in a directory.
pub fn contains_file(path: &str, file: &str) -> bool {
@ -28,19 +41,17 @@ pub fn run_command(command: &str, path: &str) -> Result<ExitStatus, io::Error> {
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)
let mut child = Command::new(command)
.current_dir(path)
.args(args)
.spawn() {
Err(x) => return Err(x),
Ok(child) => child,
};
.spawn()?;
child.wait()
}
/// Tries to build a certain directory using the specified builders.
pub fn build(path: &PathBuf, builders: &Vec<Builder>) -> Result<(), ()> {
pub fn build(path: &PathBuf, builders: &Vec<Box<Builder>>) -> Result<(), ()> {
let mut path = path.clone();
@ -74,10 +85,9 @@ pub fn destroy<T, E>(result: Result<T, E>) -> Result<(), ()> {
}
/// A general builders that contains many builders and can build any type of code.
#[derive(Clone)]
pub struct GeneralBuilder {
/// The builders contained.
builders: Vec<Builder>,
builders: Vec<Box<Builder>>,
}
impl GeneralBuilder {
@ -85,8 +95,9 @@ impl GeneralBuilder {
pub fn new() -> GeneralBuilder {
GeneralBuilder {
builders: vec![
Builder::make(),
Builder::cargo(),
Box::new(MakeBuilder::new()),
Box::new(CMakeBuilder::new()),
Box::new(CargoBuilder::new()),
],
}
}
@ -97,50 +108,90 @@ impl GeneralBuilder {
}
}
/// The builder that looks for makefiles.
#[derive(Clone)]
pub struct Builder {
/// The file to lookup to check build.
file: String,
/// The command to launch to build.
command: String,
/// The arguments to the command.
args: Vec<String>,
}
impl Builder {
/// Creates a new make builder.
pub fn make() -> Builder {
Builder {
file: "Makefile".to_string(),
command: "make".to_string(),
args: vec![],
}
}
/// Creates a new cargo builder.
pub fn cargo() -> Builder {
Builder {
file: "Cargo.toml".to_string(),
command: "cargo".to_string(),
args: vec!["build".to_string()],
}
}
/// 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 {
contains_file(path, &self.file)
}
fn is_buildable(&self, path: &str) -> bool;
/// Trigger all the commands to build the project.
fn build(&self, path: &str) -> Result<(), ()> {
match run_command_with_args(&self.command, path, &self.args) {
Ok(_) => Ok(()),
Err(_) => Err(()),
}
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<(), ()> {
destroy!(run_command("make", path));
Ok(())
}
}
/// 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")
}
fn build(&self, path: &str) -> Result<(), ()> {
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
destroy!(create_dir_all(&build));
// Run cmake .. in build directory
destroy!(run_command_with_args("cmake", build.to_str().unwrap(), [".."].iter()));
}
// Run make in build directory
destroy!(run_command("make", build.to_str().unwrap()));
Ok(())
}
}
/// 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()))
}
}