Sha256: 93506cb6d1dfb525423f5d09350cadb6376b17c5d4cc6953eeb6094ae267df11

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

class Jax::Packager::SprocketsTemplate < Sprockets::SourceFile
  def initialize(environment)
    pathname = environment.pathname_from(File.join(Jax.root, "template.js"))
    super(environment, pathname)
  end
  
  def template
    added_files.clear
    @template ||= begin
      template = [
        'Jax.environment = Jax.PRODUCTION;',
        '',
        '//= provide "public/"',
        ''
      ]
      # Need to verify that helpers come first
      Dir[Jax.root.join("app/helpers/**/*.js")].each { |jsfi| try_to_add_file(template, jsfi) }
      Dir[Jax.root.join("app/**/*.js")].each { |jsfi| try_to_add_file(template, jsfi) }

      template
    end
  end
  
  def try_to_add_file(template, jsfi)
    if File.file?(jsfi) && !already_added?(jsfi)
      add_file(template, jsfi)
    end
  end
  
  def add_file(template, jsfi)
    relative_path = jsfi.sub(/^#{Regexp::escape Jax.root.to_s}[\/\\]?/, '')
    template << "//= require \"#{relative_path}\""
    added_files << jsfi
  end
  
  def already_added?(jsfi)
    added_files.include?(jsfi)
  end
  
  def added_files
    @added_files ||= []
  end
  
  def source_lines
    # basically the same as super but with a string instead of a file
    # TODO would templating be a worthy addition to Sprockets itself?
    @lines ||= begin
      lines = []
      comments = []
      
      template.each_with_index do |line, lineno|
        lines << line = Sprockets::SourceLine.new(self, line, lineno+1)
        if line.begins_pdoc_comment? || comments.any?
          comments << line
        end
        
        if line.ends_multiline_comment?
          if line.ends_pdoc_comment?
            comments.each { |l| l.comment! }
          end
          comments.clear
        end
      end

      lines
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jax-1.0.1 lib/jax/packager/sprockets_template.rb
jax-1.0.1.rc2 lib/jax/packager/sprockets_template.rb
jax-1.0.1.rc1 lib/jax/packager/sprockets_template.rb
jax-1.0.0 lib/jax/packager/sprockets_template.rb
jax-0.0.0.10 lib/jax/packager/sprockets_template.rb
jax-0.0.0.9 lib/jax/packager/sprockets_template.rb
jax-0.0.0.8 lib/jax/packager/sprockets_template.rb
jax-0.0.0.7 lib/jax/packager/sprockets_template.rb