#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'rubygems' require 'activesupport' require 'pathname' require 'digest/md5' project_name = ARGV[0] fail("Usage: #{File.basename(__FILE__)} new_project_name") unless project_name fail("Project name must only contain [a-z0-9_]") unless project_name =~ /^[a-z0-9_]+$/ base_directory = Pathname.new(File.join(File.dirname(__FILE__), '..', '..')).realpath project_directory = base_directory + project_name fail("Project directory (#{project_directory}) already exists") if project_directory.exist? template_url = "git://github.com/dancroak/heroku_suspenders.git" changeme = "rebirth" changesession = "CHANGESESSION" changetitle = "Rebirth" def run(cmd) puts "Running '#{cmd}'" out = `#{cmd}` if $? != 0 fail "Command #{cmd} failed: #$?\n#{out}" end out end def search_and_replace(file, search, replace) if File.file?(file) contents = File.read(file) if contents[search] puts "Replacing #{search} with #{replace} in #{file}" contents.gsub!(search, replace) File.open(file, "w") { |f| f << contents } end end end def not_installed?(gem_name) gems = Gem.source_index.find_name(gem_name) gems.detect { |gem| gem.name == gem_name }.nil? end def installed?(gem_name) installed_gems = Gem.source_index.find_name(gem_name) installed_gems.any? end run("cp -r . #{project_directory}") Dir.chdir(project_directory) or fail("Couldn't change to #{project_directory}") run("git init") run("git remote add heroku_suspenders #{template_url}") Dir.glob("#{project_directory}/**/*").each do |file| search_and_replace(file, changeme, project_name) search_and_replace(file, changetitle, project_name.titleize) end Dir.glob("#{project_directory}/**/session_store.rb").each do |file| datestring = Time.now.strftime("%j %U %w %A %B %d %Y %I %M %S %p %Z") search_and_replace(file, changesession, Digest::MD5.hexdigest("#{project_name} #{datestring}")) end run("git mv public/stylesheets/screen.css public/stylesheets/#{project_name}.css") run("sudo chmod 0666 public/stylesheets/#{project_name}.css") run("git add .") run("git commit -m 'New Heroku Suspenders app'") unless installed?("rails") run "sudo gem install rails" end # can't vendor nokogiri because it has native extensions unless installed?("nokogiri") run "sudo gem install nokogiri --version='1.3.3'" end # need to install heroku gem for the 'heroku' commands unless installed?("heroku") run "sudo gem install heroku" end unless installed?("sqlite3-ruby") run "sudo gem install sqlite3-ruby" end # can't install rubaidh-google_analytics via rake gems:install because # it is a production gem and we don't have a production database unless installed?("rubaidh-google_analytics") run "sudo gem install rubaidh-google_analytics --version='1.1.4'" end run("sudo touch log/development.log") run("sudo chmod 0666 log/development.log") run("sudo touch log/test.log") run("sudo chmod 0666 log/test.log") run("sudo touch log/cucumber.log") run("sudo chmod 0666 log/cucumber.log") run("rake db:migrate RAILS_ENV=development") run("rake db:migrate RAILS_ENV=test") run("rake db:migrate RAILS_ENV=cucumber") puts puts "Next: run the tests, configure Heroku, deploy!" puts puts "cd ../#{project_name} && rake && rake heroku:setup"