Skip to content
Snippets Groups Projects
Commit 7b389fbf authored by Jack Nagel's avatar Jack Nagel
Browse files

Replace files atomically in inreplace

parent 4fe0be37
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,10 @@ module Utils
module Inreplace
def inreplace paths, before=nil, after=nil
Array(paths).each do |path|
f = File.open(path, 'rb')
s = f.read
s = File.open(path, "rb", &:read)
if before.nil? && after.nil?
s.extend(StringInreplaceExtension)
yield s
yield s.extend(StringInreplaceExtension)
else
after = after.to_s if Symbol === after
unless s.gsub!(before, after)
......@@ -19,8 +17,7 @@ module Utils
end
end
f.reopen(path, 'wb').write(s)
f.close
Pathname(path).atomic_write(s)
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment