Class: Sumcli::Commands::Add::Service

Inherits:
Sumcli::Command show all
Defined in:
lib/sumcli/commands/add/service.rb

Constant Summary collapse

DOCKER_FILE =
'docker-compose.yml'
TEMPLATES_PATH =
File.expand_path('../../templates/add/service', __dir__)
CONFIGS_PATH =
'config'
WORKERS_PATH =
'app/workers'
BIN_PATH =
'bin'

Instance Method Summary collapse

Methods inherited from Sumcli::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(name, version, options) ⇒ Service

Returns a new instance of Service.



16
17
18
19
20
# File 'lib/sumcli/commands/add/service.rb', line 16

def initialize(name, version, options)
  @name = name.downcase
  @version = version
  @options = options
end

Instance Method Details

#add_postgresObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sumcli/commands/add/service.rb', line 78

def add_postgres
  ver = '10'
  response = ask_to_set(ver) if ver != @version && !@version.nil?
  ver = @version if response

  content = 
<<-TEXT
  db:
    image: 'postgres:#{ver}'
    ports:
      - '5432:5432'
TEXT

  generator.safe_inject_into_file DOCKER_FILE, content, after: "services:\n"
  copy_database_files
end

#add_rabbitmqObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sumcli/commands/add/service.rb', line 95

def add_rabbitmq
  ver = '3-management'
  response = ask_to_set(ver) if ver != @version && !@version.nil?
  ver = @version if response

  content = 
<<-TEXT
  rabbitmq:
    image: 'rabbitmq:#{ver}'
    ports:
      - '15672:15672'
      - '5672:5672'
TEXT

  generator.safe_inject_into_file DOCKER_FILE, content, after: "services:\n"
  copy_messaging_files
end

#ask_to_set(ver) ⇒ Object



117
118
119
120
# File 'lib/sumcli/commands/add/service.rb', line 117

def ask_to_set(ver)
  prompt.yes?("The default version of #{@name} is #{ver}." + 
    "\n\nDo you want to add version #{@version}?")
end

#copy_database_filesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sumcli/commands/add/service.rb', line 32

def copy_database_files
  generator.safe_inject_into_file('Gemfile', after: "# GEMS\n\n") do <<~RUBY
    gem 'pg', '~> 1.1'
    gem "otr-activerecord", '~> 1.3.0'
    RUBY
  end
  generator.copy_file("#{TEMPLATES_PATH}/database.yml", "#{CONFIGS_PATH}/database.yml")
  generator.copy_file("#{TEMPLATES_PATH}/database.yml.ctmpl", "#{CONFIGS_PATH}/database.yml.ctmpl")
  generator.copy_file("#{TEMPLATES_PATH}/database.rb", "#{CONFIGS_PATH}/initializers/database.rb")
  generator.safe_inject_into_file("#{CONFIGS_PATH}/application.rb", after: "require_rel '../api'\n") do
    'require_rel \'initializers\''
  end
  generator.safe_inject_into_file('Rakefile', after: "# TASKS\n") do <<~RUBY.strip
    load "tasks/otr-activerecord.rake"

    namespace :db do
      task :environment do
        require_relative 'config/environment'
      end
    end
    RUBY
  end
end

#copy_messaging_filesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sumcli/commands/add/service.rb', line 56

def copy_messaging_files
  generator.safe_inject_into_file('Gemfile', after: "# GEMS\n\n") do <<~RUBY
    gem 'sneakers', '~> 2.11'
    gem 'sneakers_handlers', '~> 0.0.6'
    RUBY
  end
  generator.copy_file("#{TEMPLATES_PATH}/rabbitmq.yml", "#{CONFIGS_PATH}/rabbitmq.yml")
  generator.copy_file("#{TEMPLATES_PATH}/rabbitmq.yml.ctmpl", "#{CONFIGS_PATH}/rabbitmq.yml.ctmpl")
  generator.copy_file("#{TEMPLATES_PATH}/sneakers.rb", "#{CONFIGS_PATH}/initializers/sneakers.rb")
  generator.safe_inject_into_file("#{CONFIGS_PATH}/application.rb", after: "require_rel '../api'\n") do
    'require_rel \'initializers\''
  end
  generator.copy_file("#{TEMPLATES_PATH}/sample_worker.rb", "#{WORKERS_PATH}/sample_worker.rb")

  generator.copy_file("#{TEMPLATES_PATH}/sneakers", "#{BIN_PATH}/sneakers")
  File.chmod(0744, 'bin/sneakers')
end

#execute(input: $stdin, output: $stdout) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/sumcli/commands/add/service.rb', line 22

def execute(input: $stdin, output: $stdout)
  if !service_exists?
    self.send("add_#{@name}")
    command.run("bundle install")
    output.puts "\n  #{@name} added to docker-compose"
  else
    output.puts "  #{@name} already added to docker-compose"
  end
end

#require_exists?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sumcli/commands/add/service.rb', line 74

def require_exists?
  File.readlines(APPLICATION_FILE).grep(/#{INITIALIZERS_REQUIRE}/).any?
end

#service_exists?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/sumcli/commands/add/service.rb', line 113

def service_exists?
  File.readlines(DOCKER_FILE).grep(/#{@name}/).any?
end