Sha256: 257913d4c7b3e2232cb9cbc4b751c588706ee6fdc2bd7f6d0965fc2bb0ae1505

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

module Terraspace::Compiler
  class Builder
    include Basename

    def initialize(mod)
      @mod = mod
    end

    def build
      build_config
      build_module
      build_tfvars
    end

    # build common config files: provider and backend for the root module
    def build_config
      return unless @mod.root_module?
      build_config_templates
    end

    def build_module
      with_mod_file do |src_path|
        build_mod_file(src_path)
      end
    end

    def build_tfvars
      return unless @mod.root_module?
      Strategy::Tfvar.new(@mod).run # writer within Strategy to control file ordering
    end

  private
    def build_config_templates
      expr = "#{Terraspace.root}/config/terraform/**/*"
      Dir.glob(expr).each do |path|
        next unless File.file?(path)
        build_config_file(basename(path))
      end
    end

    def build_config_file(file)
      existing = Dir.glob("#{@mod.root}/#{file}").first
      return if existing && existing.ends_with?(".tf") # do not overwrite existing backend.tf, provider.tf, etc

      if file.ends_with?(".rb")
        src_path = Dir.glob("#{@mod.root}/#{basename(file)}").first # existing source. IE: backend.rb in module folder
      end
      src_path ||= Dir.glob("#{Terraspace.root}/config/terraform/#{file}").first
      build_mod_file(src_path) if src_path
    end

    def build_mod_file(src_path)
      content = Strategy::Mod.new(@mod, src_path).run
      Writer.new(@mod, src_path: src_path).write(content)
    end

    def with_mod_file(&block)
      with_path("#{@mod.root}/**/*", &block) # Only all files
    end

    def with_path(path)
      Dir.glob(path).each do |src_path|
        next if skip?(src_path)
        yield(src_path)
      end
    end

    def skip?(src_path)
      return true unless File.file?(src_path)
      # certain folders will be skipped
      src_path.include?("#{@mod.root}/test")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
terraspace-0.1.2 lib/terraspace/compiler/builder.rb
terraspace-0.1.1 lib/terraspace/compiler/builder.rb
terraspace-0.1.0 lib/terraspace/compiler/builder.rb