#!/usr/bin/env ruby # # Created by Tamara Temple on 2007-03-31. # Copyright (c) 2007. All rights reserved. # Take a rails application and put it into SVN # From Rails Cookbook, Recipe 1.10 (O'Reilly) require 'pp' if $DEBUG require 'FileUtils' include FileUtils::Verbose require 'getoptlong' SVNADMIN_CMD = "svnadmin" SVN_CMD = "svn" opts = GetoptLong.new( [ "--svnroot", "-r", GetoptLong::OPTIONAL_ARGUMENT] ) opts.each do |opt, arg| case opt when /--svnroot|-r/ SVNROOT = File.expand_path(arg) end end SVNROOT = File.expand_path("~/SVN") unless defined? SVNROOT def checkvalidpath(p) begin fail("#{p} is not a directory") unless (File.stat(p).directory?) fail("#{p} is not readable") unless (File.stat(p).readable?) fail("#{p} is not writeable") unless (File.stat(p).writable?) fail("#{p} is not executable") unless (File.stat(p).executable?) rescue SystemCallError fail($!) end end # Get the full path name for the project directory def getfullpathname(project) p = File.expand_path(project) checkvalidpath(p) return p end # Create a subversion repository and confirm the repository was created def makerepo(reponame) cd(SVNROOT) do puts `#{SVNADMIN_CMD} create #{reponame}` files = Dir.glob("#{SVNROOT}/#{reponame}/*") print "Files in Repository #{reponame}:\n",files.join("\n"),"\n" end end # Import entire project into repository def importproject(projectdir,reponame) cd(projectdir) do puts `#{SVN_CMD} import -m 'initial import' . file:///#{SVNROOT}/#{reponame}` end end # Check out the repository into the current working directory (which will be the parent of the project) def checkout(reponame) puts `#{SVN_CMD} checkout file:///#{SVNROOT}/#{reponame}` end # Exempt log files from the repository def exemptlogs(project) cd(project) do puts `#{SVN_CMD} remove log/*` puts `#{SVN_CMD} commit -m 'removed log files'` puts `#{SVN_CMD} propset svn:ignore "*.log" log/` puts `#{SVN_CMD} update log/` puts `#{SVN_CMD} commit -m 'svn ignore new log/*.log files'` end end def exemptyaml(project) cd(project) do puts `#{SVN_CMD} move config/database.yml config/database.orig` puts `#{SVN_CMD} commit -m 'move database.yml to database.orig'` puts `#{SVN_CMD} propset svn:ignore "database.yml" config/` puts `#{SVN_CMD} update config/` puts `#{SVN_CMD} commit -m 'Ingnoring database.yml'` cd("config") do pp Dir.glob("*") if $DEBUG copy_file("database.orig","database.yml") # restore yml file for working end end end def main(project) project = getfullpathname(project) checkvalidpath(project) reponame = File.basename(project) print("Creating repository for poject #{reponame}\n") makerepo(reponame) importproject(project,reponame) cd(project);cd("..");rm_rf(reponame) # get rid of the original project, which we will fill back up from the repository checkout(reponame) exemptlogs(project) exemptyaml(project) end checkvalidpath(SVNROOT) if ARGV.length > 0 then ARGV.each {|project| main(project)} else main(".") end