Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:00
Show Gist options
  • Save knomedia/2528c6d1054e71402a1f to your computer and use it in GitHub Desktop.
Save knomedia/2528c6d1054e71402a1f to your computer and use it in GitHub Desktop.
ember_cli_rails initial thoughts (see: for a more complete solution)
require 'nokogiri'
namespace :ember_cli_rails do
desc "compile ember and update application.html.erb with ENV vars"
task :build => [:ember, :update_env] do
desc 'Build ember app and copies css and js files to rails'
task :ember do
sh 'cd ember-app && ember build && cd ../'
sh 'rm app/assets/stylesheets/application.css'
sh 'rm app/assets/javascripts/application.js'
sh 'cp ember-app/dist/assets/app.css app/assets/stylesheets/application.css'
sh 'cp ember-app/dist/assets/app.js app/assets/javascripts/application.js'
desc "copy ember env from ember-cli to application.html.erb"
task :update_env do
app_file_path = 'app/views/layouts/application.html.erb'
tmp_file_path = 'app/views/layouts/application_template.html'
ember_file = 'ember-app/dist/index.html'
data =, "rb") {|io|}
doc = Nokogiri::HTML(data)
env_data = ''
doc.css('script').each do |s|
if s.content.match(/window\.ENV/)
env_data = s.content.strip
puts '!!!!!! No window.ENV found' if env_data == ''
app_file_data =, 'rb') {|io| }
app_file_data = app_file_data.gsub(/{{EMBER-ENV}}/, env_data), 'w') {|io| io.write(app_file_data) }
desc "create boiler plate application_template.html file to be used to generate application.html.erb"
task :application_template do
data = <<-CODE
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<base href="/" />
<%= stylesheet_link_tag "application", media: "all" %>
<%= csrf_meta_tags %>
<%= yield %>
<%# from ember-cli %>
<%# Your environment vars from ember-cli will go here %>
<%= javascript_include_tag "application" %>
<%# from ember-cli %>
window.EmberApp = require('ember-app/app')['default'].create(ENV.APP);
template_path = 'app/views/layouts/application_template.html', 'w') {|io| io.write data}
puts "created #{template_path}"
# This script is used with the Ruby on Rails' new project generator:
# rails new my_app -m path/to/this/ember_cli_template.rb
# For more information about the template API, please see the following Rails
# guide:
# assumes:
# npm installed
# npm install -g bower
# npm install -g ember-cli
# Install required gems
gem "active_model_serializers"
gem "nokogiri"
# kill un-needed gems
run "sed -i.bck '/turbolinks/d' Gemfile"
run "sed -i.bck '/coffee/d' Gemfile"
run "sed -i.bck '/jbuilder/d' Gemfile"
run "sed -i.bck '/jquery-rails/d' Gemfile"
run "bundle install"
# cleanup
run "rm Gemfile.bck"
# create ember-cli app
run "ember new ember-app"
run "cd ember-app && npm link ember-cli"
#don't track node_modules via git
run "echo '/ember-app/node_modules/*' >> .gitignore"
# build AssetsController and default to it
generate :controller, "Assets", "index"
run "rm app/views/assets/index.html.erb"
file "app/views/assets/index.html.erb", "<%# Ember Starting Point %>"
route "root 'assets#index'"
generate :serializer, "application", "--parent", "ActiveModel::Serializer"
inject_into_class "app/serializers/application_serializer.rb", "ApplicationSerializer" do
" embed :ids, :include => true\n"
# pull rake tasks
run "curl -o lib/tasks/ember_cli_rails.rake"
#generate application_template, and build ember app
run "bin/rake ember_cli_rails:application_template"
run "bin/rake ember_cli_rails:build"
puts <<-MESSAGE
Your ember-cli app is located at: 'ember-app'
AssetsController#index is the root route to your ember app
Make changes to app/views/layouts/application_template.html
To build the ember project run:
`bin/rails ember_cli_rails:build`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment