Implement the watch command

This commit is contained in:
Sergey Isaev 2020-08-12 15:44:56 +03:00
parent 3980ff8e1e
commit 3e01f0c5a6
4 changed files with 65 additions and 0 deletions

View File

@ -34,6 +34,7 @@ env_logger = { version = "0.7" }
from-pest = { version = "0.3.1" }
lazy_static = { version = "1.4.0" }
log = { version = "0.4" }
notify= { version = "4.0.15" }
rand = { version = "0.7" }
rand_core = { version = "0.5.1" }
reqwest = { version = "0.10.7", features = ["blocking", "json"] }

View File

@ -39,3 +39,6 @@ pub use self::test::*;
pub mod unload;
pub use self::unload::*;
pub mod watch;
pub use self::watch::*;

59
leo/commands/watch.rs Normal file
View File

@ -0,0 +1,59 @@
use crate::{cli::CLI, cli_types::*, commands::BuildCommand, errors::CLIError};
use clap::ArgMatches;
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
use std::{sync::mpsc::channel, time::Duration};
const LEO_SOURCE_DIR: &str = "src/";
// Time interval for watching files, in seconds
const INTERVAL: u64 = 3;
pub struct WatchCommand;
impl CLI for WatchCommand {
type Options = ();
type Output = ();
const ABOUT: AboutType = "Watch the changes of the leo's source files";
const ARGUMENTS: &'static [ArgumentType] = &[];
const FLAGS: &'static [FlagType] = &[];
const NAME: NameType = "watch";
const OPTIONS: &'static [OptionType] = &[];
const SUBCOMMANDS: &'static [SubCommandType] = &[];
#[cfg_attr(tarpaulin, skip)]
fn parse(_arguments: &ArgMatches) -> Result<Self::Options, CLIError> {
Ok(())
}
fn output(_options: Self::Options) -> Result<Self::Output, CLIError> {
let (tx, rx) = channel();
let mut watcher = watcher(tx, Duration::from_secs(INTERVAL)).unwrap();
watcher.watch(LEO_SOURCE_DIR, RecursiveMode::Recursive).unwrap();
log::info!("Watching leo's source files");
loop {
match rx.recv() {
// See changes on the write event
Ok(DebouncedEvent::Write(_write)) => {
let options = ();
match BuildCommand::output(options) {
Ok(_output) => {
log::info!("Build successfully");
}
Err(e) => {
// Syntax error
log::error!("Error {:?}", e);
}
};
}
// Other events
Ok(_event) => {}
// Watch error
Err(e) => println!("watch error: {:?}", e),
}
}
// Ok(())
}
}

View File

@ -34,6 +34,7 @@ fn main() -> Result<(), CLIError> {
PublishCommand::new().display_order(11),
DeployCommand::new().display_order(12),
CleanCommand::new().display_order(13),
WatchCommand::new().display_order(14),
])
.set_term_width(0)
.get_matches();
@ -53,6 +54,7 @@ fn main() -> Result<(), CLIError> {
("publish", Some(arguments)) => PublishCommand::process(arguments),
("deploy", Some(arguments)) => DeployCommand::process(arguments),
("clean", Some(arguments)) => CleanCommand::process(arguments),
("watch", Some(arguments)) => WatchCommand::process(arguments),
_ => unreachable!(),
}
}