From a75dd4911496090deac3334e7a07795b2881f934 Mon Sep 17 00:00:00 2001 From: Matt Green Date: Wed, 19 Oct 2016 19:32:07 -0400 Subject: [PATCH] Set $WATCHEXEC_UPDATED_PATH to first changed path in child process; closes #11 --- src/main.rs | 10 ++++++++-- src/runner.rs | 34 ++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index a2823de..ca49d5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -176,7 +176,7 @@ fn main() { let mut runner = Runner::new(args.is_present("restart"), args.is_present("clear")); if args.is_present("run-initially") { - runner.run_command(&cmd); + runner.run_command(&cmd, vec![]); } loop { @@ -184,7 +184,13 @@ fn main() { debug!("{:?}: {:?}", e.op, e.path); - runner.run_command(&cmd); + // TODO: update wait to return all paths + let updated: Vec<&str> = e.path + .iter() + .map(|p| p.to_str().unwrap()) + .collect(); + + runner.run_command(&cmd, updated); } } diff --git a/src/runner.rs b/src/runner.rs index f3a3df2..9403396 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -26,24 +26,30 @@ impl Runner { } #[cfg(target_family = "windows")] - fn invoke(&self, cmd: &str) -> Option { - Command::new("cmd.exe") - .arg("/C") - .arg(cmd) - .spawn() - .ok() + fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option { + let mut command = Command::new("cmd.exe"); + command.arg("/C").arg(cmd); + + if updated_files.len() > 0 { + command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]); + } + + command.spawn().ok() } #[cfg(target_family = "unix")] - fn invoke(&self, cmd: &str) -> Option { - Command::new("sh") - .arg("-c") - .arg(cmd) - .spawn() - .ok() + fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option { + let mut command = Command::new("sh"); + command.arg("-c").arg(cmd); + + if updated_paths.len() > 0 { + command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]); + } + + command.spawn().ok() } - pub fn run_command(&mut self, cmd: &str) { + pub fn run_command(&mut self, cmd: &str, updated_paths: Vec<&str>) { if let Some(ref mut child) = self.process { if self.restart { debug!("Killing child process (pid: {})", child.id()); @@ -60,6 +66,6 @@ impl Runner { debug!("Executing: {}", cmd); - self.process = self.invoke(cmd); + self.process = self.invoke(cmd, updated_paths); } }