Class: Closure::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/closure/templates.rb

Defined Under Namespace

Classes: Error

Class Method Summary (collapse)

Class Method Details

+ compile(args, base = nil)

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

Examples:

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

Parameters:

  • args (String)

    Arguments for SoyToJsSrcCompiler.jar.

  • base (String) (defaults to: nil)

    All source filenames will be expanded to this location.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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