Skip to content

Instantly share code, notes, and snippets.

@science
Created February 20, 2011 01:29
Show Gist options
  • Save science/835586 to your computer and use it in GitHub Desktop.
Save science/835586 to your computer and use it in GitHub Desktop.
# Scans a CouchApp folder for changes, and on detection auto pushes to Couch
# this allows you to simply save your JS files and test them from CLI without re-loading manually every time
# (c) Steve Midgley 2011
# Released under Apache 2.0 license
# (http://www.apache.org/licenses/LICENSE-2.0.html)
require 'rake'
def running_windows?
!(RUBY_PLATFORM =~ /win32/).nil?
end
# set cmd_line to run on file change based on platform (windows needs ".bat" ext)
cmd_line = running_windows? ? "couchapp.bat push" : "couchapp push"
puts "Working folder: #{Dir::pwd}"
# monitor current folder and all subs for any file changes. On file change issue "couchapp push"
keep_running = true
while keep_running do
# Loop until interrupted by ctrl-c
trap("INT") { puts "\nExiting"; keep_running = false; exit;}
test_list = FileList['./**/*']
orig_dates = {}
test_list.each do |file|
orig_dates[file] = File.stat(file).mtime
end
# loop through test_list looking for date changes
test_file = ""
keep_searching = true
while keep_searching && keep_running do
print "\r**** Waiting for file changes. (#{Time::now.strftime('%b %d %I:%M:%S %p')}), ctrl-c exits"
Kernel::sleep(0.3) # wait 3/10 second between searches for changed files
test_list.each do |file|
Kernel::sleep(0.05) # keep the cpu/disk from maxing out with short pause between each file read
if orig_dates[file] != File.stat(file).mtime
puts "\n File change detected: #{file}"
puts " Time is: #{Time::now}"
keep_searching = false
test_file = file
break
end
end
if test_file != "" && keep_running
# invoke the test b/c the file has changed
puts "Pushing code due to file change in file #{test_file}..."
puts `#{cmd_line}`
end
end # while keep_searching... -- change detection loop
end # while keep_running do -- main loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment