Permalink
Browse files

Fix require path in specs and fix native function jade compilation in…

… single file mode.
  • Loading branch information...
1 parent c749255 commit 833d46bafc89bda61f73d9dd0b276ed8d36eb8f3 @thegreatape committed Mar 31, 2012
Showing with 27 additions and 6 deletions.
  1. +7 −5 lib/guard/templates.rb
  2. +20 −1 spec/guard-templates-spec.rb
View
@@ -50,23 +50,22 @@ def run_on_change(paths)
paths.each do |path|
@watchers.each do |watcher|
if target = get_target(path, watcher)
- type = File.extname(path).gsub(/^\./,'')
contents = File.read(path)
if @single_file_mode
- templates[target[:name]] = contents
+ templates[target] = contents
else
dir = Pathname.new(target[:path]).dirname.to_s
FileUtils.mkdir_p(dir) if !File.exist?(dir)
File.open(target[:path], 'w') do |f|
- f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, type)}")
+ f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, target[:type])}")
end
end
end
end
end
if @single_file_mode
File.open(@options[:output], 'w') do |f|
- js = templates.map{|k,v| "#{k.dump}: #{v.dump}"}.join(",\n")
+ js = templates.map{|target,content| "#{target[:name].dump}: #{compile(content, target[:type])}" }.join(",\n")
f.write "#{@options[:namespace]}.templates = {#{js}}"
end
end
@@ -95,8 +94,11 @@ def run_on_deletion(paths)
def get_target(path, watcher)
if match = path.match(watcher.pattern)
subpath = match[1]
+ jspath = File.join(@options[:output], "#{subpath}.js")
+
{:name => subpath,
- :path => File.join(@options[:output], "#{subpath}.js")}
+ :type => File.extname(path).gsub(/^\./,''),
+ :path => jspath}
end
end
@@ -3,7 +3,7 @@
require 'execjs'
require 'fakefs/safe'
-require 'guard-templates'
+require 'guard/templates'
require 'guard/watcher'
# ExecJS needs real filesystem access to find the js engine
@@ -115,6 +115,25 @@ def eval(source, options={})
fn = "app['jade-test']()"
eval_js(fn, context).should == '<div class="greetings">O HAI</div>'
end
+
+ it "should compile jade to native functions in single-file mode" do
+ jade_template = 'javascripts/templates/jade-test.jade'
+ jade_content = '.greetings O HAI'
+ File.open(jade_template, 'w') {|f| f.write(jade_content)}
+ @guard_templates = Guard::Templates.new([Guard::Watcher.new(/javascripts\/templates\/(.*)\.jade/)],
+ :namespace => 'app',
+ :output => 'public/templates/all.js')
+ @guard_templates.run_on_change [jade_template]
+ File.exists?('public/templates/all.js').should == true
+ File.exists?('public/templates/jade-test.js').should == false
+ compiled = File.read('public/templates/all.js')
+ jade = File.read('lib/engines/jade-runtime.js')
+ context = %{var window = {}, app = {};
+ #{jade}
+ #{compiled} }
+ fn = "app.templates['jade-test']()"
+ eval_js(fn, context).should == '<div class="greetings">O HAI</div>'
+ end
end
describe "#run_on_deletion" do

0 comments on commit 833d46b

Please sign in to comment.