Skip to content

Instantly share code, notes, and snippets.

@knomedia
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: https://github.com/knomedia/ember-cli-rails 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
end
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'
end
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 = File.open(ember_file, "rb") {|io| io.read}
doc = Nokogiri::HTML(data)
env_data = ''
doc.css('script').each do |s|
if s.content.match(/window\.ENV/)
env_data = s.content.strip
break
end
end
puts '!!!!!! No window.ENV found' if env_data == ''
app_file_data = File.open(tmp_file_path, 'rb') {|io| io.read }
app_file_data = app_file_data.gsub(/{{EMBER-ENV}}/, env_data)
File.open(app_file_path, 'w') {|io| io.write(app_file_data) }
end
desc "create boiler plate application_template.html file to be used to generate application.html.erb"
task :application_template do
data = <<-CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>ember_cli_rails</title>
<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 %>
</head>
<body>
<%= yield %>
<%# from ember-cli %>
<script>
<%# Your environment vars from ember-cli will go here %>
{{EMBER-ENV}}
</script>
<%= javascript_include_tag "application" %>
<%# from ember-cli %>
<script>
window.EmberApp = require('ember-app/app')['default'].create(ENV.APP);
</script>
</body>
</html>
CODE
template_path = 'app/views/layouts/application_template.html'
File.open(template_path, 'w') {|io| io.write data}
puts "created #{template_path}"
end
end
# 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:
#
# http://edgeguides.rubyonrails.org/rails_application_templates.html
# 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"
end
# pull rake tasks
run "curl -o lib/tasks/ember_cli_rails.rake https://gist.githubusercontent.com/knomedia/2528c6d1054e71402a1f/raw/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'
see: https://github.com/stefanpenner/ember-cli
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`
***********************************************************
MESSAGE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment