Sha256: a91e59caa3dde844e65089c6610d913bb64e9a85f8b82c64b5628d8117afad42

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'erb'

module Karafka
  # Karafka framework Cli
  class Cli < Thor
    # Install Karafka Cli action
    class Install < Base
      desc 'Install all required things for Karafka application in current directory'

      # Directories created by default
      INSTALL_DIRS = %w[
        app/consumers
        app/responders
        app/workers
        config
        lib
        log
        tmp/pids
      ].freeze

      # Where should we map proper files from templates
      INSTALL_FILES_MAP = {
        'karafka.rb.erb' => Karafka.boot_file.basename,
        'application_consumer.rb.erb' => 'app/consumers/application_consumer.rb',
        'application_responder.rb.erb' => 'app/responders/application_responder.rb'
      }.freeze

      # @param args [Array] all the things that Thor CLI accepts
      def initialize(*args)
        super
        @rails = Bundler::LockfileParser.new(
          Bundler.read_file(
            Bundler.default_lockfile
          )
        ).dependencies.key?('railties')
      end

      # Install all required things for Karafka application in current directory
      def call
        INSTALL_DIRS.each do |dir|
          FileUtils.mkdir_p Karafka.root.join(dir)
        end

        INSTALL_FILES_MAP.each do |source, target|
          target = Karafka.root.join(target)

          template = File.read(Karafka.core_root.join("templates/#{source}"))
          # @todo Replace with the keyword argument version once we don't have to support
          # Ruby < 2.6
          render = ::ERB.new(template, nil, '-').result(binding)

          File.open(target, 'w') { |file| file.write(render) }
        end
      end

      # @return [Boolean] true if we have Rails loaded
      # This allows us to generate customized karafka.rb template with some tweaks specific for
      # Rails
      def rails?
        @rails
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
karafka-1.4.7 lib/karafka/cli/install.rb
karafka-1.4.6 lib/karafka/cli/install.rb
karafka-1.4.5 lib/karafka/cli/install.rb
karafka-1.4.4 lib/karafka/cli/install.rb
karafka-1.4.3 lib/karafka/cli/install.rb