Class: Rolemodel::HerokuGenerator

Inherits:
GeneratorBase
  • Object
show all
Defined in:
lib/generators/rolemodel/heroku/heroku_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_assets_rake_tasksObject

rubocop:disable Metrics/MethodLength



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 62

def create_assets_rake_tasks # rubocop:disable Metrics/MethodLength
  say 'Enhancing assets:precompile task to remove node_modules directory during production build.', :green
  rakefile('assets.rake', <<~RAKE)
    # All runtime asset dependencies should be bundled by Webpack during asset precompilation.
    # Therefore, the node_modules directory can be removed after assets are compiled to significantly reduce slug size.
    # In rare cases, you may have a runtime dependency into node_modules directly. If this is the case and you are unable
    # to bundle the dependency, delete this file and the node_modules directory will be included in your production slug.

    Rake::Task['assets:precompile'].enhance do
      if Rails.env.production?
        puts '----> Removing node_modules directory to reduce slug size.'
        FileUtils.rm_rf(Rails.root.join('node_modules'))
      end
    end
  RAKE
end

#enable_log_level_configurabilityObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 51

def enable_log_level_configurability
  say 'Enable log-level adjustment via "LOG_LEVEL" environment variable', :green

  gsub_file(
    'config/environments/production.rb',
    'config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")',
    "config.log_level = ENV.fetch('LOG_LEVEL', 'INFO')"
  )
  gsub_file('config/environments/production.rb', 'config.log_level = :info', "config.log_level = ENV.fetch('LOG_LEVEL', 'INFO')")
end

#force_sslObject



45
46
47
48
49
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 45

def force_ssl
  say 'Require SSL for production environment.', :green

  uncomment_lines('config/environments/production.rb', 'config.force_ssl = true')
end

#install_app_jsonObject



5
6
7
8
9
10
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 5

def install_app_json
  say 'Install app.json file', :green

  @project_name = Rails.application.class.try(:parent_name) || Rails.application.class.module_parent_name
  template 'app.json'
end

#install_procfileObject



12
13
14
15
16
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 12

def install_procfile
  say 'Install Procfile', :green

  template 'Procfile'
end

#pin_ruby_version_for_buildpackObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 18

def pin_ruby_version_for_buildpack
  say 'Pin the Ruby version in the Gemfile so the Heroku buildpack respects it.', :green

  # A bare .ruby-version file is not enough: without a `ruby` directive the version
  # never lands in Gemfile.lock, so the Heroku Ruby buildpack falls back to its own
  # default and can install an incompatible Ruby. Tie the Gemfile to .ruby-version.
  gemfile = File.join(destination_root, 'Gemfile')
  return if File.exist?(gemfile) && File.read(gemfile).match?(/^\s*ruby\s/)

  inject_into_file 'Gemfile', "\nruby file: '.ruby-version'\n", after: /^source .*$/
end

#show_deploy_app_skill_pathObject



79
80
81
82
83
84
85
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 79

def show_deploy_app_skill_path
  # The deploy-app skill is one-time deployment setup, so it ships inside the
  # rolemodel-rails gem instead of being copied into the app. Point the user
  # at it so they can hand it to a coding agent when they're ready to deploy.
  say "\nWhen you're ready to deploy, point your coding agent at the deploy-app skill:", :green
  say File.expand_path('templates/deploy-app/SKILL.md', __dir__), :yellow
end

#use_database_url_in_productionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/rolemodel/heroku/heroku_generator.rb', line 30

def use_database_url_in_production
  say 'Point the production database at DATABASE_URL for Heroku.', :green

  # Rails' generated production block hardcodes database/username/<APP>_DATABASE_PASSWORD,
  # none of which exist on Heroku, where the Postgres add-on provides a full DATABASE_URL.
  # Replace the whole block with a url-based config.
  gsub_file 'config/database.yml',
            /^production:\n(?:[ \t]+.*\n?)+/,
            <<~YAML
              production:
                <<: *default
                url: <%= ENV["DATABASE_URL"] %>
            YAML
end