Created
November 25, 2012 17:54
-
-
Save daytonn/4144556 to your computer and use it in GitHub Desktop.
Revisions
-
Dayton Nolan revised this gist
Nov 28, 2012 . 1 changed file with 13 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ namespace :jst do @javascripts_path = File.join(Rails.root, 'app', 'assets', 'javascripts') @templates_path = File.join(@javascripts_path, 'templates') @erb_template = File.join(@assets_path, 'compiled_templates.erb') @template_file = File.join(@javascripts_path, 'templates.js') desc "Compile the javascript templates" task :build => :environment do @@ -18,7 +18,7 @@ namespace :jst do File.open(@template_file, "w+") { |f| f << compiled_templates.result(binding) } puts "#{@template_file.gsub(Rails.root.to_s, '')} was built successfully" rescue Exception => e puts "There was an error compiling the templates: #{e.message}\n" raise e end end @@ -49,8 +49,17 @@ namespace :jst do data end def template_string(templates, result="{") templates.map do |key, value| result << "'#{key}':" if value.is_a?(Hash) result << "{" template_string(value, result) else result << "#{value}," end end result << "}" end end -
Dayton Nolan revised this gist
Nov 27, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,7 +50,7 @@ namespace :jst do end def template_string(templates) templates.to_s.gsub(/\=\>/, ': ').gsub(/\"function/, 'function').gsub(/\}\"/, '}').gsub(/\\n/, "\n") end end -
Dayton Nolan revised this gist
Nov 26, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ require 'erb' require 'ejs' require 'listen' -
Dayton Nolan revised this gist
Nov 26, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ group :development do gem 'ejs' gem 'rb-fsevent' # optional gem 'listen' end -
Dayton Nolan revised this gist
Nov 26, 2012 . 3 changed files with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ group :development do gem 'ejs' gem 'listen' end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,2 @@ var <%= "#{Rails.application.class.parent_name}" %> = <%= "#{Rails.application.class.parent_name}" %> || {}; <%= "#{Rails.application.class.parent_name}" %>.Templates = <%= templates %>; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ require 'erb require 'ejs' require 'listen' @@ -32,7 +33,7 @@ namespace :jst do def templates_hash(path, data = {}, namespace = nil) Dir.foreach(path) do |entry| next if entry =~ /^\./ full_path = File.join(path, entry) -
Dayton Nolan revised this gist
Nov 26, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ require 'ejs' require 'listen' namespace :jst do @assets_path = File.join(Rails.root, 'lib', 'assets') -
Dayton Nolan revised this gist
Nov 26, 2012 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,15 +37,14 @@ namespace :jst do next if entry =~ /^\.{1,2}$/ full_path = File.join(path, entry) if File.directory?(full_path) namespace = File.basename(entry, '.jst') templates_hash(full_path, data[namespace] = {}) else template_name = File.basename(entry, '.jst') data[template_name] = EJS.compile(File.read(full_path)) end end data -
Dayton Nolan revised this gist
Nov 26, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ namespace :jst do compiled_templates = ERB.new(File.read(@erb_template)) templates = template_string templates File.open(@template_file, "w+") { |f| f << compiled_templates.result(binding) } puts "#{@template_file.gsub(Rails.root.to_s, '')} was built successfully" rescue Exception => e puts "There was an error compiling the templates\n" raise e @@ -43,7 +43,7 @@ namespace :jst do templates_hash(full_path, data[namespace] = {}) else template_name = File.basename(entry, '.jst') data[template_name] = EJS.compile(File.read(full_path)) end end -
Dayton Nolan revised this gist
Nov 25, 2012 . 1 changed file with 58 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ require 'ejs' require 'erb' require 'listen' require 'fileutils' namespace :jst do @assets_path = File.join(Rails.root, 'lib', 'assets') @javascripts_path = File.join(Rails.root, 'app', 'assets', 'javascripts') @templates_path = File.join(@javascripts_path, 'templates') @erb_template = File.join(@assets_path, 'compiled_templates.erb') @template_file = File.join(@javascripts_path, 'application', 'templates.js') desc "Compile the javascript templates" task :build => :environment do begin templates = templates_hash @templates_path compiled_templates = ERB.new(File.read(@erb_template)) templates = template_string templates File.open(@template_file, "w+") { |f| f << compiled_templates.result(binding) } puts "#{@template_file} was built successfully" rescue Exception => e puts "There was an error compiling the templates\n" raise e end end desc "Watch the templates for changes and compile automatically" task :watch => :environment do puts "Watching for js template changes. Press Ctrl+C to quit" Listen.to(@templates_path, filter: /\.jst$/) do Rake::Task["jst:build"].execute end end def templates_hash(path, data = {}, namespace = nil) Dir.foreach(path) do |entry| next if entry =~ /^\.{1,2}$/ full_path = File.join(path, entry) namespace = File.basename(entry, '.jst') if File.directory?(full_path) templates_hash(full_path, data[namespace] = {}) else template_name = File.basename(entry, '.jst') data[template_name] = EJS.compile(File.read(full_path)) if data.is_a? Hash end end data end def template_string(templates) templates.to_s.gsub(/\=\>/, ': ').gsub(/\"function/, 'function').gsub(/\}\"/, '}') end end -
Dayton Nolan revised this gist
Nov 25, 2012 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1 @@ <%= "#{Rails.application.class.parent_name}" %>.Templates = <%= templates %>; -
Dayton Nolan created this gist
Nov 25, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ <%= "#{Rails.application.class.parent_name}" %>.Templates = { <%= templates.map { |n, t| "'#{n}': #{t}" }.join(",\n ") %> };