Replace files atomically in inreplace
This commit is contained in:
parent
4fe0be37c0
commit
7b389fbfed
@ -2,12 +2,10 @@ module Utils
|
|||||||
module Inreplace
|
module Inreplace
|
||||||
def inreplace paths, before=nil, after=nil
|
def inreplace paths, before=nil, after=nil
|
||||||
Array(paths).each do |path|
|
Array(paths).each do |path|
|
||||||
f = File.open(path, 'rb')
|
s = File.open(path, "rb", &:read)
|
||||||
s = f.read
|
|
||||||
|
|
||||||
if before.nil? && after.nil?
|
if before.nil? && after.nil?
|
||||||
s.extend(StringInreplaceExtension)
|
yield s.extend(StringInreplaceExtension)
|
||||||
yield s
|
|
||||||
else
|
else
|
||||||
after = after.to_s if Symbol === after
|
after = after.to_s if Symbol === after
|
||||||
unless s.gsub!(before, after)
|
unless s.gsub!(before, after)
|
||||||
@ -19,8 +17,7 @@ module Utils
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
f.reopen(path, 'wb').write(s)
|
Pathname(path).atomic_write(s)
|
||||||
f.close
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user