Skip to content

Commit

Permalink
Pathname.atomic_write
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Mar 16, 2012
1 parent 0ec3aef commit fa215d3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
6 changes: 1 addition & 5 deletions Library/Homebrew/cmd/tap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'tempfile'

HOMEBREW_LIBRARY = HOMEBREW_REPOSITORY/"Library"

Expand Down Expand Up @@ -43,10 +42,7 @@ def link_tap_formula formulae
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
Expand Down
8 changes: 2 additions & 6 deletions Library/Homebrew/cmd/untap.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,9 +17,6 @@ def untap
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
9 changes: 9 additions & 0 deletions Library/Homebrew/extend/pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ def write content
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
Expand Down

0 comments on commit fa215d3

Please sign in to comment.