Skip to content

Instantly share code, notes, and snippets.

@TikiTDO
Last active December 22, 2015 23:48
Show Gist options
  • Save TikiTDO/6548979 to your computer and use it in GitHub Desktop.
Save TikiTDO/6548979 to your computer and use it in GitHub Desktop.
Dust + Slim multi builder Watches a directory in a sleep loop, and executes build chains based on file extensions. Currently supports dust and slim
#!/usr/bin/env ruby
require 'rubygems'
require 'slim'
require 'slim/command'
require 'pry'
def dust(tag, tag_end = nil)
ret = "{##{tag}}"
ret += yield
if tag_end then ret += "{#{tag_end}}" else ret += "{/#{tag}}" end
return ret
end
handlers = {
'slim' => proc do |input_file, name|
ret = Slim::Template.new(input_file).render()
input_file.close
StringIO.new(ret)
end,
'dust' => proc do |input_file, name|
IO.popen("dustc -n=#{name} -", "w+").tap do |io|
io.write input_file.read
input_file.close
io.close_write
end
end
}
stats = {}
while true
Dir.glob('*').each do |file_name|
# Only process updated files
new_stat = File.stat(file_name)
next if stats[file_name] == new_stat
stats[file_name] = new_stat
# Open first file for reading
file = File.open(file_name, 'r')
file_handlers = file_name.split('.')
# Try to go down the processing chain, and write any final files
skip = true
while handlers.has_key?(cur_handler = file_handlers.pop)
skip = false
file = handlers[cur_handler].call(file, file_handlers.join('.'))
end
next if skip
next if !file
# Return the last popped cur_handler onto the
file_handlers.push(cur_handler) if cur_handler
out_name = file_handlers.join('.')
File.open(out_name, 'w') do |out_file|
puts "Writing: #{out_name}"
out_file.write(file.read)
file.close
end
end
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment