Class: DiscoApp::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- DiscoApp::Generators::InstallGenerator
- Defined in:
- lib/generators/disco_app/install/install_generator.rb
Instance Method Summary collapse
-
#add_env_variables ⇒ Object
Add entries to .env and .env.local.
-
#bundle_install ⇒ Object
Run bundle install to add our new gems before running tasks.
-
#configure_application ⇒ Object
Make any required adjustments to the application configuration.
-
#configure_gemfile ⇒ Object
Configure the application’s Gemfile.
- #configure_rspec ⇒ Object
-
#copy_and_remove_files ⇒ Object
Copy template files to the appropriate location.
-
#copy_root_files ⇒ Object
Copy a number of template files to the top-level directory of our application:.
-
#create_database ⇒ Object
Create PG database.
-
#install_migrations ⇒ Object
Copy engine migrations over.
-
#migrate ⇒ Object
Run migrations.
-
#remove_root_files ⇒ Object
Remove a number of root files.
-
#run_generators ⇒ Object
Run generators.
-
#set_ruby_version ⇒ Object
Lock down the application to a specific Ruby version:.
-
#setup_routes ⇒ Object
Set up routes.
- #support_staging_environment ⇒ Object
-
#update_database_config ⇒ Object
copy template for pg configuration.
Instance Method Details
#add_env_variables ⇒ Object
Add entries to .env and .env.local
193 194 195 196 197 198 199 200 201 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 193 def add_env_variables configuration = "\n MAILGUN_API_KEY=\n MAILGUN_API_DOMAIN=\n CONFIG\n append_to_file '.env', configuration\n append_to_file '.env.local', configuration\nend\n".strip_heredoc |
#bundle_install ⇒ Object
Run bundle install to add our new gems before running tasks.
92 93 94 95 96 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 92 def bundle_install Bundler.with_clean_env do run 'bundle install' end end |
#configure_application ⇒ Object
Make any required adjustments to the application configuration.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 103 def configure_application # The force_ssl flag is commented by default for production. # Uncomment to ensure config.force_ssl = true in production. uncomment_lines 'config/environments/production.rb', /force_ssl/ # Set time zone to UTC application "config.time_zone = 'UTC'" application '# Ensure UTC is the default timezone' # Set server side rendereing for components.js application " # Enable server side react rendering\n config.react.server_renderer_options = {\n # files to load for prerendering\n files: ['components.js']\n }\n CONFIG\n\n # Set defaults for various charge attributes.\n application \"config.x.shopify_charges_default_trial_days = 14\\n\"\n application 'config.x.shopify_charges_default_price = 10.00'\n application 'config.x.shopify_charges_default_type = :recurring'\n application '# Set defaults for charges created by the application'\n\n # Set the \"real charges\" config variable to false explicitly by default.\n # Only in production do we read from the environment variable and\n # potentially have it become true.\n application \"config.x.shopify_charges_real = false\\n\"\n application '# Explicitly prevent real charges being created by default'\n application \"config.x.shopify_charges_real = ENV['SHOPIFY_CHARGES_REAL'] == 'true'\\n\", env: :production\n application '# Allow real charges in production with an ENV variable', env: :production\n\n # Configure session storage.\n application \"ActiveRecord::SessionStore::Session.table_name = 'disco_app_sessions'\"\n application 'ActionDispatch::Session::ActiveRecordStore.session_class = DiscoApp::Session'\n application '# Configure custom session storage'\n\n # Set Sidekiq as the queue adapter in production.\n application \"config.active_job.queue_adapter = :sidekiq\\n\", env: :production\n application '# Use Sidekiq as the active job backend', env: :production\n\n # Set Sidekiq as the queue adapter in staging.\n application \"config.active_job.queue_adapter = :sidekiq\\n\", env: :staging\n application '# Use Sidekiq as the active job backend', env: :staging\n\n # Ensure the application configuration uses the DEFAULT_HOST environment\n # variable to set up support for reverse routing absolute URLS (needed when\n # generating Webhook URLs for example).\n application \"routes.default_url_options[:host] = ENV['DEFAULT_HOST']\\n\"\n application '# Set the default host for absolute URL routing purposes'\n\n # Configure React in development, staging and production.\n application 'config.react.variant = :development', env: :development\n application '# Use development variant of React in development.', env: :development\n application 'config.react.variant = :production', env: :staging\n application '# Use production variant of React in staging.', env: :staging\n application 'config.react.variant = :production', env: :production\n application '# Use production variant of React in production.', env: :production\n\n # Configure Factory Bot as the Rails testing fixture replacement\n application <<~CONFIG\n config.generators do |g|\n g.test_framework :rspec, fixtures: true, view_specs: false, helper_specs: false, routing_specs: false\n g.fixture_replacement :factory_bot, dir: 'spec/factories'\n end\n CONFIG\n\n # Copy over the default puma configuration.\n copy_file 'config/puma.rb', 'config/puma.rb'\n\n # Mail configuration\n configuration = <<-CONFIG.strip_heredoc\n\n # Configure ActionMailer to use MailGun\n if ENV['MAILGUN_API_KEY']\n config.action_mailer.delivery_method = :mailgun\n config.action_mailer.mailgun_settings = {\n api_key: ENV['MAILGUN_API_KEY'],\n domain: ENV['MAILGUN_API_DOMAIN']\n }\n end\n CONFIG\n application configuration, env: :production\n application configuration, env: :staging\n\n # Monitoring configuration\n copy_file 'config/appsignal.yml', 'config/appsignal.yml'\nend\n" |
#configure_gemfile ⇒ Object
Configure the application’s Gemfile.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 30 def configure_gemfile # Remove sqlite. gsub_file 'Gemfile', /^# Use sqlite3 as the database for Active Record\ngem 'sqlite3'/m, '' # Add gem requirements. gem 'active_link_to' gem 'activeresource' gem 'acts_as_singleton' gem 'appsignal' gem 'classnames-rails' gem 'nokogiri' gem 'oj' gem 'pg' gem 'premailer-rails' gem 'react-rails' gem 'render_anywhere' gem 'shopify_app' gem 'sidekiq' gem 'timber', '~> 3.0' gem 'timber-rails', '~> 1.0' # Indicate which gems should only be used in production. gem_group :production do gem 'mailgun_rails' gem 'rails_12factor' end # Indicate which gems should only be used in development. gem_group :production do gem 'rb-readline' end gem_group :development do gem 'rubocop' gem 'rubocop-performance' gem 'rubocop-rails' end # Indicate which gems should only be used in development and test. gem_group :development, :test do gem 'coveralls' gem 'dotenv-rails' gem 'factory_bot_rails' gem 'faker' gem 'mechanize' gem 'rspec-rails' gem 'vcr' gem 'webmock' end gem_group :test do gem 'database_cleaner' gem 'shoulda-matchers' end end |
#configure_rspec ⇒ Object
215 216 217 218 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 215 def configure_rspec directory 'spec' copy_file 'root/.rspec', '.rspec' end |
#copy_and_remove_files ⇒ Object
Copy template files to the appropriate location. In some cases, we’ll be overwriting or removing existing files or those created by ShopifyApp.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 222 def copy_and_remove_files # Copy initializers copy_file 'initializers/shopify_app.rb', 'config/initializers/shopify_app.rb' copy_file 'initializers/disco_app.rb', 'config/initializers/disco_app.rb' copy_file 'initializers/shopify_session_repository.rb', 'config/initializers/shopify_session_repository.rb' copy_file 'initializers/session_store.rb', 'config/initializers/session_store.rb' copy_file 'initializers/timber.rb', 'config/initializers/timber.rb' # Copy default home controller and view copy_file 'controllers/home_controller.rb', 'app/controllers/home_controller.rb' copy_file 'views/home/index.html.erb', 'app/views/home/index.html.erb' # Copy assets copy_file 'assets/javascripts/application.js', 'app/assets/javascripts/application.js' copy_file 'assets/javascripts/components.js', 'app/assets/javascripts/components.js' copy_file 'assets/stylesheets/application.scss', 'app/assets/stylesheets/application.scss' # Remove application.css remove_file 'app/assets/stylesheets/application.css' # Remove the layout files created by ShopifyApp remove_file 'app/views/layouts/application.html.erb' remove_file 'app/views/layouts/embedded_app.html.erb' # Remove the test directory generated by rails new remove_dir 'test' end |
#copy_root_files ⇒ Object
Copy a number of template files to the top-level directory of our application:
- .env and .env.local for settings environment variables in development with dotenv-rails;
- Slightly customised version of the default Rails .gitignore;
- Default simple Procfile for Heroku;
- .editorconfig to help enforce 2-space tabs, newlines and truncated whitespace for editors that support it.
- README/PULL REQUEST template
15 16 17 18 19 20 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 15 def copy_root_files %w[.editorconfig .env .env.local .gitignore .rubocop.yml Procfile CHECKS README.md].each do |file| copy_file "root/#{file}", file end directory 'root/.github' end |
#create_database ⇒ Object
Create PG database
256 257 258 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 256 def create_database rake 'db:create' end |
#install_migrations ⇒ Object
Copy engine migrations over.
251 252 253 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 251 def install_migrations rake 'disco_app:install:migrations' end |
#migrate ⇒ Object
Run migrations.
261 262 263 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 261 def migrate rake 'db:migrate' end |
#remove_root_files ⇒ Object
Remove a number of root files.
23 24 25 26 27 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 23 def remove_root_files %w[README.rdoc].each do |file| remove_file file end end |
#run_generators ⇒ Object
Run generators.
209 210 211 212 213 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 209 def run_generators generate 'shopify_app:install' generate 'shopify_app:home_controller' generate 'react:install' end |
#set_ruby_version ⇒ Object
Lock down the application to a specific Ruby version:
- Via .ruby-version file for rbenv in development;
- Via a Gemfile line in production.
This should be the last operation, to allow all other operations to run in the initial Ruby version.
271 272 273 274 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 271 def set_ruby_version copy_file 'root/.ruby-version', '.ruby-version' prepend_to_file 'Gemfile', "ruby '2.5.0'\n" end |
#setup_routes ⇒ Object
Set up routes.
204 205 206 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 204 def setup_routes route "mount DiscoApp::Engine, at: '/'" end |
#support_staging_environment ⇒ Object
98 99 100 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 98 def support_staging_environment copy_file 'config/environments/staging.rb', 'config/environments/staging.rb' end |
#update_database_config ⇒ Object
copy template for pg configuration
87 88 89 |
# File 'lib/generators/disco_app/install/install_generator.rb', line 87 def update_database_config template 'config/database.yml.tt' end |