Sha256: 371e67be8a534e23baab63dfe4303f0940f5f9a0ccccb3c7c0510c3468f52d66

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Terraspace::Compiler::Erb
  class Rewrite
    def initialize(src_path)
      @src_path = src_path
    end

    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

    # Replace contents so only the `output` and `depends_on` are evaluated
    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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.16 lib/terraspace/compiler/erb/rewrite.rb