Class: Underwear::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Includes:
Actions
Defined in:
lib/underwear/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/underwear/app_builder.rb', line 274

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



242
243
244
# File 'lib/underwear/app_builder.rb', line 242

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

#configure_ciObject



212
213
214
# File 'lib/underwear/app_builder.rb', line 212

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

#configure_generatorsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/underwear/app_builder.rb', line 39

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_i18n_for_missing_translationsObject



232
233
234
235
# File 'lib/underwear/app_builder.rb', line 232

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

#configure_i18n_for_test_environmentObject



228
229
230
# File 'lib/underwear/app_builder.rb', line 228

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

#configure_i18n_tasksObject



237
238
239
240
# File 'lib/underwear/app_builder.rb', line 237

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



61
62
63
# File 'lib/underwear/app_builder.rb', line 61

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

#configure_pumaObject



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

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

#configure_rack_timeoutObject



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

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



205
206
207
208
209
210
# File 'lib/underwear/app_builder.rb', line 205

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_smtpObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/underwear/app_builder.rb', line 65

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



200
201
202
203
# File 'lib/underwear/app_builder.rb', line 200

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

#configure_time_formatsObject



246
247
248
249
# File 'lib/underwear/app_builder.rb', line 246

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

#copy_miscellaneous_filesObject



426
427
428
429
430
# File 'lib/underwear/app_builder.rb', line 426

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



164
165
166
167
168
# File 'lib/underwear/app_builder.rb', line 164

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

#create_databaseObject



175
176
177
# File 'lib/underwear/app_builder.rb', line 175

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

#create_github_repo(repo_name) ⇒ Object



407
408
409
410
# File 'lib/underwear/app_builder.rb', line 407

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



349
350
351
352
# File 'lib/underwear/app_builder.rb', line 349

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

#create_partials_directoryObject



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

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_production_heroku_app(flags) ⇒ Object



343
344
345
346
347
# File 'lib/underwear/app_builder.rb', line 343

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

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

#create_shared_flashesObject



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

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



160
161
162
# File 'lib/underwear/app_builder.rb', line 160

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

#create_staging_heroku_app(flags) ⇒ Object



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

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



432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/underwear/app_builder.rb', line 432

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



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

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

#disallow_wrapping_parametersObject



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

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

#enable_database_cleanerObject



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

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

#enable_rack_canonical_hostObject



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

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



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/underwear/app_builder.rb', line 95

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



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

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



289
290
291
# File 'lib/underwear/app_builder.rb', line 289

def generate_rspec
  generate 'rspec:install'
end

#gitignore_filesObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/underwear/app_builder.rb', line 315

