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

#options

Instance Method Summary collapse

Methods included from Helpers::Colorize

#green, #grey, #red, #yellow

Methods inherited from Base

aliases, commands, desc, load, name, names, option, parse_options

Constructor Details

#initializeInstall

Returns a new instance of Install.



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

def initialize
  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
67
# 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)
    FileUtils.mkdir_p File.dirname(pathed_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



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

def rails?
  @rails
end