Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# Written by Kieran P
# Feel free to fork and modify.
# If you do, send me a message on
# Github details changes and I'll
# pull them back if they work and
# don't conflict
# Changes:
# 2010-01-05 - First Stable Feature Complete Version
# 2010-01-16 - Manual bundling no longer required, Rails generator does this now
# 2010-01-28 - Adding much more scaffolding data, mailers, observers, and metal
# 2010-01-29 - Adding scaffolding data is now the default, use --without-data to stop it
# 2010-01-30 - Adding check for Ruby version, since Rails 3 requires 1.8.7 or higher
# 2010-02-02 - Bundler 0.9.0 now uses 'bundle' command, not 'gem bundle'
# (Ben) had to install these gems before script/rails would work
# => sudo gem uninstall bundler
# => sudo gem install bundler --pre
# => sudo gem install i18n
# => sudo gem install tzinfo
if ARGV.empty?
puts "
ruby generate_rails_app.rb APP_PATH [options]
[--environment=env] # The environment you want data to be populated into,
# or the server to start into.
[--rails-clone=path] # Path to existing Rails Git clone.
# If not present, will create clone at that location.
# If not provided, will clone to 'rails' in current directory
[--start-server] # Start the server once every other operation is complete
[--without-data] # Do not populate new Rails app with Game review style scaffolding
ruby generate_rails_app.rb ~/Code/Ruby/game_reviewer
WITHOUT_DATA = ARGV.any? { |arg| arg == '--without-data' }
RAILS_CLONE = File.expand_path((ARGV.detect { |arg| arg =~ /--rails-clone=(.*)/ } && $1) || 'rails')
START_SERVER = ARGV.any? { |arg| arg == '--start-server' }
ENVIRONMENT = (ARGV.detect { |arg| arg =~ /--environment=(.*)/ } && $1) || 'development'
require 'fileutils'
def exe(cmd); puts cmd; system(cmd); end
def sep; " ----------------------------- "; end
def heading(text); puts "\n" + sep + text + sep + "\n\n"; end
heading("Checking for required software")
raise "ERROR: Rails 3 now requires Ruby 1.8.7 or higher. You have #{RUBY_VERSION}. Please upgrade." if RUBY_VERSION < "1.8.7"
raise "ERROR: Git does not appear to be installed with your $PATH. See" unless system('which -s git')
raise "ERROR: Gem bundler does not appear to be installed or isn't the latest version. Run '[sudo] gem install bundler --pre'" unless system('which -s bundle')
puts "Ruby, Git and Gem Bundler dependencies satisfied. Continuing.."
heading("Preparing Rails clone")
exe("git clone git:// #{RAILS_CLONE}") unless, :verbose => true) do
exe("git checkout master")
exe("git pull origin master")
heading("Creating new application")
exe("rm -rf #{APP_NAME}") if
exe("ruby #{File.join(RAILS_CLONE, 'railties/bin/rails')} #{APP_NAME} --dev"), :verbose => true) do
heading("Adding initial test data")
exe("ruby script/rails generate scaffold User login:string password:string salt:string email:string website:string")
exe("ruby script/rails generate scaffold Game title:string description:text published:timestamp")
exe("ruby script/rails generate model Rating game_id:integer user_id:integer value:integer")
exe("ruby script/rails generate scaffold Review game_id:integer user_id:integer title:string body:text published:boolean")
exe("ruby script/rails generate scaffold Comment user_id:integer review_id:integer title:string body:text")
exe("ruby script/rails generate mailer Notifications account_created new_review new_comment")
exe("ruby script/rails generate observer Review")
exe("ruby script/rails generate observer Comment")
exe("ruby script/rails generate metal Download")
exe("rake db:migrate")
heading("Replacing public/index.html with links")'public/index.html', "w") do |f|
f.puts <<-HTML
<title>Rails 3 Test App</title>
<li><a href="/users">Users</a></li>
<li><a href="/games">Games</a></li>
<li><a href="/reviews">Reviews</a></li>
<li><a href="/comments">Comments</a></li>
heading("Starting up application server")
exe("ruby script/rails server --environment=#{ENVIRONMENT}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment