Created
July 17, 2011 03:39
-
-
Save nikhgupta/1087122 to your computer and use it in GitHub Desktop.
Default template for new Rails applications
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
initializer 'generators.rb', <<-RUBY | |
Rails.application.config.generators do |g| | |
end | |
RUBY | |
@recipes = ["activerecord", "cucumber", "devise", "git", "haml", "heroku", "jammit", "jquery", "rails_admin", "sass", "settingslogic"] | |
def recipes; @recipes end | |
def recipe?(name); @recipes.include?(name) end | |
def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end | |
def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end | |
def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end | |
def ask_wizard(question) | |
ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m" | |
end | |
def yes_wizard?(question) | |
answer = ask_wizard(question + " \033[33m(y/n)\033[0m") | |
case answer.downcase | |
when "yes", "y" | |
true | |
when "no", "n" | |
false | |
else | |
yes_wizard?(question) | |
end | |
end | |
def no_wizard?(question); !yes_wizard?(question) end | |
def multiple_choice(question, choices) | |
say_custom('question', question) | |
values = {} | |
choices.each_with_index do |choice,i| | |
values[(i + 1).to_s] = choice[1] | |
say_custom (i + 1).to_s + ')', choice[0] | |
end | |
answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer) | |
values[answer] | |
end | |
@current_recipe = nil | |
@configs = {} | |
@after_blocks = [] | |
def after_bundler(&block); @after_blocks << [@current_recipe, block]; end | |
@after_everything_blocks = [] | |
def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end | |
@before_configs = {} | |
def before_config(&block); @before_configs[@current_recipe] = block; end | |
# >-----------------------------[ ActiveRecord ]------------------------------< | |
@current_recipe = "activerecord" | |
@before_configs["activerecord"].call if @before_configs["activerecord"] | |
say_recipe 'ActiveRecord' | |
config = {} | |
config['database'] = multiple_choice("Which database are you using?", [["MySQL", "mysql"], ["Oracle", "oracle"], ["PostgreSQL", "postgresql"], ["SQLite", "sqlite3"], ["Frontbase", "frontbase"], ["IBM DB", "ibm_db"]]) if true && true unless config.key?('database') | |
config['auto_create'] = yes_wizard?("Automatically create database with default configuration?") if true && true unless config.key?('auto_create') | |
@configs[@current_recipe] = config | |
if config['database'] | |
say_wizard "Configuring '#{config['database']}' database settings..." | |
old_gem = gem_for_database | |
@options = @options.dup.merge(:database => config['database']) | |
gsub_file 'Gemfile', "gem '#{old_gem}'", "gem '#{gem_for_database}'" | |
template "config/databases/#{@options[:database]}.yml", "config/database.yml.new" | |
run 'mv config/database.yml.new config/database.yml' | |
end | |
after_bundler do | |
rake "db:create:all" if config['auto_create'] | |
end | |
# >-------------------------------[ Cucumber ]--------------------------------< | |
@current_recipe = "cucumber" | |
@before_configs["cucumber"].call if @before_configs["cucumber"] | |
say_recipe 'Cucumber' | |
@configs[@current_recipe] = config | |
gem 'cucumber-rails', :group => [:development, :test] | |
gem 'capybara', :group => [:development, :test] | |
after_bundler do | |
generate "cucumber:install --capybara#{' --rspec' if recipes.include?('rspec')}#{' -D' unless recipes.include?('activerecord')}" | |
end | |
# >--------------------------------[ Devise ]---------------------------------< | |
@current_recipe = "devise" | |
@before_configs["devise"].call if @before_configs["devise"] | |
say_recipe 'Devise' | |
@configs[@current_recipe] = config | |
gem 'devise' | |
after_bundler do | |
generate 'devise:install' | |
if recipes.include? 'mongo_mapper' | |
gem 'mm-devise' | |
gsub_file 'config/initializers/devise.rb', 'devise/orm/', 'devise/orm/mongo_mapper_active_model' | |
generate 'mongo_mapper:devise User' | |
elsif recipes.include? 'mongoid' | |
gsub_file 'config/initializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid' | |
end | |
generate 'devise user' | |
end | |
# >----------------------------------[ Git ]----------------------------------< | |
@current_recipe = "git" | |
@before_configs["git"].call if @before_configs["git"] | |
say_recipe 'Git' | |
@configs[@current_recipe] = config | |
after_everything do | |
git :init | |
git :add => '.' | |
git :commit => '-m "Initial import."' | |
end | |
# >---------------------------------[ HAML ]----------------------------------< | |
@current_recipe = "haml" | |
@before_configs["haml"].call if @before_configs["haml"] | |
say_recipe 'HAML' | |
@configs[@current_recipe] = config | |
gem 'haml', '>= 3.0.0' | |
gem 'haml-rails' | |
# >--------------------------------[ Heroku ]---------------------------------< | |
@current_recipe = "heroku" | |
@before_configs["heroku"].call if @before_configs["heroku"] | |
say_recipe 'Heroku' | |
config = {} | |
config['create'] = yes_wizard?("Automatically create appname.heroku.com?") if true && true unless config.key?('create') | |
config['staging'] = yes_wizard?("Create staging app? (appname-staging.heroku.com)") if config['create'] && true unless config.key?('staging') | |
config['domain'] = ask_wizard("Specify custom domain (or leave blank):") if config['create'] && true unless config.key?('domain') | |
config['deploy'] = yes_wizard?("Deploy immediately?") if config['create'] && true unless config.key?('deploy') | |
@configs[@current_recipe] = config | |
heroku_name = app_name.gsub('_','') | |
after_everything do | |
if config['create'] | |
say_wizard "Creating Heroku app '#{heroku_name}.heroku.com'" | |
while !system("heroku create #{heroku_name}") | |
heroku_name = ask_wizard("What do you want to call your app? ") | |
end | |
end | |
if config['staging'] | |
staging_name = "#{heroku_name}-staging" | |
say_wizard "Creating staging Heroku app '#{staging_name}.heroku.com'" | |
while !system("heroku create #{staging_name}") | |
staging_name = ask_wizard("What do you want to call your staging app?") | |
end | |
git :remote => "rm heroku" | |
git :remote => "add production [email protected]:#{heroku_name}.git" | |
git :remote => "add staging [email protected]:#{staging_name}.git" | |
say_wizard "Created branches 'production' and 'staging' for Heroku deploy." | |
end | |
unless config['domain'].blank? | |
run "heroku addons:add custom_domains" | |
run "heroku domains:add #{config['domain']}" | |
end | |
git :push => "#{config['staging'] ? 'staging' : 'heroku'} master" if config['deploy'] | |
end | |
# >--------------------------------[ Jammit ]---------------------------------< | |
@current_recipe = "jammit" | |
@before_configs["jammit"].call if @before_configs["jammit"] | |
say_recipe 'Jammit' | |
config = {} | |
config['pre_commit'] = yes_wizard?("Create a git pre-commit hook to generate assets for Heroku?") if true && recipe?('heroku') unless config.key?('pre_commit') | |
@configs[@current_recipe] = config | |
gem 'jammit' | |
after_bundler do | |
if config['pre_commit'] | |
say_wizard "Adding git pre-commit hook..." | |
create_file ".git/hooks/pre-commit", <<-BASH | |
#!/bin/sh | |
echo "Packaging assets with Jammit..." | |
jammit | |
git add public/assets | |
BASH | |
run "chmod +x .git/hooks/pre-commit" | |
end | |
create_file "config/assets.yml", <<-YAML | |
javascripts: | |
app: | |
- public/javascripts/*.js | |
stylesheets: | |
app: | |
- public/stylesheets/*.css | |
YAML | |
gsub_file "app/views/layouts/application.html.erb", "<%= javascript_include_tag :defaults %>", "<%= include_javascripts :app %>" | |
gsub_file "app/views/layouts/application.html.erb", "<%= stylesheet_link_tag :all %>", "<%= include_stylesheets :app %>" | |
end | |
# >--------------------------------[ jQuery ]---------------------------------< | |
@current_recipe = "jquery" | |
@before_configs["jquery"].call if @before_configs["jquery"] | |
say_recipe 'jQuery' | |
config = {} | |
config['ui'] = yes_wizard?("Install jQuery UI?") if true && true unless config.key?('ui') | |
@configs[@current_recipe] = config | |
gem 'jquery-rails' | |
after_bundler do | |
ui = config['ui'] ? ' --ui' : '' | |
generate "jquery:install#{ui}" | |
end | |
# >------------------------------[ RailsAdmin ]-------------------------------< | |
@current_recipe = "rails_admin" | |
@before_configs["rails_admin"].call if @before_configs["rails_admin"] | |
say_recipe 'RailsAdmin' | |
config = {} | |
config['ckeditor'] = yes_wizard?("Install CKEditor?") if true && true unless config.key?('ckeditor') | |
@configs[@current_recipe] = config | |
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git' | |
after_bundler do | |
generate 'rails_admin:install_admin' | |
rake 'admin:copy_assets' | |
rake 'admin:ckeditor_download' if config['ckeditor'] | |
end | |
# >---------------------------------[ SASS ]----------------------------------< | |
@current_recipe = "sass" | |
@before_configs["sass"].call if @before_configs["sass"] | |
say_recipe 'SASS' | |
@configs[@current_recipe] = config | |
unless recipes.include? 'haml' | |
gem 'haml', '>= 3.0.0' | |
end | |
# >-----------------------------[ Settingslogic ]-----------------------------< | |
@current_recipe = "settingslogic" | |
@before_configs["settingslogic"].call if @before_configs["settingslogic"] | |
say_recipe 'Settingslogic' | |
@configs[@current_recipe] = config | |
gem 'settingslogic' | |
say_wizard "Generating config/application.yml..." | |
append_file "config/application.rb", <<-RUBY | |
require 'settings' | |
RUBY | |
create_file "lib/settings.rb", <<-RUBY | |
class Settings < Settingslogic | |
source "#\{Rails.root\}/config/application.yml" | |
namespace Rails.env | |
end | |
RUBY | |
create_file "config/application.yml", <<-YAML | |
defaults: &defaults | |
cool: | |
saweet: nested settings | |
neat_setting: 24 | |
awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %> | |
development: | |
<<: *defaults | |
neat_setting: 800 | |
test: | |
<<: *defaults | |
production: | |
<<: *defaults | |
YAML | |
@current_recipe = nil | |
# >-----------------------------[ Run Bundler ]-------------------------------< | |
say_wizard "Running Bundler install. This will take a while." | |
run 'rvmsudo bundle install' | |
say_wizard "Running after Bundler callbacks." | |
@after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call} | |
@current_recipe = nil | |
say_wizard "Running after everything callbacks." | |
@after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment