From 630355323a17d3724dc1dee2281615d43f89220b Mon Sep 17 00:00:00 2001 From: yozhgoor Date: Mon, 22 Jun 2026 04:19:59 +0200 Subject: [PATCH] Bypass default commands when `--exec`/`--shell` are provided --- CHANGELOG.md | 10 +++++ src/lib.rs | 102 ++++++++++++++++++++++++++------------------------- 2 files changed, 63 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e18f963..470dddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- **BREAKING**: `Watch::run` now uses only the commands from `--exec` or `--shell` + when provided, ignoring the `commands` argument entirely. Previously, both the + `commands` argument and CLI flags were combined into a single command list. (#37) + +### Added + +- `FromIterator` for `CommandList`. (#37) + ## [0.3.5] - 2026-06-02 ### Changed diff --git a/src/lib.rs b/src/lib.rs index 8ece4af..e6f7eb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -173,34 +173,30 @@ impl Watch { /// configured [`debounce`](Self::debounce) duration. /// /// Workspace's `target` directory and hidden paths are excluded by default. + /// + /// If `--exec` or `--shell` flags are provided, the `commands` argument is ignored + /// and only the commands supplied from them are run. pub fn run(mut self, commands: impl Into) -> Result<()> { let metadata = metadata(); - let list = commands.into(); - - { - let mut commands = list - .commands - .lock() - .expect("no panic-prone code runs while this lock is held"); - commands.extend(self.shell_commands.iter().map(|x| { - let mut command = - Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string())); - command.arg("-c"); - command.arg(x); - - command - })); - - commands.extend(self.cargo_commands.iter().map(|x| { - let mut command = - Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string())); - command.arg("-c"); - command.arg(format!("cargo {x}")); - - command - })); - } + let list = if self.cargo_commands.is_empty() && self.shell_commands.is_empty() { + commands.into() + } else { + let shell = env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()); + self.shell_commands + .iter() + .map(|x| { + let mut cmd = Command::new(&shell); + cmd.args(["-c", x]); + cmd + }) + .chain(self.cargo_commands.iter().map(|x| { + let mut cmd = Command::new(&shell); + cmd.args(["-c", format!("cargo {x}").as_ref()]); + cmd + })) + .collect::() + }; self.prepare_excludes()?; @@ -553,30 +549,6 @@ pub struct CommandList { commands: Arc>>, } -impl From for CommandList { - fn from(command: Command) -> Self { - Self { - commands: Arc::new(Mutex::new(vec![command])), - } - } -} - -impl From> for CommandList { - fn from(commands: Vec) -> Self { - Self { - commands: Arc::new(Mutex::new(commands)), - } - } -} - -impl From<[Command; SIZE]> for CommandList { - fn from(commands: [Command; SIZE]) -> Self { - Self { - commands: Arc::new(Mutex::new(Vec::from(commands))), - } - } -} - impl CommandList { /// Returns `true` if the list is empty. pub fn is_empty(&self) -> bool { @@ -620,6 +592,38 @@ impl CommandList { } } +impl From for CommandList { + fn from(command: Command) -> Self { + Self { + commands: Arc::new(Mutex::new(vec![command])), + } + } +} + +impl From> for CommandList { + fn from(commands: Vec) -> Self { + Self { + commands: Arc::new(Mutex::new(commands)), + } + } +} + +impl From<[Command; SIZE]> for CommandList { + fn from(commands: [Command; SIZE]) -> Self { + Self { + commands: Arc::new(Mutex::new(Vec::from(commands))), + } + } +} + +impl FromIterator for CommandList { + fn from_iter>(iter: T) -> Self { + Self { + commands: Arc::new(Mutex::new(iter.into_iter().collect())), + } + } +} + /// Guard returned by [`WatchLock::acquire`]. /// /// Keep this value alive for the duration of the protected read section.