Class: Suspenders::AppBuilder

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

Instance Method Summary collapse

Methods included from Actions

#action_mailer_host, #configure_application_file, #configure_environment, #replace_in_file

Instance Method Details

#configure_action_mailerObject



274
275
276
277
278
279
# File 'lib/suspenders/app_builder.rb', line 274

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

#configure_action_mailer_in_specsObject



253
254
255
# File 'lib/suspenders/app_builder.rb', line 253

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

#configure_active_jobObject



281
282
283
284
285
286
# File 'lib/suspenders/app_builder.rb', line 281

def configure_active_job
  configure_application_file(
    "config.active_job.queue_adapter = :delayed_job"
  )
  configure_environment "test", "config.active_job.queue_adapter = :inline"
end

#configure_automatic_deploymentObject



422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/suspenders/app_builder.rb', line 422

def configure_automatic_deployment
  staging_remote_name = heroku_app_name_for("staging")
  deploy_command = <<-YML.strip_heredoc
  deployment:
    staging:
      branch: master
      commands:
        - git remote add staging [email protected]:#{staging_remote_name}.git
        - bin/deploy staging
  YML

  append_file "circle.yml", deploy_command
end

#configure_background_jobs_for_rspecObject



249
250
251
# File 'lib/suspenders/app_builder.rb', line 249

def configure_background_jobs_for_rspec
  run 'rails g delayed_job:active_record'
end

#configure_ciObject



231
232
233
# File 'lib/suspenders/app_builder.rb', line 231

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

#configure_generatorsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/suspenders/app_builder.rb', line 47

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



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

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

#configure_i18n_for_test_environmentObject



235
236
237
# File 'lib/suspenders/app_builder.rb', line 235

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

#configure_i18n_tasksObject



244
245
246
247
# File 'lib/suspenders/app_builder.rb', line 244

def configure_i18n_tasks
  run "cp $(i18n-tasks gem-path)/templates/rspec/i18n_spec.rb spec/"
  copy_file "config_i18n_tasks.yml", "config/i18n-tasks.yml"
end

#configure_newrelicObject



77
78
79
# File 'lib/suspenders/app_builder.rb', line 77

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

#configure_pumaObject



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

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

#configure_quiet_assetsObject



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

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

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

#configure_rack_timeoutObject



262
263
264
265
266
267
268
# File 'lib/suspenders/app_builder.rb', line 262

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



224
225
226
227
228
229
# File 'lib/suspenders/app_builder.rb', line 224

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_simple_formObject



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

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

#configure_smtpObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/suspenders/app_builder.rb', line 81

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



219
220
221
222
# File 'lib/suspenders/app_builder.rb', line 219

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

#configure_time_formatsObject



257
258
259
260
# File 'lib/suspenders/app_builder.rb', line 257

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

#copy_dotfilesObject



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

def copy_dotfiles
  directory("dotfiles", ".")
end

#copy_miscellaneous_filesObject



455
456
457
458
459
# File 'lib/suspenders/app_builder.rb', line 455

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"
end

#create_application_layoutObject



176
177
178
179
180
# File 'lib/suspenders/app_builder.rb', line 176

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

#create_databaseObject



187
188
189
# File 'lib/suspenders/app_builder.rb', line 187

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

#create_github_repo(repo_name) ⇒ Object



436
437
438
439
# File 'lib/suspenders/app_builder.rb', line 436

def create_github_repo(repo_name)
  path_addition = override_path_for_tests
  run "#{path_addition} hub create #{repo_name}"
end

#create_heroku_apps(flags) ⇒ Object



364
365
366
367
# File 'lib/suspenders/app_builder.rb', line 364

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

#create_partials_directoryObject



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

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_production_heroku_app(flags) ⇒ Object



358
359
360
361
362
# File 'lib/suspenders/app_builder.rb', line 358

def create_production_heroku_app(flags)
  app_name = heroku_app_name_for("production")

  run_heroku "create #{app_name} #{flags}", "production"
end

#create_shared_flashesObject



167
168
169
170
# File 'lib/suspenders/app_builder.rb', line 167

def create_shared_flashes
  copy_file "_flashes.html.erb", "app/views/application/_flashes.html.erb"
  copy_file "flashes_helper.rb", "app/helpers/flashes_helper.rb"
end

#create_shared_javascriptsObject



172
173
174
# File 'lib/suspenders/app_builder.rb', line 172

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

#create_staging_heroku_app(flags) ⇒ Object



350
351
352
353
354
355
356
# File 'lib/suspenders/app_builder.rb', line 350

def create_staging_heroku_app(flags)
  rack_env = "RACK_ENV=staging RAILS_ENV=staging"
  app_name = heroku_app_name_for("staging")

  run_heroku "create #{app_name} #{flags}", "staging"
  run_heroku "config:add #{rack_env}", "staging"
end

#customize_error_pagesObject



461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/suspenders/app_builder.rb', line 461

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



502
503
504
# File 'lib/suspenders/app_builder.rb', line 502

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

#disallow_wrapping_parametersObject



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

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

#enable_database_cleanerObject



208
209
210
# File 'lib/suspenders/app_builder.rb', line 208

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

#enable_rack_canonical_hostObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/suspenders/app_builder.rb', line 97

def enable_rack_canonical_host
  config = <<-RUBY

  # Ensure requests are only served from one, canonical host name
  config.middleware.use Rack::CanonicalHost, ENV.fetch("APPLICATION_HOST")
  RUBY

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

#enable_rack_deflaterObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/suspenders/app_builder.rb', line 111

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



