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



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

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

#configure_action_mailer_in_specsObject



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

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

#configure_active_jobObject



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

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

#configure_airbrakeObject



547
548
549
# File 'lib/suspenders/app_builder.rb', line 547

def configure_airbrake
  template 'airbrake.rb', 'config/initializers/airbrake.rb'
end

#configure_generatorsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/suspenders/app_builder.rb', line 34

def configure_generators
  config = "\nconfig.generators do |generate|\n  generate.helper false\n  generate.javascript_engine false\n  generate.request_specs false\n  generate.routing_specs false\n  generate.stylesheets false\n  generate.test_framework :rspec\n  generate.view_specs false\nend\n\n  RUBY\n\n  inject_into_class 'config/application.rb', 'Application', config\nend\n"

#configure_heroku_app(environment) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/suspenders/app_builder.rb', line 450

def configure_heroku_app(environment)
  run_heroku "addons:create mandrill", environment
  run_heroku "addons:create airbrake:free_heroku", environment
  run_heroku "addons:create papertrail", environment

  domain = "#{heroku_app_name_for(environment)}.herokuapp.com"

  env_vars = [
    'SMTP_DOMAIN=heroku.com',
    'SMTP_ADDRESS=smtp.mandrillapp.com',
    'SMTP_PROVIDER=mandrill',
    "HOST=#{domain}",
    "ASSET_HOST=#{domain}"
  ]

  run_heroku "config:add #{env_vars.join(' ')}", environment
end

#configure_i18n_for_missing_translationsObject



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

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

#configure_i18n_for_test_environmentObject



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

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

#configure_i18n_tasksObject



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

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



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

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

#configure_rack_timeoutObject



250
251
252
253
254
255
256
# File 'lib/suspenders/app_builder.rb', line 250

def configure_rack_timeout
  rack_timeout_config = "Rack::Timeout.timeout = (ENV[\"RACK_TIMEOUT\"] || 10).to_i\n  RUBY\n\n  append_file \"config/environments/production.rb\", rack_timeout_config\nend\n"

#configure_rspecObject



220
221
222
223
224
225
# File 'lib/suspenders/app_builder.rb', line 220

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



543
544
545
# File 'lib/suspenders/app_builder.rb', line 543

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

#configure_simple_formObject



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

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

#configure_smtpObject



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

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

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

  config = "\n  config.action_mailer.delivery_method = :smtp\n  config.action_mailer.smtp_settings = SMTP_SETTINGS\n  RUBY\n\n  inject_into_file 'config/environments/production.rb', config,\n    :after => 'config.action_mailer.raise_delivery_errors = false'\nend\n"

#configure_spec_support_featuresObject



215
216
217
218
# File 'lib/suspenders/app_builder.rb', line 215

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

#configure_time_formatsObject



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

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

#configure_unicornObject



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

def configure_unicorn
  copy_file 'unicorn.rb', 'config/unicorn.rb'
end

#copy_miscellaneous_filesObject



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

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



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

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

#create_databaseObject



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

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

#create_github_repo(repo_name) ⇒ Object



480
481
482
483
# File 'lib/suspenders/app_builder.rb', line 480

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

#create_heroku_apps(flags) ⇒ Object



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

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

#create_initial_commitObject



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

def create_initial_commit
  run 'git add -A'
  run 'git commit -m "Initial app setup."'
end

#create_partials_directoryObject



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

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_production_heroku_app(flags) ⇒ Object



438
439
440
441
442
443
# File 'lib/suspenders/app_builder.rb', line 438

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

  run_heroku "create #{app_name} --ssh-git #{flags}", "production"
  configure_heroku_app("production")
end

#create_shared_flashesObject



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

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



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

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

#create_staging_heroku_app(flags) ⇒ Object



429
430
431
432
433
434
435
436
# File 'lib/suspenders/app_builder.rb', line 429

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} --ssh-git #{flags}", "staging"
  run_heroku "config:add #{rack_env}", "staging"
  configure_heroku_app("staging")
end

#customize_error_pagesObject



503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/suspenders/app_builder.rb', line 503

def customize_error_pages
  meta_tags ="  <meta charset=\"utf-8\" />\n  <meta name=\"ROBOTS\" content=\"NOODP\" />\n  <meta name=\"viewport\" content=\"initial-scale=1\" />\n  EOS\n\n  %w(500 404 422).each do |page|\n    inject_into_file \"public/\#{page}.html\", meta_tags, :after => \"<head>\\n\"\n    replace_in_file \"public/\#{page}.html\", /<!--.+-->\\n/, ''\n  end\nend\n"

#disable_xml_paramsObject



522
523
524
# File 'lib/suspenders/app_builder.rb', line 522

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

#disallow_wrapping_parametersObject



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

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

#enable_database_cleanerObject



211
212
213
# File 'lib/suspenders/app_builder.rb', line 211

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

#enable_rack_canonical_hostObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/suspenders/app_builder.rb', line 76

def enable_rack_canonical_host
  config = "\n  # Ensure requests are only served from one, canonical host name\n  config.middleware.use Rack::CanonicalHost, ENV.fetch(\"HOST\")\n  RUBY\n\n  inject_into_file(\n    \"config/environments/production.rb\",\n    config,\n    after: serve_static_files_line\n  )\nend\n"

#enable_rack_deflaterObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/suspenders/app_builder.rb', line 90

def enable_rack_deflater
  config = "\n  # Enable deflate / gzip compression of controller-generated responses\n  config.middleware.use Rack::Deflater\n  RUBY\n\n  inject_into_file(\n    \"config/environments/production.rb\",\n    config,\n    after: serve_static_files_line\n  )\nend\n"

#fix_i18n_deprecation_warningObject



276
277
278
279
280
281
282
# File 'lib/suspenders/app_builder.rb', line 276

def fix_i18n_deprecation_warning
  config = "config.i18n.enforce_available_locales = true\n  RUBY\n\n  inject_into_class 'config/application.rb', 'Application', config\nend\n"

#generate_rspecObject



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

def generate_rspec
  generate 'rspec:install'
end

#gitignore_filesObject



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/suspenders/app_builder.rb', line 394

def gitignore_files
  remove_file '.gitignore'
  copy_file 'suspenders_gitignore', '.gitignore'
  [
    'app/workers',
    'spec/lib',
    'spec/controllers',
    'spec/helpers',
    'spec/support/matchers',
    'spec/support/mixins',
    'spec/support/shared_examples'
  ].each do |dir|
    create_empty_directory(dir)
  end
end

#init_gitObject



410
411
412
# File 'lib/suspenders/app_builder.rb', line 410

def init_git
  run 'git init'
end

#install_bootstrapObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/suspenders/app_builder.rb', line 328

def install_bootstrap
  inject_into_file(
    'Gemfile',
    %{\ngem 'bootstrap-sass'},
    after: /gem 'sass-rails',.*$/
  )
  bundle_command 'install'

  inject_into_file(
    'app/assets/stylesheets/application.scss',
    %{\n@import "bootstrap-sprockets";\n@import "bootstrap";},
    after: /@charset "utf-8";/
  )

  inject_into_file(
    'app/assets/javascripts/application.js',
    %{\n\/\/= require bootstrap-sprockets},
    after: /\/\/= require jquery-ujs/
  )
end

#install_bourbon_n_friendsObject



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/suspenders/app_builder.rb', line 308

def install_bourbon_n_friends
  inject_into_file(
    'Gemfile',
    %{\ngem 'bourbon'\ngem 'neat'},
    after: Regexp.new("gem 'rails', '#{Suspenders::RAILS_VERSION}'")
  )
  bundle_command 'install'

  inject_into_file(
    'app/assets/stylesheets/application.scss',
%{
@import "bourbon";
@import "neat";
@import "base/grid-settings";
@import "base/base";
},
    after: /@charset "utf-8";/
  )
end

#install_foundationObject



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/suspenders/app_builder.rb', line 349

def install_foundation
  inject_into_file(
    'Gemfile',
    %{\ngem 'foundation-rails'},
    after: /gem 'sass-rails',.*$/
  )

  inject_into_file(
    'app/assets/stylesheets/application.scss',
    %{\n@import "foundation_and_overrides";},
    after: /@charset "utf-8";/
  )

  inject_into_file(
    'app/assets/javascripts/application.js',
    %{\n\/\/= require foundation},
    after: /\/\/= require jquery-ujs/
  )

  inject_into_file(
    'app/assets/javascripts/application.js',
    %{\n$(document).foundation();},
    after: /\/\/= require_tree ./
  )

  inject_into_file(
    'app/views/layouts/application.html.erb',
    %{<%= javascript_include_tag "vendor/modernizr" %>\n},
    before: /<title>.*$/
  )

  bundle_command 'install'

  result = run('bundle show foundation-rails', capture: true ).gsub("\n",'')
  settings_file = File.join(
    result,
    'vendor', 'assets', 'stylesheets', 'foundation', '_settings.scss'
  )

  create_file(
    "app/assets/stylesheets/foundation_and_overrides.scss",
    File.read(settings_file)
  )
