Sha256: ba5e0c9de91166b0ce3a67368e299ded097185f2b0c9785897ed78c478dc8253

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'json'

module AwsCftTools
  class Template
    ##
    # Manage template and parameter files.
    #
    module FileSystem
      ##
      # Returns the path to the cloud formation template.
      #
      # @return [Pathname]
      def template_file
        filename_path(:template_dir, filename)
      end

      ##
      # Returns the path to the template parameters file.
      #
      # @return [Pathname]
      def parameter_file
        filename_path(:parameter_dir, filename)
      end

      ##
      # The unparsed source of the template.
      #
      # @return [String]
      def template_source
        @template_source ||= @options[:template_content] || read_file(template_file)
      end

      ##
      # The unparsed source of the parameters file for this template.
      #
      # @return [String]
      def parameters_source
        @parameters_source ||= @options[:parameters_content] || read_file(parameter_file)
      end

      private

      def read_file(file)
        file ? file.read : nil
      end

      ##
      # Given the filename relative to the template/parameter root and a symbol indicating which type of
      # file to point to, returns the full path to the file
      #
      # @return [Pathname]
      def filename_path(dir, filename)
        # we need to check .yaml, .yml, and .json versions
        filename = filename.to_s.sub(/\.[^.]*$/, '')
        base = @options[:root] + @options[dir]
        %w[.yaml .yml .json .rb].map { |ext| base + (filename + ext) }.detect(&:exist?)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
aws-cft-tools-0.1.4 lib/aws_cft_tools/template/file_system.rb
aws-cft-tools-0.1.3 lib/aws_cft_tools/template/file_system.rb
aws-cft-tools-0.1.2 lib/aws_cft_tools/template/file_system.rb
aws-cft-tools-0.1.1 lib/aws_cft_tools/template/file_system.rb
aws-cft-tools-0.1.0 lib/aws_cft_tools/template/file_system.rb