Sha256: fbd5e4ad14b34051259cd4189e0b90bd8f6f8129154dcea32216733d92972a9b

Contents?: true

Size: 1.99 KB

Versions: 20

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require 'erb'

module Karafka
  # Karafka framework Cli
  class Cli
    # Install Karafka Cli action
    class Install < Base
      include Helpers::Colorize

      desc 'Installs all required things for Karafka application in current directory'

      # Directories created by default
      INSTALL_DIRS = %w[
        app/consumers
        config
        log
        lib
      ].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',
        'example_consumer.rb.erb' => 'app/consumers/example_consumer.rb'
      }.freeze

      def initialize
        super

        dependencies = Bundler::LockfileParser.new(
          Bundler.read_file(
            Bundler.default_lockfile
          )
        ).dependencies

        @rails = dependencies.key?('railties') || dependencies.key?('rails')
      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

        puts
        puts 'Installing Karafka framework...'
        puts 'Ruby on Rails detected...' if rails?
        puts

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

          template = File.read(Karafka.core_root.join("templates/#{source}"))
          render = ::ERB.new(template, trim_mode: '-').result(binding)

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

          puts "#{green('Created')} #{target}"
        end

        puts
        puts("Installation #{green('completed')}. Have fun!")
        puts
      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

20 entries across 20 versions & 1 rubygems

Version Path
karafka-2.4.0 lib/karafka/cli/install.rb
karafka-2.4.0.rc1 lib/karafka/cli/install.rb
karafka-2.3.4 lib/karafka/cli/install.rb
karafka-2.4.0.beta2 lib/karafka/cli/install.rb
karafka-2.4.0.beta1 lib/karafka/cli/install.rb
karafka-2.3.3 lib/karafka/cli/install.rb
karafka-2.3.2 lib/karafka/cli/install.rb
karafka-2.3.1 lib/karafka/cli/install.rb
karafka-2.3.0 lib/karafka/cli/install.rb
karafka-2.3.0.rc1 lib/karafka/cli/install.rb
karafka-2.3.0.alpha2 lib/karafka/cli/install.rb
karafka-2.3.0.alpha1 lib/karafka/cli/install.rb
karafka-2.2.14 lib/karafka/cli/install.rb
karafka-2.2.13 lib/karafka/cli/install.rb
karafka-2.2.12 lib/karafka/cli/install.rb
karafka-2.2.11 lib/karafka/cli/install.rb
karafka-2.2.10 lib/karafka/cli/install.rb
karafka-2.2.9 lib/karafka/cli/install.rb
karafka-2.2.8 lib/karafka/cli/install.rb
karafka-2.2.8.beta1 lib/karafka/cli/install.rb