Sha256: e3a8c1c0bce530883bd4dd6c70472af69a57d06743e7014c8b149e6fd6f63a30

Contents?: true

Size: 1.6 KB

Versions: 32

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require 'eac_templates/variables/file'

module EacTemplates
  module Variables
    class FsObject
      TEMPLATE_EXTNAME = '.template'

      common_constructor :source_directory, :source_relative, :target_root_directory,
                         :variables_source

      def apply
        if file?
          apply_file
        elsif directory?
          apply_directory
        else
          raise "Unknown filesystem type: #{source_absolute}"
        end
      end

      private

      def apply_directory
        ::FileUtils.mkdir_p(target_absolute)
        Dir.entries(source_absolute).each do |entry|
          child(entry).apply unless %w[. ..].include?(entry)
        end
      end

      def apply_file
        if ::File.extname(source_absolute) == TEMPLATE_EXTNAME
          ::EacTemplates::Variables::File.new(source_absolute).apply_to_file(
            variables_source, target_absolute
          )
        else
          ::FileUtils.cp(source_absolute, target_absolute)
        end
      end

      def child(entry)
        self.class.new(source_directory, ::File.join(source_relative, entry),
                       target_root_directory, variables_source)
      end

      def file?
        ::File.file?(source_absolute)
      end

      def directory?
        ::File.directory?(source_absolute)
      end

      def source_absolute
        ::File.expand_path(source_relative, source_directory.path)
      end

      def target_absolute
        ::File.expand_path(source_relative, target_root_directory)
          .gsub(/#{::Regexp.quote(TEMPLATE_EXTNAME)}\z/, '')
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
eac_tools-0.66.0 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.65.1 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.65.0 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.64.0 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.63.0 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.62.1 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.62.0 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_templates-0.5.0 lib/eac_templates/variables/fs_object.rb
eac_tools-0.61.1 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_tools-0.61.0 sub/eac_templates/lib/eac_templates/variables/fs_object.rb
eac_templates-0.4.0 lib/eac_templates/variables/fs_object.rb
eac_tools-0.60.3 sub/eac_templates/lib/eac_templates/variables/fs_object.rb