Skip to content

Instantly share code, notes, and snippets.

@daytonn
Created November 25, 2012 17:54
Show Gist options
  • Select an option

  • Save daytonn/4144556 to your computer and use it in GitHub Desktop.

Select an option

Save daytonn/4144556 to your computer and use it in GitHub Desktop.

Revisions

  1. Dayton Nolan revised this gist Nov 28, 2012. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions jstemplates.rake
    Original 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, 'application', 'templates.js')
    @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\n"
    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)
    templates.to_s.gsub(/\=\>/, ': ').gsub(/\"function/, 'function').gsub(/\}\"/, '}').gsub(/\\n/, "\n")
    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
  2. Dayton Nolan revised this gist Nov 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jstemplates.rake
    Original 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(/\}\"/, '}')
    templates.to_s.gsub(/\=\>/, ': ').gsub(/\"function/, 'function').gsub(/\}\"/, '}').gsub(/\\n/, "\n")
    end

    end
  3. Dayton Nolan revised this gist Nov 26, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jstemplates.rake
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    require 'erb
    require 'erb'
    require 'ejs'
    require 'listen'

  4. Dayton Nolan revised this gist Nov 26, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Gemfile
    Original 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
  5. Dayton Nolan revised this gist Nov 26, 2012. 3 changed files with 7 additions and 1 deletion.
    4 changes: 4 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    group :development do
    gem 'ejs'
    gem 'listen'
    end
    1 change: 1 addition & 0 deletions compiled_templates.erb
    Original 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 %>;
    3 changes: 2 additions & 1 deletion jstemplates.rake
    Original 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 =~ /^\.{1,2}$/
    next if entry =~ /^\./
    full_path = File.join(path, entry)
  6. Dayton Nolan revised this gist Nov 26, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions jstemplates.rake
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    require 'ejs'
    require 'erb'
    require 'listen'
    require 'fileutils'

    namespace :jst do
    @assets_path = File.join(Rails.root, 'lib', 'assets')
  7. Dayton Nolan revised this gist Nov 26, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions jstemplates.rake
    Original 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)
    namespace = File.basename(entry, '.jst')

    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
  8. Dayton Nolan revised this gist Nov 26, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jstemplates.rake
    Original 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} was built successfully"
    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)) if data.is_a? Hash
    data[template_name] = EJS.compile(File.read(full_path))
    end

    end
  9. Dayton Nolan revised this gist Nov 25, 2012. 1 changed file with 58 additions and 0 deletions.
    58 changes: 58 additions & 0 deletions jstemplates.rake
    Original 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
  10. Dayton Nolan revised this gist Nov 25, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions compiled_templates.erb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1 @@
    <%= "#{Rails.application.class.parent_name}" %>.Templates = {
    <%= templates.map { |n, t| "'#{n}': #{t}" }.join(",\n ") %>
    };
    <%= "#{Rails.application.class.parent_name}" %>.Templates = <%= templates %>;
  11. Dayton Nolan created this gist Nov 25, 2012.
    3 changes: 3 additions & 0 deletions compiled_templates.erb
    Original 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 ") %>
    };