Class: Karafka::Cli::Install

Inherits:
Base
  • Object
show all
Includes:
Helpers::Colorize
Defined in:
lib/karafka/cli/install.rb

Overview

Install Karafka Cli action

Constant Summary collapse

INSTALL_DIRS =

Directories created by default

%w[
  app/consumers
  config
  log
  lib
].freeze
INSTALL_FILES_MAP =

Where should we map proper files from templates

{
  '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

Instance Attribute Summary

Attributes inherited from Base

#cli

Instance Method Summary collapse

Methods included from Helpers::Colorize

#green, #red, #yellow

Methods inherited from Base

bind_to, desc, load, option

Constructor Details

#initialize(*args) ⇒ Install

Returns a new instance of Install.

Parameters:

  • args (Array)

    all the things that Thor CLI accepts



30
31
32
33
34
35
36
37
38
39
# File 'lib/karafka/cli/install.rb', line 30

def initialize(*args)
  super
  dependencies = Bundler::LockfileParser.new(
    Bundler.read_file(
      Bundler.default_lockfile
    )
  ).dependencies

  @rails = dependencies.key?('railties') || dependencies.key?('rails')
end

Instance Method Details

#callObject

Install all required things for Karafka application in current directory



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/karafka/cli/install.rb', line 42

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

#rails?Boolean

This allows us to generate customized karafka.rb template with some tweaks specific for Rails

Returns:

  • (Boolean)

    true if we have Rails loaded



71
72
73
# File 'lib/karafka/cli/install.rb', line 71

def rails?
  @rails
end