Class: AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Includes:
Kratos::Actions
Defined in:
lib/kratos/app_builder.rb

Instance Method Summary collapse

Methods included from Kratos::Actions

#action_mailer_host, #configure_environment, #replace_in_file

Instance Method Details

#add_bullet_gem_configurationObject



39
40
41
# File 'lib/kratos/app_builder.rb', line 39

def add_bullet_gem_configuration
  copy_file 'bullet.rb', 'config/initializers/bullet.rb'
end

#configure_acceptance_helpersObject



146
147
148
# File 'lib/kratos/app_builder.rb', line 146

def configure_acceptance_helpers
  copy_file 'acceptance_helpers_rspec.rb', 'spec/support/acceptance_helpers.rb'
end

#configure_action_mailerObject



263
264
265
266
267
# File 'lib/kratos/app_builder.rb', line 263

def configure_action_mailer
  action_mailer_host 'development', %("localhost:3000")
  action_mailer_host 'test', %("www.example.com")
  action_mailer_host 'production', %{ENV.fetch("APPLICATION_HOST")}
end

#configure_action_mailer_in_specsObject



134
135
136
# File 'lib/kratos/app_builder.rb', line 134

def configure_action_mailer_in_specs
  copy_file 'action_mailer.rb', 'spec/support/action_mailer.rb'
end

#configure_app_servicesObject



309
310
311
312
313
314
315
316
317
318
# File 'lib/kratos/app_builder.rb', line 309

def configure_app_services
  empty_directory 'app/services'

  config = <<-RUBY

config.autoload_paths += ['#\{config.root}/app/services']
  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#configure_capybara_poltergeistObject



138
139
140
# File 'lib/kratos/app_builder.rb', line 138

def configure_capybara_poltergeist
  copy_file 'capybara_poltergeist.rb', 'spec/support/capybara_poltergeist.rb'
end

#configure_ciObject



126
127
128
# File 'lib/kratos/app_builder.rb', line 126

def configure_ci
  template 'circle.yml.erb', 'circle.yml'
end

#configure_generatorsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kratos/app_builder.rb', line 60

def configure_generators
  config = <<-RUBY

  config.generators do |generate|
    generate.helper false
    generate.javascript_engine false
    generate.request_specs false
    generate.routing_specs false
    generate.stylesheets false
    generate.test_framework :rspec
    generate.view_specs false
  end

  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#configure_i18n_for_missing_translationsObject



78
79
80
81
# File 'lib/kratos/app_builder.rb', line 78

def configure_i18n_for_missing_translations
  raise_on_missing_translations_in('development')
  raise_on_missing_translations_in('test')
end

#configure_i18n_for_test_environmentObject



130
131
132
# File 'lib/kratos/app_builder.rb', line 130

def configure_i18n_for_test_environment
  copy_file 'i18n.rb', 'spec/support/i18n.rb'
end

#configure_i18n_messagesObject



300
301
302
303
# File 'lib/kratos/app_builder.rb', line 300

def configure_i18n_messages
  template 'config_locales_en_messages.yml.erb', 'config/locales/en.messages.yml'
  template 'config_locales_pt-BR_messages.yml.erb', 'config/locales/pt-BR.messages.yml'
end

#configure_i18n_tasksObject



150
151
152
153
# File 'lib/kratos/app_builder.rb', line 150

def configure_i18n_tasks
  copy_file 'i18n_rspec.rb', 'spec/i18n_spec.rb'
  copy_file 'i18n-tasks.yml', 'config/i18n-tasks.yml'
end

#configure_logrageObject



213
214
215
# File 'lib/kratos/app_builder.rb', line 213

def configure_lograge
  copy_file 'lograge.rb', 'config/initializers/lograge.rb'
end

#configure_newrelicObject



155
156
157
# File 'lib/kratos/app_builder.rb', line 155

def configure_newrelic
  template 'newrelic.yml.erb', 'config/newrelic.yml'
end

#configure_nginxObject



