Sha256: d859db715b74dfccd2eea82e0160065fe509c00a391bae4d0d6c3e86307e16dc

Contents?: true

Size: 1.55 KB

Versions: 20

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8
module TestServer
  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
          TestServer.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
          TestServer.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, JSON.dump(message: "Unknown erb template \"#{template_path}\".")
      end

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
test_server-0.5.2 lib/test_server/actions/create_file.rb
test_server-0.5.1 lib/test_server/actions/create_file.rb
test_server-0.5.0 lib/test_server/actions/create_file.rb
test_server-0.4.1 lib/test_server/actions/create_file.rb
test_server-0.4.0 lib/test_server/actions/create_file.rb
test_server-0.3.7 lib/test_server/actions/create_file.rb
test_server-0.3.6 lib/test_server/actions/create_file.rb
test_server-0.3.5 lib/test_server/actions/create_file.rb
test_server-0.3.4 lib/test_server/actions/create_file.rb
test_server-0.3.2 lib/test_server/actions/create_file.rb
test_server-0.3.1 lib/test_server/actions/create_file.rb
test_server-0.2.4 lib/test_server/actions/create_file.rb
test_server-0.2.3 lib/test_server/actions/create_file.rb
test_server-0.2.2 lib/test_server/actions/create_file.rb
test_server-0.2.1 lib/test_server/actions/create_file.rb
test_server-0.2.0 lib/test_server/actions/create_file.rb
test_server-0.1.2 lib/test_server/actions/create_file.rb
test_server-0.1.1 lib/test_server/actions/create_file.rb
test_server-0.1.0 lib/test_server/actions/create_file.rb
test_server-0.0.11 lib/test_server/actions/create_file.rb