Skip to content

Instantly share code, notes, and snippets.

@shokai
Created October 31, 2010 14:17
Show Gist options
  • Save shokai/656630 to your computer and use it in GitHub Desktop.
Save shokai/656630 to your computer and use it in GitHub Desktop.
~$
\.DS_Store$
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
if ARGV.size < 1
STDERR.puts 'command required'
STDERR.puts 'e.g. handgun ruby test.rb'
exit 1
end
puts cmd = ARGV.join(' ')
pid = nil
last = 0
@ignores = Array.new
@ignores << /\/\..+/ # dotfile
if File.exists? Dir.pwd+'/.handgunignore'
open(Dir.pwd+'/.handgunignore').readlines.each{|line|
line.gsub!(/[\r\n]/,'')
if line.size > 0
@ignores << /#{line}/
end
}
end
def last_update
tmp = Array.new
for i in 1..3 do
tmp << Dir.glob(Dir.pwd+'/*'*i).delete_if{|f| File::ftype(f) != 'file'}
end
tmp.flatten!
files = Array.new
tmp.each{|f|
unless @ignores.map{|ig|
res = true if f =~ ig
res
}.include?(true)
files << f
end
}
files.map{|f| File::mtime(f).to_i}.max
end
EventMachine::run do
EventMachine::defer do
loop do
now = last_update
if last < now and pid == nil
last = now
IO.popen(cmd){|io|
pid = io.pid
loop do
res = io.gets
break unless res
puts res
sleep 0.1
end
}
pid = nil
end
sleep 0.1
end
end
EventMachine::defer do
loop do
now = last_update
if last < now and pid
puts "-- kill (pid:#{pid}), reload at #{Time.now} --"
`kill #{pid}`
pid = nil
end
sleep 0.1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment