Support arguments

This commit is contained in:
Thomas Forgione 2018-09-25 15:42:41 +02:00
parent 96189faf3f
commit 482966462d
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 41 additions and 25 deletions

View File

@ -25,7 +25,14 @@ fn main() {
}, },
}; };
let url = format!("http://localhost:1500{}", current_dir); let args = env::args().skip(1).collect::<Vec<_>>().join("&");
let url = if args.is_empty() {
format!("http://localhost:1500{}", current_dir)
} else {
format!("http://localhost:1500{}?{}", current_dir, args)
};
let url = url.parse::<hyper::Uri>().unwrap(); let url = url.parse::<hyper::Uri>().unwrap();
rt::run(fetch_url(url)); rt::run(fetch_url(url));
@ -33,13 +40,12 @@ fn main() {
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> { fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new(); let client = Client::new();
let path = url.path().to_owned(); client.get(url.clone())
client.get(url)
.map(|_| ()) .map(|_| ())
// If there was an error, build in client // If there was an error, build in client
.map_err(move |_| { .map_err(move |_| {
eprintln!("{}", "Server not listening, building in client...".bold().yellow()); eprintln!("{}", "Server not listening, building in client...".bold().yellow());
let builder = GeneralBuilder::new(); let builder = GeneralBuilder::new();
let _ = builder.build(&path); let _ = builder.build(&url);
}) })
} }

View File

