Skip to content

Commit

Permalink
Merge pull request #2883 from mistydemeo/allow_passing_hash_to_system
Browse files Browse the repository at this point in the history
Allow passing hash to system
  • Loading branch information
mistydemeo authored Jul 18, 2017
2 parents c6f8887 + 32b7e32 commit f8300b2
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 64 deletions.
79 changes: 39 additions & 40 deletions Library/Homebrew/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,53 +102,52 @@ def install
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)
Expand Down
10 changes: 9 additions & 1 deletion Library/Homebrew/extend/fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ def rake(*args)
# 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
Expand Down
33 changes: 15 additions & 18 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -955,30 +955,27 @@ def run_post_install
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

Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/shims/super/make
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash

export MAKE=${HOMEBREW_MAKE:-make}
export HOMEBREW_CCCFG="O$HOMEBREW_CCCFG"
exec xcrun make "$@"
exec xcrun $MAKE "$@"
26 changes: 26 additions & 0 deletions Library/Homebrew/test/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,30 @@ def esc(code)
}.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
31 changes: 27 additions & 4 deletions Library/Homebrew/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,9 @@ def with_custom_locale(locale)
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
Expand Down Expand Up @@ -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

0 comments on commit f8300b2

Please sign in to comment.