From 52ef41b9aac9e4a0b1b4b701a331e0d58ced2b74 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 2 Apr 2019 14:18:10 +0200 Subject: [PATCH] Works but needs modifications --- src/lib.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 08549d9..a3864c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,13 +2,16 @@ extern crate log; use std::{io, thread}; +use std::env::current_dir; use std::process::{Command, ExitStatus}; use std::path::{Path, PathBuf}; use std::fs::{File, create_dir_all}; use std::sync::mpsc::channel; use std::time::Duration; -use notify::{Watcher, RecursiveMode, DebouncedEvent, watcher}; +use colored::*; + +use notify::{Watcher, RecursiveMode, watcher}; use notify_rust::Notification; @@ -109,20 +112,35 @@ pub fn builder_arguments_from_string(uri: &str) -> (PathBuf, Vec) { /// Watches a directory and builds it when a modification occurs. pub fn watch>(p: P) -> Result<(), Error> { - let path = PathBuf::from(p.as_ref()); + let mut path = current_dir()?; + path.push(p.as_ref()); + thread::spawn(move || { let (tx, rx) = channel(); - let mut watcher = watcher(tx, Duration::from_secs(10)).unwrap(); + let mut watcher = watcher(tx, Duration::from_millis(200)).unwrap(); watcher.watch(&path, RecursiveMode::Recursive).unwrap(); + info!("watching {}", path.display()); + loop { match rx.recv() { - Ok(DebouncedEvent::NoticeWrite(_)) => { + Ok(_) => { let builder = GeneralBuilder::new(); - builder.build(&path, &vec![]).ok(); + + let start_string = format!("---- STARTING BUILD ---- from {}", path.display()); + println!("{}", start_string.bold().green()); + + match builder.build(&path, &vec![]) { + Err(_) => { + println!("{}", "--------- FAIL ---------".bold().red()); + }, + Ok(_) => { + println!("{}", "----- SUCCESSFUL -----".bold().green()); + }, + }; + println!(); }, Err(e) => error!("watch error: {:?}", e), - _ => (), } } });