Skip to content

Instantly share code, notes, and snippets.

@jaryl
Created December 9, 2011 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaryl/1452661 to your computer and use it in GitHub Desktop.
Save jaryl/1452661 to your computer and use it in GitHub Desktop.
Rails 3.1 Templater
# set up git and perform initial commit
git :init
append_file '.gitignore' do
<<-EOS
*.rbc
*.sassc
.sass-cache
capybara-*.html
.rspec
/.bundle
/vendor/bundle
/log/*
/tmp/*
/db/*.sqlite3
/public/system/*
/coverage/
/spec/tmp/*
**.orig
rerun.txt
pickle-email-*.html
EOS
end
git :add => "."
git :commit => "-a -m 'initial commit'"
# set up gems
gem 'compass'
gem 'animate-sass'
gem 'haml-rails'
gem 'html5-boilerplate'
gem 'formtastic'
gem 'mailcatcher', :group => :development
include_tests = ask("Include gems for testing? (Y for yes)")
if include_tests == "Y"
gem 'minitest', :group => [:development, :test]
gem 'rspec-rails', :group => [:development, :test]
gem 'shoulda-matchers', :group => [:development, :test]
gem 'cucumber-rails', :group => [:development, :test]
gem 'capybara', :group => [:development, :test]
gem 'factory_girl_rails', :group => [:development, :test]
gem 'spork', :group => [:development, :test]
gem 'simplecov', :group => :test, :require => false
gem 'database_cleaner', :group => :test, :require => false
gem 'guard', :group => [:development, :test]
gem 'guard-spork', :group => [:development, :test]
gem 'guard-cucumber', :group => [:development, :test]
gem 'guard-rspec', :group => [:development, :test]
end
include_auth = ask("Include gems for authentication and authorization? (Y for yes)")
if include_auth == "Y"
gem 'sorcery'
gem 'cancan'
gem 'role_model'
end
# install stuff
run "bundle install"
git :add => "."
git :commit => "-a -m 'bundled template gems'"
run "compass init rails -r html5-boilerplate -u html5-boilerplate --force"
generate 'formtastic:install'
inject_into_file 'config/compass.rb', :before => "# Require any additional compass plugins here." do
<<-RUBY
require 'html5-boilerplate'
require 'animate-sass'
RUBY
end
inject_into_file 'config/application.rb', "\nrequire 'sass-rails'", :after => "require 'rails/all'"
inject_into_file 'config/application.rb', :before => " end\nend" do
<<-'RUBY'
# Configure load paths for sass
config.sass.load_paths ||= []
config.sass.load_paths << "#{Gem.loaded_specs['html5-boilerplate'].full_gem_path}/stylesheets"
config.sass.load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"
config.sass.load_paths << "#{Gem.loaded_specs['animate-sass'].full_gem_path}/stylesheets"
config.sass.load_paths << "#{Rails.root}/app/assets/stylesheets/"
RUBY
end
git :add => "."
git :commit => "-a -m 'configuration for compass, boilerplate, animate-sass and formtastic'"
# install stuff for authentication/authorization
if include_auth == "Y"
generate 'cancan:ability'
user_model_class = ask("User model class [User]")
user_model_class = "User" if user_model_class.blank?
submodules = ask("submodules for sorcery (remember_me, reset_passowrd, activity_logging, session_timeout, brute_force_protection)")
generate "sorcery:install #{submodules} --model #{user_model_class}"
git :add => "."
git :commit => "-a -m 'installation for sorcery and cancan'"
end
# set up for testing frameworks
if include_tests == "Y"
generate 'rspec:install'
generate 'cucumber:install --capybara --rspec'
# wire up rspec to work with other frameworks
inject_into_file 'spec/spec_helper.rb', :after => "require 'rspec/autorun'" do
<<-RUBY
require 'factory_girl'
require 'simplecov'
RUBY
end
# wire up cucumber to work with other frameworks
inject_into_file 'features/support/env.rb', :after => "require 'cucumber/rails'" do
<<-RUBY
require 'cucumber/rails/rspec'
require 'capybara/rails'
require 'factory_girl/step_definitions'
require 'simplecov'
RUBY
end
# set up spork (for a fast test suite) and guard (for automated testing)
run "spork --bootstrap"
run "spork cucumber --bootstrap"
run "guard init"
run "guard init spork"
run "guard init rspec"
run "guard init cucumber"
gsub_file 'config/application.rb', 'config.cache_classes = false', 'config.cache_classes = true'
# configure simplecov for both rspec and cucumber
create_file '.simplecov' do
<<-RUBY
require 'simplecov'
SimpleCov.start 'rails'
RUBY
end
# configure rspec to work with spork
inject_into_file '.rspec', ' --drb', :after => '--colour'
# add sorcery helpers if authentication is included
if include_auth == "Y"
inject_into_file 'spec/spec_helper.rb', :after => "require 'simplecov'" do
<<-RUBY
include Sorcery::TestHelpers::Rails
RUBY
end
inject_into_file 'features/support/env.rb', :after => "require 'simplecov'" do
<<-RUBY
include Sorcery::TestHelpers::Rails
RUBY
end
end
git :add => "."
git :commit => "-a -m 'set up for cucumber, rspec, capybara, factory_girl, spork, guard, simplecov'"
end
# configure application files
inject_into_file 'config/environments/development.rb', :after => "config.action_controller.perform_caching = false" do
<<-RUBY
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :host => "localhost", :port => 1025 }
RUBY
end
git :add => "."
git :commit => "-a -m 'configuration for mailcatcher in development'"
gsub_file 'config/application.rb', /:password\]/, ':password, :password_confirmation]'
gsub_file 'config/application.rb', /# config.autoload_paths \+\= %W\(\#\{config.root\}\/extras\)/, 'config.autoload_paths += %W(
#{config.root}/lib
#{config.root}/app/presenters
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)'
inject_into_file 'config/application.rb', :before => " end\nend" do
<<-RUBY
# Configure generators to generate haml files
config.generators do |g|
g.stylesheet_engine :scss
g.template_engine :haml
g.form_builder :formtastic
end
RUBY
end
if include_tests == "Y"
inject_into_file 'config/application.rb', :after => "g.form_builder :formtastic" do
<<-RUBY
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.test_framework :rspec, :fixture => true, :views => false
RUBY
end
end
git :add => "."
git :commit => "-a -m 'smart application paths and generator configuration'"
# remove unecessary files & shuffle files around
remove_file 'public/index.html'
remove_file 'public/favicon.ico'
remove_file 'public/images/rails.png'
remove_file 'app/assets/images/rails.png'
remove_file 'app/views/layouts/application.html.erb'
remove_file 'README'
run 'mv app/stylesheets/* app/assets/stylesheets/'
run 'mkdir lib/assets/javascripts'
run 'mv public/javascripts/jquery.js lib/assets/javascripts/jquery.js'
run 'mv public/javascripts/jquery.min.js lib/assets/javascripts/jquery.min.js'
run 'mv public/javascripts/modernizr.min.js lib/assets/javascripts/modernizr.min.js'
run 'mv public/javascripts/rails.js lib/assets/javascripts/rails.js'
run 'mv public/javascripts/respond.min.js lib/assets/javascripts/respond.min.js'
run 'mv public/javascripts/plugins.js app/assets/javascripts/plugins.js'
git :add => "."
git :commit => "-a -m 'removed useless files and rearrange static files to suit asset pipeline'"
# add authorization/authentication?
create_file 'README.md' do
<<-EOS
The intended audience for this README are system administrators or developers that may need to deploy the project. It is a quick and dirty guide to setting things up.
# Start Here
Install the necessary gems and run the migrations:
bundle install
rake db:migrate
The project provides an .rvmrc for use with [rvm](https://rvm.beginrescueend.com/), so set up a `#{@app_name}` gemset like so:
rvm gemset create #{@app_name}
If you are going to check for emails sent out by the app, run [mailcatcher](https://github.com/sj26/mailcatcher) like so:
mailcatcher
Once it is running, point your browser to: http://localhost:1080/
You should be good to go, now start the server:
rails server
EOS
end
if include_tests == "Y"
append_file 'README.md' do
<<-EOS
# Running Tests
This project's test suite uses:
* [cucumber](https://github.com/cucumber/cucumber)
* [rspec](https://github.com/rspec/rspec)
* [factory_girl](https://github.com/thoughtbot/factory_girl)
* [capybara](https://github.com/jnicklas/capybara)
* [shoulda](https://github.com/thoughtbot/shoulda)
The test suite is managed by [guard](https://github.com/guard/guard), with the following guards in place:
* spork
* cucumber
* rspec
The [spork](https://github.com/sporkrb/spork) gem keeps thing fast by preloading code so that tests need not reload the entire rails stack each time. However, in cases where these codes need to be reloaded, guard reloads spork when needed.
To start guard:
guard
In order to generate the correct test coverage reports, close guard and run the test suite manually, like so:
rspec .
bundle exec cucumber
To view the [simplecov](https://github.com/colszowka/simplecov) report:
open coverage/index.html
EOS
end
end
git :add => "."
git :commit => "-a -m 'update readme'"
# First, find out where rvm is installed
rvm_path = File.expand_path(ENV['rvm_path'] || '~/.rvm')
# Secondly, add the ruby library to the load path
$LOAD_PATH.unshift File.join(rvm_path, 'lib')
require 'rvm'
# check command-line arguments
rvm_ruby = ARGV[0]
app_name = ARGV[1]
unless rvm_ruby
puts "\n You need to specify a which rvm ruby to use."
end
unless app_name
puts "\n You need to name your app."
end
# set up environment
RVM.use_from_path! File.dirname(File.dirname(__FILE__))
@env = RVM.environment(rvm_ruby)
# create and use gemset
puts "Creating gemset #{app_name} in #{rvm_ruby}"
puts "Successfully created gemset #{app_name}" if @env.gemset_create(app_name)
puts "Using gemset #{app_name}" if @env.gemset_use! app_name
# install bundler and rails
system("gem install bundler")
system("gem install rails")
# create an .rvmrc
system("echo 'rvm use #{rvm_ruby}@#{app_name}' >> .rvmrc")
# generate the app using the template
template_file = File.join(File.expand_path(File.dirname(__FILE__)), 'template.rb')
system("rails new #{app_name} -JT -m #{template_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment