Permalink
Newer
Older
100755 78 lines (55 sloc) 1.83 KB
1
#!/usr/local/bin/ruby
2
3
require "rubygems"
7
trap("SIGINT") { exit }
8
9
if ARGV.empty?
10
puts "Usage: #{$0} watch_folder"
11
puts " Example: #{$0} ."
12
exit
13
end
14
15
watch_folder = ARGV[0]
17
18
puts "Watching #{watch_folder} and subfolders for changes in SASS & HAML files..."
19
20
while true do
21
files = Dir.glob( File.join( watch_folder, "**", "*.haml" ) )
22
files += Dir.glob( File.join( watch_folder, "**", "*.sass" ) )
23
24
new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] }
25
hash ||= new_hash
26
diff_hash = new_hash - hash
27
28
unless diff_hash.empty?
29
hash = new_hash
30
31
diff_hash.each do |df|
32
f = df.first
34
output_file = ""
35
options = ""
36
is_haml = false
37
38
ex = f.match(/(sass|haml)$/)[1]
39
case ex
40
when "haml"
41
output_folder = "#{watch_folder}/output"
42
Dir.mkdir(output_folder) unless File.directory?(output_folder)
43
44
output_file = f.gsub(/\/haml\/([^\/]+)\.haml/, '/output/\1.html')
45
is_haml = true
47
when "sass"
48
output_folder = "#{watch_folder}/css"
49
Dir.mkdir(output_folder) unless File.directory?(output_folder)
50
51
output_file = f.gsub(/\/sass\/([^\/]+)\.sass/, '/css/\1.css')
52
options = "--style expanded"
53
56
cmd = "#{ex} #{options} #{f} #{output_file}"
57
puts "- #{cmd}"
58
system(cmd)
59
60
next unless is_haml
61
62
html = Hpricot( File.read(output_file) )
63
(html/"include").each do |inc|
64
fragment = File.read("#{watch_folder}/output/#{ inc['file'] }.html") rescue nil
65
next unless fragment
67
inc.swap("\n<!-- INCLUDE: #{ inc['file'] } START -->\n#{fragment}<!-- INCLUDE: #{ inc['file'] } END -->\n")
68
end
69
70
File.open(output_file, "w") do |f|
74
end
75
end
76
77
sleep 1
78
end