From 43587531c881f7baa7a5c7314ebed15111459f76 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 14:39:59 +0700 Subject: [PATCH 1/5] Build CommonJS just like any other target, using "pre" and "post" wrappers --- .gitignore | 1 + Rakefile | 5 +++-- mustache-commonjs/commonjs.mustache.js.tpl.post | 7 +++++++ mustache-commonjs/commonjs.mustache.js.tpl.pre | 6 ++++++ 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 mustache-commonjs/commonjs.mustache.js.tpl.post create mode 100644 mustache-commonjs/commonjs.mustache.js.tpl.pre diff --git a/.gitignore b/.gitignore index 252c5ae..a7ed15c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ runner.js jquery.mustache.js dojox yui3 +commonjs.mustache.js diff --git a/Rakefile b/Rakefile index 3298479..2946e97 100644 --- a/Rakefile +++ b/Rakefile @@ -14,8 +14,9 @@ task :spec desc "Package for CommonJS" task :commonjs do puts "Packaging for CommonJS" - `mkdir lib` - `cp mustache.js lib/mustache.js` + source = "mustache-commonjs" + target_jq = "commonjs.mustache.js" + `cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}` puts "Done." end diff --git a/mustache-commonjs/commonjs.mustache.js.tpl.post b/mustache-commonjs/commonjs.mustache.js.tpl.post new file mode 100644 index 0000000..885803f --- /dev/null +++ b/mustache-commonjs/commonjs.mustache.js.tpl.post @@ -0,0 +1,7 @@ + +exports.name = Mustache.name; +exports.version = Mustache.version; + +exports.to_html = function() { + return Mustache.to_html.apply(this, arguments); +}; diff --git a/mustache-commonjs/commonjs.mustache.js.tpl.pre b/mustache-commonjs/commonjs.mustache.js.tpl.pre new file mode 100644 index 0000000..5e33b34 --- /dev/null +++ b/mustache-commonjs/commonjs.mustache.js.tpl.pre @@ -0,0 +1,6 @@ +/* + * CommonJS-compatible mustache.js module + * + * See http://github.com/janl/mustache.js for more info. + */ + From f54312e879be6217be407b14f66296d4870200b9 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 14:43:44 +0700 Subject: [PATCH 2/5] Execute commands properly. You can see what's happening, and errors halt the build --- Rakefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Rakefile b/Rakefile index 2946e97..213bbda 100644 --- a/Rakefile +++ b/Rakefile @@ -16,7 +16,7 @@ task :commonjs do puts "Packaging for CommonJS" source = "mustache-commonjs" target_jq = "commonjs.mustache.js" - `cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}` + sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" puts "Done." end @@ -25,7 +25,7 @@ task :jquery do puts "Packaging for jQuery" source = "mustache-jquery" target_jq = "jquery.mustache.js" - `cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}` + sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" puts "Done, see ./#{target_jq}" end @@ -34,8 +34,8 @@ task :dojo do puts "Packaging for dojo" source = "mustache-dojo" target_js = "mustache.js" - `mkdir -p dojox; mkdir -p dojox/string` - `cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > dojox/string/#{target_js}` + sh "mkdir -p dojox; mkdir -p dojox/string" + sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > dojox/string/#{target_js}" puts "Done, see ./dojox/string/#{target_js} Include using dojo.require('dojox.string.mustache.'); " end @@ -44,12 +44,12 @@ task :yui3 do puts "Packaging for YUI3" source = "mustache-yui3" target_js = "mustache.js" - `mkdir -p yui3; mkdir -p yui3/mustache` - `cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > yui3/mustache/#{target_js}` + sh "kdir -p yui3; mkdir -p yui3/mustache" + sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > yui3/mustache/#{target_js}" puts "Done, see ./yui3/mustache/#{target_js}" end desc "Remove temporary files." task :clean do - `git clean -fdx` + sh "git clean -fdx" end From 01272005fe9e0ca9ee96ea4ce2127fd310fc86c6 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 15:08:28 +0700 Subject: [PATCH 3/5] Refactor template-style builds into a common function --- Rakefile | 54 +++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/Rakefile b/Rakefile index 213bbda..9e7712c 100644 --- a/Rakefile +++ b/Rakefile @@ -11,43 +11,27 @@ end desc "Run all specs" task :spec -desc "Package for CommonJS" -task :commonjs do - puts "Packaging for CommonJS" - source = "mustache-commonjs" - target_jq = "commonjs.mustache.js" - sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" - puts "Done." +def templated_build(name, opts={}) + # Create a rule that uses the .tmpl.{pre,post} stuff to make a final, wrapped, output file. + # There is some extra complexity because Dojo and YUI3 use different template files and final locations. + short = name.downcase + source = "mustache-#{short}" + dependencies = ["mustache.js"] + Dir.glob("#{source}/*.tpl.*") + + desc "Package for #{name}" + task short.to_sym => dependencies do + target_js = opts[:location] ? "mustache.js" : "#{short}.mustache.js" + + puts "Packaging for #{name}" + sh "mkdir -p #{opts[:location]}" if opts[:location] + sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" + end end -desc "Package for jQuery" -task :jquery do - puts "Packaging for jQuery" - source = "mustache-jquery" - target_jq = "jquery.mustache.js" - sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" - puts "Done, see ./#{target_jq}" -end - -desc "Package for dojo" -task :dojo do - puts "Packaging for dojo" - source = "mustache-dojo" - target_js = "mustache.js" - sh "mkdir -p dojox; mkdir -p dojox/string" - sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > dojox/string/#{target_js}" - puts "Done, see ./dojox/string/#{target_js} Include using dojo.require('dojox.string.mustache.'); " -end - -desc "Package for YUI3" -task :yui3 do - puts "Packaging for YUI3" - source = "mustache-yui3" - target_js = "mustache.js" - sh "kdir -p yui3; mkdir -p yui3/mustache" - sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > yui3/mustache/#{target_js}" - puts "Done, see ./yui3/mustache/#{target_js}" -end +templated_build "CommonJS" +templated_build "jQuery" +templated_build "Dojo", :location => "dojox/string" +templated_build "YUI3", :location => "yui3/mustache" desc "Remove temporary files." task :clean do From d82a1174d768c0de6e8e70e75a3366f2866b7fc4 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 15:21:31 +0700 Subject: [PATCH 4/5] Actually ignore comment tags; and correct the expected output in the test case --- examples/comments.txt | 2 +- mustache.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/comments.txt b/examples/comments.txt index 071e026..0133517 100644 --- a/examples/comments.txt +++ b/examples/comments.txt @@ -1 +1 @@ -

A Comedy of Errors{{! just something interesting... or not... }}

+

A Comedy of Errors

diff --git a/mustache.js b/mustache.js index fe87d9f..95088d2 100644 --- a/mustache.js +++ b/mustache.js @@ -157,7 +157,7 @@ var Mustache = function() { lines[i] = lines[i].replace(regex, function(match, operator, name) { switch(operator) { case "!": // ignore comments - return match; + return ""; case "=": // set new delimiters, rebuild the replace regexp that.set_delimiters(name); regex = new_regex(); From 0117a42532a834a0d0c3e9471a267e97e47a36bc Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 16:16:13 +0700 Subject: [PATCH 5/5] Output the "Done" message as before --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index 9e7712c..2941527 100644 --- a/Rakefile +++ b/Rakefile @@ -25,6 +25,7 @@ def templated_build(name, opts={}) puts "Packaging for #{name}" sh "mkdir -p #{opts[:location]}" if opts[:location] sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" + puts "Done, see #{opts[:location] || '.'}/#{target_js}" end end