Pathname: add cp_path_sub method.
This method allows copying a file to a new location by performing a substitution on the pathname.
This commit is contained in:
parent
caa2f87728
commit
19852bc47c
@ -115,6 +115,27 @@ class Pathname
|
|||||||
return dst
|
return dst
|
||||||
end
|
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
|
# extended to support common double extensions
|
||||||
alias extname_old extname
|
alias extname_old extname
|
||||||
def extname(path=to_s)
|
def extname(path=to_s)
|
||||||
|
|||||||
@ -6,6 +6,12 @@ module InstallRenamed
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cp_path_sub pattern, replacement
|
||||||
|
super do |src, dst|
|
||||||
|
append_default_if_different(src, dst)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def append_default_if_different src, dst
|
def append_default_if_different src, dst
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user