From 9ed3645088767911aafe798f17203c1c93bff2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Wed, 29 Dec 2021 20:55:09 +1300 Subject: [PATCH] Add environment vars support --- cli/src/args.rs | 8 ++--- cli/src/config/runtime.rs | 33 +++++++++++++++++++-- cli/tests/snapshots/help__help_unix.snap | 2 +- cli/tests/snapshots/help__help_windows.snap | 2 +- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/cli/src/args.rs b/cli/src/args.rs index 47ed591..c796292 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -135,9 +135,9 @@ pub fn get_args(tagged_filterer: bool) -> Result> { .help("Do not wrap command in a shell. Deprecated: use --shell=none instead.") .short("n") .long("no-shell")) - .arg(Arg::with_name("no-environment") // TODO + .arg(Arg::with_name("no-environment") .help_heading(Some(OPTSET_OUTPUT)) - .help("Do not set WATCHEXEC_*_PATH environment variables for the command") + .help("Do not set WATCHEXEC_* environment variables for the command") .long("no-environment")) .arg(Arg::with_name("no-process-group") .help_heading(Some(OPTSET_COMMAND)) @@ -201,7 +201,7 @@ pub fn get_args(tagged_filterer: bool) -> Result> { .takes_value(true), ) .arg( - Arg::with_name("filter") // TODO + Arg::with_name("filter") .help_heading(Some(OPTSET_FILTERING)) .help("Ignore all modifications except those matching the pattern") .short("f") @@ -212,7 +212,7 @@ pub fn get_args(tagged_filterer: bool) -> Result> { .value_name("pattern"), ) .arg( - Arg::with_name("ignore") // TODO + Arg::with_name("ignore") .help_heading(Some(OPTSET_FILTERING)) .help("Ignore modifications to paths matching the pattern") .short("i") diff --git a/cli/src/config/runtime.rs b/cli/src/config/runtime.rs index 966a286..98f98a0 100644 --- a/cli/src/config/runtime.rs +++ b/cli/src/config/runtime.rs @@ -4,11 +4,13 @@ use clap::ArgMatches; use miette::{IntoDiagnostic, Result}; use notify_rust::Notification; use watchexec::{ - action::{Action, Outcome}, + action::{Action, Outcome, PostSpawn, PreSpawn}, command::Shell, config::RuntimeConfig, event::ProcessEnd, fs::Watcher, + handler::SyncFnHandler, + paths::summarise_events_to_env, signal::{process::SubSignal, source::MainSignal}, }; @@ -194,7 +196,34 @@ pub fn runtime(args: &ArgMatches<'static>) -> Result { fut }); - // TODO: pre-command (environment vars) + let no_env = args.is_present("no-environment"); + config.on_pre_spawn(move |prespawn: PreSpawn| async move { + if !no_env { + let envs = summarise_events_to_env(prespawn.events.iter()); + if let Some(mut command) = prespawn.command().await { + for (k, v) in envs { + command.env(format!("WATCHEXEC_{}", k), v); + } + } + } + + Ok::<(), Infallible>(()) + }); + + config.on_post_spawn(SyncFnHandler::from(move |postspawn: PostSpawn| { + if notif { + Notification::new() + .summary("Watchexec: change detected") + .body(&format!("Running `{}`", postspawn.command.join(" "))) + .show() + .map(drop) + .unwrap_or_else(|err| { + eprintln!("Failed to send desktop notification: {}", err); + }); + } + + Ok::<(), Infallible>(()) + })); Ok(config) } diff --git a/cli/tests/snapshots/help__help_unix.snap b/cli/tests/snapshots/help__help_unix.snap index 2041c2b..147bdbf 100644 --- a/cli/tests/snapshots/help__help_unix.snap +++ b/cli/tests/snapshots/help__help_unix.snap @@ -13,7 +13,7 @@ FLAGS: -c, --clear Clear screen before executing command -h, --help Prints help information --no-default-ignore Skip auto-ignoring of commonly ignored globs - --no-environment Do not set WATCHEXEC_*_PATH environment variables for the command + --no-environment Do not set WATCHEXEC_* environment variables for the command --no-global-ignore Skip auto-loading of global or environment-wide ignore files --no-meta Ignore metadata changes --no-process-group Do not use a process group when running the command diff --git a/cli/tests/snapshots/help__help_windows.snap b/cli/tests/snapshots/help__help_windows.snap index 4f16eb2..ff77b6f 100644 --- a/cli/tests/snapshots/help__help_windows.snap +++ b/cli/tests/snapshots/help__help_windows.snap @@ -13,7 +13,7 @@ FLAGS: -c, --clear Clear screen before executing command -h, --help Prints help information --no-default-ignore Skip auto-ignoring of commonly ignored globs - --no-environment Do not set WATCHEXEC_*_PATH environment variables for the command + --no-environment Do not set WATCHEXEC_* environment variables for the command --no-global-ignore Skip auto-loading of global or environment-wide ignore files --no-meta Ignore metadata changes --no-process-group Do not use a process group when running the command