Source_Checkout.rb

The following script runs through each url you've set and pulls down the source for it. I use it for installing merb & datamapper onto a new server because they require an svn checkout and a few git clones and I'm lazy :)

Tested On

#!/usr/bin/env ruby
# 
#  source_checkout.rb
#  Checks out the source repositories defined
# 
# Only works for subversion (svn), mercurial (hg)
# and git (git) for now.
#  
#  Created by Caius Durling on 2008-02-09.
#  Released into the Public Domain, read the code
#  through and don't blame me if it runs away
#  with your wife!'
# 
 
# Setup the repos to pull from
repos = {
# Example
  # :folder_name => {
  #   :url => "url to repo",
  #   # Specify the scm if you want, I'll attempt to 
  #   # work it out if you don't
  #   :scm => "svn"
  # }  
  :datamapper => {
    :url => "http://datamapper.rubyforge.org/svn/trunk",
    :scm => "svn"
  },
  :merb_core => {
    :url => "git://github.com/wycats/merb-core.git"
  },
  :merb_more => {
    :url => "git://github.com/wycats/merb-more.git"
  },
  :merb_plugins => {
    :url => "git://github.com/wycats/merb-plugins.git"
  }
}
 
class String
  # Attempt to figure out the scm from the url
  def parse_scm
      self.scan( %r{(?:svn|git)://|/?/(?:svn|mercurial|hg|git)(?:/|\.)|(?:trunk|hg|git)} ).each { |e | e.gsub!(/[^\w]/, "") }.uniq.first
  end
 
  # Return the correct command for checking out
  def command
    case self
    when "svn"
      return "svn checkout"
    # Nifty trick
    when "hg"
    when "git"
      return "#{self} clone"
    end
  end
end
 
# Loop through them and check them out
repos.each do |name, arr|
  puts "Checking out the #{name} repo"
 
  # Get the scm name
  scm = arr[:url].to_s.parse_scm unless arr.has_key?(:scm)    
  scm ||= arr[:scm].to_s
 
  # Sanitise scm
  # "Common Name" => "command"
  {
    "subversion" => "svn",
    "trunk" => "svn",
    "mercurial" => "hg"
  }.each do |key, val|
    scm.gsub!(/#{key}/i, "#{val}")
  end
  scm.downcase!
 
  %x{
    # export PATH=":/opt/local/bin"; # For OS X
    #{scm.command} #{arr[:url].to_s} #{name.to_s}
    }
 
  puts "#{name} repo was successfully checked out"
 
end
Example 1 -- ruby snippet -- Select Code