Skip to content

Commit

Permalink
Added support for external templates system
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrdis committed Jan 4, 2013
1 parent 0d875aa commit 5abc034
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/guard/templates.rb
Expand Up @@ -58,15 +58,15 @@ def run_on_change(paths)
dir = Pathname.new(target[:path]).dirname.to_s dir = Pathname.new(target[:path]).dirname.to_s
FileUtils.mkdir_p(dir) if !File.exist?(dir) FileUtils.mkdir_p(dir) if !File.exist?(dir)
File.open(target[:path], 'w') do |f| File.open(target[:path], 'w') do |f|
f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, target[:type])}") f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, target)}")
end end
end end
end end
end end
end end
if @single_file_mode if @single_file_mode
File.open(@options[:output], 'w') do |f| File.open(@options[:output], 'w') do |f|
js = templates.map{|target,content| "#{target[:name].dump}: #{compile(content, target[:type])}" }.join(",\n") js = templates.map{|target,content| "#{target[:name].dump}: #{compile(content, target)}" }.join(",\n")
f.write "#{@options[:namespace]}.templates = {#{js}}" f.write "#{@options[:namespace]}.templates = {#{js}}"
end end
end end
Expand Down Expand Up @@ -103,11 +103,17 @@ def get_target(path, watcher)
end end
end end


def compile(str, type) def compile(str, target)
type = target[:type]
if Compilers.methods.include?("compile_#{type}") if Compilers.methods.include?("compile_#{type}")
Compilers.send("compile_#{type}", str) Compilers.send("compile_#{type}", str)
else else
str.dump begin
require "guard/templates/#{type}/compiler"
return Templates.const_get(type.capitalize)::Compiler.compile(str, target)
rescue LoadError
return str.dump
end
end end
end end


Expand Down

0 comments on commit 5abc034

Please sign in to comment.