163
164
165
# File 'lib/kratos/app_builder.rb', line 163

def configure_nginx
  copy_file 'nginx.conf.erb', 'config/deploy/nginx.conf.erb'
end

#configure_pumaObject



370
371
372
# File 'lib/kratos/app_builder.rb', line 370

def configure_puma
  copy_file 'puma.rb', 'config/puma.rb'
end

#configure_quiet_assetsObject



83
84
85
86
87
88
89
# File 'lib/kratos/app_builder.rb', line 83

def configure_quiet_assets
  config = <<-RUBY
  config.quiet_assets = true
  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#configure_redisObject



274
275
276
# File 'lib/kratos/app_builder.rb', line 274

def configure_redis
  copy_file 'redis.rb', 'config/initializers/redis.rb'
end

#configure_rollbarObject



159
160
161
# File 'lib/kratos/app_builder.rb', line 159

def configure_rollbar
  template 'rollbar.rb.erb', 'config/initializers/rollbar.rb'
end

#configure_routesObject



269
270
271
272
# File 'lib/kratos/app_builder.rb', line 269

def configure_routes
  remove_file 'config/routes.rb'
  copy_file 'routes.rb', 'config/routes.rb'
end

#configure_rspecObject



104
105
106
107
108
109
# File 'lib/kratos/app_builder.rb', line 104

def configure_rspec
  remove_file 'spec/rails_helper.rb'
  remove_file 'spec/spec_helper.rb'
  copy_file 'rails_helper.rb', 'spec/rails_helper.rb'
  copy_file 'spec_helper.rb', 'spec/spec_helper.rb'
end

#configure_rspec_api_documentationObject



142
143
144
# File 'lib/kratos/app_builder.rb', line 142

def configure_rspec_api_documentation
  template 'rspec_api_documentation_rspec.rb.erb', 'spec/support/rspec_api_documentation.rb'
end

#configure_rubocopObject



320
321
322
323
324
# File 'lib/kratos/app_builder.rb', line 320

def configure_rubocop
  copy_file 'rubocop.yml', '.rubocop.yml'
  copy_file 'rubocop_database.yml', 'db/migrate/.rubocop.yml'
  copy_file 'rubocop_rspec.yml', 'spec/.rubocop.yml'
end

#configure_sidekiqObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/kratos/app_builder.rb', line 278

def configure_sidekiq
  copy_file 'sidekiq.yml', 'config/sidekiq.yml'
  copy_file 'sidekiq_rspec.yml', 'spec/support/sidekiq.rb'
  copy_file 'sidekiq_security.rb', 'config/initializers/sidekiq.rb'
  empty_directory 'tmp/pids'

  route = <<-HERE
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
  HERE

  inject_into_file('config/routes.rb',
                   route,
                   after: "Rails.application.routes.draw do\n")
end

#configure_smtpObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/kratos/app_builder.rb', line 167

def configure_smtp
  copy_file 'smtp.rb', 'config/smtp.rb'

  prepend_file 'config/environments/production.rb',
               %{require Rails.root.join("config/smtp")\n}

  config = <<-RUBY

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = SMTP_SETTINGS
  RUBY

  inject_into_file 'config/environments/production.rb', config,
                   after: 'config.action_mailer.raise_delivery_errors = false'
end

#configure_spec_support_featuresObject



122
123
124
# File 'lib/kratos/app_builder.rb', line 122

def configure_spec_support_features
  empty_directory_with_keep_file 'spec/features'
end

#configure_time_formatsObject



294
295
296
297
298
# File 'lib/kratos/app_builder.rb', line 294

def configure_time_formats
  remove_file 'config/locales/en.yml'
  copy_file 'config_locales_en_datetime.yml', 'config/locales/en.datetime.yml'
  copy_file 'config_locales_pt-BR_datetime.yml', 'config/locales/pt-BR.datetime.yml'
end

#configure_wheneverObject



305
306
307
# File 'lib/kratos/app_builder.rb', line 305

