Browse | Submit A New Snippet | Create A Package

 

FileUtils.cp_r wrapper for patching a tree of files

Type:
Full Script
Category:
File Management
License:
Ruby License
Language:
Ruby
 
Description:
A convenience script wrapping FileUtils.cp_r's ability to copy all contents of the source directory instead of the directory itself. I needed to patch a set of PHP files with a newer subset of those files. The :preserve argument is set to TRUE in the FileUtils.cp_r call so the modification dates of all the files will be usable as a version identifier (otherwise, the modification date becomes "today", which only indicates the date the patch was performed, rather than what version the patch applied was).

Versions Of This Snippet::

Albert Davidson Chou
Snippet ID Download Version Date Posted Author Delete
1760.12006-08-20 04:27Albert Davidson Chou

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :0.1

#!/usr/bin/env ruby -w

require 'fileutils'

def usage
  puts "usage: #{File.basename( __FILE__ )} source_directory destination_directory"
end

if ARGV.length < 2 then
  usage
  exit( 1 )
else
  source_directory = ARGV[0].dup

  # FileUtils.cp_r uses the syntax "source_directory/." when specifying the source directory when
  # what is desired is to copy all contents of the source directory (rather than the directory itself).
  # Therefore we need to ensure that the source_directory variable we'll be using as an argument
  # to FileUtils.cp_r ends in '/.' ('\.' on Windows, and whatever else is appropriate on other OS's).
  Dot = '.'
  Slash_dot = "#{File::SEPARATOR}#{Dot}"

  if source_directory[-2..-1] != Slash_dot then
    if source_directory[-1..-1] != File::SEPARATOR then
      source_directory << Slash_dot
    else
      source_directory << Dot
    end
  end


  destination_directory = ARGV[1]
end

FileUtils.cp_r( source_directory, destination_directory, :preserve => TRUE )
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..