Class: Underlay::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Extended by:
Forwardable
Includes:
Actions
Defined in:
lib/underlay/app_builder.rb

Instance Method Summary collapse

Methods included from Actions

#action_mailer_asset_host, #action_mailer_host, #configure_application_file, #configure_environment, #replace_in_file

Instance Method Details

#add_bullet_gem_configurationObject



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

def add_bullet_gem_configuration
  config = <<-RUBY
  config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.rails_logger = true
  end

  RUBY

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

#configure_action_mailerObject



322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/underlay/app_builder.rb', line 322

def configure_action_mailer
  action_mailer_host 'development', %("localhost:3000")
  action_mailer_asset_host 'development', %("http://localhost:3000")
  action_mailer_host 'test', %("www.example.com")
  action_mailer_asset_host 'test', %("http://www.example.com")
  action_mailer_host 'production', "ENV['APPLICATION_HOST']"
  action_mailer_asset_host(
    'production',
    "ENV.fetch(\n    'ASSET_HOST',\n" \
    "    ENV.fetch('APPLICATION_HOST'\n)\n  )"
  )
end

#configure_action_mailer_in_specsObject



297
298
299
# File 'lib/underlay/app_builder.rb', line 297

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

#configure_active_jobObject



335
336
337
338
339
340
341
# File 'lib/underlay/app_builder.rb', line 335

def configure_active_job
  configure_application_file(
    'config.active_job.queue_adapter = :sidekiq'
  )
  configure_environment 'test', 'config.active_job.queue_adapter = :inline'
  copy_file 'sidekiq.yml', 'config/sidekiq.yml', force: true
end

#configure_automatic_deploymentObject



397
398
399
400
401
402
403
404
405
406
407
# File 'lib/underlay/app_builder.rb', line 397

def configure_automatic_deployment
  deploy_command = <<-YML.strip_heredoc
  deployment:
    staging:
      branch: master
      commands:
        - bin/deploy staging
  YML

  append_file 'circle.yml', deploy_command
end

#configure_capybaraObject



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

def configure_capybara
  copy_file 'capybara.rb', 'spec/support/capybara.rb'
end

#configure_ciObject



275
276
277
# File 'lib/underlay/app_builder.rb', line 275

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

#configure_generatorsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/underlay/app_builder.rb', line 111

def configure_generators
  config = <<-RUBY

config.generators do |generate|
  generate.helper false
  generate.javascripts 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



292
293
294
295
# File 'lib/underlay/app_builder.rb', line 292

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

#configure_i18n_for_test_environmentObject



288
289
290
# File 'lib/underlay/app_builder.rb', line 288

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

#configure_lintersObject



283
284
285
286
# File 'lib/underlay/app_builder.rb', line 283

def configure_linters
  template 'scss-lint.yml.erb', '.scss-lint.yml'
  template 'slim-lint.yml.erb', '.slim-lint.yml'
end

#configure_quiet_assetsObject



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

def configure_quiet_assets
  config = <<-RUBY
config.assets.quiet = true
  RUBY

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

#configure_rack_timeoutObject



310
311
312
313
314
315
316
# File 'lib/underlay/app_builder.rb', line 310

def configure_rack_timeout
  rack_timeout_config = <<~RUBY
    Rack::Timeout.timeout = (ENV["RACK_TIMEOUT"] || 10).to_i
  RUBY

  append_file 'config/environments/production.rb', rack_timeout_config
end

#configure_rspecObject



268
269
270
271
272
273
# File 'lib/underlay/app_builder.rb', line 268

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_rubocopObject



279
280
281
# File 'lib/underlay/app_builder.rb', line 279

def configure_rubocop
  template 'rubocop.yml.erb', '.rubocop.yml'
end

#configure_simple_formObject



318
319
320
# File 'lib/underlay/app_builder.rb', line 318

def configure_simple_form
  bundle_command 'exec rails generate simple_form:install'
end

#configure_smtpObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/underlay/app_builder.rb', line 137

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



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

def configure_spec_support_features
  empty_directory_with_keep_file 'spec/features'
  empty_directory_with_keep_file 'spec/support/features'
end

#configure_time_formatsObject



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

def configure_time_formats
  remove_file 'config/locales/en.yml'
  template 'config_locales_en.yml.erb', 'config/locales/en.yml'
end

#copy_figaro_filesObject



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

def copy_figaro_files
  copy_file 'application.example.yml', 'config/application.example.yml'
  copy_file 'application.example.yml', 'config/application.yml'
end

#copy_miscellaneous_filesObject



427
428
429
430
431
432
433
434
# File 'lib/underlay/app_builder.rb', line 427

def copy_miscellaneous_files
  copy_file 'browserslist', 'browserslist'
  copy_file 'errors.rb', 'config/initializers/errors.rb'
  copy_file 'json_encoding.rb', 'config/initializers/json_encoding.rb'
  copy_file 'lograge.rb', 'config/initializers/lograge.rb'
  copy_file 'redis.rb', 'config/initializers/redis.rb'
  copy_file 'sentry.rb', 'config/initializers/sentry.rb'