def configure_whenever
  copy_file 'schedule.rb', 'config/schedule.rb'
end

#copy_dotfilesObject



258
259
260
261
# File 'lib/kratos/app_builder.rb', line 258

def copy_dotfiles
  remove_file '.gitignore'
  directory('dotfiles', '.')
end

#copy_miscellaneous_filesObject



341
342
343
344
# File 'lib/kratos/app_builder.rb', line 341

def copy_miscellaneous_files
  copy_file 'errors.rb', 'config/initializers/errors.rb'
  copy_file 'json_encoding.rb', 'config/initializers/json_encoding.rb'
end

#create_application_layoutObject



252
253
254
255
256
# File 'lib/kratos/app_builder.rb', line 252

def create_application_layout
  template 'kratos_layout.html.erb.erb',
           'app/views/layouts/application.html.erb',
           force: true
end

#create_capfileObject



442
443
444
# File 'lib/kratos/app_builder.rb', line 442

def create_capfile
  copy_file 'Capfile', 'Capfile'
end

#create_capistrano_configObject



446
447
448
449
450
# File 'lib/kratos/app_builder.rb', line 446

def create_capistrano_config
  template 'deploy_config.rb.erb', 'config/deploy.rb'
  copy_file 'cap_environment.rb', 'config/deploy/production.rb'
  copy_file 'cap_environment.rb', 'config/deploy/staging.rb'
end

#create_capistrano_tasksObject



437
438
439
440
# File 'lib/kratos/app_builder.rb', line 437

def create_capistrano_tasks
  copy_file 'deploy.rake', 'lib/capistrano/tasks/deploy.rake'
  copy_file 'foreman.rake', 'lib/capistrano/tasks/foreman.rake'
end

#create_databaseObject



433
434
435
# File 'lib/kratos/app_builder.rb', line 433

def create_database
  bundle_command 'exec rake db:create db:migrate'
end

#create_factories_directoryObject



96
97
98
# File 'lib/kratos/app_builder.rb', line 96

def create_factories_directory
  empty_directory 'spec/factories'
end

#create_partials_directoryObject



243
244
245
# File 'lib/kratos/app_builder.rb', line 243

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_shared_javascriptsObject



247
248
249
250
# File 'lib/kratos/app_builder.rb', line 247

def create_shared_javascripts
  copy_file '_javascript.html.erb',
            'app/views/application/_javascript.html.erb'
end

#customize_error_pagesObject



378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/kratos/app_builder.rb', line 378

def customize_error_pages
  meta_tags = <<-EOS
<meta charset="utf-8" />
<meta name="ROBOTS" content="NOODP" />
<meta name="viewport" content="initial-scale=1" />
  EOS

  %w(500 404 422).each do |page|
    inject_into_file "public/#{page}.html", meta_tags, after: "<head>\n"
    replace_in_file "public/#{page}.html", /<!--.+-->\n/, ''
  end
end

#disable_xml_paramsObject



337
338
339
# File 'lib/kratos/app_builder.rb', line 337

def disable_xml_params
  copy_file 'disable_xml_params.rb', 'config/initializers/disable_xml_params.rb'
end

#enable_database_cleanerObject



111
112
113
# File 'lib/kratos/app_builder.rb', line 111

def enable_database_cleaner
  copy_file 'database_cleaner_rspec.rb', 'spec/support/database_cleaner.rb'
end

#enable_rack_deflaterObject



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/kratos/app_builder.rb', line 183

def enable_rack_deflater
  config = <<-RUBY

# Enable deflate / gzip compression of controller-generated responses
config.middleware.use Rack::Deflater
  RUBY

  inject_into_file(
    'config/environments/production.rb',
    config,
    after: serve_static_files_line
  )
end

#fix_i18n_deprecation_warningObject



346
347
348
349
350
351
352
# File 'lib/kratos/app_builder.rb', line 346

def fix_i18n_deprecation_warning
  config = <<-RUBY
  config.i18n.enforce_available_locales = false
  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#generate_rspecObject



