Sha256: cc69987967a9f5bda14fdf668fc3ac91c6b891408a854e9fcd4962b235d18f3b

Contents?: true

Size: 1.34 KB

Versions: 14

Compression:

Stored size: 1.34 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 = "/tmp/terraspace/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

14 entries across 14 versions & 1 rubygems

Version Path
terraspace-2.2.15 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.14 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.13 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.12 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.11 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.10 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.9 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.8 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.7 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.6 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.5 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.4 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.3 lib/terraspace/compiler/erb/rewrite.rb
terraspace-2.2.2 lib/terraspace/compiler/erb/rewrite.rb