class Underwear::AppBuilder

Public Instance Methods

configure_action_mailer() click to toggle source
# 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_specs() click to toggle source
# 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_ci() click to toggle source
# File lib/underwear/app_builder.rb, line 212
def configure_ci
  template "circle.yml.erb", "circle.yml"
end
configure_generators() click to toggle source
# File lib/underwear/app_builder.rb, line 39
    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_translations() click to toggle source
# 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_environment() click to toggle source
# 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_tasks() click to toggle source
# 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_newrelic() click to toggle source
# File lib/underwear/app_builder.rb, line 61
def configure_newrelic
  template 'newrelic.yml.erb', 'config/newrelic.yml'
end
configure_puma() click to toggle source
# File lib/underwear/app_builder.rb, line 293
def configure_puma
  copy_file "puma.rb", "config/puma.rb"
end
configure_rack_timeout() click to toggle source
# File lib/underwear/app_builder.rb, line 251
    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_rspec() click to toggle source
# 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_smtp() click to toggle source
# 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 = <<-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_features() click to toggle source
# 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_formats() click to toggle source
# 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_files() click to toggle source
# 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_layout() click to toggle source
# 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_database() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_directory() click to toggle source
# File lib/underwear/app_builder.rb, line 151
def create_partials_directory
  empty_directory 'app/views/application'
end
create_production_heroku_app(flags) click to toggle source
# 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_flashes() click to toggle source
# 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_javascripts() click to toggle source
# 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) click to toggle source
# 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_pages() click to toggle source
# File lib/underwear/app_builder.rb, line 432
    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_params() click to toggle source
# 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_parameters() click to toggle source
# File lib/underwear/app_builder.rb, line 147
def disallow_wrapping_parameters
  remove_file "config/initializers/wrap_parameters.rb"
end
enable_database_cleaner() click to toggle source
# 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_host() click to toggle source
# File lib/underwear/app_builder.rb, line 81
    def enable_rack_canonical_host
      config = <<-RUBY

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

      inject_into_file(
        "config/environments/production.rb",
        config,
        after: serve_static_files_line
      )
    end
enable_rack_deflater() click to toggle source
# File lib/underwear/app_builder.rb, line 95
    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_warning() click to toggle source
# File lib/underwear/app_builder.rb, line 281
    def fix_i18n_deprecation_warning
      config = <<-RUBY
    config.i18n.enforce_available_locales = true
      RUBY

      inject_into_class 'config/application.rb', 'Application', config
    end
generate_rspec() click to toggle source
# File lib/underwear/app_builder.rb, line 289
def generate_rspec
  generate 'rspec:install'
end
gitignore_files() click to toggle source
# 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_git() click to toggle source
# File lib/underwear/app_builder.rb, line 331
def init_git
  run 'git init'
end
join_heroku_app(environment) click to toggle source
# File lib/underwear/app_builder.rb, line 365
    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} git@heroku.com:#{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_script() click to toggle source
# File lib/underwear/app_builder.rb, line 389
    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_task() click to toggle source
# File lib/underwear/app_builder.rb, line 35
def provide_dev_prime_task
  copy_file 'dev.rake', 'lib/tasks/dev.rake'
end
provide_setup_script() click to toggle source
# 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_errors() click to toggle source
# 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_parameters() click to toggle source
# File lib/underwear/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
readme() click to toggle source
# File lib/underwear/app_builder.rb, line 5
def readme
  template 'README.md.erb', 'README.md'
end
remove_routes_comment_lines() click to toggle source
# 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_gemfile() click to toggle source
# File lib/underwear/app_builder.rb, line 179
def replace_gemfile
  remove_file 'Gemfile'
  template 'Gemfile.erb', 'Gemfile'
end
set_heroku_rails_secrets() click to toggle source
# 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_remotes() click to toggle source
# File lib/underwear/app_builder.rb, line 354
    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_files() click to toggle source
# 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_used() click to toggle source
# 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_method() click to toggle source
# 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_rspec() click to toggle source
# 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_host() click to toggle source
# 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_audit() click to toggle source
# 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_task() click to toggle source
# File lib/underwear/app_builder.rb, line 455
    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_devise() click to toggle source