end

#create_application_layoutObject



221
222
223
224
225
226
227
# File 'lib/underlay/app_builder.rb', line 221

def create_application_layout
  template 'dvelp_layout.html.slim',
    'app/views/layouts/application.html.slim',
    force: true

  remove_file 'app/views/layouts/application.html.erb'
end

#create_databaseObject



234
235
236
# File 'lib/underlay/app_builder.rb', line 234

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

#create_deploy_scriptObject



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/underlay/app_builder.rb', line 379

def create_deploy_script
  copy_file 'bin_deploy', 'bin/deploy'

  instructions = <<~MARKDOWN

    ## Deploying

    If you have previously run the `./bin/setup` script,
    you can deploy to staging and production with:

        % ./bin/deploy staging
        % ./bin/deploy production
  MARKDOWN

  append_file 'README.md', instructions
  run 'chmod a+x bin/deploy'
end

#create_github_repo(repo_name) ⇒ Object



409
410
411
# File 'lib/underlay/app_builder.rb', line 409

def create_github_repo(repo_name)
  run "hub create #{repo_name} -p"
end

#create_heroku_apps(flags) ⇒ Object



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

def create_heroku_apps(flags)
  create_staging_heroku_app(flags)
  create_production_heroku_app(flags)
end

#create_partials_directoryObject



198
199
200
# File 'lib/underlay/app_builder.rb', line 198

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_shared_css_overridesObject



214
215
216
217
218
219
# File 'lib/underlay/app_builder.rb', line 214

def create_shared_css_overrides
  copy_file(
    '_css_overrides.html.slim',
    'app/views/application/_css_overrides.html.slim'
  )
end

#create_shared_flashesObject



202
203
204
205
# File 'lib/underlay/app_builder.rb', line 202

def create_shared_flashes
  copy_file '_flashes.html.slim', 'app/views/application/_flashes.html.slim'
  copy_file 'flashes_helper.rb', 'app/helpers/flashes_helper.rb'
end

#create_shared_javascriptsObject



207
208
209
210
211
212
# File 'lib/underlay/app_builder.rb', line 207

def create_shared_javascripts
  copy_file(
    '_javascript.html.slim',
    'app/views/application/_javascript.html.slim'
  )
end

#customize_error_pagesObject



436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/underlay/app_builder.rb', line 436

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

  %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

#disallow_wrapping_parametersObject



194
195
196
# File 'lib/underlay/app_builder.rb', line 194

def disallow_wrapping_parameters
  remove_file 'config/initializers/wrap_parameters.rb'
end

#enable_database_cleanerObject



252
253
254
# File 'lib/underlay/app_builder.rb', line 252

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

#enable_rack_canonical_hostObject



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/underlay/app_builder.rb', line 153

def enable_rack_canonical_host
  config = <<-RUBY

  if ENV.fetch("HEROKU_APP_NAME", "").include?("staging-pr-")
ENV["APPLICATION_HOST"] = ENV["HEROKU_APP_NAME"] + ".herokuapp.com"
  end

  config.middleware.use Rack::CanonicalHost, ENV.fetch("APPLICATION_HOST")
  RUBY

  configure_environment 'production', config
end

#enable_rack_deflaterObject



166
167
168
# File 'lib/underlay/app_builder.rb', line 166

def enable_rack_deflater
  configure_environment 'production', 'config.middleware.use Rack::Deflater'
end

#gemfileObject



31
32
33
# File 'lib/underlay/app_builder.rb', line 31

def gemfile
  template 'Gemfile.erb', 'Gemfile'
end

#generate_factories_fileObject



133
134
135
# File 'lib/underlay/app_builder.rb', line 133

def generate_factories_file
  copy_file 'factories.rb', 'spec/factories.rb'
end

#generate_rspecObject



343
344
345
# File 'lib/underlay/app_builder.rb', line 343

def generate_rspec
  generate 'rspec:install'
end

#gitignoreObject



27
28
29
# File 'lib/underlay/app_builder.rb', line 27

def gitignore
  copy_file 'dvelp_gitignore', '.gitignore'
end

#manual_code_correctObject



511
512
513
# File 'lib/underlay/app_builder.rb', line 511

def manual_code_correct
  simple_form_corrections unless api_mode?
end

#non_api_files_removalObject



515
516
517
518
519
520
521
# File 'lib/underlay/app_builder.rb', line 515

def non_api_files_removal
  remove_file 'app.json'
  remove_file 'browserslist'
  remove_file 'config/cable.yml'
  run 'rm -r -f app/channels'
  run 'rm -r -f app/views/pages'
end

#provide_dev_prime_taskObject



107
108
109
# File 'lib/underlay/app_builder.rb', line 107

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

#provide_rake_scriptObject



97
98
99
100
# File 'lib/underlay/app_builder.rb', line 97

def provide_rake_script
  template 'bin_rake', 'bin/rake', force: true
  run 'chmod a+x bin/rake'
end

#provide_setup_scriptObject



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

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

