Class: Terraspace::Compiler::Erb::Rewrite

Inherits:
Object
  • Object
show all
Defined in:
lib/terraspace/compiler/erb/rewrite.rb

Instance Method Summary collapse

Constructor Details

#initialize(src_path) ⇒ Rewrite

Returns a new instance of Rewrite.



3
4
5
# File 'lib/terraspace/compiler/erb/rewrite.rb', line 3

def initialize(src_path)
  @src_path = src_path
end

Instance Method Details

#new_line(line) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/terraspace/compiler/erb/rewrite.rb', line 26

def new_line(line)
  md = line.match(/.*(<% |<%=)/) || line.match(/.*<%$/)
  if md
    words = %w[output depends_on]
    dependency_words = [Terraspace.config.build.dependency_words].flatten
    words += dependency_words # custom user words to evaluated in first pass also
    return line if words.include?('*') # passthrough for special case '*'
    # IE: <%= output or <% depends_on
    regexp = Regexp.new(".*<%.*#{words.join('|')}.*")
    if line.match(regexp)
      line # passthrough
    else
      line.sub('<%', '<%#') # replace with ERB opening comment
    end
  else
    line # passthrough
  end
end

#replace(input) ⇒ Object

Replace contents so only the ‘output` and `depends_on` are evaluated



18
19
20
21
22
23
24
# File 'lib/terraspace/compiler/erb/rewrite.rb', line 18

def replace(input)
  lines = input.split("\n").map {|l| l+"\n"} # mimic IO.readlines
  new_lines = lines.map do |line|
    new_line(line)
  end
  new_lines.join('')
end

#rewriteObject



7
8
9
10
11
12
13
14
15
# File 'lib/terraspace/compiler/erb/rewrite.rb', line 7

def rewrite
  input = IO.read(@src_path)
  output = replace(input)
  tfvar_path = @src_path.sub(Terraspace.root,'')
  temp_path = "#{Terraspace.tmp_root}/rewrite#{tfvar_path}"
  FileUtils.mkdir_p(File.dirname(temp_path))
  IO.write(temp_path, output)
  temp_path
end