# 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 = 'please-change-me-at-config-initializers-devise@example.com'"
      bundle_command "exec rails generate devise User"
      devise_routes = <<-RUBY
        devise_scope :user do
          get 'signup' => 'devise/registrations#new'
          get 'login' => 'devise/sessions#new'
          delete 'logout' => 'devise/sessions#destroy'
        end
      RUBY
      append_file "config/routes.rb", devise_routes
    end
setup_foreman() click to toggle source
# File lib/underwear/app_builder.rb, line 297
def setup_foreman
  copy_file 'sample.env', '.sample.env'
  copy_file 'Procfile', 'Procfile'
end
setup_friendly_id() click to toggle source
# File lib/underwear/app_builder.rb, line 220
def setup_friendly_id
  bundle_command "exec rails g friendly_id"
end
setup_heroku_specific_gems() click to toggle source
# 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_javascripts() click to toggle source
# File lib/underwear/app_builder.rb, line 310
def setup_javascripts
  append_file "app/assets/javascripts/application.js",
              %{//= require semantic-ui}
end
setup_pages() click to toggle source
# 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_admin() click to toggle source
# File lib/underwear/app_builder.rb, line 224
def setup_rails_admin
  bundle_command "exec rails g rails_admin:install"
end
setup_secret_token() click to toggle source
# File lib/underwear/app_builder.rb, line 143
def setup_secret_token
  template 'secrets.yml', 'config/secrets.yml', force: true
end
setup_segment() click to toggle source
# File lib/underwear/app_builder.rb, line 412
def setup_segment
  copy_file '_analytics.html.erb',
    'app/views/application/_analytics.html.erb'
end
setup_sidekiq() click to toggle source
# File lib/underwear/app_builder.rb, line 216
def setup_sidekiq
  template "sidekiq.rb", "config/initializers/sidekiq.rb"
end
setup_spring() click to toggle source
# File lib/underwear/app_builder.rb, line 422
def setup_spring
  bundle_command "exec spring binstub --all"
end
setup_staging_environment() click to toggle source
# 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 = <<-RUBY

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

      append_file staging_file, config
    end
setup_stylesheets() click to toggle source
# 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_environment() click to toggle source
# File lib/underwear/app_builder.rb, line 139
def setup_test_environment
  copy_file 'test.rb', 'config/environments/test.rb'
end
use_postgres_config_template() click to toggle source
# File lib/underwear/app_builder.rb, line 170
def use_postgres_config_template
  template 'postgresql_database.yml.erb', 'config/database.yml',
    force: true
end

Private Instance Methods

generate_secret() click to toggle source
# File lib/underwear/app_builder.rb, line 495
def generate_secret
  SecureRandom.hex(64)
end
heroku_app_name_for(environment) click to toggle source
# File lib/underwear/app_builder.rb, line 507
def heroku_app_name_for(environment)
  "#{app_name.dasherize}-#{environment}"
end
override_path_for_tests() click to toggle source
# File lib/underwear/app_builder.rb, line 483
def override_path_for_tests
  if ENV['TESTING']
    support_bin = File.expand_path(File.join('..', '..', 'spec', 'fakes', 'bin'))
    "PATH=#{support_bin}:$PATH"
  end
end
port() click to toggle source
# File lib/underwear/app_builder.rb, line 499
def port
  @port ||= [3000, 4000, 5000, 7000, 8000, 9000].sample
end
raise_on_missing_translations_in(environment) click to toggle source
# File lib/underwear/app_builder.rb, line 477
def raise_on_missing_translations_in(environment)
  config = 'config.action_view.raise_on_missing_translations = true'

  uncomment_lines("config/environments/#{environment}.rb", config)
end
run_heroku(command, environment) click to toggle source
# File lib/underwear/app_builder.rb, line 490
def run_heroku(command, environment)
  path_addition = override_path_for_tests
  run "#{path_addition} heroku #{command} --remote #{environment}"
end
serve_static_files_line() click to toggle source
# File lib/underwear/app_builder.rb, line 503
def serve_static_files_line
  "config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?\n"
end