Sha256: 695251565ff4e9815853f94c52e61ead3ba34d0b4ce794700204c415fe9c22dd

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module AnsibleDocGenerator
  class DocGenerator
    class Interpolator
      class FileExtractor
        attr_reader :input, :role_path

        def initialize input, role_path
          @input = input
          @role_path = role_path
        end

        def call
          output = input

          input.scan(/f\{[^\}]+\}/).each do |interpolation|
            file_content = get_file_content(interpolation)
            output.gsub!(interpolation, file_content)
          end

          output
        end

        private

        def get_file_content interpolation
          file_name = interpolation.match(/f\{(\S*)\}/)[1]
          file_path = get_file_path_from(file_name)

          return interpolation if file_path.nil?
          File.read(file_path)
        end

        def get_file_path_from file_name
          %w(files templates)
            .map{|folder_name| File.join(main_folder, folder_name, file_name) }
            .find{|file_path| File.exists?(file_path) }
        end

        def main_folder
          @main_folder ||= File.expand_path("..", File.dirname(role_path))
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ansible_doc_generator-0.1.0 lib/ansible_doc_generator/doc_generator/interpolator/file_extractor.rb