Skip to content

Instantly share code, notes, and snippets.

@yortz
Created March 10, 2010 10:21
Show Gist options
  • Save yortz/327733 to your computer and use it in GitHub Desktop.
Save yortz/327733 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'
# 2010-02-03 - Update to script/rails (Jack Dempsey), suppress output, start server by default
# 2010-02-05 - Fixes, add links to index.html, don't overwrite it
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
[--without-data] # Do not populate new Rails app with Game review style scaffolding
[--dont-start-server] # Do not start the server once every other operation is complete
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')
DONT_START_SERVER = ARGV.any? { |arg| arg == '--dont-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'" 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 --quiet")
exe("git pull origin master --quiet")
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 --quiet")
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 --quiet")
exe("ruby script/rails generate scaffold Game title:string description:text published:timestamp --quiet")
exe("ruby script/rails generate model Rating game_id:integer user_id:integer value:integer --quiet")
exe("ruby script/rails generate scaffold Review game_id:integer user_id:integer title:string body:text published:boolean --quiet")
exe("ruby script/rails generate scaffold Comment user_id:integer review_id:integer title:string body:text --quiet")
exe("ruby script/rails generate mailer Notifications account_created new_review new_comment --quiet")
exe("ruby script/rails generate observer Review --quiet")
exe("ruby script/rails generate observer Comment --quiet")
exe("ruby script/rails generate metal Download --quiet")
exe("bundle exec rake db:migrate")
heading("Adding sample model links to public/index.html")
contents = File.read('public/index.html')
links = <<-HTML
<h3>Sample Scaffolds</h3>
<ul class="links">
<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>
</li>
<li>
<h3>Join the community</h3>
HTML
File.open('public/index.html', "w") do |f|
f.puts contents.gsub('<h3>Join the community</h3>', links)
end
end
unless DONT_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