orats 0.9.7 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -2
  3. data/.rubocop.yml +24 -0
  4. data/.travis.yml +9 -0
  5. data/README.md +103 -194
  6. data/bin/orats +2 -5
  7. data/lib/orats/cli.rb +6 -67
  8. data/lib/orats/cli_help/destroy +11 -0
  9. data/lib/orats/cli_help/new +2 -26
  10. data/lib/orats/commands/new.rb +127 -0
  11. data/lib/orats/common.rb +0 -29
  12. data/lib/orats/templates/base/.dockerignore +5 -0
  13. data/lib/orats/templates/base/.gitignore +22 -0
  14. data/lib/orats/templates/base/.rubocop.yml +29 -0
  15. data/lib/orats/templates/base/Dockerfile +102 -0
  16. data/lib/orats/templates/base/Gemfile +69 -0
  17. data/lib/orats/templates/base/Gemfile.lock +205 -0
  18. data/lib/orats/templates/base/README.md +24 -0
  19. data/lib/orats/templates/base/Rakefile +7 -0
  20. data/lib/orats/templates/base/app/assets/config/manifest.js +3 -0
  21. data/lib/orats/templates/base/app/assets/images/.keep +0 -0
  22. data/lib/orats/templates/base/app/assets/javascripts/application.js +33 -0
  23. data/lib/orats/templates/base/app/assets/javascripts/cable.js +14 -0
  24. data/lib/orats/templates/base/app/assets/javascripts/channels/.keep +0 -0
  25. data/lib/orats/templates/base/app/assets/stylesheets/application.scss +89 -0
  26. data/lib/orats/templates/base/app/channels/application_cable/channel.rb +4 -0
  27. data/lib/orats/templates/base/app/channels/application_cable/connection.rb +4 -0
  28. data/lib/orats/templates/base/app/controllers/application_controller.rb +3 -0
  29. data/lib/orats/templates/base/app/controllers/concerns/.keep +0 -0
  30. data/lib/orats/templates/base/app/controllers/pages_controller.rb +4 -0
  31. data/lib/orats/templates/base/app/helpers/application_helper.rb +31 -0
  32. data/lib/orats/templates/base/app/helpers/pages_helper.rb +2 -0
  33. data/lib/orats/templates/base/app/jobs/application_job.rb +2 -0
  34. data/lib/orats/templates/base/app/mailers/application_mailer.rb +4 -0
  35. data/lib/orats/templates/base/app/models/application_record.rb +3 -0
  36. data/lib/orats/templates/base/app/models/concerns/.keep +0 -0
  37. data/lib/orats/templates/{includes/new/rails → base}/app/views/layouts/_flash.html.erb +1 -1
  38. data/lib/orats/templates/base/app/views/layouts/_footer.html.erb +3 -0
  39. data/lib/orats/templates/{includes/new/rails/app/views/layouts/_google_analytics_snippet.html.erb → base/app/views/layouts/_google_analytics.html.erb} +5 -9
  40. data/lib/orats/templates/{includes/new/rails → base}/app/views/layouts/_navigation.html.erb +5 -4
  41. data/lib/orats/templates/base/app/views/layouts/application.html.erb +48 -0
  42. data/lib/orats/templates/base/app/views/layouts/mailer.html.erb +13 -0
  43. data/lib/orats/templates/base/app/views/layouts/mailer.text.erb +1 -0
  44. data/lib/orats/templates/base/app/views/pages/home.html.erb +46 -0
  45. data/lib/orats/templates/base/bin/bundle +3 -0
  46. data/lib/orats/templates/base/bin/rails +4 -0
  47. data/lib/orats/templates/base/bin/rake +4 -0
  48. data/lib/orats/templates/base/bin/setup +34 -0
  49. data/lib/orats/templates/base/bin/update +29 -0
  50. data/lib/orats/templates/base/cable/config.ru +4 -0
  51. data/lib/orats/templates/base/config.ru +5 -0
  52. data/lib/orats/templates/base/config/application.rb +71 -0
  53. data/lib/orats/templates/base/config/boot.rb +3 -0
  54. data/lib/orats/templates/base/config/cable.yml +14 -0
  55. data/lib/orats/templates/base/config/database.yml +13 -0
  56. data/lib/orats/templates/base/config/environment.rb +5 -0
  57. data/lib/orats/templates/base/config/environments/development.rb +49 -0
  58. data/lib/orats/templates/base/config/environments/production.rb +56 -0
  59. data/lib/orats/templates/base/config/environments/staging.rb +6 -0
  60. data/lib/orats/templates/base/config/environments/test.rb +40 -0
  61. data/lib/orats/templates/base/config/initializers/application_controller_renderer.rb +6 -0
  62. data/lib/orats/templates/base/config/initializers/assets.rb +12 -0
  63. data/lib/orats/templates/base/config/initializers/backtrace_silencers.rb +9 -0
  64. data/lib/orats/templates/base/config/initializers/cookies_serializer.rb +5 -0
  65. data/lib/orats/templates/base/config/initializers/filter_parameter_logging.rb +4 -0
  66. data/lib/orats/templates/base/config/initializers/inflections.rb +16 -0
  67. data/lib/orats/templates/base/config/initializers/mime_types.rb +4 -0
  68. data/lib/orats/templates/base/config/initializers/new_framework_defaults.rb +27 -0
  69. data/lib/orats/templates/base/config/initializers/session_store.rb +4 -0
  70. data/lib/orats/templates/{includes/new/rails → base}/config/initializers/sidekiq.rb +1 -4
  71. data/lib/orats/templates/base/config/initializers/timeout.rb +1 -0
  72. data/lib/orats/templates/base/config/initializers/wrap_parameters.rb +15 -0
  73. data/lib/orats/templates/base/config/locales/en.yml +23 -0
  74. data/lib/orats/templates/base/config/puma.rb +60 -0
  75. data/lib/orats/templates/base/config/routes.rb +3 -0
  76. data/lib/orats/templates/{includes/new/rails → base}/config/secrets.yml +3 -2
  77. data/lib/orats/templates/base/config/sidekiq.yml.erb +5 -0
  78. data/lib/orats/templates/base/config/spring.rb +6 -0
  79. data/lib/orats/templates/base/db/seeds.rb +9 -0
  80. data/lib/orats/templates/base/docker-compose.yml +59 -0
  81. data/lib/orats/templates/base/lib/assets/.keep +0 -0
  82. data/lib/orats/templates/base/lib/tasks/.keep +0 -0
  83. data/lib/orats/templates/base/log/.keep +0 -0
  84. data/lib/orats/templates/base/public/404.html +67 -0
  85. data/lib/orats/templates/base/public/422.html +67 -0
  86. data/lib/orats/templates/base/public/500.html +66 -0
  87. data/lib/orats/templates/base/public/apple-touch-icon-precomposed.png +0 -0
  88. data/lib/orats/templates/base/public/apple-touch-icon.png +0 -0
  89. data/lib/orats/templates/base/public/favicon.ico +0 -0
  90. data/lib/orats/templates/base/public/robots.txt +5 -0
  91. data/lib/orats/templates/base/test/controllers/.keep +0 -0
  92. data/lib/orats/templates/base/test/controllers/pages_controller_test.rb +9 -0
  93. data/lib/orats/templates/base/test/fixtures/.keep +0 -0
  94. data/lib/orats/templates/base/test/fixtures/files/.keep +0 -0
  95. data/lib/orats/templates/base/test/helpers/.keep +0 -0
  96. data/lib/orats/templates/base/test/integration/.keep +0 -0
  97. data/lib/orats/templates/base/test/mailers/.keep +0 -0
  98. data/lib/orats/templates/base/test/models/.keep +0 -0
  99. data/lib/orats/templates/base/test/test_helper.rb +11 -0
  100. data/lib/orats/templates/base/tmp/.keep +0 -0
  101. data/lib/orats/templates/base/vendor/assets/javascripts/.keep +0 -0
  102. data/lib/orats/templates/base/vendor/assets/stylesheets/.keep +0 -0
  103. data/lib/orats/ui.rb +5 -5
  104. data/lib/orats/util.rb +14 -0
  105. data/lib/orats/version.rb +1 -1
  106. data/orats.gemspec +3 -2
  107. data/test/integration/cli_test.rb +43 -65
  108. data/test/test_helper.rb +9 -58
  109. metadata +114 -66
  110. data/lib/orats/argv_adjust.rb +0 -61
  111. data/lib/orats/cli_help/nuke +0 -19
  112. data/lib/orats/commands/new/exec.rb +0 -59
  113. data/lib/orats/commands/new/rails.rb +0 -215
  114. data/lib/orats/commands/new/server.rb +0 -67
  115. data/lib/orats/commands/nuke.rb +0 -108
  116. data/lib/orats/postgres.rb +0 -92
  117. data/lib/orats/process.rb +0 -35
  118. data/lib/orats/redis.rb +0 -25
  119. data/lib/orats/shell.rb +0 -12
  120. data/lib/orats/templates/auth.rb +0 -394
  121. data/lib/orats/templates/base.rb +0 -546
  122. data/lib/orats/templates/includes/common/.gitignore +0 -6
  123. data/lib/orats/templates/includes/common/LICENSE +0 -22
  124. data/lib/orats/templates/includes/new/rails/.env +0 -59
  125. data/lib/orats/templates/includes/new/rails/Gemfile +0 -48
  126. data/lib/orats/templates/includes/new/rails/Procfile +0 -3
  127. data/lib/orats/templates/includes/new/rails/README.md +0 -3
  128. data/lib/orats/templates/includes/new/rails/app/assets/favicon/favicon_base.png +0 -0
  129. data/lib/orats/templates/includes/new/rails/app/helpers/application_helper.rb +0 -53
  130. data/lib/orats/templates/includes/new/rails/app/models/account.rb +0 -40
  131. data/lib/orats/templates/includes/new/rails/app/views/devise/confirmations/new.html.erb +0 -26
  132. data/lib/orats/templates/includes/new/rails/app/views/devise/mailer/confirmation_instructions.html.erb +0 -5
  133. data/lib/orats/templates/includes/new/rails/app/views/devise/mailer/reset_password_instructions.html.erb +0 -10
  134. data/lib/orats/templates/includes/new/rails/app/views/devise/mailer/unlock_instructions.html.erb +0 -8
  135. data/lib/orats/templates/includes/new/rails/app/views/devise/passwords/edit.html.erb +0 -28
  136. data/lib/orats/templates/includes/new/rails/app/views/devise/passwords/new.html.erb +0 -26
  137. data/lib/orats/templates/includes/new/rails/app/views/devise/registrations/edit.html.erb +0 -51
  138. data/lib/orats/templates/includes/new/rails/app/views/devise/registrations/new.html.erb +0 -31
  139. data/lib/orats/templates/includes/new/rails/app/views/devise/sessions/new.html.erb +0 -39
  140. data/lib/orats/templates/includes/new/rails/app/views/devise/shared/_links.html.erb +0 -38
  141. data/lib/orats/templates/includes/new/rails/app/views/devise/unlocks/new.html.erb +0 -26
  142. data/lib/orats/templates/includes/new/rails/app/views/layouts/_disqus_comments_snippet.html.erb +0 -19
  143. data/lib/orats/templates/includes/new/rails/app/views/layouts/_disqus_count_snippet.html.erb +0 -12
  144. data/lib/orats/templates/includes/new/rails/app/views/layouts/_footer.html.erb +0 -2
  145. data/lib/orats/templates/includes/new/rails/app/views/layouts/_navigation_auth.html.erb +0 -15
  146. data/lib/orats/templates/includes/new/rails/app/views/layouts/_navigation_links.html.erb +0 -3
  147. data/lib/orats/templates/includes/new/rails/app/views/layouts/application.html.erb +0 -47
  148. data/lib/orats/templates/includes/new/rails/app/views/pages/home.html.erb +0 -58
  149. data/lib/orats/templates/includes/new/rails/config/database.yml +0 -6
  150. data/lib/orats/templates/includes/new/rails/config/environments/staging.rb +0 -5
  151. data/lib/orats/templates/includes/new/rails/config/initializers/devise_async.rb +0 -1
  152. data/lib/orats/templates/includes/new/rails/config/initializers/mini_profiler.rb +0 -4
  153. data/lib/orats/templates/includes/new/rails/config/puma.rb +0 -39
  154. data/lib/orats/templates/includes/new/rails/config/schedule.rb +0 -3
  155. data/lib/orats/templates/includes/new/rails/config/sidekiq.yml +0 -7
  156. data/lib/orats/templates/includes/new/rails/config/sitemap.rb +0 -20
  157. data/lib/orats/templates/includes/new/rails/config/unicorn.rb +0 -84
  158. data/lib/orats/templates/includes/new/rails/lib/tasks/orats/favicon.rake +0 -48
  159. data/lib/orats/templates/includes/new/rails/public/404.html +0 -13
  160. data/lib/orats/templates/includes/new/rails/public/422.html +0 -13
  161. data/lib/orats/templates/includes/new/rails/public/500.html +0 -13
  162. data/lib/orats/templates/includes/new/rails/public/502.html +0 -13
  163. data/lib/orats/templates/includes/new/rails/public/deploy.html +0 -14
  164. data/lib/orats/templates/includes/new/rails/test/fixtures/accounts.yml +0 -27
  165. data/lib/orats/templates/includes/new/rails/test/models/account_test.rb +0 -46
