Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# RAILSLOVE.com template
#
# with help and ideas from:
# http://gist.github.com/33337 By Peter Cooper
# http://github.com/jeremymcanally/rails-templates/tree/master/suspenders.rb Suspenders by Thoughtbot Nathan Esquenazi
if yes?("symlink local rails copy to vendor?")
path = ask("what's the directory of your local rails copy?")
inside('vendor') { run "ln -s #{path} rails" }
end
submodules = yes?("Do you want to add plugins as submodules?")
# init the git repository
# files added
git :init
# ---------------------------------
# gems
# ---------------------------------
gem 'thoughtbot-factory_girl', :lib => 'factory_girl', :source => 'http://gems.github.com'
gem 'thoughtbot-shoulda', :lib => 'shoulda', :source => 'http://gems.github.com'
gem 'ruby-openid', :lib => 'openid'
gem 'mislav-will_paginate', :lib => 'will_paginate', :source => 'http://gems.github.com'
gem 'mocha'
# ---------------------------------
# plugins
# ---------------------------------
plugin 'find_by_param', :git => 'git://github.com/bumi/find_by_param.git', :submodule => submodules
plugin 'redirect_love', :git => 'git://github.com/bumi/redirect_love.git', :submodule => submodules
plugin 'serialize_fu', :git => 'git://github.com/bumi/serializefu.git', :submodule => submodules
plugin 'limerick_rake', :git => "git://github.com/thoughtbot/limerick_rake.git", :submodule => submodules
plugin 'hoptoad_notifier', :git => "git://github.com/thoughtbot/hoptoad_notifier.git", :submodule => submodules
plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git', :submodule => submodules
plugin 'squirrel', :git => "git://github.com/thoughtbot/squirrel.git", :submodule => submodules
plugin 'aasm', :git => 'git://github.com/rubyist/aasm.git', :submodule => submodules
plugin 'annotate_models', :git => 'git://github.com/ctran/annotate_models.git', :submodule => submodules
# ---------------------------------
# we don't need that
# ---------------------------------
run "rm README && touch README"
run "rm public/index.html"
# ---------------------------------
# we love edge rails
# ---------------------------------
freeze!
# ---------------------------------
# initializers
# ---------------------------------
# mail
initializer 'mail.rb', <<-END
ActionMailer::Base.smtp_settings = {
:address => "smtp.mailer.com",
:port => 25,
:domain => "mailer.com",
:password => ""
}
ActionMailer::Base.default_url_options[:host] = ""
END
# exceptions love hoptoad or getexceptional? don't know yet
initializer 'hoptoad.rb',<<-END
HoptoadNotifier.configure do |config|
config.api_key = ''
end
END
# require extensions
initializer 'requires.rb', <<-END
Dir[File.join(RAILS_ROOT, 'lib', 'extensions', '**/*.rb')].each do |file|
require file
end
Dir[File.join(RAILS_ROOT, 'lib', 'fixes', '**/*.rb')].each do |file|
require file
end
END
# ---------------------------------
# Config
# ---------------------------------
# not sure if that's really needed.
# add session config to environment.rb
in_root do
gsub_file 'config/environment.rb', /(#{Regexp.escape('Rails::Initializer.run do |config|')})/mi do |match|
<<-END
#{match}\n
config.action_controller.session = { :session_key => '_#{(1..6).map { |x| (65 + rand(26)).chr }.join}_session', :secret => '#{(1..40).map { |x| (65 + rand(26)).chr }.join}' }
config.action_controller.session_store = :active_record_store
END
end
end
# we love factories
inside ('test') do
run "mkdir factories"
end
# folders for extensions and fixes
inside ('lib') do
run "mkdir extensions"
run "mkdir fixes"
end
# ---------------------------------
# common files
# ---------------------------------
# get application_controller started
file 'app/controllers/application_controller.rb',
<<-END
class ApplicationController < ActionController::Base
include HoptoadNotifier::Catcher
helper :all
protect_from_forgery
filter_parameter_logging :password
end
END
# add partial for flash messages
file 'app/views/layouts/_flash_messages.html.erb',
<<-END
<% %w{error notice ok}.each do |type| %>
<% unless flash[type.to_sym].blank? %>
<div class="flash <%= type %>">
<% if flash[type.to_sym].is_a?(ActiveRecord::Errors) %>
<%= error_messages_for(flash[type.to_sym]) %>
<% else %>
<%= flash[type.to_sym] %>
<% end %>
</div>
<% end %>
<% end %>
END
# ---------------------------------
# Deploy scripts
# ---------------------------------
capify!
file 'config/deploy.rb', %q{
set :stages, %w(staging production)
set :default_stage, 'staging'
require 'capistrano/ext/multistage'
namespace :deploy do
# from thoughtbot
desc "Default deploy - updated to run migrations"
task :default do
set :migrate_target, :latest
update_code
migrate
symlink
restart
end
end
desc "tail log files"
task :tail_log, :roles => :app do
run "tail -f #{shared_path}/log/#{ENV['RAILS_ENV']}.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end
desc "remotely console"
task :console, :roles => :app do
input = ''
run "cd #{current_path} && ./script/console #{ENV['RAILS_ENV']}" do |channel, stream, data|
next if data.chomp == input.chomp || data.chomp == ''
print data
channel.send_data(input = $stdin.gets) if data =~ /^(>|\?)>/
end
end
task :ssh do
#sorry I don't know how to make this nice and DRY... *shame on me*
don_ips = []
# if you enter an role - just connect to these servers
role = Capistrano::CLI.ui.ask("Role: (leave blank to connect to all server)")
if not role.empty?
roles[role.to_sym].each do |server|
if don_ips.include?(server.host)
puts("we've already hacked on #{server.host}")
next
end
puts server.host
system("ssh #{user}@#{server.host}")
don_ips << server.host
sleep 1
end
# else connect to all servers
else
roles.each do |environment,settings|
puts "----------------------"
puts "#{environment.to_s.capitalize}:"
settings.each do |server|
if don_ips.include?(server.host)
puts("we've already hacked on #{server.host}")
next
end
puts server.host
system("ssh #{user}@#{server.host}")
don_ips << server.host
sleep 1
end
end
end
end
}
file 'config/deploy/staging.rb',%q{
set :rails_env, 'staging'
set :application, ''
set :repository, "git@github.com:user/#{application}.git"
set :scm, "git"
set :deploy_via, :remote_cache
set :branch, "staging"
set :user, "#{application}"
set :ssh_options, {:forward_agent => true}
on :start do
`ssh-add`
end
role :web, "staging.example.com"
role :app, "staging.example.com"
role :db, "staging.example.com", :primary => true
}
file 'config/deploy/production.rb', %q{
set :rails_env, 'production'
set :application, ''
set :repository, "git@github.com:user/#{application}.git"
set :scm, "git"
set :deploy_via, :remote_cache
set :branch, "staging"
set :user, "#{application}"
set :ssh_options, {:forward_agent => true}
on :start do
`ssh-add`
end
role :web, "production.example.com"
role :app, "production.example.com"
role :db, "production.example.com", :primary => true
}
run "cp config/database.yml config/database.yml.example"
file '.gitignore', <<-END
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
*.pid
END
# ---------------------------------
# find empty filders and add a .gitignore # thanks Peter Cooper
# ---------------------------------
run "touch tmp/.gitignore log/.gitignore"
run 'find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;'
# ---------------------------------
# run installer and generator
# ---------------------------------
rake('gems:install', :sudo => true)
rake('gems:unpack')
generate("authenticated", "user session")
rake('db:sessions:create')
# ---------------------------------
# generate your resources
# ---------------------------------
if yes?("Want to generate some resources?")
resources = ask("Which resources? (comma-seperated list)")
resources.split(",").each do |resource|
generate(:resource, resource)
end
end
git :add => '.'
if submodules
git :submodule => "init"
end
git :commit => "-a -m 'initial commit'"
puts "DONE"
puts "Happy coding!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment