-
Notifications
You must be signed in to change notification settings - Fork 3
/
morea-watch.rb
executable file
·96 lines (80 loc) · 2.52 KB
/
morea-watch.rb
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
#!/usr/bin/env ruby
# morea-watch.rb [email protected]
# Adapted from watch.rb by Brett Terpstra, 2011 <http://brettterpstra.com>
# with credit to Carlo Zottmann <https://github.com/carlo/haml-sass-file-watcher>
#
# This file must be in the same directory as morea-run-local.sh
#
# *Warning* If you watch a folder (or subfolder) that jekyll generates files in when run in it may unpleasantly
# stop and re-start for each generated file since we don't check when output is completed
# after forking morea-run-local (probably should do this though...)
#
if RUBY_PLATFORM =~ /mswin32/ then
puts "Sorry, morea-watch does not work on Windows"
exit
end
def fork_MOREA()
# stdout, stderr pipes
rout, wout = IO.pipe
rerr, werr = IO.pipe
pid = Process.spawn("./morea-run-local.sh", :out => wout, :err => werr)
Process.detach(pid)
# Wait until we see the MOREA server is running
until (line=rout.gets) =~ /Server running/
puts(line)
end
wout.close
werr.close
return pid
end
trap("SIGINT") { exit }
if ARGV.length > 1
puts "Usage: #{$0} watch_folder"
puts "Example: #{$0} ./master/src/morea"
exit
elseif ARGV.length == 1
watch_folder = watch_folder = ARGV[0]
else
watch_folder = "./master/src/morea"
end
filetypes = ['css','html','htm', 'js', 'md']
morea_pid = fork_MOREA
puts "Watching #{watch_folder} and subfolders for changes in project files..."
while true do
files = []
filetypes.each {|type|
files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) )
}
new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] }
hash ||= new_hash
diff_hash = new_hash - hash
unless diff_hash.empty?
hash = new_hash
diff_hash.each do |df|
puts "Detected change in #{df[0]}"
end
unless `pgrep -f .*morea-run-local.*` == ""
base=Process.pid
descendants = Hash.new{|ht,k| ht[k]=[k]}
Hash[*`ps -eo pid,ppid`.scan(/\d+/).map{|x|x.to_i}].each{|pid,ppid|
descendants[ppid] << descendants[pid]
}
descendants[base].flatten - [base]
kills = descendants[base].flatten - [base]
kills.each do |cpid|
begin
puts "killing pid #{cpid}"
Process.kill("SIGTERM", cpid)
rescue
next
end
end
Process.waitall
puts "goodby pid #{morea_pid}, restarting MOREA"
end
morea_pid = fork_MOREA
# If using Chrome, tell it to reload the page so changes can be seen
`osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'`
end
sleep 1
end