Pathname.atomic_write

This commit is contained in:
Max Howell 2012-03-16 12:58:39 +00:00
parent 2ace9422bc
commit e6cb8cbee9
3 changed files with 12 additions and 11 deletions

View File

@ -1,4 +1,3 @@
require 'tempfile'
HOMEBREW_LIBRARY = HOMEBREW_REPOSITORY/"Library"
@ -43,10 +42,7 @@ module Homebrew extend self
end
end
tf = Tempfile.new("brew-tap")
tf.write(ignores.uniq.join("\n"))
tf.close
mv tf.path, "#{HOMEBREW_LIBRARY}/Formula/.gitignore"
HOMEBREW_LIBRARY.join("Formula/.gitignore").atomic_write(ignores.uniq.join("\n"))
end
private

View File

@ -1,5 +1,4 @@
require 'cmd/tap' # for Pathname.recursive_formula
require 'tempfile'
require 'cmd/tap' # for tap_args
module Homebrew extend self
def untap
@ -18,9 +17,6 @@ module Homebrew extend self
end
rm_rf tapd
tf = Tempfile.new("brew-untap")
tf.write(gitignores.join("\n"))
tf.close
mv tf.path, "#{HOMEBREW_PREFIX}/Library/Formula/.gitignore"
HOMEBREW_REPOSITORY.join("Library/Formula/.gitignore").atomic_write(gitignores * "\n")
end
end

View File

@ -90,6 +90,15 @@ class Pathname
File.open(self, 'w') {|f| f.write content }
end
# NOTE always overwrites
def atomic_write content
require 'tempfile'
tf = Tempfile.new(self.basename.to_s)
tf.write(content)
tf.close
FileUtils.mv tf.path, self.to_s
end
def cp dst
if file?
FileUtils.cp to_s, dst