diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f0babb..88b20b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +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 - `--commit` flag to restart the command when git HEAD changes. (#36) +- `FromIterator` for `CommandList`. (#37) ### Fixed diff --git a/src/lib.rs b/src/lib.rs index 99cc399..895bb03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -237,34 +237,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()?; @@ -694,30 +690,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 { @@ -761,6 +733,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.