Skip to content

Instantly share code, notes, and snippets.

@onyxraven
Created December 23, 2012 21:22
Show Gist options
  • Save onyxraven/4366239 to your computer and use it in GitHub Desktop.
Save onyxraven/4366239 to your computer and use it in GitHub Desktop.
ruby fsevent wrapper to sync directories
#!/usr/bin/env ruby
#requires osx fsevent
#requires growl with network registration enabled (at least once)
#requires the below rubygems
#requires rsync
require 'rubygems'
require 'rb-fsevent'
require 'ruby-growl'
local='/Volumes/Work/dev/'
remote='dev:/home/username/dev/'
# Decide on the local path.
Dir.mkdir(local) unless File.directory?(local)
local = File.expand_path(local)
# Parse the server and remote path. Decide on the remote username.
raise unless remote =~ /^(?:([^@]+)@)?([^:]+):(.+)$/
user, server, remote = $1, $2, $3
user ||= ENV["USER"]
g = Growl.new("localhost", "dirsync-monitor", ["dirsync-monitor Push"])
g.notify("dirsync-monitor Push", "Watch Started", "#{local} => #{remote}")
fsevent = FSEvent.new
fsevent.watch local do |directories|
#$stderr.puts "# detected change inside: #{directories.inspect}"
directories.each do |dir|
pathname2 = "#{dir.sub(local, "")}"
#ignore some stuff
if pathname2.match('/\.svn/.*$') != nil ||
pathname2.match('/\.hg/.*$') != nil ||
pathname2.match('/compile/$') != nil ||
pathname2.match('/cache/$') != nil then
next
end
l = "#{local}#{pathname2}".gsub(' ', '\\\\ ')
r = "#{user}@#{server}:#{remote}#{pathname2}".gsub(' ', '\\\\ ')
cmd = "rsync -tzplr --delete-after --exclude='/*/*' --exclude='custom.ini' --exclude='.svn' --exclude='.hg' --exclude='compile' --exclude='cache' --exclude='*.swp' #{l} #{r}"
#$stderr.puts cmd
g.notify("dirsync-monitor Push", "Sync", "#{l} => #{r}")
system(cmd)
end
end
fsevent.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment