Skip to content

Commit

Permalink
Add rails g ember-cli:heroku
Browse files Browse the repository at this point in the history
To configure your Ember CLI Rails app to be ready to deploy on Heroku:

1. Run `rails g ember-cli:heroku` generator
1. [Add the NodeJS buildpack][buildpack] and configure NPM to include the
   `bower` dependency's executable file.

```sh
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-nodejs
heroku config:set NPM_CONFIG_PRODUCTION=false
```

[buildpack]: https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app#adding-a-buildpack
  • Loading branch information
seanpdoyle committed Oct 30, 2015
1 parent 2dda73a commit 89bf511
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
master
------

* Introduce `rails g ember-cli:heroku` generator to setup a project for
deploying to Heroku
* Introduce `include_ember_index_html` helper

0.3.5
Expand Down
46 changes: 8 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,50 +301,20 @@ directories %w[app config lib spec your-appname/app]

## Heroku

In order to deploy Ember CLI Rails app to Heroku:
To configure your Ember CLI Rails app to be ready to deploy on Heroku:

First, enable Heroku Multi Buildpack by running the following command:
1. Run `rails g ember-cli:heroku` generator
1. [Add the NodeJS buildpack][buildpack] and configure NPM to include the
`bower` dependency's executable file.

```sh
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-nodejs
heroku config:set NPM_CONFIG_PRODUCTION=false
```

Next, specify which buildpacks to use by creating a `.buildpacks` file in the project root containing:
You should be ready to deploy.

```
https://github.com/heroku/heroku-buildpack-nodejs
https://github.com/heroku/heroku-buildpack-ruby
```

Add `rails_12factor` gem to your production group in Gemfile, then run `bundle
install`:

```ruby
gem "rails_12factor", group: :production
```

Add a `package.json` file containing `{}` to the root of your Rails project.
This is to make sure it'll be detected by the NodeJS buildpack.

Make sure you have `bower` as a npm dependency of your ember-cli app.

Add a `postinstall` task to your Ember CLI app's `package.json`. This will
ensure that during the deployment process, Heroku will install all dependencies
found in both `node_modules` and `bower_components`.

```javascript
{
# ...
"scripts": {
# ...
"postinstall": "node_modules/bower/bin/bower install"¬
}
}
```

ember-cli-rails adds your ember apps' build process to the rails asset compilation process.

Now you should be ready to deploy.
[buildpack]: https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app#adding-a-buildpack

## Capistrano

Expand Down
19 changes: 17 additions & 2 deletions lib/ember-cli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,23 @@ def compile
end

def install_dependencies
exec "#{bundler_path} install" if gemfile_path.exist?
exec "#{npm_path} install"
if gemfile_path.exist?
exec "#{bundler_path} install"
end

exec "#{npm_path} prune && #{npm_path} install"

if bower_path.nil?
fail <<-FAIL
Bower is required by EmberCLI.
Install it with:
$ npm install -g bower
FAIL
else
exec "#{bower_path} prune && #{bower_path} install"
end
end

def run
Expand Down
4 changes: 4 additions & 0 deletions lib/ember-cli/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def tee_path
@tee_path = Helpers.which("tee")
end

def bower_path
@bower_path ||= Helpers.which("bower")
end

def npm_path
@npm_path ||= Helpers.which("npm")
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ember-cli/path_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def initialize(app)
app_options.fetch(:tee_path){ configuration.tee_path }
end

define_path :bower do
app_options.fetch(:bower_path) { configuration.bower_path }
end

define_path :npm do
app_options.fetch(:npm_path){ configuration.npm_path }
end
Expand Down
9 changes: 9 additions & 0 deletions lib/generators/ember-cli/heroku/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Description:
Generates files necessary to deploy the project to Heroku

Example:
rails generate ember-cli:heroku

This will create:
.buildpacks
package.json
26 changes: 26 additions & 0 deletions lib/generators/ember-cli/heroku/heroku_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module EmberCLI
class HerokuGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)

namespace "ember-cli:heroku"

def copy_package_json_file
template "package.json.erb", "package.json"
end

def copy_setup_heroku_file
template "bin_heroku_install.erb", "bin/heroku_install"
run "chmod a+x bin/heroku_install"
end

def inject_12factor_gem
gem "rails_12factor", group: [:staging, :production]
end

def app_paths
EmberCLI.apps.values.map do |app|
app.root.relative_path_from(Rails.root)
end
end
end
end
13 changes: 13 additions & 0 deletions lib/generators/ember-cli/heroku/templates/bin_heroku_install.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env sh

set -e

bower="$(pwd)/node_modules/.bin/bower"

for app in <%= app_paths.map { |app_path| %{"#{app_path}"} }.join(" ") -%>; do
cd $app &&
npm prune &&
npm install &&
$bower prune &&
$bower install
done
15 changes: 15 additions & 0 deletions lib/generators/ember-cli/heroku/templates/package.json.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"scripts": {
"postinstall": "./bin/heroku_install"
},
"devDependencies": {
"bower": "*"
},
"cacheDirectories": [
<%- app_paths.each do |app_path| -%>
"<%= app_path %>/node_modules",
"<%= app_path %>/bower_components",
<%- end -%>
"node_modules"
]
}

0 comments on commit 89bf511

Please sign in to comment.