diff options
| author | Misty De Meo | 2017-07-18 12:53:54 -0700 |
|---|---|---|
| committer | GitHub | 2017-07-18 12:53:54 -0700 |
| commit | f8300b2cb720ca0c25c6b8fbee2df84b5a210b2a (patch) | |
| tree | 9b439c569172d33dfd7f5dd725b9497a3ed11c26 | |
| parent | c6f8887deb892f9ad34cb70a97aa195d40b040e0 (diff) | |
| parent | 32b7e32856e61f256f2703faa17cdbcee26e17b7 (diff) | |
| download | brew-f8300b2cb720ca0c25c6b8fbee2df84b5a210b2a.tar.bz2 | |
Merge pull request #2883 from mistydemeo/allow_passing_hash_to_system
Allow passing hash to system
| -rw-r--r-- | Library/Homebrew/build.rb | 79 | ||||
| -rw-r--r-- | Library/Homebrew/extend/fileutils.rb | 10 | ||||
| -rw-r--r-- | Library/Homebrew/formula.rb | 33 | ||||
| -rwxr-xr-x | Library/Homebrew/shims/super/make | 3 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils_spec.rb | 26 | ||||
| -rw-r--r-- | Library/Homebrew/utils.rb | 31 |
6 files changed, 118 insertions, 64 deletions
diff --git a/Library/Homebrew/build.rb b/Library/Homebrew/build.rb index c0f15158d..8dd4fb245 100644 --- a/Library/Homebrew/build.rb +++ b/Library/Homebrew/build.rb @@ -102,53 +102,52 @@ class Build end end - old_tmpdir = ENV["TMPDIR"] - old_temp = ENV["TEMP"] - old_tmp = ENV["TMP"] - ENV["TMPDIR"] = ENV["TEMP"] = ENV["TMP"] = HOMEBREW_TEMP + new_env = { + "TMPDIR" => HOMEBREW_TEMP, + "TEMP" => HOMEBREW_TEMP, + "TMP" => HOMEBREW_TEMP, + } - formula.extend(Debrew::Formula) if ARGV.debug? + with_env(new_env) do + formula.extend(Debrew::Formula) if ARGV.debug? - formula.brew do |_formula, staging| - staging.retain! if ARGV.keep_tmp? - formula.patch - - if ARGV.git? - system "git", "init" - system "git", "add", "-A" - end - if ARGV.interactive? - ohai "Entering interactive mode" - puts "Type `exit' to return and finalize the installation" - puts "Install to this prefix: #{formula.prefix}" + formula.brew do |_formula, staging| + staging.retain! if ARGV.keep_tmp? + formula.patch if ARGV.git? - puts "This directory is now a git repo. Make your changes and then use:" - puts " git diff | pbcopy" - puts "to copy the diff to the clipboard." + system "git", "init" + system "git", "add", "-A" + end + if ARGV.interactive? + ohai "Entering interactive mode" + puts "Type `exit' to return and finalize the installation" + puts "Install to this prefix: #{formula.prefix}" + + if ARGV.git? + puts "This directory is now a git repo. Make your changes and then use:" + puts " git diff | pbcopy" + puts "to copy the diff to the clipboard." + end + + interactive_shell(formula) + else + formula.prefix.mkpath + + (formula.logs/"00.options.out").write \ + "#{formula.full_name} #{formula.build.used_options.sort.join(" ")}".strip + formula.install + + stdlibs = detect_stdlibs(ENV.compiler) + tab = Tab.create(formula, ENV.compiler, stdlibs.first) + tab.write + + # Find and link metafiles + formula.prefix.install_metafiles formula.buildpath + formula.prefix.install_metafiles formula.libexec if formula.libexec.exist? end - - interactive_shell(formula) - else - formula.prefix.mkpath - - (formula.logs/"00.options.out").write \ - "#{formula.full_name} #{formula.build.used_options.sort.join(" ")}".strip - formula.install - - stdlibs = detect_stdlibs(ENV.compiler) - tab = Tab.create(formula, ENV.compiler, stdlibs.first) - tab.write - - # Find and link metafiles - formula.prefix.install_metafiles formula.buildpath - formula.prefix.install_metafiles formula.libexec if formula.libexec.exist? end end - ensure - ENV["TMPDIR"] = old_tmpdir - ENV["TEMP"] = old_temp - ENV["TMP"] = old_tmp end def detect_stdlibs(compiler) diff --git a/Library/Homebrew/extend/fileutils.rb b/Library/Homebrew/extend/fileutils.rb index 287a1408f..52d4cbf51 100644 --- a/Library/Homebrew/extend/fileutils.rb +++ b/Library/Homebrew/extend/fileutils.rb @@ -111,10 +111,18 @@ module FileUtils # path to the actually-installed make on Tiger or older. def make(*args) if Utils.popen_read("/usr/bin/make", "--version").match(/Make (\d\.\d+)/)[1] > "3.80" - system "/usr/bin/make", *args + make_path = "/usr/bin/make" else make = Formula["make"].opt_bin/"make" make_path = make.exist? ? make.to_s : (Formula["make"].opt_bin/"gmake").to_s + end + + if superenv? + make_name = File.basename(make_path) + with_env "HOMEBREW_MAKE" => make_name do + system "make", *args + end + else system make_path, *args end end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index da375d8a7..b2e4ff988 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -955,30 +955,27 @@ class Formula build = self.build self.build = Tab.for_formula(self) - old_tmpdir = ENV["TMPDIR"] - old_temp = ENV["TEMP"] - old_tmp = ENV["TMP"] - old_path = ENV["HOMEBREW_PATH"] - - ENV["TMPDIR"] = ENV["TEMP"] = ENV["TMP"] = HOMEBREW_TEMP - ENV["HOMEBREW_PATH"] = nil + new_env = { + "TMPDIR" => HOMEBREW_TEMP, + "TEMP" => HOMEBREW_TEMP, + "TMP" => HOMEBREW_TEMP, + "HOMEBREW_PATH" => nil, + } - ENV.clear_sensitive_environment! + with_env(new_env) do + ENV.clear_sensitive_environment! - Pathname.glob("#{bottle_prefix}/{etc,var}/**/*") do |path| - path.extend(InstallRenamed) - path.cp_path_sub(bottle_prefix, HOMEBREW_PREFIX) - end + Pathname.glob("#{bottle_prefix}/{etc,var}/**/*") do |path| + path.extend(InstallRenamed) + path.cp_path_sub(bottle_prefix, HOMEBREW_PREFIX) + end - with_logging("post_install") do - post_install + with_logging("post_install") do + post_install + end end ensure self.build = build - ENV["TMPDIR"] = old_tmpdir - ENV["TEMP"] = old_temp - ENV["TMP"] = old_tmp - ENV["HOMEBREW_PATH"] = old_path @prefix_returns_versioned_prefix = false end diff --git a/Library/Homebrew/shims/super/make b/Library/Homebrew/shims/super/make index 028e3462a..7b49e56c0 100755 --- a/Library/Homebrew/shims/super/make +++ b/Library/Homebrew/shims/super/make @@ -1,4 +1,5 @@ #!/bin/bash +export MAKE=${HOMEBREW_MAKE:-make} export HOMEBREW_CCCFG="O$HOMEBREW_CCCFG" -exec xcrun make "$@" +exec xcrun $MAKE "$@" diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb index f3bf98486..37bd83c4f 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -270,4 +270,30 @@ describe "globally-scoped helper methods" do }.to raise_error(MethodDeprecatedError, %r{method.*replacement.*homebrew/homebrew-core.*homebrew/core}m) end end + + describe "#with_env" do + it "sets environment variables within the block" do + expect(ENV["PATH"]).not_to eq("/bin") + with_env "PATH" => "/bin" do + expect(ENV["PATH"]).to eq("/bin") + end + end + + it "restores ENV after the block" do + with_env "PATH" => "/bin" do + expect(ENV["PATH"]).to eq("/bin") + end + expect(ENV["PATH"]).not_to eq("/bin") + end + + it "restores ENV if an exception is raised" do + expect { + with_env "PATH" => "/bin" do + raise StandardError, "boom" + end + }.to raise_error(StandardError) + + expect(ENV["PATH"]).not_to eq("/bin") + end + end end 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 |