def gitignore_files
  remove_file '.gitignore'
  copy_file 'underwear_gitignore', '.gitignore'
  [
    '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



331
332
333
# File 'lib/underwear/app_builder.rb', line 331

def init_git
  run 'git init'
end

#join_heroku_app(environment) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/underwear/app_builder.rb', line 365

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

#provide_deploy_scriptObject



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/underwear/app_builder.rb', line 389

def provide_deploy_script
  copy_file "bin_deploy", "bin/deploy"

  instructions = "\n## Deploying\n\nIf you have previously run the `./bin/setup` script,\nyou can deploy to staging and production with:\n\n$ ./bin/deploy staging\n$ ./bin/deploy production\n  MARKDOWN\n\n  append_file \"README.md\", instructions\n  run \"chmod a+x bin/deploy\"\nend\n"

#provide_dev_prime_taskObject



35
36
37
# File 'lib/underwear/app_builder.rb', line 35

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

#provide_setup_scriptObject



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

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

#raise_on_delivery_errorsObject



9
10
11
12
# File 'lib/underwear/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/underwear/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/underwear/app_builder.rb', line 5

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

#remove_routes_comment_linesObject



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

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



179
180
181
182
# File 'lib/underwear/app_builder.rb', line 179

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

#set_heroku_rails_secretsObject



377
378
379
380
381
# File 'lib/underwear/app_builder.rb', line 377

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



354
355
356
357
358
359
360
361
362
363
# File 'lib/underwear/app_builder.rb', line 354

def set_heroku_remotes
  remotes = "\n# Set up the staging and production apps.\n\#{join_heroku_app('staging')}\n\#{join_heroku_app('production')}\n  SHELL\n\n  append_file 'bin/setup', remotes\nend\n"

#set_heroku_serve_static_filesObject



383
384
385
386
387
# File 'lib/underwear/app_builder.rb', line 383

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



184
185
186
# File 'lib/underwear/app_builder.rb', line 184

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

#set_test_delivery_methodObject



14
15
16
17
18
19
20
# File 'lib/underwear/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



57
58
59
# File 'lib/underwear/app_builder.rb', line 57

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

#setup_asset_hostObject



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

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



417
418
419
420
# File 'lib/underwear/app_builder.rb', line 417

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



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

def setup_default_rake_task
  append_file 'Rakefile' do
    "task(:default).clear\ntask default: [:spec]\n\nif defined? RSpec\n  task(:spec).clear\n  RSpec::Core::RakeTask.new(:spec) do |t|\nt.verbose = false\n  end\nend\n    EOS\n  end\nend\n"

#setup_deviseObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/underwear/app_builder.rb', line 259

def setup_devise
  bundle_command "exec rails generate devise:install"
  inject_into_file 'config/initializers/devise.rb', "config.secret_key = '12345'\n\n",
    :before => "config.mailer_sender = '[email protected]'"
  bundle_command "exec rails generate devise User"
  devise_routes = "    devise_scope :user do\n      get 'signup' => 'devise/registrations#new'\n      get 'login' => 'devise/sessions#new'\n      delete 'logout' => 'devise/sessions#destroy'\n    end\n  RUBY\n  append_file \"config/routes.rb\", devise_routes\nend\n"

#setup_foremanObject



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

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

#setup_friendly_idObject



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

def setup_friendly_id
  bundle_command "exec rails g friendly_id"
end

#setup_heroku_specific_gemsObject



188
189
190
191
192
193
194
# File 'lib/underwear/app_builder.rb', line 188

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

#setup_javascriptsObject



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

def setup_javascripts
  append_file "app/assets/javascripts/application.js",
              %{//= require semantic-ui}
end

#setup_pagesObject



471
472
473
# File 'lib/underwear/app_builder.rb', line 471

def setup_pages
  bundle_command "exec rails generate controller pages home about terms privacy"
end

#setup_rails_adminObject



224
225
226
# File 'lib/underwear/app_builder.rb', line 224

def setup_rails_admin
  bundle_command "exec rails g rails_admin:install"
end

#setup_secret_tokenObject



143
144
145
# File 'lib/underwear/app_builder.rb', line 143

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

#setup_segmentObject



412
413
414
415
# File 'lib/underwear/app_builder.rb', line 412

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

#setup_sidekiqObject



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

def setup_sidekiq
  template "sidekiq.rb", "config/initializers/sidekiq.rb"
end

#setup_springObject



422
423
424
# File 'lib/underwear/app_builder.rb', line 422

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

#setup_staging_environmentObject



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

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



302
303
304
305
306
307
308
# File 'lib/underwear/app_builder.rb', line 302

def setup_stylesheets
  remove_file "app/assets/stylesheets/application.css"
  copy_file "application.scss",
            "app/assets/stylesheets/application.scss"
  prepend_file "app/assets/stylesheets/application.scss",
              %{@import "semantic-ui";}
end

#setup_test_environmentObject



139
140
141
# File 'lib/underwear/app_builder.rb', line 139

def setup_test_environment
  copy_file 'test.rb', 'config/environments/test.rb'
end

#use_postgres_config_templateObject



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

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