Class: Soca::Plugins::Mustache

Inherits:
Soca::Plugin show all
Defined in:
lib/soca/plugins/mustache.rb

Instance Attribute Summary

Attributes inherited from Soca::Plugin

#options, #pusher

Instance Method Summary collapse

Methods inherited from Soca::Plugin

#app_dir, #config, #initialize, #logger, name, plugins

Constructor Details

This class inherits a constructor from Soca::Plugin

Instance Method Details

#before_buildObject

Run the mustache plugin. Available options:

  • :files - Run these files through mustache. Can be an array of patterns or a single file. The default is '*.mustache'.
  • :vars - Additional variables to interpolate. By default the env and config are interpolated.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/soca/plugins/mustache.rb', line 17

def before_build
  file_patterns = options[:files] || '*.mustache'
  files = Dir[*[file_patterns].flatten]
  vars = {
    :env => pusher.env,
    :config => pusher.config
  }.merge(options[:vars] || {})
  Soca.logger.debug "Mustache vars: #{vars.inspect}"
  files.each do |file|
    Soca.logger.debug "Running #{file} through mustache."
    basename = File.basename(file)
    dir      = File.dirname(file)
    parts    = basename.split(/\./)
    new_file = parts.length > 2 ? parts[0..-2].join('.') : parts[0] + ".html"
    File.open(File.join(dir, new_file), 'w') do |f|
      f << ::Mustache.render(File.read(file), vars)
    end
    Soca.logger.debug "Wrote to #{new_file}"
  end
end