diff --git a/src/client.rs b/src/client.rs index ad3aa3b..1007403 100644 --- a/src/client.rs +++ b/src/client.rs @@ -25,7 +25,14 @@ fn main() { }, }; - let url = format!("http://localhost:1500{}", current_dir); + let args = env::args().skip(1).collect::>().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::().unwrap(); rt::run(fetch_url(url)); @@ -33,13 +40,12 @@ fn main() { fn fetch_url(url: hyper::Uri) -> impl Future { let client = Client::new(); - let path = url.path().to_owned(); - client.get(url) + client.get(url.clone()) .map(|_| ()) // If there was an error, build in client .map_err(move |_| { eprintln!("{}", "Server not listening, building in client...".bold().yellow()); let builder = GeneralBuilder::new(); - let _ = builder.build(&path); + let _ = builder.build(&url); }) } diff --git a/src/lib.rs b/src/lib.rs index 83d2dfc..37176bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,13 +37,11 @@ pub fn contains_file(path: &str, file: &str) -> bool { /// Tries to build a certain directory using the specified builders. 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. -pub fn run_command_with_args(command: &str, path: &str, args: Vec<&str>) -> Result<(), Error> { - - let new_args: Vec = args.iter().map(|x| String::from(*x)).collect(); +pub fn run_command_with_args(command: &str, path: &str, args: &Vec) -> Result<(), Error> { let mut child = Command::new(command) .current_dir(path) @@ -55,12 +53,12 @@ pub fn run_command_with_args(command: &str, path: &str, args: Vec<&str>) -> Resu if exit.success() { Ok(()) } 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. -pub fn build(path: &PathBuf, builders: &Vec>) -> Result<(), Error> { +pub fn build(path: &PathBuf, args: &Vec, builders: &Vec>) -> Result<(), Error> { let mut path = path.clone(); @@ -74,7 +72,7 @@ pub fn build(path: &PathBuf, builders: &Vec>) -> Result<(), Error> for builder in builders { if builder.is_buildable(path.to_str().unwrap()) { - builder.build(path.to_str().unwrap())?; + builder.build(path.to_str().unwrap(), &args)?; return Ok(()); } @@ -152,8 +150,16 @@ impl GeneralBuilder { } /// Triggers a build. - pub fn build(&self, path: &str) -> Result<(), Error> { - let result = build(&PathBuf::from(path), &self.builders); + pub fn build(&self, uri: &hyper::Uri) -> Result<(), Error> { + let path = uri.path(); + + let args = if let Some(query) = uri.query() { + query.split("&").map(|x| x.to_owned()).collect::>() + } else { + vec![] + }; + + let result = build(&PathBuf::from(path), &args, &self.builders); match result { Ok(_) => self.notify_success(), @@ -210,7 +216,7 @@ pub trait Builder { fn is_buildable(&self, path: &str) -> bool; /// Trigger all the commands to build the project. - fn build(&self, path: &str) -> Result<(), Error>; + fn build(&self, path: &str, args: &Vec) -> Result<(), Error>; } /// The builder that looks for makefiles. @@ -228,8 +234,8 @@ impl Builder for MakeBuilder { contains_file(path, "Makefile") } - fn build(&self, path: &str) -> Result<(), Error> { - run_command("make", path)?; + fn build(&self, path: &str, args: &Vec) -> Result<(), Error> { + run_command_with_args("make", path, args)?; Ok(()) } } @@ -249,7 +255,7 @@ impl Builder for CMakeBuilder { contains_file(path, "CMakeLists.txt") } - fn build(&self, path: &str) -> Result<(), Error> { + fn build(&self, path: &str, args: &Vec) -> Result<(), Error> { let mut build = PathBuf::from(path); build.push("build"); @@ -261,11 +267,11 @@ impl Builder for CMakeBuilder { create_dir_all(&build)?; // 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_command("make", build.to_str().unwrap())?; + run_command_with_args("make", build.to_str().unwrap(), args)?; Ok(()) } @@ -286,7 +292,11 @@ impl Builder for CargoBuilder { contains_file(path, "Cargo.toml") } - fn build(&self, path: &str) -> Result<(), Error> { - run_command_with_args("cargo", path, vec!["build"]) + fn build(&self, path: &str, args: &Vec) -> Result<(), Error> { + if args.is_empty() { + run_command_with_args("cargo", path, &vec!["build".to_owned()]) + } else { + run_command_with_args("cargo", path, args) + } } } diff --git a/src/server.rs b/src/server.rs index 6ed3b43..9a9b471 100644 --- a/src/server.rs +++ b/src/server.rs @@ -31,7 +31,7 @@ fn main() { let clone = clone.clone(); let tx_clone = tx.clone(); 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(); Response::new(Body::from("Ok")) }); @@ -48,10 +48,10 @@ fn main() { 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()); - match builder.build(&path) { + println!("{}", format!("---- STARTING BUILD ---- from {}", uri).bold().green()); + match builder.build(&uri) { Err(_) => { println!("{}", "--------- FAIL ---------".bold().red()); },