Sha256: 1a78df144d0c0dd2090c7bdd8e219d6ed2bcb7613b5bb4707193ffcef73ea9e9

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8
module LocalPac
  module Actions
    class CreateFile
      private

      attr_reader :name, :destination, :data, :options, :engine, :repository

      public

      def initialize(name, destination, data, options = {}, engine = ErbGenerator, repository = TemplateRepository.new)
        @name        = name
        @destination = destination
        @data        = data
        @options     = options
        @engine      = engine
        @repository  = repository
      end

      def run
        if need_to_run? || options[:force] == true
          LocalPac.ui_logger.warn "Creating file \"#{destination}\"."

          file = template(name, destination, data)
          FileUtils.chmod('+x', file) if options[:executable] == true
        else
          LocalPac.ui_logger.warn "File \"#{destination}\" already exists. Do not create it again!."
        end
      end

      private

      def template(local_name, local_destination, local_data)
        template = repository.find(local_name)

        generator = engine.new(local_data)
        generator.compile(template, ::File.new(local_destination, 'w'))

        local_destination
      rescue Errno::ENOENT
        fail Exceptions::ErbTemplateIsUnknown, "Unknown erb template \"#{template_path}\"."
      end

      def need_to_run?
        !File.exists?(destination)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
local_pac-0.1.9 lib/local_pac/actions/create_file.rb
local_pac-0.1.8 lib/local_pac/actions/create_file.rb
local_pac-0.1.7 lib/local_pac/actions/create_file.rb
local_pac-0.1.6 lib/local_pac/actions/create_file.rb
local_pac-0.1.5 lib/local_pac/actions/create_file.rb
local_pac-0.1.4 lib/local_pac/actions/create_file.rb
local_pac-0.1.3 lib/local_pac/actions/create_file.rb
local_pac-0.1.2 lib/local_pac/actions/create_file.rb
local_pac-0.1.1 lib/local_pac/actions/create_file.rb