Skip to content

Instantly share code, notes, and snippets.

@xiaods
Created January 3, 2010 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaods/267900 to your computer and use it in GitHub Desktop.
Save xiaods/267900 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
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
[--with-data] # Populate new Rails app with Blog style scaffolding
Example:
ruby generate_rails_app.rb ~/Code/Ruby/weblog
"
exit
end
APP_NAME = ARGV.first
WITH_DATA = ARGV.any? { |arg| arg == '--with-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: 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 `gem bundle` =~ /Gemfile to use/
puts "Git and Gem Bundler found. 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}")
FileUtils.cd(APP_NAME, :verbose => true) do
heading("Bundling required gems")
File.open('Gemfile', 'a') do |f|
f.puts "directory '#{RAILS_CLONE}', :glob => '{*/,}*.gemspec'"
f.puts "git 'git://github.com/rails/rack.git'"
f.puts "git 'git://github.com/rails/arel.git'"
end
exe("gem bundle")
if WITH_DATA
heading("Adding initial test data")
exe("ruby script/generate scaffold Post title:string body:text published:boolean")
exe("ruby script/generate scaffold Comment post_id:integer title:string body:text author_name:string author_email:string author_website:string")
exe("bin/rake db:migrate")
heading("Removing public/index.html and changing routes")
exe("rm public/index.html")
routes = File.read('config/routes.rb')
routes.gsub!( '# root :to => "welcome#index"', 'root :to => "posts#index"' )
File.open('config/routes.rb', "w") { |f| f.puts routes }
end
if START_SERVER
heading("Starting up application server")
exe("ruby script/server --environment=#{ENVIRONMENT}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment