Gen new rails project with rspec and auto check-in and ignore for svn

Tagged with Ruby Rails rspec subversion svn

Language: Ruby

View as text

#!/usr/bin/env ruby

require 'fileutils'

# README!!!
# To use this script create a new file somewhere on your local file system and paste this code into it. 
# Save the file and navigate to the file's containing directory in your terminal. 
# Begin executing this script by typing whatever you named it at the command line like so:  
# $ ruby GenNewProject
# The script will prompt you for input, upon doing so hit enter to continue running the script.
# If all goes well you should see all the commands execute and finally receive the output "Finished."

# Prerequisites: You must have previously created a subversion repository and made it accessible via the svn:// protocol or HTTP:// or file:/// etc.


puts  "########################################"
puts  "This script creates a new rails project, installs the 2 required rSpec plugins, bootstraps the app for rSpec and does the initial svn import with ignoring/deleting files from subversion"
puts  "#######################################"

puts  "Enter the svn url of the repository that you have already created:(eg: file:///Users/jon/Sites/svn/repo_name --OR-- svn://path_to_svn) "
svn_url = gets.strip
puts  "Enter svn username for commit logs: "
username = gets.strip

puts  "Enter application name: (eg: MyApplication) This is the DIRECTORY that will contain your rails app on your local file system. Your app, config, db, etc. folders will be directly under this folder."
app_name = gets.strip
puts  "Enter rails application path without app directory on the end: (eg: /Users/jon/Sites)"
app_path = gets.strip

puts  "######################################"


puts  "Please verify the following variables: "
puts  "SVN URL: #{svn_url}"
puts  "SVN username: #{username}"
puts  "Application name: #{app_name}"
puts  "Application path: #{app_path}"

puts  "Proceed (y/n)"
proceed = gets
proceed = proceed.strip.upcase
if proceed == 'N' 
    puts  "Terminating..."
    exit 0
elsif proceed == 'Y' 
    if system("rails -v")
      s = "/"
    elsif   system("rails.cmd -v")
      s = "\\"
    else
        puts "Cannot find rails. Terminating..."
        exit 0
    end    
    app_root = app_path + s + app_name
    
    puts  "Generating rails project: (#{app_root})"
    if system("rails #{app_root}")
      s = "/"
    elsif   system("rails.cmd #{app_root}")
      s = "\\"
    else
        puts "Cannot create rails project. Terminating..."
        exit 0
    end    
    
    #rSpec commands
    rspec_git_repo = "git://github.com/dchelimsky/rspec.git"
    rspec_rails_git_repo = "git://github.com/dchelimsky/rspec-rails.git"
    
    puts("Adding rSpec, and rSpec-Rails plugins")
    
    system "git clone --depth=1 #{rspec_git_repo} #{app_root + s}vendor#{s}plugins#{s}rspec"
    system "git clone --depth=1 #{rspec_rails_git_repo} #{app_root + s}vendor#{s}plugins#{s}rspec-rails"
    system "rm -rf #{app_root + s}vendor#{s}plugins#{s}rspec#{s}.git*"
    system "rm -rf #{app_root + s}vendor#{s}plugins#{s}rspec-rails#{s}.git*"

    puts("Bootstraping app for rSpec")
    FileUtils.cd(app_root)
    system("script/generate rspec")
    
    #SVN commands
    puts  "SVNinitial import: "
    system("svn import #{app_root} #{svn_url} -m \"Initial Import\" --username #{username}")
       
    FileUtils.remove_dir(app_root, true)
       
    puts  "Checking out from svn: "
    
    system("svn checkout #{svn_url} #{app_root}")
    FileUtils.cd(app_root, :verbose => true)
    
    puts  "Removing all log files from SVN"
    system("svn remove log"+s+"*")
    puts  "commiting..."
    system("svn commit -m \"removing all log files from subversion \" ")
    puts  "Ignoring all log files under log dir"
    system("svn propset svn:ignore \"*.log\" log"+s)
    puts  "Updating and commiting..."
    system("svn update log"+s)
    system("svn commit -m \"Ignoring all files in "+s+"log"+s+" ending in .log \" ")
    
    
    puts  "Ignoring all tmp files"
    system("svn remove tmp"+s+"*")
    puts  "Ignoring tmp dir"
    system("svn propset svn:ignore \"*\" tmp"+s)
    puts  "Updating and commiting again...."
    
    system("svn update tmp"+s)
    system("svn commit -m \"Ignoring all files in "+s+"tmp"+s+" \" ")
            
    puts  "Moving database.yml to database.example"
    system("svn move config"+s+"database.yml config"+s+"database.example")
    puts  "commiting..."
    system("svn commit -m \"Moving database.yml to database.example to provide a template for anyone who checks out the code \" ")
    puts  "Ignoring database.yml , updating and commiting..."
    system("svn propset svn:ignore 'database.yml' config"+s)
    system("svn update config"+s)
    system("svn commit -m \"Ignoring database.yml\" ")
    
    puts  "Finished."

else
    puts  "Unknown Input. Terminating..."
	exit 0
end
Original snippet written by Jon Kinney
Last updated at 22:55 PM on Aug 08, 2008

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.