end

#provide_setup_scriptObject



30
31
32
# File 'lib/suspenders/app_builder.rb', line 30

def provide_setup_script
  template "setup.rb", "setup.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 = "config.action_controller.action_on_unpermitted_parameters = :raise\n  RUBY\n\n  inject_into_class \"config/application.rb\", \"Application\", config\nend\n"

#readmeObject



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

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

#remove_routes_comment_linesObject



516
517
518
519
520
# File 'lib/suspenders/app_builder.rb', line 516

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



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

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

#run_stairsObject



551
552
553
554
# File 'lib/suspenders/app_builder.rb', line 551

def run_stairs
  bundle_command 'install'
  bundle_command 'exec stairs --use-defaults'
end

#set_heroku_rails_secretsObject



468
469
470
471
472
# File 'lib/suspenders/app_builder.rb', line 468

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_serve_static_filesObject



474
475
476
477
478
# File 'lib/suspenders/app_builder.rb', line 474

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



199
200
201
# File 'lib/suspenders/app_builder.rb', line 199

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



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

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

#setup_and_push_to_origin_remote(remote_url) ⇒ Object



424
425
426
427
# File 'lib/suspenders/app_builder.rb', line 424

def setup_and_push_to_origin_remote(remote_url)
  run "git remote add origin #{remote_url}"
  run 'git push --all origin'
end

#setup_asset_hostObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/suspenders/app_builder.rb', line 104

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



490
491
492
# File 'lib/suspenders/app_builder.rb', line 490

def setup_bundler_audit
  copy_file "bundler_audit.rake", "lib/tasks/bundler_audit.rake"
end

#setup_default_rake_taskObject



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/suspenders/app_builder.rb', line 526

def setup_default_rake_task
  append_file 'Rakefile' do
    "task(:default).clear\n\nif defined?(RSpec) && defined?(RuboCop)\n  require 'rubocop/rake_task'\n  require 'rspec/core/rake_task'\n\n  RuboCop::RakeTask.new\n\n  task default: [:spec, :rubocop]\nend\n    EOS\n  end\nend\n"

#setup_deployment_environment_branchesObject



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

def setup_deployment_environment_branches
  run 'git branch staging'
  run 'git branch production'
end

#setup_foremanObject



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

def setup_foreman
  copy_file 'Procfile', 'Procfile'
end

#setup_heroku_specific_gemsObject



203
204
205
206
207
208
209
# File 'lib/suspenders/app_builder.rb', line 203

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

#setup_javascriptsObject



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

def setup_javascripts
  remove_file "app/assets/javascripts/application.js"
  copy_file "application.js", "app/assets/javascripts/application.js"
  create_empty_directory('app/assets/javascripts/application')
end

#setup_secret_tokenObject



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

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

#setup_segmentObject



485
486
487
488
# File 'lib/suspenders/app_builder.rb', line 485

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

#setup_springObject



494
495
496
# File 'lib/suspenders/app_builder.rb', line 494

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

#setup_staging_environmentObject



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/suspenders/app_builder.rb', line 120

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

  config = "\nRails.application.configure do\n  # ...\nend\n  RUBY\n\n  append_file staging_file, config\nend\n"

#setup_stylesheetsObject



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

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

#use_postgres_config_templateObject



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

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

#version_gems_in_gemfileObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/suspenders/app_builder.rb', line 175

def version_gems_in_gemfile
  gemfile = File.read('Gemfile')

  require 'bundler'
  locks = Bundler.locked_gems
  specs = locks.specs

  # Naively convert to single quotes
  gemfile.gsub! '"', "'"

  specs.each do |gem|
    name, version = gem.name, gem.version

    # Don't try to set version if:
    # * source is git or github
    # * version is already set
    gemfile.gsub! /^(?!.*, (git:|github:|'))( *gem '#{name}')/, "\\2, '~> #{version}'"
  end

  File.open 'Gemfile', 'w+' do |file|
    file.puts gemfile
  end
end