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]
|
2021-10-19 13:48:47 +02:00
|
|
|
![MSRV: 1.56.0 (breaking)](https://badgen.net/badge/MSRV/1.56.0%20%28breaking%29/green)
|
2021-05-08 17:39:13 +02:00
|
|
|
[![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].
|
2021-10-19 13:48:47 +02:00
|
|
|
- Minimum Supported Rust Version: 1.56.0.
|
2021-10-16 16:37:12 +02:00
|
|
|
- Status: in preview (`2.0.0-pre.N` series).
|
2021-05-08 17:39:13 +02:00
|
|
|
|
|
|
|
[docs]: https://docs.rs/watchexec
|
|
|
|
[license]: ../LICENSE
|
|
|
|
|
|
|
|
|
|
|
|
## Quick start
|
|
|
|
|
|
|
|
```rust
|
2021-10-16 16:37:12 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-05-08 17:39:13 +02:00
|
|
|
use watchexec::{
|
2021-10-16 16:37:12 +02:00
|
|
|
Watchexec,
|
|
|
|
action::{Action, Outcome},
|
|
|
|
config::{InitConfig, RuntimeConfig},
|
|
|
|
handler::{Handler as _, PrintDebug},
|
2021-05-08 17:39:13 +02:00
|
|
|
};
|
|
|
|
|
2021-10-16 16:37:12 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
let mut init = InitConfig::default();
|
|
|
|
init.on_error(PrintDebug(std::io::stderr()));
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2021-10-16 16:37:12 +02:00
|
|
|
let mut runtime = RuntimeConfig::default();
|
|
|
|
runtime.pathset(["watchexec.conf"]);
|
|
|
|
|
|
|
|
let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;
|
|
|
|
conf.apply(&mut runtime);
|
|
|
|
|
|
|
|
let we = Watchexec::new(init, runtime.clone())?;
|
|
|
|
let w = we.clone();
|
|
|
|
|
|
|
|
let c = runtime.clone();
|
|
|
|
runtime.on_action(move |action: Action| {
|
|
|
|
let mut c = c.clone();
|
|
|
|
let w = w.clone();
|
|
|
|
async move {
|
|
|
|
for event in &action.events {
|
|
|
|
if event.paths().any(|(p, _)| p.ends_with("/watchexec.conf")) {
|
|
|
|
let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;
|
|
|
|
|
|
|
|
conf.apply(&mut c);
|
|
|
|
w.reconfigure(c.clone());
|
|
|
|
// tada! self-reconfiguring watchexec on config file change!
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2021-10-16 16:37:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 17:39:13 +02:00
|
|
|
|
2021-10-16 16:37:12 +02:00
|
|
|
action.outcome(Outcome::if_running(
|
|
|
|
Outcome::DoNothing,
|
|
|
|
Outcome::both(Outcome::Clear, Outcome::Start),
|
|
|
|
));
|
2021-05-08 17:39:13 +02:00
|
|
|
|
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
|
|
|
we.main().await.into_diagnostic()?;
|
|
|
|
Ok(())
|
2021-05-08 17:39:13 +02:00
|
|
|
}
|
|
|
|
```
|
2021-10-16 16:37:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Kitchen sink
|
|
|
|
|
|
|
|
The library also exposes a large amount of components which are available to make your own tool, or
|
|
|
|
to make anything else you may want:
|
|
|
|
|
|
|
|
- **[Command handling](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/command/index.html)**, to
|
|
|
|
build a command with an arbitrary shell, deal with grouped and ungrouped processes the same way,
|
|
|
|
and supervise a process while also listening for & acting on interventions such as sending signals.
|
|
|
|
|
2021-10-16 16:53:05 +02:00
|
|
|
- **Event sources**: [Filesystem](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/fs/index.html),
|
2021-10-16 16:37:12 +02:00
|
|
|
[Signals](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/signal/source/index.html), (more to come).
|
|
|
|
|
|
|
|
- Finding **[a common prefix](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/paths/fn.common_prefix.html)**
|
|
|
|
of a set of paths.
|
|
|
|
|
|
|
|
- Detecting the **[origin(s)](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/project/fn.origins.html)**
|
|
|
|
and **[types](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/project/fn.types.html)** of projects.
|
|
|
|
|
|
|
|
- Discovering project-local and global
|
|
|
|
**[ignore files](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/ignore_files/index.html)**.
|
|
|
|
|
|
|
|
There are also separate, standalone crates used to build Watchexec which you can tap into:
|
|
|
|
|
|
|
|
- **[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.
|
|
|
|
|
|
|
|
|
|
|
|
## Tagged filters (alpha)
|
|
|
|
|
|
|
|
This library is also the home of Watchexec's current _two_ filtering implementations: the v1
|
|
|
|
behaviour which has proven confusing and inconsistent over the years, and an upcoming complete
|
|
|
|
overhaul called "tagged filtering."
|
|
|
|
|
|
|
|
Have a look at the [docs](https://docs.rs/watchexec/2.0.0-pre.0/watchexec/filter/tagged/struct.TaggedFilterer.html)!
|