#provide_shoulda_matchers_configObject



256
257
258
259
260
261
# File 'lib/underlay/app_builder.rb', line 256

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

#provide_update_scriptObject



102
103
104
105
# File 'lib/underlay/app_builder.rb', line 102

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

#raise_on_delivery_errorsObject



46
47
48
49
# File 'lib/underlay/app_builder.rb', line 46

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



42
43
44
# File 'lib/underlay/app_builder.rb', line 42

def raise_on_missing_assets_in_test
  configure_environment 'test', 'config.assets.raise_runtime_errors = true'
end

#raise_on_unpermitted_parametersObject



76
77
78
79
80
81
82
# File 'lib/underlay/app_builder.rb', line 76

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



23
24
25
# File 'lib/underlay/app_builder.rb', line 23

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

#remove_app_comment_linesObject



449
450
451
452
453
# File 'lib/underlay/app_builder.rb', line 449

def remove_app_comment_lines
  files = ['jobs/application_job.rb']

  remove_comment_lines(files: files, path_key: 'app')
end

#remove_comment_lines(files:, path_key:) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/underlay/app_builder.rb', line 477

def remove_comment_lines(files:, path_key:)
  files.each do |config_file|
    path = File.join(destination_root, "#{path_key}/#{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

#remove_config_comment_linesObject



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/underlay/app_builder.rb', line 455

def remove_config_comment_lines
  files = [
    'application.rb',
    'initializers/backtrace_silencers.rb',
    'initializers/wrap_parameters.rb',
    '../db/seeds.rb',
    'environment.rb',
    'environments/development.rb',
    'environments/production.rb',
    'environments/test.rb',
    '../Rakefile'
  ] + mode_dependent_files

  remove_comment_lines(files: files, path_key: 'config')
end

#remove_routes_comment_linesObject



471
472
473
474
475
# File 'lib/underlay/app_builder.rb', line 471

def remove_routes_comment_lines
  replace_in_file 'config/routes.rb',
    /Rails\.application\.routes\.draw do.*end/m,
    "Rails.application.routes.draw do\nend"
end

#replace_default_puma_configurationObject



347
348
349
# File 'lib/underlay/app_builder.rb', line 347

def replace_default_puma_configuration
  copy_file 'puma.rb', 'config/puma.rb', force: true
end

#replace_gemfile(path) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/underlay/app_builder.rb', line 238

def replace_gemfile(path)
  template 'Gemfile.erb', 'Gemfile', force: true do |content|
    if path
      content.gsub(/gem .underlay./) { |s| %(#{s}, path: "#{path}") }
    else
      content
    end
  end
end

#rubocop_autocorrectObject



507
508
509
# File 'lib/underlay/app_builder.rb', line 507

def rubocop_autocorrect
  run 'bundle exec rubocop -a'
end

#set_ruby_to_version_being_usedObject



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

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

#set_test_delivery_methodObject



51
52
53
54
55
56
57
# File 'lib/underlay/app_builder.rb', line 51

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

#set_up_factory_bot_for_rspecObject



129
130
131
# File 'lib/underlay/app_builder.rb', line 129

def set_up_factory_bot_for_rspec
  copy_file 'factory_bot_rspec.rb', 'spec/support/factory_bot.rb'
end

#set_up_foregoObject



351
352
353
# File 'lib/underlay/app_builder.rb', line 351

def set_up_forego
  copy_file 'Procfile', 'Procfile'
end

#setup_asset_hostObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/underlay/app_builder.rb', line 170

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(\n    'ASSET_HOST',\n" \
    "    ENV.fetch('APPLICATION_HOST'\n)\n  )"

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

  config = <<~HERE
    config.public_file_server.headers = {
        "Cache-Control" => "public, max-age=31557600",
      }
  HERE

  configure_environment('production', config)
end

#setup_bundler_auditObject



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

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

#setup_default_directoriesObject



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

def setup_default_directories
  [
    'app/views/pages',
    'spec/lib',
    'spec/controllers',
    'spec/helpers',
    'spec/support/matchers',
    'spec/support/mixins',
    'spec/support/shared_examples'
  ].each do |dir|
    empty_directory_with_keep_file dir
  end
end

#setup_default_rake_taskObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/underlay/app_builder.rb', line 491

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

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

#setup_google_tag_managerObject



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

def setup_google_tag_manager
  copy_file '_analytics.html.slim',
    'app/views/application/_analytics.html.slim'
end

#setup_rack_mini_profilerObject



35
36
37
38
39
40
# File 'lib/underlay/app_builder.rb', line 35

def setup_rack_mini_profiler
  copy_file(
    'rack_mini_profiler.rb',
    'config/initializers/rack_mini_profiler.rb'
  )
end

#setup_secret_tokenObject



190
191
192
# File 'lib/underlay/app_builder.rb', line 190

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

#setup_springObject



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

def setup_spring
  bundle_command 'exec spring binstub --all'
end

#use_postgres_config_templateObject



229
230
231
232
# File 'lib/underlay/app_builder.rb', line 229

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