Created
January 12, 2010 20:10
-
-
Save jasonwyatt/275556 to your computer and use it in GitHub Desktop.
Livesync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/ruby -w | |
################################################ | |
# Settings | |
################################################ | |
# Directory to watch | |
watch_location = "." | |
# Pattern for files to watch | |
watch_pattern = "**/*" #all files/subdirectories | |
# Hostname of the Growl Server | |
growl_host = "localhost" | |
# Password for the Growl Server | |
growl_password = "password" | |
# Command to run to try and synchronize with the server. | |
synchronize_command = "some command" | |
# Function to determine success based on the output of the command. | |
def determine_success?(output) | |
# return true if the output says the result was successful, return false otherwise. | |
true | |
end | |
# Frequency to check for changes to the filesystem (seconds) | |
synchronize_interval = 1.0 | |
# Success Wording | |
success_title = "Livesync Successful" | |
success_message = "Livesync.rb has successfully synchronized." | |
# Failure Wording | |
failure_title = "Livesync Unsuccessful" | |
failure_message = "There was an error livesyncing! See the log for details." | |
################################################ | |
# Don't change this stuff. | |
################################################ | |
if not require 'rubygems' | |
puts "You must have rubygems installed to use livesync." | |
exit | |
end | |
if not require 'directory_watcher' | |
puts "You must have the 'directory_watcher' gem installed to use livesync." | |
puts "Attempting to install it now. If it fails, try the following command: " | |
puts "'sudo gem install directory_watcher'" | |
puts "" | |
pipe = IO.popen("gem install directory_watcher") | |
output = pipe.readlines | |
puts output.join | |
exit | |
end | |
if not require 'ruby-growl' | |
puts "You must have the 'ruby-growl' gem installed to use livesync." | |
puts "Attempting to install it now. If it fails, try the following command: " | |
puts "'sudo gem install ruby-growl'" | |
puts "" | |
pipe = IO.popen("gem install ruby-growl") | |
output = pipe.readlines | |
puts output.join | |
exit | |
end | |
g = Growl.new(growl_host, "Live Sync", ["ready","error"], ["ready"], growl_password) | |
dw = DirectoryWatcher.new watch_location, :glob => watch_pattern, :pre_load => true, :interval => synchronize_interval | |
dw.add_observer do |*args| | |
args.each do | |
|event| puts event | |
end | |
puts "Running command '#{synchronize_command}'" | |
pipe = IO.popen(synchronize_command) | |
output = pipe.readlines | |
puts output.join | |
if determine_success?(output) | |
g.notify("ready", success_title, success_message) | |
else | |
g.notify("error", failure_title, failure_message) | |
end | |
end | |
dw.start | |
gets # when the user hits "enter" the script will terminate | |
dw.stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment