Skip to content

Instantly share code, notes, and snippets.

@jackdempsey
Created February 3, 2010 01:41
Show Gist options
  • Save jackdempsey/293249 to your computer and use it in GitHub Desktop.
Save jackdempsey/293249 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Written by Kieran P
# http://github.com/KieranP
# http://twitter.com/k776
# http://k776.tumblr.com
#
# 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'
if ARGV.empty?
puts "
Usage:
ruby generate_rails_app.rb APP_PATH [options]
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
Example:
ruby generate_rails_app.rb ~/Code/Ruby/game_reviewer
"
exit
end
APP_NAME = ARGV.first
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'
ENV['RAILS_ENV'] = ENVIRONMENT
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 http://git-scm.com/download" 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://github.com/rails/rails.git #{RAILS_CLONE}") unless File.directory?(RAILS_CLONE)
FileUtils.cd(RAILS_CLONE, :verbose => true) do
exe("git checkout master")
exe("git pull origin master")
end
heading("Creating new application")
exe("rm -rf #{APP_NAME}") if File.directory?(APP_NAME)
exe("ruby #{File.join(RAILS_CLONE, 'railties/bin/rails')} #{APP_NAME} --dev")
FileUtils.cd(APP_NAME, :verbose => true) do
unless WITHOUT_DATA
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("bin/rake db:migrate")
heading("Replacing public/index.html with links")
File.open('public/index.html', "w") do |f|
f.puts <<-HTML
<html>
<head>
<title>Rails 3 Test App</title>
</head>
<body>
<ul>
<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>
</ul>
</body>
</html>
HTML
end
end
if START_SERVER
heading("Starting up application server")
exe("ruby script/rails server --environment=#{ENVIRONMENT}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment