2021-08-24 12:45:31 +02:00
|
|
|
use std::{
|
2021-09-28 11:24:06 +02:00
|
|
|
convert::Infallible, env::current_dir, io::stderr, path::Path, str::FromStr, sync::Arc,
|
|
|
|
time::Duration,
|
2021-08-24 12:45:31 +02:00
|
|
|
};
|
|
|
|
|
2021-08-24 10:23:37 +02:00
|
|
|
use clap::ArgMatches;
|
2021-10-09 07:43:51 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-09-02 19:22:15 +02:00
|
|
|
use watchexec::{
|
|
|
|
action::{Action, Outcome, Signal},
|
|
|
|
command::Shell,
|
|
|
|
config::{InitConfig, RuntimeConfig},
|
2021-09-28 11:24:06 +02:00
|
|
|
filter::tagged::TaggedFilterer,
|
2021-09-02 19:22:15 +02:00
|
|
|
fs::Watcher,
|
|
|
|
handler::PrintDisplay,
|
|
|
|
signal::Signal as InputSignal,
|
|
|
|
};
|
2021-08-24 10:23:37 +02:00
|
|
|
|
2021-09-28 11:24:06 +02:00
|
|
|
pub fn new(args: &ArgMatches<'static>) -> Result<(InitConfig, RuntimeConfig, Arc<TaggedFilterer>)> {
|
|
|
|
let r = runtime(args)?;
|
|
|
|
Ok((init(args)?, r.0, r.1))
|
2021-08-24 10:23:37 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 12:45:31 +02:00
|
|
|
fn init(_args: &ArgMatches<'static>) -> Result<InitConfig> {
|
2021-10-09 07:37:13 +02:00
|
|
|
let mut config = InitConfig::default();
|
2021-08-24 12:45:31 +02:00
|
|
|
config.on_error(PrintDisplay(stderr()));
|
2021-10-09 07:37:13 +02:00
|
|
|
Ok(config)
|
2021-08-24 10:23:37 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:24:06 +02:00
|
|
|
fn runtime(args: &ArgMatches<'static>) -> Result<(RuntimeConfig, Arc<TaggedFilterer>)> {
|
2021-08-24 12:45:31 +02:00
|
|
|
let mut config = RuntimeConfig::default();
|
|
|
|
|
|
|
|
config.command(
|
|
|
|
args.values_of_lossy("command")
|
2021-10-09 07:43:51 +02:00
|
|
|
.expect("(clap) Bug: command is not present")
|
2021-08-24 12:45:31 +02:00
|
|
|
.iter(),
|
|
|
|
);
|
|
|
|
|
|
|
|
config.pathset(match args.values_of_os("paths") {
|
|
|
|
Some(paths) => paths.map(|os| Path::new(os).to_owned()).collect(),
|
2021-10-09 07:43:51 +02:00
|
|
|
None => vec![current_dir().into_diagnostic()?],
|
2021-08-24 12:45:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
config.action_throttle(Duration::from_millis(
|
2021-10-09 07:43:51 +02:00
|
|
|
args.value_of("debounce").unwrap_or("100").parse().into_diagnostic()?,
|
2021-08-24 12:45:31 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
if let Some(interval) = args.value_of("poll") {
|
2021-10-09 07:43:51 +02:00
|
|
|
config.file_watcher(Watcher::Poll(Duration::from_millis(interval.parse().into_diagnostic()?)));
|
2021-08-24 12:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config.command_shell(if args.is_present("no-shell") {
|
|
|
|
Shell::None
|
|
|
|
} else if let Some(s) = args.value_of("shell") {
|
|
|
|
if s.eq_ignore_ascii_case("powershell") {
|
|
|
|
Shell::Powershell
|
|
|
|
} else if s.eq_ignore_ascii_case("none") {
|
|
|
|
Shell::None
|
|
|
|
} else if s.eq_ignore_ascii_case("cmd") {
|
|
|
|
cmd_shell(s.into())
|
|
|
|
} else {
|
|
|
|
Shell::Unix(s.into())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
default_shell()
|
|
|
|
});
|
|
|
|
|
|
|
|
let clear = args.is_present("clear");
|
|
|
|
let mut on_busy = args
|
|
|
|
.value_of("on-busy-update")
|
|
|
|
.unwrap_or("queue")
|
|
|
|
.to_owned();
|
|
|
|
|
|
|
|
if args.is_present("restart") {
|
|
|
|
on_busy = "restart".into();
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.is_present("watch-when-idle") {
|
|
|
|
on_busy = "do-nothing".into();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut signal = args
|
|
|
|
.value_of("signal")
|
|
|
|
.map(|s| Signal::from_str(s))
|
2021-10-09 07:43:51 +02:00
|
|
|
.transpose().into_diagnostic()?
|
2021-08-24 12:45:31 +02:00
|
|
|
.unwrap_or(Signal::SIGTERM);
|
|
|
|
|
|
|
|
if args.is_present("kill") {
|
|
|
|
signal = Signal::SIGKILL;
|
|
|
|
}
|
|
|
|
|
|
|
|
let print_events = args.is_present("print-events");
|
|
|
|
let once = args.is_present("once");
|
|
|
|
|
2021-09-29 17:03:46 +02:00
|
|
|
let filterer = TaggedFilterer::new(".", ".")?;
|
2021-09-28 11:24:06 +02:00
|
|
|
config.filterer(filterer.clone());
|
|
|
|
|
2021-08-24 12:45:31 +02:00
|
|
|
config.on_action(move |action: Action| {
|
|
|
|
let fut = async { Ok::<(), Infallible>(()) };
|
|
|
|
|
|
|
|
if print_events {
|
2021-08-24 13:20:21 +02:00
|
|
|
for (n, event) in action.events.iter().enumerate() {
|
2021-09-02 21:58:20 +02:00
|
|
|
eprintln!("[EVENT {}] {}", n, event);
|
2021-08-24 12:45:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:15:38 +02:00
|
|
|
if once {
|
2021-09-02 23:25:23 +02:00
|
|
|
action.outcome(Outcome::both(Outcome::Start, Outcome::wait(Outcome::Exit)));
|
2021-08-24 13:15:38 +02:00
|
|
|
return fut;
|
|
|
|
}
|
|
|
|
|
|
|
|
let signals: Vec<InputSignal> = action.events.iter().flat_map(|e| e.signals()).collect();
|
|
|
|
let has_paths = action
|
|
|
|
.events
|
|
|
|
.iter()
|
|
|
|
.flat_map(|e| e.paths())
|
|
|
|
.next()
|
|
|
|
.is_some();
|
|
|
|
|
|
|
|
if signals.contains(&InputSignal::Terminate) {
|
|
|
|
action.outcome(Outcome::both(Outcome::Stop, Outcome::Exit));
|
|
|
|
return fut;
|
|
|
|
}
|
|
|
|
|
|
|
|
if signals.contains(&InputSignal::Interrupt) {
|
2021-08-24 13:20:21 +02:00
|
|
|
action.outcome(Outcome::both(Outcome::Stop, Outcome::Exit));
|
2021-08-24 13:15:38 +02:00
|
|
|
return fut;
|
|
|
|
}
|
|
|
|
|
2021-09-02 23:25:06 +02:00
|
|
|
if !has_paths {
|
|
|
|
if !signals.is_empty() {
|
2021-09-02 23:25:23 +02:00
|
|
|
let mut out = Outcome::DoNothing;
|
|
|
|
for sig in signals {
|
|
|
|
out = Outcome::both(
|
|
|
|
out,
|
|
|
|
Outcome::Signal(match sig {
|
|
|
|
InputSignal::Hangup => Signal::SIGHUP,
|
|
|
|
InputSignal::Interrupt => Signal::SIGINT,
|
|
|
|
InputSignal::Quit => Signal::SIGQUIT,
|
|
|
|
InputSignal::Terminate => Signal::SIGTERM,
|
|
|
|
InputSignal::User1 => Signal::SIGUSR1,
|
|
|
|
InputSignal::User2 => Signal::SIGUSR2,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2021-08-24 13:15:38 +02:00
|
|
|
|
2021-09-02 23:25:23 +02:00
|
|
|
action.outcome(out);
|
|
|
|
return fut;
|
2021-09-02 23:25:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let completion = action.events.iter().flat_map(|e| e.completions()).next();
|
|
|
|
if let Some(status) = completion {
|
|
|
|
match status {
|
|
|
|
Some(ex) if !ex.success() => {
|
|
|
|
eprintln!("[Command exited with {}]", ex);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
action.outcome(Outcome::DoNothing);
|
|
|
|
return fut;
|
|
|
|
}
|
2021-08-24 12:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let when_running = match (clear, on_busy.as_str()) {
|
|
|
|
(_, "do-nothing") => Outcome::DoNothing,
|
|
|
|
(true, "restart") => {
|
|
|
|
Outcome::both(Outcome::Stop, Outcome::both(Outcome::Clear, Outcome::Start))
|
|
|
|
}
|
|
|
|
(false, "restart") => Outcome::both(Outcome::Stop, Outcome::Start),
|
|
|
|
(_, "signal") => Outcome::Signal(signal),
|
2021-09-02 23:25:23 +02:00
|
|
|
(true, "queue") => Outcome::wait(Outcome::both(Outcome::Clear, Outcome::Start)),
|
|
|
|
(false, "queue") => Outcome::wait(Outcome::Start),
|
2021-08-24 12:45:31 +02:00
|
|
|
_ => Outcome::DoNothing,
|
|
|
|
};
|
|
|
|
|
|
|
|
let when_idle = if clear {
|
|
|
|
Outcome::both(Outcome::Clear, Outcome::Start)
|
|
|
|
} else {
|
|
|
|
Outcome::Start
|
|
|
|
};
|
|
|
|
|
|
|
|
action.outcome(Outcome::if_running(when_running, when_idle));
|
2021-08-24 10:23:37 +02:00
|
|
|
|
2021-08-24 12:45:31 +02:00
|
|
|
fut
|
|
|
|
});
|
2021-08-24 10:23:37 +02:00
|
|
|
|
2021-09-28 11:24:06 +02:00
|
|
|
Ok((config, filterer))
|
2021-08-24 10:23:37 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:24:06 +02:00
|
|
|
// until 2.0, then Powershell
|
2021-08-24 10:23:37 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn default_shell() -> Shell {
|
|
|
|
Shell::Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn default_shell() -> Shell {
|
|
|
|
Shell::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
// because Shell::Cmd is only on windows
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn cmd_shell(_: String) -> Shell {
|
|
|
|
Shell::Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn cmd_shell(s: String) -> Shell {
|
|
|
|
Shell::Unix(s)
|
|
|
|
}
|