Created
December 30, 2009 18:32
-
-
Save shanlalit/266270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
if ARGV.empty? | |
puts " | |
Usage: | |
./generate_rails_app 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: | |
./generate_rails_app ~/Code/Ruby/weblog | |
" | |
exit | |
end | |
APP_NAME = ARGV.first | |
WITH_DATA = ARGV.any? { |arg| arg == '--with-data' } | |
RAILS_CLONE = (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("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 -rubygems #{File.join(RAILS_CLONE, 'railties/bin/rails')} #{APP_NAME} --edge") | |
FileUtils.cd(APP_NAME, :verbose => true) do | |
heading("Bundling required gems") | |
File.open('Gemfile', 'a') do |f| | |
f.puts "gem 'rack', :git => 'git://github.com/rails/rack.git'" | |
f.puts "gem 'arel', :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