2021-05-08 17:39:13 +02:00
|
|
|
[![Crates.io page](https://badgen.net/crates/v/watchexec)](https://crates.io/crates/watchexec)
|
|
|
|
[![API Docs](https://docs.rs/watchexec/badge.svg)][docs]
|
|
|
|
[![Crate license: Apache 2.0](https://badgen.net/badge/license/Apache%202.0)][license]
|
|
|
|
[![CI status](https://github.com/watchexec/watchexec/actions/workflows/check.yml/badge.svg)](https://github.com/watchexec/watchexec/actions/workflows/check.yml)
|
|
|
|
|
|
|
|
# Watchexec library
|
|
|
|
|
2021-06-28 15:15:00 +02:00
|
|
|
_The library which powers [Watchexec CLI](https://watchexec.github.io) and other tools._
|
2021-05-08 17:39:13 +02:00
|
|
|
|
|
|
|
- **[API documentation][docs]**.
|
|
|
|
- Licensed under [Apache 2.0][license].
|
2022-06-15 05:25:05 +02:00
|
|
|
- Status: maintained.
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
[docs]: https://docs.rs/watchexec
|
|
|
|
[license]: ../../LICENSE
|
2021-05-08 17:39:13 +02:00
|
|
|
|
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
Here's a complete example showing some of the library's features:
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2022-06-17 01:18:31 +02:00
|
|
|
```rust ,no_run
|
2021-10-16 16:37:12 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2023-11-25 21:33:44 +01:00
|
|
|
use std::{
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
time::Duration,
|
|
|
|
};
|
2021-05-08 17:39:13 +02:00
|
|
|
use watchexec::{
|
2023-11-25 21:33:44 +01:00
|
|
|
command::{Command, Program, Shell},
|
|
|
|
job::CommandState,
|
2021-10-16 16:37:12 +02:00
|
|
|
Watchexec,
|
2021-05-08 17:39:13 +02:00
|
|
|
};
|
2023-11-25 21:33:44 +01:00
|
|
|
use watchexec_events::{Event, Priority};
|
|
|
|
use watchexec_signals::Signal;
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2021-10-16 16:37:12 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2023-11-25 21:33:44 +01:00
|
|
|
// this is okay to start with, but Watchexec logs a LOT of data,
|
|
|
|
// even at error level. you will quickly want to filter it down.
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
|
|
.init();
|
|
|
|
|
|
|
|
// initialise Watchexec with a simple initial action handler
|
|
|
|
let job = Arc::new(Mutex::new(None));
|
|
|
|
let wx = Watchexec::new({
|
|
|
|
let outerjob = job.clone();
|
|
|
|
move |mut action| {
|
|
|
|
let (_, job) = action.create_job(Arc::new(Command {
|
|
|
|
program: Program::Shell {
|
|
|
|
shell: Shell::new("bash"),
|
|
|
|
command: "
|
|
|
|
echo 'Hello world'
|
|
|
|
trap 'echo Not quitting yet!' TERM
|
|
|
|
read
|
|
|
|
"
|
|
|
|
.into(),
|
|
|
|
args: Vec::new(),
|
|
|
|
},
|
|
|
|
options: Default::default(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
// store the job outside this closure too
|
|
|
|
*outerjob.lock().unwrap() = Some(job.clone());
|
|
|
|
|
|
|
|
// block SIGINT
|
|
|
|
#[cfg(unix)]
|
|
|
|
job.set_spawn_hook(|cmd, _| {
|
|
|
|
use nix::sys::signal::{sigprocmask, SigSet, SigmaskHow, Signal};
|
|
|
|
unsafe {
|
|
|
|
cmd.pre_exec(|| {
|
|
|
|
let mut newset = SigSet::empty();
|
|
|
|
newset.add(Signal::SIGINT);
|
|
|
|
sigprocmask(SigmaskHow::SIG_BLOCK, Some(&newset), None)?;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
// start the command
|
|
|
|
job.start();
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
action
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// start the engine
|
|
|
|
let main = wx.main();
|
|
|
|
|
|
|
|
// send an event to start
|
|
|
|
wx.send_event(Event::default(), Priority::Urgent)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// ^ this will cause the action handler we've defined above to run,
|
|
|
|
// creating and starting our little bash program, and storing it in the mutex
|
|
|
|
|
|
|
|
// spin until we've got the job
|
|
|
|
while job.lock().unwrap().is_none() {
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch the job and restart it when it exits
|
|
|
|
let job = job.lock().unwrap().clone().unwrap();
|
|
|
|
let auto_restart = tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
job.to_wait().await;
|
|
|
|
job.run(|context| {
|
|
|
|
if let CommandState::Finished {
|
|
|
|
status,
|
|
|
|
started,
|
|
|
|
finished,
|
|
|
|
} = context.current
|
|
|
|
{
|
|
|
|
let duration = *finished - *started;
|
|
|
|
eprintln!("[Program stopped with {status:?}; ran for {duration:?}]")
|
2021-10-16 16:37:12 +02:00
|
|
|
}
|
2023-11-25 21:33:44 +01:00
|
|
|
})
|
|
|
|
.await;
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
eprintln!("[Restarting...]");
|
|
|
|
job.start().await;
|
|
|
|
}
|
|
|
|
});
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
// now we change what the action does:
|
|
|
|
let auto_restart_abort = auto_restart.abort_handle();
|
|
|
|
wx.config.on_action(move |mut action| {
|
|
|
|
// if we get Ctrl-C on the Watchexec instance, we quit
|
|
|
|
if action.signals().any(|sig| sig == Signal::Interrupt) {
|
|
|
|
eprintln!("[Quitting...]");
|
|
|
|
auto_restart_abort.abort();
|
|
|
|
action.quit_gracefully(Signal::ForceStop, Duration::ZERO);
|
|
|
|
return action;
|
|
|
|
}
|
2022-06-17 01:18:31 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
// if the action was triggered by file events, gracefully stop the program
|
|
|
|
if action.paths().next().is_some() {
|
|
|
|
// watchexec can manage ("supervise") more than one program;
|
|
|
|
// here we only have one but we don't know its Id so we grab it out of the iterator
|
|
|
|
if let Some(job) = action.list_jobs().next().map(|(_, job)| job.clone()) {
|
|
|
|
eprintln!("[Asking program to stop...]");
|
|
|
|
job.stop_with_signal(Signal::Terminate, Duration::from_secs(5));
|
|
|
|
}
|
2021-10-16 16:37:12 +02:00
|
|
|
}
|
2023-11-25 21:33:44 +01:00
|
|
|
|
|
|
|
action
|
2021-10-16 16:37:12 +02:00
|
|
|
});
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
// and watch all files in the current directory:
|
|
|
|
wx.config.pathset(["."]);
|
|
|
|
|
|
|
|
// then keep running until Watchexec quits!
|
|
|
|
let _ = main.await.into_diagnostic()?;
|
|
|
|
auto_restart.abort();
|
2021-10-16 16:37:12 +02:00
|
|
|
Ok(())
|
2021-05-08 17:39:13 +02:00
|
|
|
}
|
|
|
|
```
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
Other examples:
|
|
|
|
- [Only Commands](./examples/only_commands.rs): skip watching files, only use the supervisor.
|
|
|
|
- [Only Events](./examples/only_events.rs): never start any processes, only print events.
|
|
|
|
- [Restart `cargo run` only when `cargo build` succeeds](./examples/restart_run_on_successful_build.rs)
|
2021-10-16 16:37:12 +02:00
|
|
|
|
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
## Kitchen sink
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
Though not its primary usecase, the library exposes most of its relatively standalone components,
|
|
|
|
available to make other tools that are not Watchexec-shaped:
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
- **Event sources**: [Filesystem](https://docs.rs/watchexec/3/watchexec/sources/fs/index.html),
|
|
|
|
[Signals](https://docs.rs/watchexec/3/watchexec/sources/signal/index.html),
|
|
|
|
[Keyboard](https://docs.rs/watchexec/3/watchexec/sources/keyboard/index.html).
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
- Finding **[a common prefix](https://docs.rs/watchexec/3/watchexec/paths/fn.common_prefix.html)**
|
2021-10-16 16:37:12 +02:00
|
|
|
of a set of paths.
|
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
- A **[Changeable](https://docs.rs/watchexec/3/watchexec/changeable/index.html)** type, which
|
|
|
|
powers the "live" configuration system.
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
- And [more][docs]!
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
Filterers are split into their own crates, so they can be evolved independently:
|
2022-01-18 10:38:15 +01:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
- The **[Globset](https://docs.rs/watchexec-filterer-globset) filterer** implements the default
|
2023-11-25 21:33:44 +01:00
|
|
|
Watchexec CLI filtering, based on the regex crate's ignore mechanisms.
|
2022-06-15 05:25:05 +02:00
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
- ~~The **[Tagged](https://docs.rs/watchexec-filterer-tagged) filterer**~~ was an experiment in
|
|
|
|
creating a more powerful filtering solution, which could operate on every part of events, not
|
|
|
|
just their paths, using a custom syntax. It is no longer maintained.
|
2022-06-15 05:25:05 +02:00
|
|
|
|
|
|
|
- The **[Ignore](https://docs.rs/watchexec-filterer-ignore) filterer** implements ignore-file
|
|
|
|
semantics, and especially supports _trees_ of ignore files. It is used as a subfilterer in both
|
|
|
|
of the main filterers above.
|
2021-10-16 16:37:12 +02:00
|
|
|
|
|
|
|
There are also separate, standalone crates used to build Watchexec which you can tap into:
|
|
|
|
|
2023-11-25 21:33:44 +01:00
|
|
|
- **[Supervisor](https://docs.rs/watchexec-supervisor)** is Watchexec's process supervisor and
|
|
|
|
command abstraction.
|
|
|
|
|
2021-10-16 16:37:12 +02:00
|
|
|
- **[ClearScreen](https://docs.rs/clearscreen)** makes clearing the terminal screen in a
|
|
|
|
cross-platform way easy by default, and provides advanced options to fit your usecase.
|
|
|
|
|
|
|
|
- **[Command Group](https://docs.rs/command-group)** augments the std and tokio `Command` with
|
|
|
|
support for process groups, portable between Unix and Windows.
|
|
|
|
|
2023-03-18 09:32:24 +01:00
|
|
|
- **[Event types](https://docs.rs/watchexec-events)** contains the event types used by Watchexec,
|
|
|
|
including the JSON format used for passing event data to child processes.
|
|
|
|
|
|
|
|
- **[Signal types](https://docs.rs/watchexec-signals)** contains the signal types used by Watchexec.
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
- **[Ignore files](https://docs.rs/ignore-files)** finds, parses, and interprets ignore files.
|
2021-10-16 16:37:12 +02:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
- **[Project Origins](https://docs.rs/project-origins)** finds the origin (or root) path of a
|
|
|
|
project, and what kind of project it is.
|
2023-03-02 03:33:39 +01:00
|
|
|
|
|
|
|
## Rust version (MSRV)
|
|
|
|
|
|
|
|
Due to the unpredictability of dependencies changing their MSRV, this library no longer tries to
|
|
|
|
keep to a minimum supported Rust version behind stable. Instead, it is assumed that developers use
|
|
|
|
the latest stable at all times.
|
|
|
|
|
|
|
|
Applications that wish to support lower-than-stable Rust (such as the Watchexec CLI does) should:
|
|
|
|
- use a lock file
|
|
|
|
- recommend the use of `--locked` when installing from source
|
|
|
|
- provide pre-built binaries (and [Binstall](https://github.com/cargo-bins/cargo-binstall) support) for non-distro users
|
|
|
|
- avoid using newer features until some time has passed, to let distro users catch up
|
|
|
|
- consider recommending that distro-Rust users switch to distro `rustup` where available
|