guard/guard — Issue #218: Watching directory only reports a deleted file the first time around.
Author: thepuzzlemaster
Created: 2012-01-05 19:29:25+00:00
State: open

I have guard set up to watch a directory for any file changes. When it detects changes, I'd like it to copy the files to a location on a server. Or remove the files if they get deleted. The copying works perfectly every time. Deletion, however, only works the first time around. After one deletion, it no longer seems to detect any more deletions. Even though changes through saving or re-creating the file works fine.

I am using guard-shell to copy, move, and remove files as well as create custom notifications. It's probably something simple I'm overlooking as I'm not too experienced in any of this. Any help would be appreciated.

Here is my terminal output:
wa-003381-ap:src ilan.epstein$ bundle exec guard -A
Guard uses Growl to send notifications.
Guard is now watching at '/Users/ilan.epstein/Desktop/NGL/phaseIII/src'
path = output/output_newFile
File updated: /Volumes/wa-projects/NGL_USA/NGL004_PhaseIII/02_Design/03_Design_Tech/output/output_newFile
path = output/output_newFile
File deleted: output/output_newFile
path = output/output_newFile
File updated: /Volumes/wa-projects/NGL_USA/NGL004_PhaseIII/02_Design/03_Design_Tech/output/output_newFile
path = output/output_newFile
File updated: /Volumes/wa-projects/NGL_USA/NGL004_PhaseIII/02_Design/03_Design_Tech/output/output_newFile
path = output/output_newFile
File updated: /Volumes/wa-projects/NGL_USA/NGL004_PhaseIII/02_Design/03_Design_Tech/output/output_newFile

And here is my guard file:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
# code to run: "bundle exec guard -A"
require 'pathname'
require 'growl'

notification :growl

server_path = '/Volumes/wa-projects/NGL_USA/'
project_path = 'NGL004_PhaseIII/02_Design/03_Design_Tech/'

guard :shell do
watch(/output\/*.*/) do |m|
puts "path = #{m[0]}"
serverPath = Pathname.new(server_path)
localPath = Pathname.new(m[0])

#check to see if connected to server
if serverPath.exist?
serverPath = serverPath + project_path + "#{m[0]}"
#check to see if file exists or if it's been deleted
if localPath.exist?
puts "File updated: #{serverPath}"
#check to make sure the directory exists on the server
unless serverPath.parent.exist?
#if not, make the dir
`mkdir -p #{serverPath.parent}`
end
`cp #{m} #{serverPath}`
`growlnotify -i pkg -m "File updated: #{m}"`
elsif serverPath.exist?
#file has been deleted, remove from server if exists there
#if serverPath.exist?
puts "File deleted: #{m}"
`growlnotify -i pkg -m "File deleted: #{m}"`
`rm #{serverPath}`
end
else
#puts "path exist? #{path.exist?}"
puts "Not connected to server."
`growlnotify -s -i pkg -m "!IMPORTANT! You are NOT connected to wa-projects. Please reconnect and resave your file."`
end
end
end