class Closure::Templates

Public Class Methods

compile(args, base = nil) click to toggle source

Compiles soy to javascript with SoyToJsSrcCompiler.jar. Supports globbing on source filename arguments. @example

Closure::Templates.compile %w{
  --shouldProvideRequireSoyNamespaces
  --cssHandlingScheme goog
  --shouldGenerateJsdoc
  --outputPathFormat {INPUT_DIRECTORY}{INPUT_FILE_NAME_NO_EXT}.js
  app/javascripts/**/*.soy
  vendor/javascripts/**/*.soy
}

@param (String) args Arguments for SoyToJsSrcCompiler.jar. @param (String) base All source filenames will be expanded to this location.

# File lib/closure/templates.rb, line 37
def self.compile(args, base = nil)
  mtimes = mtimes(args, base)
  new_mtimes = {}
  args = args.collect {|a| a.to_s } # for bools and numerics
  files = []
  # expand filename globs
  mode = :start
  args_index = 0
  while args_index < args.length
    if mode == :start
      if args[args_index] == '--outputPathFormat'
        mode = :expand
        args_index += 1
      end
      args_index += 1
    else
      arg = args[args_index]
      arg = File.expand_path(arg, base) if base
      if arg =~ /\*/
        args[args_index,1] = Dir.glob arg
      else
        args[args_index,1] = arg
        args_index += 1
      end
    end
  end
  # extract filenames
  mode = :start
  args.each do |argument|
    mode = :out and next if argument == '--outputPathFormat'
    files << argument if mode == :collect
    mode = :collect if mode == :out
  end
  # detect source changes
  compiled = true
  files.each do |file|
    filename = File.expand_path file
    mtime = File.mtime filename rescue Errno::ENOENT
    last_mtime = mtimes[filename]
    if !mtime or !last_mtime or last_mtime != mtime
      compiled = false
    end
    new_mtimes[filename] = mtime
  end
  mtimes.clear
  # compile as needed
  if !compiled
    _, err = Closure.run_java Closure.config.soy_js_jar, 'com.google.template.soy.SoyToJsSrcCompiler', args
    unless err.empty?
      raise Error, err
    end
  end
  # success, keep the mtimes for next time
  mtimes.merge! new_mtimes
end

Private Class Methods

mtimes(args, base) click to toggle source
# File lib/closure/templates.rb, line 99
def self.mtimes(args, base)
  mtimes = @mtimes_cache[[args, base]] ||= {:mtimes => {}}
  mtimes[:used] = Time.now
  if @mtimes_cache.length > 25
    @mtimes_cache.delete @mtimes_cache.min{|a,b|a[1][:used]<=>b[1][:used]}[0]
  end
  mtimes[:mtimes]
end