diff options
| author | Mike McQuaid | 2013-10-05 20:29:02 +0100 |
|---|---|---|
| committer | Mike McQuaid | 2013-10-10 16:46:47 +0100 |
| commit | f4d29a04943a390e39f7ce51a7f2d45a595592ca (patch) | |
| tree | df712d3367c6a98404c5ad873b0d9629131d8bb1 | |
| parent | 8db4a6c18d3f69a1aa41b9db17b63ae751ef1ff8 (diff) | |
| download | homebrew-f4d29a04943a390e39f7ce51a7f2d45a595592ca.tar.bz2 | |
Pathname: add cp_path_sub method.
This method allows copying a file to a new location by performing a
substitution on the pathname.
| -rw-r--r-- | Library/Homebrew/extend/pathname.rb | 21 | ||||
| -rw-r--r-- | Library/Homebrew/install_renamed.rb | 6 |
2 files changed, 27 insertions, 0 deletions
diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index fd5cf67f1..eb0bfefad 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -115,6 +115,27 @@ class Pathname return dst end + def cp_path_sub pattern, replacement + raise "#{self} does not exist" unless self.exist? + + src = self.to_s + dst = src.sub(pattern, replacement) + raise "#{src} is the same file as #{dst}" if src == dst + + dst_path = Pathname.new dst + + if self.directory? + dst_path.mkpath + return + end + + dst_path.dirname.mkpath + + dst = yield(src, dst) if block_given? + + FileUtils.cp(src, dst) + end + # extended to support common double extensions alias extname_old extname def extname(path=to_s) diff --git a/Library/Homebrew/install_renamed.rb b/Library/Homebrew/install_renamed.rb index e0a69beec..5f34eb674 100644 --- a/Library/Homebrew/install_renamed.rb +++ b/Library/Homebrew/install_renamed.rb @@ -6,6 +6,12 @@ module InstallRenamed end end + def cp_path_sub pattern, replacement + super do |src, dst| + append_default_if_different(src, dst) + end + end + private def append_default_if_different src, dst |
