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 = 
"  db:\n    image: 'postgres:\#{ver}'\n    ports:\n      - '5432:5432'\n"

  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 = 
"  rabbitmq:\n    image: 'rabbitmq:\#{ver}'\n    ports:\n      - '15672:15672'\n      - '5672:5672'\n"

  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 "    gem 'pg', '~> 1.1'\n    gem \"otr-activerecord\", '~> 1.3.0'\n    RUBY\n  end\n  generator.copy_file(\"\#{TEMPLATES_PATH}/database.yml\", \"\#{CONFIGS_PATH}/database.yml\")\n  generator.copy_file(\"\#{TEMPLATES_PATH}/database.yml.ctmpl\", \"\#{CONFIGS_PATH}/database.yml.ctmpl\")\n  generator.copy_file(\"\#{TEMPLATES_PATH}/database.rb\", \"\#{CONFIGS_PATH}/initializers/database.rb\")\n  generator.safe_inject_into_file(\"\#{CONFIGS_PATH}/application.rb\", after: \"require_rel '../api'\\n\") do\n    'require_rel \\'initializers\\''\n  end\n  generator.safe_inject_into_file('Rakefile', after: \"# TASKS\\n\") do <<~RUBY.strip\n    load \"tasks/otr-activerecord.rake\"\n\n    namespace :db do\n      task :environment do\n        require_relative 'config/environment'\n      end\n    end\n    RUBY\n  end\nend\n"

#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 "    gem 'sneakers', '~> 2.11'\n    gem 'sneakers_handlers', '~> 0.0.6'\n    RUBY\n  end\n  generator.copy_file(\"\#{TEMPLATES_PATH}/rabbitmq.yml\", \"\#{CONFIGS_PATH}/rabbitmq.yml\")\n  generator.copy_file(\"\#{TEMPLATES_PATH}/rabbitmq.yml.ctmpl\", \"\#{CONFIGS_PATH}/rabbitmq.yml.ctmpl\")\n  generator.copy_file(\"\#{TEMPLATES_PATH}/sneakers.rb\", \"\#{CONFIGS_PATH}/initializers/sneakers.rb\")\n  generator.safe_inject_into_file(\"\#{CONFIGS_PATH}/application.rb\", after: \"require_rel '../api'\\n\") do\n    'require_rel \\'initializers\\''\n  end\n  generator.copy_file(\"\#{TEMPLATES_PATH}/sample_worker.rb\", \"\#{WORKERS_PATH}/sample_worker.rb\")\n\n  generator.copy_file(\"\#{TEMPLATES_PATH}/sneakers\", \"\#{BIN_PATH}/sneakers\")\n  File.chmod(0744, 'bin/sneakers')\nend\n"

#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