diff options
Diffstat (limited to 'Library/Homebrew/utils.rb')
| -rw-r--r-- | Library/Homebrew/utils.rb | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 529f3492d..5a6295394 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -287,10 +287,9 @@ ensure end def run_as_not_developer(&_block) - old = ENV.delete "HOMEBREW_DEVELOPER" - yield -ensure - ENV["HOMEBREW_DEVELOPER"] = old + with_env "HOMEBREW_DEVELOPER" => nil do + yield + end end # Kernel.system but with exceptions @@ -533,3 +532,27 @@ def migrate_legacy_keg_symlinks_if_necessary end FileUtils.rm_rf legacy_pinned_kegs end + +# Calls the given block with the passed environment variables +# added to ENV, then restores ENV afterwards. +# Example: +# with_env "PATH" => "/bin" do +# system "echo $PATH" +# end +# +# Note that this method is *not* thread-safe - other threads +# which happen to be scheduled during the block will also +# see these environment variables. +def with_env(hash) + old_values = {} + begin + hash.each do |key, value| + old_values[key] = ENV.delete(key) + ENV[key] = value + end + + yield if block_given? + ensure + ENV.update(old_values) + end +end |
