From ff4d16deebaabc374f4dae2786bd01e2865875a4 Mon Sep 17 00:00:00 2001 From: Dominyk Tiller Date: Tue, 16 Feb 2016 16:34:51 +0000 Subject: [PATCH] pathname: add append_lines method * Blocks writing of new files via accidental typos, etc, which the normal open("blah", "a") doesn't. * Where files don't exist they should ideally be using `(buildpath/"dog").write` instead of open("blah", "a") already. * It's a bit less cluttered looking if you need several writes to different files in the formula, IMO. --- Library/Homebrew/extend/pathname.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index c6b575c036..68115632d9 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -131,6 +131,12 @@ class Pathname open("w", *open_args) { |f| f.write(content) } end + # Only appends to a file that is already created. + def append_lines(content, *open_args) + raise "Cannot append file that doesn't exist: #{self}" unless exist? + open("a", *open_args) { |f| f.puts(content) } + end + def binwrite(contents, *open_args) open("wb", *open_args) { |f| f.write(contents) } end unless method_defined?(:binwrite)