@ -37,13 +37,11 @@ pub fn contains_file(path: &str, file: &str) -> bool {
/// Tries to build a certain directory using the specified builders. /// Tries to build a certain directory using the specified builders.
pub fn run_command(command: &str, path: &str) -> Result<(), Error> { pub fn run_command(command: &str, path: &str) -> Result<(), Error> {
run_command_with_args(command, path, vec![]) run_command_with_args(command, path, &vec![])
} }
/// Run a build commands, and wait untils its finished, returning its result. /// Run a build commands, and wait untils its finished, returning its result.
pub fn run_command_with_args(command: &str, path: &str, args: Vec<&str>) -> Result<(), Error> { pub fn run_command_with_args(command: &str, path: &str, args: &Vec<String>) -> Result<(), Error> {
let new_args: Vec<String> = args.iter().map(|x| String::from(*x)).collect();
let mut child = Command::new(command) let mut child = Command::new(command)
.current_dir(path) .current_dir(path)
@ -55,12 +53,12 @@ pub fn run_command_with_args(command: &str, path: &str, args: Vec<&str>) -> Resu
if exit.success() { if exit.success() {
Ok(()) Ok(())
} else { } else {
Err(Error::CommandError(command.to_owned(), new_args, exit)) Err(Error::CommandError(command.to_owned(), args.clone(), exit))
} }
} }
/// Tries to build a certain directory using the specified builders. /// Tries to build a certain directory using the specified builders.
pub fn build(path: &PathBuf, builders: &Vec<Box<Builder>>) -> Result<(), Error> { pub fn build(path: &PathBuf, args: &Vec<String>, builders: &Vec<Box<Builder>>) -> Result<(), Error> {
let mut path = path.clone(); let mut path = path.clone();
@ -74,7 +72,7 @@ pub fn build(path: &PathBuf, builders: &Vec<Box<Builder>>) -> Result<(), Error>
for builder in builders { for builder in builders {
if builder.is_buildable(path.to_str().unwrap()) { if builder.is_buildable(path.to_str().unwrap()) {
builder.build(path.to_str().unwrap())?; builder.build(path.to_str().unwrap(), &args)?;
return Ok(()); return Ok(());
} }
@ -152,8 +150,16 @@ impl GeneralBuilder {
} }
/// Triggers a build. /// Triggers a build.
pub fn build(&self, path: &str) -> Result<(), Error> { pub fn build(&self, uri: &hyper::Uri) -> Result<(), Error> {
let result = build(&PathBuf::from(path), &self.builders); let path = uri.path();
let args = if let Some(query) = uri.query() {
query.split("&").map(|x| x.to_owned()).collect::<Vec<_>>()
} else {
vec![]
};
let result = build(&PathBuf::from(path), &args, &self.builders);
match result { match result {
Ok(_) => self.notify_success(), Ok(_) => self.notify_success(),
@ -210,7 +216,7 @@ pub trait Builder {
fn is_buildable(&self, path: &str) -> bool; fn is_buildable(&self, path: &str) -> bool;
/// Trigger all the commands to build the project. /// Trigger all the commands to build the project.
fn build(&self, path: &str) -> Result<(), Error>; fn build(&self, path: &str, args: &Vec<String>) -> Result<(), Error>;
} }
/// The builder that looks for makefiles. /// The builder that looks for makefiles.
@ -228,8 +234,8 @@ impl Builder for MakeBuilder {
contains_file(path, "Makefile") contains_file(path, "Makefile")
} }
fn build(&self, path: &str) -> Result<(), Error> { fn build(&self, path: &str, args: &Vec<String>) -> Result<(), Error> {
run_command("make", path)?; run_command_with_args("make", path, args)?;
Ok(()) Ok(())
} }
} }
@ -249,7 +255,7 @@ impl Builder for CMakeBuilder {
contains_file(path, "CMakeLists.txt") contains_file(path, "CMakeLists.txt")
} }
fn build(&self, path: &str) -> Result<(), Error> { fn build(&self, path: &str, args: &Vec<String>) -> Result<(), Error> {
let mut build = PathBuf::from(path); let mut build = PathBuf::from(path);
build.push("build"); build.push("build");
@ -261,11 +267,11 @@ impl Builder for CMakeBuilder {
create_dir_all(&build)?; create_dir_all(&build)?;
// Run cmake .. in build directory // Run cmake .. in build directory
run_command_with_args("cmake", build.to_str().unwrap(), vec![".."])?; run_command_with_args("cmake", build.to_str().unwrap(), &vec!["..".to_owned()])?;
} }
// Run make in build directory // Run make in build directory
run_command("make", build.to_str().unwrap())?; run_command_with_args("make", build.to_str().unwrap(), args)?;
Ok(()) Ok(())
} }
@ -286,7 +292,11 @@ impl Builder for CargoBuilder {
contains_file(path, "Cargo.toml") contains_file(path, "Cargo.toml")
} }
fn build(&self, path: &str) -> Result<(), Error> { fn build(&self, path: &str, args: &Vec<String>) -> Result<(), Error> {
run_command_with_args("cargo", path, vec!["build"]) if args.is_empty() {
run_command_with_args("cargo", path, &vec!["build".to_owned()])
} else {
run_command_with_args("cargo", path, args)
}
} }
} }

View File

@ -31,7 +31,7 @@ fn main() {
let clone = clone.clone(); let clone = clone.clone();
let tx_clone = tx.clone(); let tx_clone = tx.clone();
let r = service_fn_ok(move |r| { let r = service_fn_ok(move |r| {
clone.lock().unwrap().push(String::from(r.uri().path())); clone.lock().unwrap().push(r.uri().clone());
tx_clone.send(()).unwrap(); tx_clone.send(()).unwrap();
Response::new(Body::from("Ok")) Response::new(Body::from("Ok"))
}); });
@ -48,10 +48,10 @@ fn main() {
let mut tasks = tasks.lock().unwrap(); let mut tasks = tasks.lock().unwrap();
while let Some(path) = tasks.pop() { while let Some(uri) = tasks.pop() {
println!("{}", format!("---- STARTING BUILD ---- from {}", path).bold().green()); println!("{}", format!("---- STARTING BUILD ---- from {}", uri).bold().green());
match builder.build(&path) { match builder.build(&uri) {
Err(_) => { Err(_) => {
println!("{}", "--------- FAIL ---------".bold().red()); println!("{}", "--------- FAIL ---------".bold().red());
}, },