public
Last active

  • Download Gist
generate_rails_app.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#!/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'
 
# (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 "
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("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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.