100
101
102
# File 'lib/kratos/app_builder.rb', line 100

def generate_rspec
  generate 'rspec:install'
end

#init_gitObject



419
420
421
# File 'lib/kratos/app_builder.rb', line 419

def init_git
  run 'git init'
end

#provide_dev_prime_taskObject



56
57
58
# File 'lib/kratos/app_builder.rb', line 56

def provide_dev_prime_task
  copy_file 'dev.rake', 'lib/tasks/dev.rake'
end

#provide_setup_scriptObject



51
52
53
54
# File 'lib/kratos/app_builder.rb', line 51

def provide_setup_script
  template 'bin_setup', 'bin/setup', force: true
  run 'chmod a+x bin/setup'
end

#provide_shoulda_matchers_configObject



115
116
117
118
119
120
# File 'lib/kratos/app_builder.rb', line 115

def provide_shoulda_matchers_config
  copy_file(
    'shoulda_matchers_config_rspec.rb',
    'spec/support/shoulda_matchers.rb'
  )
end

#raise_on_delivery_errorsObject



25
26
27
28
29
# File 'lib/kratos/app_builder.rb', line 25

def raise_on_delivery_errors
  replace_in_file 'config/environments/development.rb',
                  'raise_delivery_errors = false',
                  'raise_delivery_errors = true'
end

#raise_on_missing_assets_in_testObject



17
18
19
20
21
22
23
# File 'lib/kratos/app_builder.rb', line 17

def raise_on_missing_assets_in_test
  inject_into_file(
    'config/environments/test.rb',
    "\n  config.assets.raise_runtime_errors = true",
    after: 'Rails.application.configure do'
  )
end

#raise_on_unpermitted_parametersObject



43
44
45
46
47
48
49
# File 'lib/kratos/app_builder.rb', line 43

def raise_on_unpermitted_parameters
  config = <<-RUBY
  config.action_controller.action_on_unpermitted_parameters = :raise
  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#readmeObject



13
14
15
# File 'lib/kratos/app_builder.rb', line 13

def readme
  template 'README.md.erb', 'README.md'
end

#remove_config_comment_linesObject



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/kratos/app_builder.rb', line 391

def remove_config_comment_lines
  config_files = [
    'application.rb',
    'environment.rb',
    'environments/development.rb',
    'environments/production.rb',
    'environments/test.rb'
  ]

  config_files.each do |config_file|
    path = File.join(destination_root, "config/#{config_file}")

    accepted_content = File.readlines(path).reject do |line|
      line =~ /^.*#.*$/ || line =~ /^$\n/
    end

    File.open(path, 'w') do |file|
      accepted_content.each { |line| file.puts line }
    end
  end
end

#replace_gemfileObject



4
5
6
7
# File 'lib/kratos/app_builder.rb', line 4

def replace_gemfile
  remove_file 'Gemfile'
  template 'Gemfile.erb', 'Gemfile'
end

#set_ruby_to_version_being_usedObject



9
10
11
# File 'lib/kratos/app_builder.rb', line 9

def set_ruby_to_version_being_used
  create_file '.ruby-version', "#{Kratos::RUBY_VERSION}\n"
end

#set_test_delivery_methodObject



31
32
33
34
35
36
37
# File 'lib/kratos/app_builder.rb', line 31

def set_test_delivery_method
  inject_into_file(
    'config/environments/development.rb',
    "\n  config.action_mailer.delivery_method = :test",
    after: 'config.action_mailer.raise_delivery_errors = true'
  )
end

#set_up_factory_girl_for_rspecObject



92
93
94
# File 'lib/kratos/app_builder.rb', line 92

def set_up_factory_girl_for_rspec
  copy_file 'factory_girl_rspec.rb', 'spec/support/factory_girl.rb'
end

#set_up_foregoObject



374
375
376
# File 'lib/kratos/app_builder.rb', line 374

def set_up_forego
  copy_file 'Procfile', 'Procfile'
end

#setup_asset_hostObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/kratos/app_builder.rb', line 197

def setup_asset_host
  replace_in_file 'config/environments/production.rb',
                  "# config.action_controller.asset_host = 'http://assets.example.com'",
                  'config.action_controller.asset_host = ENV.fetch("ASSET_HOST", ENV.fetch("APPLICATION_HOST"))'

  replace_in_file 'config/initializers/assets.rb',
                  "config.assets.version = '1.0'",
                  'config.assets.version = (ENV["ASSETS_VERSION"] || "1.0")'

  inject_into_file(
    'config/environments/production.rb',
    '  config.static_cache_control = "public, max-age=#{1.year.to_i}"',
    after: serve_static_files_line
  )
end

#setup_brakemanObject



462
463
464
465
# File 'lib/kratos/app_builder.rb', line 462

def setup_brakeman
  copy_file 'brakeman.rake', 'lib/tasks/brakeman.rake'
  append_file 'Rakefile', %(\ntask default: "brakeman:run"\n)
end

#setup_brazilian_appObject



326
327
328
329
330
331
332
333
334
335
# File 'lib/kratos/app_builder.rb', line 326

def setup_brazilian_app
  config = <<-RUBY
  config.time_zone = 'Brasilia'
  config.i18n.default_locale = :'pt-BR'
  RUBY

  inject_into_class 'config/application.rb', 'Application', config

  copy_file 'timezones.rb', 'config/initializers/timezones.rb'
end

#setup_bundler_auditObject



452
453
454
455
# File 'lib/kratos/app_builder.rb', line 452

def setup_bundler_audit
  copy_file 'bundler_audit.rake', 'lib/tasks/bundler_audit.rake'
  append_file 'Rakefile', %(\ntask default: "bundler:audit"\n)
end

#setup_dalli_cacheObject



217
218
219
220
221
222
223
# File 'lib/kratos/app_builder.rb', line 217

def setup_dalli_cache
  inject_into_file(
    'config/environments/production.rb',
    '  config.cache_store = :dalli_store',
    after: "config.cache_classes = true\n"
  )
end

#setup_default_rake_taskObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/kratos/app_builder.rb', line 354

def setup_default_rake_task
  append_file 'Rakefile' do
    <<-EOS
task(:default).clear
task default: [:spec]

if defined? RSpec
task(:spec).clear
RSpec::Core::RakeTask.new(:spec) do |t|
  t.verbose = false
end
end
    EOS
  end
end

#setup_rubocopObject



457
458
459
460
# File 'lib/kratos/app_builder.rb', line 457

def setup_rubocop
  copy_file 'rubocop.rake', 'lib/tasks/rubocop.rake'
  append_file 'Rakefile', %(\ntask default: "rubocop"\n)
end

#setup_secret_tokenObject



239
240
241
# File 'lib/kratos/app_builder.rb', line 239

def setup_secret_token
  template 'secrets.yml', 'config/secrets.yml', force: true
end

#setup_staging_environmentObject



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/kratos/app_builder.rb', line 225

def setup_staging_environment
  staging_file = 'config/environments/staging.rb'
  copy_file 'staging.rb', staging_file

  config = <<~RUBY

                Rails.application.configure do
                  # ...
                end
              RUBY

  append_file staging_file, config
end

#setup_stylesheetsObject



413
414
415
416
417
# File 'lib/kratos/app_builder.rb', line 413

def setup_stylesheets
  remove_file 'app/assets/stylesheets/application.css'
  copy_file 'application.scss',
            'app/assets/stylesheets/application.scss'
end

#use_mysql_config_templateObject



428
429
430
431
# File 'lib/kratos/app_builder.rb', line 428

def use_mysql_config_template
  template 'mysql_database.yml.erb', 'config/database.yml',
            force: true
end

#use_postgres_config_templateObject



423
424
425
426
# File 'lib/kratos/app_builder.rb', line 423

def use_postgres_config_template
  template 'postgresql_database.yml.erb', 'config/database.yml',
            force: true
end