#xtask #command #watch #source #customizable #projects #wasm

xtask-watch

A customizable helper to watch for changes in your projects using xtask

7 releases

0.2.3 Feb 6, 2024
0.2.2 Feb 6, 2024
0.2.1 Oct 17, 2023
0.1.6 Dec 5, 2022
0.1.1 Feb 14, 2022

#355 in Development tools

Download history 47/week @ 2023-12-18 11/week @ 2023-12-25 13/week @ 2024-01-01 80/week @ 2024-01-08 60/week @ 2024-01-15 44/week @ 2024-01-22 52/week @ 2024-01-29 53/week @ 2024-02-05 25/week @ 2024-02-12 27/week @ 2024-02-19 80/week @ 2024-02-26 81/week @ 2024-03-04 46/week @ 2024-03-11 62/week @ 2024-03-18 19/week @ 2024-03-25 249/week @ 2024-04-01

382 downloads per month
Used in 3 crates (via xtask-wasm)

MIT/Apache

25KB
361 lines

xtask-watch

actions status crate version documentation dependencies status licenses

This crate provides a Watch that launch a given command, re-launching the command when changes are detected in your source code.

This Watch struct is intended to be used with the xtask concept and implements clap::Parser so it can easily be used in your xtask crate. See clap's flatten to see how to extend it.

Setup

The best way to add xtask-watch to your project is to create a workspace with two packages: your project's package and the xtask package.

Create a project using xtask

  • Create a new directory that will contains the two package of your project and the workspace's Cargo.toml

    mkdir my-project
    cd my-project
    touch Cargo.toml
    
  • Create the project package and the xtask package using cargo new:

    cargo new my-project
    cargo new xtask
    
  • Open the workspace's Cargo.toml and add the following:

    [workspace]
    members = [
        "my-project",
        "xtask",
    ]
    
  • Create a .cargo/config.toml file and add the following content:

    [alias]
    xtask = "run --package xtask --"
    

The directory layout should look like this:

my-project
├── .cargo
   └── config.toml
├── Cargo.toml
├── my-project
   ├── Cargo.toml
   └── src
       └── ...
└── xtask
    ├── Cargo.toml
    └── src
        └── main.rs

And now you can run your xtask package using:

cargo xtask

You can find more informations about xtask here.

Use xtask-watch as a dependency

Finally, add the following to the xtask package's Cargo.toml:

[dependencies]
xtask-watch = "0.1.0"

Examples

A basic implementation

use std::process::Command;
use xtask_watch::{
    anyhow::Result,
    clap,
};

#[derive(clap::Parser)]
enum Opt {
    Watch(xtask_watch::Watch),
}

fn main() -> Result<()> {
    let opt: Opt = clap::Parser::parse();

    let mut run_command = Command::new("cargo");
    run_command.arg("check");

    match opt {
        Opt::Watch(watch) => {
            log::info!("Starting to watch `cargo check`");
            watch.run(run_command)?;
        }
    }

    Ok(())
}

A more complex demonstration

examples/demo provides an implementation of xtask-watch that naively parse a command given by the user (or use cargo check by default) and watch the workspace after launching this command.

Troubleshooting

When using the re-export of clap, you might encounter this error:

error[E0433]: failed to resolve: use of undeclared crate or module `clap`
 --> xtask/src/main.rs:4:10
  |
4 | #[derive(Parser)]
  |          ^^^^^^ use of undeclared crate or module `clap`
  |
  = note: this error originates in the derive macro `Parser` (in Nightly builds, run with -Z macro-backtrace for more info)

This occurs because you need to import clap in the scope too. This error can be resolved like this:

use xtask_wasm::clap;

#[derive(clap::Parser)]

Or like this:

use xtask_wasm::{clap, clap::Parser};

#[derive(Parser)]

Dependencies

~3–13MB
~121K SLoC