public
Created

runs an arbitrary commands when files change

  • Download Gist
rego.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#! /usr/bin/env ruby
 
## rego command to run -- files to watch
#
# rego command_to_run on @ -- {lib/*,test/*}
#
require 'time'
require 'pathname'
require 'rubygems'
 
pos = ARGV.index('--')
 
if pos
command = ARGV[0...pos].join(' ')
paths = ARGV[pos + 1 .. -1]
else
command = ARGV[0..-1].join(' ')
paths = []
end
 
command = 'echo @' if paths.empty?
paths = %w[.] if paths.empty?
 
paths.map!{|path| test(?d, path) ? [path, Dir.glob(File.join(path, '**/**'))] : path}
paths.flatten!
paths.compact!
paths.uniq!
paths.map! do |path|
Pathname.new(path).realpath.to_s
end
 
##
#
require 'yaml'
#STDERR.puts({command => paths}.to_yaml)
#STDERR.puts
puts "## #{ command }"
puts "#"
puts paths.join("\n")
puts
 
##
#
directories = []
files = []
paths.each do |path|
if test(?d, path)
directories.push(path)
else
files.push(path)
directories.push(File.dirname(path))
end
end
directories.uniq!
files.uniq!
 
stats = {}
 
files.each do |file|
begin
stats[file] = File.stat(file)
rescue
nil
end
end
 
require 'rb-fsevent'
fsevent = FSEvent.new
 
n = '0'
line = '#' * 42
$running = false
 
rego = proc do |*args|
entry = args.shift
cmd = entry ? command.gsub(/@/, entry) : command
puts line
puts "# rego.#{ n } @ #{ Time.now.strftime('%H:%M:%S') } - #{ cmd }"
puts
system(cmd)
puts
puts "# rego.#{ n } @ #{ Time.now.strftime('%H:%M:%S') } - #{ $?.exitstatus }"
puts
n.succ!
end
 
rego[ false ]
 
fsevent.watch(directories) do |*args|
unless $running
$running = true
 
args.flatten.each do |dir|
glob = File.join(dir, '**/**')
entries = Dir.glob(glob)
 
entries.each do |entry|
entry = File.expand_path(entry)
next unless stats.has_key?(entry)
 
begin
stats[entry] ||= File.stat(entry)
before = stats[entry]
after = File.stat(entry)
rescue
next
end
 
unless before.mtime == after.mtime
stats[entry] = after
rego[ entry ]
end
end
end
end
$running = false
end
 
fsevent.run
 
 
 
 
=begin
require 'rb-inotify'
notifier = INotify::Notifier.new
notifier.watch(paths, :modify) {puts "foo.txt was modified!"}
#notifier.watch("path/to/bar", :moved_to, :create) do |event|
#puts "#{event.name} is now in path/to/bar!"
#end
notifier.run
=end
 
 
 
#exit
 
=begin
 
require 'directory_watcher'
dw = DirectoryWatcher.new(paths, :scanner => :rev)
 
dw.add_observer do |*args|
args.each{|event| puts event}
end
 
dw.start
 
trap('INT'){ exit! }
sleep
=end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.