288
289
290
291
292
293
294
# File 'lib/suspenders/app_builder.rb', line 288

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

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

#generate_factories_fileObject



69
70
71
# File 'lib/suspenders/app_builder.rb', line 69

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

#generate_rspecObject



296
297
298
# File 'lib/suspenders/app_builder.rb', line 296

def generate_rspec
  generate 'rspec:install'
end

#gitignore_filesObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/suspenders/app_builder.rb', line 325

def gitignore_files
  remove_file '.gitignore'
  copy_file 'suspenders_gitignore', '.gitignore'
  [
    'app/views/pages',
    'spec/lib',
    'spec/controllers',
    'spec/helpers',
    'spec/support/matchers',
    'spec/support/mixins',
    'spec/support/shared_examples'
  ].each do |dir|
    run "mkdir #{dir}"
    run "touch #{dir}/.keep"
  end
end

#init_gitObject



346
347
348
# File 'lib/suspenders/app_builder.rb', line 346

def init_git
  run 'git init'
end

#install_bittersObject



321
322
323
# File 'lib/suspenders/app_builder.rb', line 321

def install_bitters
  run "bitters install --path app/assets/stylesheets"
end

#install_refillsObject



315
316
317
318
319
# File 'lib/suspenders/app_builder.rb', line 315

def install_refills
  run "rails generate refills:import flashes"
  run "rm app/views/refills/_flashes.html.erb"
  run "rmdir app/views/refills"
end

#join_heroku_app(environment) ⇒ Object



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

def join_heroku_app(environment)
  heroku_app_name = heroku_app_name_for(environment)
  <<-SHELL
if heroku join --app #{heroku_app_name} &> /dev/null; then
  git remote add #{environment} [email protected]:#{heroku_app_name}.git || true
  printf 'You are a collaborator on the "#{heroku_app_name}" Heroku app\n'
else
  printf 'Ask for access to the "#{heroku_app_name}" Heroku app\n'
fi
  SHELL
end

#provide_deploy_scriptObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/suspenders/app_builder.rb', line 404

def provide_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

#provide_dev_prime_taskObject



43
44
45
# File 'lib/suspenders/app_builder.rb', line 43

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

#provide_setup_scriptObject



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

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

#provide_shoulda_matchers_configObject



212
213
214
215
216
217
# File 'lib/suspenders/app_builder.rb', line 212

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

#raise_on_delivery_errorsObject



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

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

#raise_on_unpermitted_parametersObject



22
23
24
25
26
27
28
# File 'lib/suspenders/app_builder.rb', line 22

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



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

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

#remove_config_comment_linesObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/suspenders/app_builder.rb', line 474

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

#remove_routes_comment_linesObject



496
497
498
499
500
# File 'lib/suspenders/app_builder.rb', line 496

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_gemfileObject



191
192
193
194
# File 'lib/suspenders/app_builder.rb', line 191

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

#set_heroku_rails_secretsObject



392
393
394
395
396
# File 'lib/suspenders/app_builder.rb', line 392

def set_heroku_rails_secrets
  %w(staging production).each do |environment|
    run_heroku "config:add SECRET_KEY_BASE=#{generate_secret}", environment
  end
end

#set_heroku_remotesObject



369
370
371
372
373
374
375
376
377
378
# File 'lib/suspenders/app_builder.rb', line 369

def set_heroku_remotes
  remotes = <<-SHELL

# Set up the staging and production apps.
#{join_heroku_app('staging')}
#{join_heroku_app('production')}
  SHELL

  append_file 'bin/setup', remotes
end

#set_heroku_serve_static_filesObject



398
399
400
401
402
# File 'lib/suspenders/app_builder.rb', line 398

def set_heroku_serve_static_files
  %w(staging production).each do |environment|
    run_heroku "config:add RAILS_SERVE_STATIC_FILES=true", environment
  end
end

#set_ruby_to_version_being_usedObject



196
197
198
# File 'lib/suspenders/app_builder.rb', line 196

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

#set_test_delivery_methodObject



14
15
16
17
18
19
20
# File 'lib/suspenders/app_builder.rb', line 14

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



65
66
67
# File 'lib/suspenders/app_builder.rb', line 65

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

#set_up_houndObject



73
74
75
# File 'lib/suspenders/app_builder.rb', line 73

def set_up_hound
  copy_file "hound.yml", ".hound.yml"
end

#setup_asset_hostObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/suspenders/app_builder.rb', line 125

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_bundler_auditObject



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

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

#setup_default_rake_taskObject



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/suspenders/app_builder.rb', line 506

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_foremanObject



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

def setup_foreman
  copy_file 'sample.env', '.sample.env'
  copy_file 'Procfile', 'Procfile'
end

#setup_heroku_specific_gemsObject



200
201
202
203
204
205
206
# File 'lib/suspenders/app_builder.rb', line 200

def setup_heroku_specific_gems
  inject_into_file(
    "Gemfile",
    %{\n\s\sgem "rails_stdout_logging"},
    after: /group :staging, :production do/
  )
end

#setup_secret_tokenObject



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

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

#setup_segmentObject



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

def setup_segment
  copy_file '_analytics.html.erb',
    'app/views/application/_analytics.html.erb'
end

#setup_springObject



451
452
453
# File 'lib/suspenders/app_builder.rb', line 451

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

#setup_staging_environmentObject



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/suspenders/app_builder.rb', line 141

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



309
310
311
312
313
# File 'lib/suspenders/app_builder.rb', line 309

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

#use_postgres_config_templateObject



182
183
184
185
# File 'lib/suspenders/app_builder.rb', line 182

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