Sha256: 39ea0d9997c87eead52579330e33704f003ae2c037d578d6b3990b8ab97b2a9d

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 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 =::File.expand_path(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}\"."

          create_directories if options[:create_directories] == true

          file = template(name, ::File.new(destination, 'w'), 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 create_directories
        FileUtils.mkdir_p(::File.dirname(destination))
      end

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

        generator = engine.new(local_data)
        generator.compile(template, local_destination)

        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

10 entries across 10 versions & 1 rubygems

Version Path
local_pac-0.6.3 lib/local_pac/actions/create_file.rb
local_pac-0.6.2 lib/local_pac/actions/create_file.rb
local_pac-0.6.1 lib/local_pac/actions/create_file.rb
local_pac-0.5.0 lib/local_pac/actions/create_file.rb
local_pac-0.4.0 lib/local_pac/actions/create_file.rb
local_pac-0.3.0 lib/local_pac/actions/create_file.rb
local_pac-0.2.3 lib/local_pac/actions/create_file.rb
local_pac-0.2.2 lib/local_pac/actions/create_file.rb
local_pac-0.2.1 lib/local_pac/actions/create_file.rb
local_pac-0.2.0 lib/local_pac/actions/create_file.rb