@@ -1,215 +0,0 @@
1
- module Orats
2
- module Commands
3
- module New
4
- # helper functions for the new::exec class
5
- module Rails
6
- def check_exit_conditions
7
- exit_if_path_exists
8
- exit_if_invalid_template
9
- exit_if_invalid_system
10
- exit_if_database_exists
11
- end
12
-
13
- def rails_template(command, flags = '')
14
- orats_template = "--template #{base_path}/templates/#{command}.rb"
15
-
16
- run "rails new #{@target_path} #{flags} --skip-bundle " + \
17
- " #{orats_template unless command.empty?}"
18
-
19
- yield if block_given?
20
- end
21
-
22
- def orats_rails_template
23
- rails_template @options[:template], '--skip ' do
24
- migrate_and_seed_database
25
- end
26
- end
27
-
28
- def custom_rails_template
29
- task 'Add custom template'
30
-
31
- rails_template '', "--skip --template #{@options[:custom]}"
32
- run_from @target_path,
33
- 'rm -rf app/assets/stylesheets/application.css'
34
-
35
- commit 'Add custom template'
36
- end
37
-
38
- def rails_template_actions
39
- gsub_postgres_info
40
- gsub_redis_info
41
- gsub_readme
42
- gsub_puma if @options[:backend] == 'puma'
43
-
44
- bundle_install
45
- bundle_binstubs
46
- spring_binstub
47
-
48
- create_and_migrate_database
49
- generate_home_page
50
- generate_favicons
51
- end
52
-
53
- def gsub_postgres_info
54
- task 'Update the DATABASE_URL'
55
-
56
- unless @options[:pg_password].empty?
57
- gsub_file "#{@target_path}/.env", 'db_user',
58
- "db_user:#{@options[:pg_password]}"
59
- end
60
-
61
- gsub_file "#{@target_path}/.env", 'db_location',
62
- @options[:pg_location]
63
- gsub_file "#{@target_path}/.env", 'db_port', @options[:pg_port]
64
- gsub_file "#{@target_path}/.env", 'db_user', @options[:pg_username]
65
-
66
- commit 'Update the DATABASE_URL'
67
- end
68
-
69
- def gsub_redis_info
70
- task 'Update the CACHE_URL'
71
-
72
- unless @options[:redis_password].empty?
73
- gsub_file "#{@target_path}/.env", '//',
74
- "//#{@options[:redis_password]}@"
75
- end
76
-
77
- gsub_file "#{@target_path}/.env", 'cache_location',
78
- @options[:redis_location]
79
- gsub_file "#{@target_path}/.env", 'cache_port', @options[:redis_port]
80
-
81
- commit 'Update the CACHE_URL'
82
- end
83
-
84
- def gsub_app_path
85
- task 'Update the app path'
86
-
87
- gsub_file "#{@target_path}/.env", ": '/tmp/yourapp'",
88
- ": '#{File.expand_path(@target_path)}'"
89
-
90
- commit 'Update the app path'
91
- end
92
-
93
- def gsub_readme
94
- task 'Update the readme'
95
-
96
- gsub_file "#{@target_path}/README.md", 'VERSION', VERSION
97
-
98
- commit 'Update the readme'
99
- end
100
-
101
- def gsub_puma
102
- task 'Update files for switching to puma'
103
-
104
- gsub_file "#{@target_path}/Procfile", 'unicorn -c config/unicorn.rb',
105
- 'puma -C config/puma.rb'
106
- gsub_file "#{@target_path}/Gemfile",
107
- "gem 'unicorn'", "#gem 'unicorn'"
108
- gsub_file "#{@target_path}/Gemfile", "#gem 'puma'", "gem 'puma'"
109
-
110
- commit 'Switch from unicorn to puma'
111
- end
112
-
113
- def bundle_install
114
- task 'Run bundle install, this may take a while'
115
- run_from @target_path, 'bundle install'
116
-
117
- commit 'Add Gemfile.lock'
118
- end
119
-
120
- def bundle_binstubs
121
- task 'Run bundle binstubs for a few gems'
122
- run_from @target_path,
123
- "bundle binstubs whenever #{@options[:backend]} sidekiq"
124
-
125
- commit 'Add binstubs for the important gems'
126
- end
127
-
128
- def spring_binstub
129
- task 'Run spring binstub'
130
- run_from @target_path, 'bundle exec spring binstub --all'
131
-
132
- commit 'Add spring binstubs for all of the bins'
133
- end
134
-
135
- def generate_home_page
136
- kill_spring_servers
137
-
138
- task 'Add pages controller with static page'
139
- run_from @target_path, 'bundle exec rails g controller Pages home'
140
-
141
- gsub_home_page
142
- copy_home_page_views
143
-
144
- commit 'Add pages controller with home page'
145
- end
146
-
147
- def generate_favicons
148
- run_rake 'orats:favicons'
149
-
150
- commit 'Add favicons'
151
- end
152
-
153
- def create_and_migrate_database
154
- task 'Create and migrate the database'
155
-
156
- create_database
157
- run_rake 'db:migrate'
158
-
159
- commit 'Add the database schema file'
160
- end
161
-
162
- def migrate_and_seed_database
163
- run_rake 'db:migrate db:seed'
164
-
165
- commit 'Update the database schema file'
166
- end
167
-
168
- def template_exist?(template)
169
- Exec::AVAILABLE_TEMPLATES.include?(template.to_sym)
170
- end
171
-
172
- private
173
-
174
- def gsub_home_page
175
- gsub_file "#{@target_path}/config/routes.rb",
176
- " # root 'welcome#index'", " root 'pages#home'"
177
- gsub_file "#{@target_path}/config/routes.rb",
178
- " get 'pages/home'\n\n", ''
179
-
180
- gsub_file "#{@target_path}/test/controllers/pages_controller_" + \
181
- 'test.rb', '"should get home"', "'expect home page'"
182
- end
183
-
184
- def copy_home_page_views
185
- run_from @target_path, 'rm app/views/pages/home.html.erb'
186
-
187
- orats_to_local 'new/rails/app/views/pages/home.html.erb',
188
- "#{@target_path}/app/views/pages/home.html.erb"
189
-
190
- gsub_file "#{@target_path}/app/views/pages/home.html.erb",
191
- 'vVERSION', VERSION
192
- end
193
-
194
- def exit_if_invalid_template
195
- template = @options[:template] || ''
196
- task 'Check if template exists'
197
-
198
- return if template.empty? || template_exist?(template)
199
-
200
- error 'Cannot find template',
201
- "'#{template}' is not a valid template name"
202
-
203
- available_templates
204
- exit 1
205
- end
206
-
207
- def kill_spring_servers
208
- # rails generators will lock up if a spring server is running,
209
- # so kill them all before continuing
210
- system 'pkill -f spring'
211
- end
212
- end
213
- end
214
- end
215
- end
@@ -1,67 +0,0 @@
1
- require 'socket'
2
- require 'timeout'
3
-
4
- module Orats
5
- module Commands
6
- module New
7
- # handle starting the server
8
- module Server
9
- START_COMMAND = 'bundle exec foreman start'
10
-
11
- def server_start
12
- if @options[:skip_server_start]
13
- message = 'Start your'
14
- else
15
- message = 'Starting'
16
- end
17
-
18
- display_notice message
19
- attempt_to_start unless @options[:skip_server_start]
20
- end
21
-
22
- private
23
-
24
- def display_notice(message)
25
- results "#{message} server with the following commands",
26
- 'command', "cd #{@target_path}"
27
- log 'command', START_COMMAND, :white
28
- end
29
-
30
- def attempt_to_start
31
- while port_taken?
32
- error 'Failed to start server',
33
- "Another application is using port 3000\n"
34
- puts
35
- exit 1 if no?('Would you like to try running ' + \
36
- ' the server again? (y/N)', :cyan)
37
- end
38
-
39
- puts
40
- run_from @target_path, START_COMMAND
41
- end
42
-
43
- def port_taken?
44
- begin
45
- start_server?
46
- rescue Timeout::Error
47
- false
48
- end
49
-
50
- false
51
- end
52
-
53
- def start_server?
54
- Timeout.timeout(5) do
55
- s = TCPSocket.new('localhost', 3000)
56
- s.close
57
-
58
- true
59
- end
60
-
61
- rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
62
- false
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,108 +0,0 @@
1
- require 'orats/common'
2
-
3
- module Orats
4
- module Commands
5
- # delete a path and its data
6
- class Nuke < Common
7
- def initialize(target_path = '', options = {})
8
- super
9
- end
10
-
11
- def init
12
- exit_if_invalid_system
13
-
14
- nuke_report
15
- nuke_details
16
- nuke_data unless @options[:skip_data]
17
- nuke_path
18
- end
19
-
20
- private
21
-
22
- def exit_if_path_missing
23
- task 'Check if path is missing'
24
-
25
- return if Dir.exist?(@target_path) || File.exist?(@target_path)
26
-
27
- error 'Path was not found', @target_path
28
- exit 1
29
- end
30
-
31
- def nuke_report
32
- puts
33
- log 'warning', 'You are about to permanently delete this path',
34
- :yellow, true
35
- log 'nuke path', File.expand_path(@target_path), :white
36
- puts
37
- end
38
-
39
- def nuke_details
40
- rails_apps = []
41
-
42
- valid_rails_apps.each do
43
- |rails_dir| rails_apps << File.basename(rails_dir)
44
- end
45
-
46
- nuke_items(rails_apps) unless @options[:skip_data]
47
- end
48
-
49
- def valid_rails_apps
50
- rails_gemfiles =
51
- run("find #{@target_path} -type f -name Gemfile | " + \
52
- "xargs grep -lE \"gem 'rails'|gem \\\"rails\\\"\"",
53
- capture: true)
54
-
55
- gemfile_paths = rails_gemfiles.split("\n")
56
-
57
- gemfile_paths.map { |gemfile| File.dirname(gemfile) }
58
- end
59
-
60
- def nuke_items(apps)
61
- if apps.empty?
62
- results 'No apps were found in this path',
63
- 'skipping', File.expand_path(@target_path)
64
- nuke_path
65
- exit
66
- else
67
- nuke_app_details(apps.join(', '))
68
- end
69
- end
70
-
71
- def nuke_app_details(app_names)
72
- puts
73
- log 'nuke', 'You are about to permanently delete all postgres' + \
74
- ' databases for', :red, true
75
- log 'databases', app_names, :yellow
76
- puts
77
- log 'nuke', 'You are about to permanently delete all redis' + \
78
- ' namespaces for', :red, true
79
- log 'namespaces', app_names, :yellow
80
- puts
81
-
82
- exit unless yes?('Are you sure? (y/N)', :cyan)
83
- end
84
-
85
- def nuke_data
86
- valid_rails_apps.each do |app|
87
- task 'Delete postgres database'
88
- drop_database app
89
-
90
- task 'Delete redis keys'
91
- drop_namespace File.basename(app)
92
- end
93
- end
94
-
95
- def nuke_path
96
- exit_if_path_missing
97
-
98
- task 'Delete path'
99
-
100
- return if @options[:skip_data]
101
-
102
- puts
103
- exit unless yes?('Are you sure? (y/N)', :cyan)
104
- run "rm -rf #{@target_path}"
105
- end
106
- end
107
- end
108
- end
@@ -1,92 +0,0 @@
1
- module Orats
2
- # manage the postgres process
3
- module Postgres
4
- def postgres_bin(bin_name = 'psql')
5
- exec = "#{bin_name} -h #{@options[:pg_location]} -U " + \
6
- "#{@options[:pg_username]}"
7
-
8
- return exec if @options[:pg_password].empty?
9
- exec.prepend("PGPASSWORD=#{@options[:pg_password]} ")
10
- end
11
-
12
- def create_database
13
- if local_postgres?
14
- run_rake 'db:create:all'
15
- else
16
- manually_create_postgres_db
17
- end
18
- end
19
-
20
- def drop_database(name)
21
- if local_postgres?
22
- run_rake 'db:drop:all'
23
- else
24
- manually_delete_postgres_db File.basename(name)
25
- end
26
- end
27
-
28
- def exit_if_postgres_unreachable
29
- task 'Check if you can connect to postgres'
30
-
31
- return if run("#{postgres_bin} -c 'select 1'")
32
-
33
- error 'Cannot connect to postgres', 'attempt to SELECT 1'
34
- exit 1
35
- end
36
-
37
- def exit_if_database_exists
38
- task 'Check if the postgres database exists'
39
-
40
- # detect if the database already exists
41
- database = "#{File.basename(@target_path)}_development"
42
- return if run("#{postgres_bin} -d #{database} -l | " + \
43
- "grep #{database} | wc -l", capture: true).chomp == '0'
44
-
45
- error "'#{database}' database already exists",
46
- 'attempt to check database existence'
47
- puts
48
-
49
- exit 1 unless yes?('Would you like to continue anyways? (y/N)',
50
- :cyan)
51
- end
52
-
53
- private
54
-
55
- def local_postgres?
56
- @options[:pg_location] == 'localhost' ||
57
- @options[:pg_location] == '127.0.0.1'
58
- end
59
-
60
- def manually_create_postgres_db
61
- database = File.basename(@target_path)
62
- dev_database = "#{database}_development"
63
- test_database = "#{database}_test"
64
-
65
- createdb(dev_database)
66
- createdb(test_database)
67
- end
68
-
69
- def createdb(database)
70
- return if run("#{postgres_bin('createdb')} #{database}")
71
-
72
- log 'Skipped creating postgres database',
73
- "#{database} already exists", :yellow
74
- end
75
-
76
- def manually_delete_postgres_db(name)
77
- database = File.basename(name)
78
- dev_database = "#{database}_development"
79
- test_database = "#{database}_test"
80
-
81
- dropdb(dev_database)
82
- dropdb(test_database)
83
- end
84
-
85
- def dropdb(database)
86
- return if run("#{postgres_bin('dropdb')} #{database}")
87
-
88
- log 'Skipped dropping postgres database',
89
- "#{database} does not exists", :yellow
90
- end
91
- end
92
- end