Set $WATCHEXEC_UPDATED_PATH to first changed path in child process;

closes #11
This commit is contained in:
Matt Green 2016-10-19 19:32:07 -04:00
parent 249e5a84e4
commit a75dd49114
2 changed files with 28 additions and 16 deletions

View File

@ -176,7 +176,7 @@ fn main() {
let mut runner = Runner::new(args.is_present("restart"), args.is_present("clear")); let mut runner = Runner::new(args.is_present("restart"), args.is_present("clear"));
if args.is_present("run-initially") { if args.is_present("run-initially") {
runner.run_command(&cmd); runner.run_command(&cmd, vec![]);
} }
loop { loop {
@ -184,7 +184,13 @@ fn main() {
debug!("{:?}: {:?}", e.op, e.path); 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);
} }
} }

View File

@ -26,24 +26,30 @@ impl Runner {
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
fn invoke(&self, cmd: &str) -> Option<Child> { fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option<Child> {
Command::new("cmd.exe") let mut command = Command::new("cmd.exe");
.arg("/C") command.arg("/C").arg(cmd);
.arg(cmd)
.spawn() if updated_files.len() > 0 {
.ok() command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]);
}
command.spawn().ok()
} }
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn invoke(&self, cmd: &str) -> Option<Child> { fn invoke(&self, cmd: &str, updated_paths: Vec<&str>) -> Option<Child> {
Command::new("sh") let mut command = Command::new("sh");
.arg("-c") command.arg("-c").arg(cmd);
.arg(cmd)
.spawn() if updated_paths.len() > 0 {
.ok() 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 let Some(ref mut child) = self.process {
if self.restart { if self.restart {
debug!("Killing child process (pid: {})", child.id()); debug!("Killing child process (pid: {})", child.id());
@ -60,6 +66,6 @@ impl Runner {
debug!("Executing: {}", cmd); debug!("Executing: {}", cmd);
self.process = self.invoke(cmd); self.process = self.invoke(cmd, updated_paths);
} }
} }