2021-09-02 19:18:30 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-10-10 10:04:40 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-08-22 17:59:02 +02:00
|
|
|
use watchexec::{
|
|
|
|
action::{Action, Outcome},
|
2022-06-16 17:36:08 +02:00
|
|
|
command::Command,
|
2021-08-24 09:59:11 +02:00
|
|
|
config::{InitConfig, RuntimeConfig},
|
2021-08-22 17:59:02 +02:00
|
|
|
error::ReconfigError,
|
2023-01-06 14:53:49 +01:00
|
|
|
event::Event,
|
2021-08-22 17:59:02 +02:00
|
|
|
fs::Watcher,
|
2021-10-15 14:13:16 +02:00
|
|
|
signal::source::MainSignal,
|
2022-01-30 14:55:47 +01:00
|
|
|
ErrorHook, Watchexec,
|
2021-08-22 17:59:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Run with: `env RUST_LOG=debug cargo run --example print_out`
|
|
|
|
#[tokio::main]
|
2021-10-10 10:04:40 +02:00
|
|
|
async fn main() -> Result<()> {
|
2021-08-22 17:59:02 +02:00
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
2021-10-09 07:37:13 +02:00
|
|
|
let mut init = InitConfig::default();
|
2022-01-30 14:55:47 +01:00
|
|
|
init.on_error(|err: ErrorHook| async move {
|
|
|
|
eprintln!("Watchexec Runtime Error: {}", err.error);
|
2021-08-22 17:59:02 +02:00
|
|
|
Ok::<(), std::convert::Infallible>(())
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut runtime = RuntimeConfig::default();
|
2021-08-24 09:59:11 +02:00
|
|
|
runtime.pathset(["src", "dontexist", "examples"]);
|
2022-06-16 17:36:08 +02:00
|
|
|
runtime.command(Command::Exec {
|
|
|
|
prog: "date".into(),
|
|
|
|
args: Vec::new(),
|
|
|
|
});
|
2021-08-22 17:59:02 +02:00
|
|
|
|
2021-10-09 07:37:13 +02:00
|
|
|
let wx = Watchexec::new(init, runtime.clone())?;
|
2021-08-22 17:59:02 +02:00
|
|
|
let w = wx.clone();
|
|
|
|
|
|
|
|
let config = runtime.clone();
|
|
|
|
runtime.on_action(move |action: Action| {
|
|
|
|
let mut config = config.clone();
|
|
|
|
let w = w.clone();
|
|
|
|
async move {
|
2023-01-06 14:53:49 +01:00
|
|
|
eprintln!("Watchexec Action: {action:?}");
|
2021-08-22 17:59:02 +02:00
|
|
|
|
|
|
|
let sigs = action
|
|
|
|
.events
|
|
|
|
.iter()
|
2023-01-06 14:53:49 +01:00
|
|
|
.flat_map(Event::signals)
|
2021-08-22 17:59:02 +02:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-10-15 14:13:16 +02:00
|
|
|
if sigs.iter().any(|sig| sig == &MainSignal::Interrupt) {
|
2021-08-22 17:59:02 +02:00
|
|
|
action.outcome(Outcome::Exit);
|
2021-10-15 14:13:16 +02:00
|
|
|
} else if sigs.iter().any(|sig| sig == &MainSignal::User1) {
|
2021-08-22 17:59:02 +02:00
|
|
|
eprintln!("Switching to native for funsies");
|
2022-01-28 13:52:06 +01:00
|
|
|
config.file_watcher(Watcher::Native);
|
2021-08-22 17:59:02 +02:00
|
|
|
w.reconfigure(config)?;
|
2021-10-15 14:13:16 +02:00
|
|
|
} else if sigs.iter().any(|sig| sig == &MainSignal::User2) {
|
2021-08-22 17:59:02 +02:00
|
|
|
eprintln!("Switching to polling for funsies");
|
2022-01-28 13:52:06 +01:00
|
|
|
config.file_watcher(Watcher::Poll(Duration::from_millis(50)));
|
2021-08-22 17:59:02 +02:00
|
|
|
w.reconfigure(config)?;
|
2023-03-05 02:57:34 +01:00
|
|
|
} else if action.events.iter().flat_map(Event::paths).next().is_some() {
|
2021-08-22 17:59:02 +02:00
|
|
|
action.outcome(Outcome::if_running(
|
|
|
|
Outcome::both(Outcome::Stop, Outcome::Start),
|
|
|
|
Outcome::Start,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok::<(), ReconfigError>(())
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
wx.reconfigure(runtime)?;
|
2021-10-10 10:04:40 +02:00
|
|
|
wx.main().await.into_diagnostic()??;
|
2021-08-22 17:59:02 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|