Skip to content

Commit

Permalink
Add Puma to Heroku Deployment generator and disable generator by default
Browse files Browse the repository at this point in the history
  • Loading branch information
robwise committed Nov 15, 2015
1 parent 36b97db commit 5ca8337
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -203,10 +203,11 @@ Usage:
rails generate react_on_rails:install [options]
Options:
-R, [--redux], [--no-redux] # Install Redux gems and Redux version of Hello World Example
-S, [--server-rendering], [--no-server-rendering] # Add necessary files and configurations for server-side rendering
-j, [--skip-js-linters], [--no-skip-js-linters] # Skip installing JavaScript linting files
-L, [--ruby-linters], [--no-ruby-linters] # Install ruby linting files, tasks, and configs
-R, [--redux], [--no-redux] # Install Redux gems and Redux version of Hello World Example
-S, [--server-rendering], [--no-server-rendering] # Add necessary files and configurations for server-side rendering
-j, [--skip-js-linters], [--no-skip-js-linters] # Skip installing JavaScript linting files
-L, [--ruby-linters], [--no-ruby-linters] # Install ruby linting files, tasks, and configs
-H, [--heroku-deployment], [--no-heroku-deployment] # Install files necessary for deploying to Heroku
Runtime options:
-f, [--force] # Overwrite files that already exist
Expand Down
6 changes: 4 additions & 2 deletions docs/additional_reading/heroku_deployment.md
@@ -1,10 +1,12 @@
# Heroku Deployment
The generator has created the necessary files and gems for deployment to Heroku. If you have installed manually, you will need to provide these files yourself:

+ `Procfile`: used by Heroku and Foreman to start the server
+ `Procfile`: used by Heroku and Foreman to start the Puma server
+ `.buildpacks`: used to install Ruby and Node environments
+ `12factor` gem: required by Heroku
+ `lib/tasks/assets.rake`: rake task that generates your JavaScript bundles for production.
+ `'puma'` gem: recommended Heroku webserver
+ `config/puma.rb`: Puma webserver config file
+ `lib/tasks/assets.rake`: This rake task file is provided by the generator regardless of whether the user chose Heroku Deployment as an option. It is highlighted here because it is helpful to understand that this task is what generates your JavaScript bundles in production.

## How to Deploy

Expand Down
14 changes: 11 additions & 3 deletions lib/generators/react_on_rails/heroku_deployment_generator.rb
Expand Up @@ -10,12 +10,20 @@ class HerokuDeploymentGenerator < Rails::Generators::Base

def copy_heroku_deployment_files
base_path = "heroku_deployment"
%w(.buildpacks Procfile).each { |file| copy_file("#{base_path}/#{file}", file) }
%w(.buildpacks
Procfile
config/puma.rb).each { |file| copy_file("#{base_path}/#{file}", file) }
end

def add_heroku_production_gems
production_gems = "# For Heroku deployment\ngem 'rails_12factor', group: :production\n"
append_to_file("Gemfile", production_gems)
gem_text = <<-GEMS.strip_heredoc
# For Heroku deployment
gem 'rails_12factor', group: :production
gem 'puma', group: :production
GEMS
append_to_file("Gemfile", gem_text)
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/generators/react_on_rails/install_generator.rb
Expand Up @@ -27,8 +27,14 @@ class InstallGenerator < Rails::Generators::Base
default: false,
desc: "Install ruby linting files, tasks, and configs",
aliases: "-L"
# --ruby-linters
class_option :heroku_deployment,
type: :boolean,
default: false,
desc: "Install files necessary for deploying to Heroku",
aliases: "-H"

def run_generators
def run_generators # rubocop:disable Metrics/CyclomaticComplexity
return unless installation_prerequisites_met?
warn_if_nvm_is_not_installed
invoke "react_on_rails:base"
Expand All @@ -37,7 +43,7 @@ def run_generators
invoke "react_on_rails:js_linters" unless options.skip_js_linters?
invoke "react_on_rails:ruby_linters" if options.ruby_linters?
invoke "react_on_rails:bootstrap"
invoke "react_on_rails:heroku_deployment"
invoke "react_on_rails:heroku_deployment" if options.heroku_deployment?
end

private
Expand Down
@@ -1 +1 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
web: bundle exec puma -C config/puma.rb
@@ -0,0 +1,15 @@
workers Integer(ENV["WEB_CONCURRENCY"] || 2)
threads_count = Integer(ENV["MAX_THREADS"] || 5)
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV["PORT"] || 3000
environment ENV["RACK_ENV"] || "development"

on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
22 changes: 10 additions & 12 deletions spec/react_on_rails/generators/install_generator_spec.rb
Expand Up @@ -10,7 +10,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -21,7 +20,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:disabled"
end

Expand All @@ -32,7 +30,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:disabled"
end

Expand All @@ -43,7 +40,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
include_examples "ruby_linters"
end
Expand All @@ -55,7 +51,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
include_examples "ruby_linters"
end
Expand All @@ -67,7 +62,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -78,7 +72,6 @@
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -89,7 +82,6 @@
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -100,7 +92,6 @@
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -111,7 +102,6 @@
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -122,7 +112,6 @@
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -133,7 +122,6 @@
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "heroku_deployment"
include_examples "js_linters:enabled"
end

Expand All @@ -143,4 +131,14 @@
assert_file("app/assets/stylesheets/application.scss")
end
end

context "--heroku-deployment" do
before(:all) { run_generator_test_with_args(%w(--heroku-deployment)) }
include_examples "heroku_deployment"
end

context "-H" do
before(:all) { run_generator_test_with_args(%w(-H)) }
include_examples "heroku_deployment"
end
end

0 comments on commit 5ca8337

Please sign in to comment.