-
-
Save sundevilyang/3962466 to your computer and use it in GitHub Desktop.
Standard Rails Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# rails new myapp --template=path/to/this/file.rb | |
appname = File.expand_path(Dir.new('.')).split('/').last | |
#--------------- Gem setup | |
# Note this is mostly copied from puzzlenode | |
# | |
file 'Gemfile', <<_____ | |
source 'http://rubygems.org' | |
######## | |
# Core # | |
######## | |
gem 'rails', '~> 3.2.0' | |
gem 'omniauth-github', '~> 1.0.0' | |
######## | |
# Data # | |
######## | |
gem 'pg' | |
################ | |
# Presentation # | |
################ | |
gem 'haml' | |
gem 'sass' | |
gem 'will_paginate', '~> 3.0' | |
gem 'jquery-rails' | |
gem 'draper' | |
gem 'rainbow' | |
############### | |
# Maintenance # | |
############### | |
gem 'capistrano' | |
group :assets do | |
gem 'sass-rails', '~> 3.2.0' | |
gem 'coffee-rails', '~> 3.2.0' | |
gem 'uglifier' | |
gem 'compass', '~> 0.12.rc.1' | |
end | |
group 'test' do | |
gem 'minitest', '~> 2.11.2' | |
gem 'capybara', '~> 1.1.1' | |
gem 'factory_girl_rails' | |
gem 'colorific' | |
gem 'test_notifier' | |
end | |
group :production do | |
gem 'exception_notification' | |
end | |
_____ | |
#--------------- Database | |
file 'config/database.yml.example', %Q{ | |
development: | |
adapter: postgresql | |
database: #{appname}-devel | |
username: root | |
host: localhost | |
# Warning: The database defined as "test" will be erased and | |
# re-generated from your development database when you run "rake". | |
# Do not set this db to the same as development or production. | |
test: | |
adapter: postgresql | |
database: #{appname}-test | |
username: root | |
host: localhost | |
min_messages: error | |
production: | |
adapter: postgresql | |
database: #{appname}-production | |
username: root | |
host: localhost | |
min_messages: error | |
} | |
run 'rm config/database.yml' | |
append_file '.gitignore', 'config/database.yml' | |
#--------------- Auth | |
file 'config/initializers/omniauth.rb.example', <<_____ | |
# https://github.com/account/applications | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
provider :developer, fields: [:name, :email, :nickname] unless Rails.env.production? | |
provider :github, "consumer_key", "consumer_secret" | |
end | |
_____ | |
append_file '.gitignore', 'config/initializers/omniauth.rb' | |
#--------------- Setup | |
file 'lib/tasks/setup.rake', <<-CODE | |
require 'fileutils' | |
require 'securerandom' | |
desc 'Setup project for development / deploy' | |
task :setup do | |
section "Configuration Files" do | |
database = File.join(Rails.root, 'config', 'database.yml') | |
secret_token = File.join(Rails.root, 'config', 'initializers', 'secret_token.rb') | |
omniauth = File.join(Rails.root, 'config', 'initializers', 'omniauth.rb') | |
unless File.exists?(database) | |
create_file(database, "Database config", true) | |
else | |
puts "Database config file already exists" | |
end | |
unless File.exists?(secret_token) | |
secret = SecureRandom.hex(64) | |
template = ERB.new(File.read(secret_token + '.example')) | |
File.open(secret_token, 'w') {|f| f.write(template.result(binding)) } | |
puts "Secret Token Generated" | |
else | |
puts "Secret Token file already exists" | |
end | |
unless File.exists?(omniauth) | |
create_file(omniauth, "Omniauth config") | |
else | |
"Omniauth config file already exists" | |
end | |
end | |
section "Database" do | |
begin | |
# Check if there are pending migrations | |
silence { Rake::Task["db:abort_if_pending_migrations"].invoke } | |
puts "Skip: Database already setup" | |
rescue Exception | |
silence do | |
Rake::Task["db:create"].invoke | |
Rake::Task["db:schema:load"].invoke | |
end | |
puts "Database setup" | |
end | |
end | |
# Load the Rails Env now that the databases are setup | |
Rake::Task["environment"].invoke | |
section "Seed Data" do | |
Rake::Task["db:seed"].invoke | |
end | |
puts # Empty Line | |
puts "==== Setup Complete ====".color(:green) | |
puts # Empty Line | |
end | |
private | |
def section(description) | |
puts # Empty Line | |
puts description.underline | |
puts # Empty Line | |
yield | |
end | |
def silence | |
begin | |
orig_stderr = $stderr.clone | |
orig_stdout = $stdout.clone | |
$stderr.reopen File.new('/dev/null', 'w') | |
$stdout.reopen File.new('/dev/null', 'w') | |
return_value = yield | |
rescue Exception => e | |
$stdout.reopen orig_stdout | |
$stderr.reopen orig_stderr | |
raise e | |
ensure | |
$stdout.reopen orig_stdout | |
$stderr.reopen orig_stderr | |
end | |
return_value | |
end | |
def create_file(file, name, requires_edit=false) | |
FileUtils.cp(file + '.example', file) | |
puts "\#{name} file created".color(:green) | |
if requires_edit | |
puts "Update \#{file} and run `bundle exec rake setup` to continue".color(:red) | |
system(ENV['EDITOR'], file) unless ENV['EDITOR'].blank? | |
exit | |
end | |
end | |
CODE | |
#--------------- CSS | |
#--------------- Javascript | |
#--------------- default file deletion | |
run 'rm public/index.html' | |
run 'rm app/assets/images/rails.png' | |
run 'rm config/initializers/secret_token.rb' | |
initializer 'secret_token.rb.example', %Q{ | |
# Be sure to restart your server when you modify this file. | |
# Your secret key for verifying the integrity of signed cookies. | |
# If you change this key, all old signed cookies will become invalid! | |
# Make sure the secret is at least 30 characters and all random, | |
# no regular words or you'll be exposed to dictionary attacks. | |
#{appname.titleize}::Application.config.secret_token = '<%= secret %>' | |
} | |
append_file '.gitignore', 'secret_token.rb' | |
#--------------- bundle install | |
run 'bundle install' | |
#--------------- after bundler actions | |
#--------------- git commit | |
git :init | |
git :add => '.' | |
git :commit => "-a -m 'Initial commit, application generated from #{__FILE__}'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment