mirror of
https://github.com/watchexec/watchexec.git
synced 2024-09-29 22:51:33 +02:00
Extract command invocation to runner module
This commit is contained in:
parent
c990c9c4d4
commit
db677c77ec
54
src/main.rs
54
src/main.rs
@ -3,44 +3,17 @@ extern crate libc;
|
|||||||
extern crate notify;
|
extern crate notify;
|
||||||
|
|
||||||
mod notification_filter;
|
mod notification_filter;
|
||||||
|
mod runner;
|
||||||
|
|
||||||
use std::sync::mpsc::{channel, Receiver, RecvError};
|
use std::sync::mpsc::{channel, Receiver, RecvError};
|
||||||
use std::{env, io, thread, time};
|
use std::{env, thread, time};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::{Child, Command};
|
|
||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use notify::{Event, RecommendedWatcher, Watcher};
|
use notify::{Event, RecommendedWatcher, Watcher};
|
||||||
|
|
||||||
use notification_filter::NotificationFilter;
|
use notification_filter::NotificationFilter;
|
||||||
|
use runner::Runner;
|
||||||
fn clear() {
|
|
||||||
// TODO: determine better way to do this
|
|
||||||
let clear_cmd;
|
|
||||||
if cfg!(target_os = "windows") {
|
|
||||||
clear_cmd = "cls";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
clear_cmd = "clear";
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = Command::new(clear_cmd).status();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn invoke(cmd: &str) -> io::Result<Child> {
|
|
||||||
if cfg!(target_os = "windows") {
|
|
||||||
Command::new("cmd.exe")
|
|
||||||
.arg("/C")
|
|
||||||
.arg(cmd)
|
|
||||||
.spawn()
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Command::new("sh")
|
|
||||||
.arg("-c")
|
|
||||||
.arg(cmd)
|
|
||||||
.spawn()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wait(rx: &Receiver<Event>, filter: &NotificationFilter, verbose: bool) -> Result<Event, RecvError> {
|
fn wait(rx: &Receiver<Event>, filter: &NotificationFilter, verbose: bool) -> Result<Event, RecvError> {
|
||||||
loop {
|
loop {
|
||||||
@ -167,32 +140,15 @@ fn main() {
|
|||||||
|
|
||||||
let cmd_parts: Vec<&str> = args.values_of("command").unwrap().collect();
|
let cmd_parts: Vec<&str> = args.values_of("command").unwrap().collect();
|
||||||
let cmd = cmd_parts.join(" ");
|
let cmd = cmd_parts.join(" ");
|
||||||
let restart = args.is_present("restart");
|
let mut runner = Runner::new(args.is_present("restart"), args.is_present("clear"), verbose);
|
||||||
let mut child_process: Option<Child> = None;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let e = wait(&rx, &filter, verbose).expect("error when waiting for filesystem changes");
|
let e = wait(&rx, &filter, verbose).expect("error when waiting for filesystem changes");
|
||||||
|
|
||||||
if let Some(mut child) = child_process {
|
|
||||||
if restart {
|
|
||||||
let _ = child.kill();
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = child.wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.is_present("clear") {
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
println!("*** {:?}: {:?}", e.op, e.path);
|
println!("*** {:?}: {:?}", e.op, e.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if verbose {
|
runner.run_command(&cmd);
|
||||||
println!("*** Executing: {}", cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
child_process = invoke(&cmd).ok();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
74
src/runner.rs
Normal file
74
src/runner.rs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
use std::process::{Child, Command};
|
||||||
|
|
||||||
|
pub struct Runner {
|
||||||
|
process: Option<Child>,
|
||||||
|
restart: bool,
|
||||||
|
cls: bool,
|
||||||
|
verbose: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Runner {
|
||||||
|
pub fn new(restart: bool, clear: bool, verbose: bool) -> Runner {
|
||||||
|
Runner {
|
||||||
|
process: None,
|
||||||
|
restart: restart,
|
||||||
|
cls: clear,
|
||||||
|
verbose: verbose
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear(&self) {
|
||||||
|
// TODO: determine better way to do this
|
||||||
|
let clear_cmd;
|
||||||
|
if cfg!(target_os = "windows") {
|
||||||
|
clear_cmd = "cls";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
clear_cmd = "clear";
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = Command::new(clear_cmd).status();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn invoke(&self, cmd: &str) -> Option<Child> {
|
||||||
|
let shell;
|
||||||
|
let shell_cmd_arg;
|
||||||
|
|
||||||
|
if cfg!(target_os = "windows") {
|
||||||
|
shell = "cmd.exe";
|
||||||
|
shell_cmd_arg = "/C";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
shell = "sh";
|
||||||
|
shell_cmd_arg = "-c";
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::new(shell)
|
||||||
|
.arg(shell_cmd_arg)
|
||||||
|
.arg(cmd)
|
||||||
|
.spawn()
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn run_command(&mut self, cmd: &str) {
|
||||||
|
if let Some(ref mut child) = self.process {
|
||||||
|
if self.restart {
|
||||||
|
let _ = child.kill();
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = child.wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.cls {
|
||||||
|
self.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.verbose {
|
||||||
|
println!("*** Executing: {}", cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.process = self.invoke(cmd);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user