aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike McQuaid2016-12-31 16:38:05 +0000
committerMike McQuaid2016-12-31 16:38:05 +0000
commita571965df9d5dc84d1a32630a111768fbac71b3b (patch)
treecd11f14a245c60e6f6de13ebd97bf9e11341c793
parentb1e27d689d8eadb267fb9890ec2e0a2a87e46b4e (diff)
downloadbrew-a571965df9d5dc84d1a32630a111768fbac71b3b.tar.bz2
formula: make prefix usually return opt_prefix.
Return `opt_prefix` if it exists and `prefix` is not called from within the same formula's `install` or `post_install` methods. Otherwise, fall back to the existing functionality. This avoids the need to use `opt_prefix` etc. everywhere and generally means we don't expose an implementation detail (i.e. the full Cellar path) to dependents that have a habit of hard-coding it.
-rw-r--r--Library/Homebrew/cmd/--prefix.rb2
-rw-r--r--Library/Homebrew/formula.rb21
-rw-r--r--Library/Homebrew/formula_installer.rb2
-rw-r--r--Library/Homebrew/keg.rb1
4 files changed, 21 insertions, 5 deletions
diff --git a/Library/Homebrew/cmd/--prefix.rb b/Library/Homebrew/cmd/--prefix.rb
index c59830833..f6e7d2ee2 100644
--- a/Library/Homebrew/cmd/--prefix.rb
+++ b/Library/Homebrew/cmd/--prefix.rb
@@ -11,7 +11,7 @@ module Homebrew
if ARGV.named.empty?
puts HOMEBREW_PREFIX
else
- puts ARGV.resolved_formulae.map { |f| f.opt_prefix.exist? ? f.opt_prefix : f.installed_prefix }
+ puts ARGV.resolved_formulae.map(&:installed_prefix)
end
end
end
diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb
index bc1dbd970..2d0a9c25e 100644
--- a/Library/Homebrew/formula.rb
+++ b/Library/Homebrew/formula.rb
@@ -198,6 +198,7 @@ class Formula
@build = active_spec.build
@pin = FormulaPin.new(self)
@follow_installed_alias = true
+ @versioned_prefix = false
end
# @private
@@ -548,9 +549,16 @@ class Formula
end
# The directory in the cellar that the formula is installed to.
- # This directory contains the formula's name and version.
+ # This directory points to {#opt_prefix} if it exists and if #{prefix} is not
+ # called from within the same formula's {#install} or {#post_install} methods.
+ # Otherwise, return the full path to the formula's versioned cellar.
def prefix(v = pkg_version)
- Pathname.new("#{HOMEBREW_CELLAR}/#{name}/#{v}")
+ prefix = rack/v
+ if !@versioned_prefix && prefix.directory? && Keg.new(prefix).optlinked?
+ opt_prefix
+ else
+ prefix
+ end
end
# Is the formula linked?
@@ -579,7 +587,7 @@ class Formula
# installed versions of this software
# @private
def rack
- prefix.parent
+ Pathname.new("#{HOMEBREW_CELLAR}/#{name}")
end
# All currently installed prefix directories.
@@ -994,6 +1002,7 @@ class Formula
# @private
def run_post_install
+ @versioned_prefix = true
build = self.build
self.build = Tab.for_formula(self)
old_tmpdir = ENV["TMPDIR"]
@@ -1008,6 +1017,7 @@ class Formula
ENV["TMPDIR"] = old_tmpdir
ENV["TEMP"] = old_temp
ENV["TMP"] = old_tmp
+ @versioned_prefix = false
end
# Tell the user about any caveats regarding this package.
@@ -1110,6 +1120,7 @@ class Formula
# where staging is a Mktemp staging context
# @private
def brew
+ @versioned_prefix = true
stage do |staging|
staging.retain! if ARGV.keep_tmp?
prepare_patches
@@ -1123,6 +1134,8 @@ class Formula
cp Dir["config.log", "CMakeCache.txt"], logs
end
end
+ ensure
+ @versioned_prefix = false
end
# @private
@@ -1624,6 +1637,7 @@ class Formula
# @private
def run_test
+ @versioned_prefix = true
old_home = ENV["HOME"]
old_curl_home = ENV["CURL_HOME"]
old_tmpdir = ENV["TMPDIR"]
@@ -1655,6 +1669,7 @@ class Formula
ENV["TEMP"] = old_temp
ENV["TMP"] = old_tmp
ENV["TERM"] = old_term
+ @versioned_prefix = false
end
# @private
diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb
index 90e283c9b..24c068460 100644
--- a/Library/Homebrew/formula_installer.rb
+++ b/Library/Homebrew/formula_installer.rb
@@ -546,7 +546,7 @@ class FormulaInstaller
def summary
s = ""
s << "#{Emoji.install_badge} " if Emoji.enabled?
- s << "#{formula.prefix}: #{formula.prefix.abv}"
+ s << "#{formula.prefix.resolved_path}: #{formula.prefix.abv}"
s << ", built in #{pretty_duration build_time}" if build_time
s
end
diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb
index 756b27288..7ac3e9e63 100644
--- a/Library/Homebrew/keg.rb
+++ b/Library/Homebrew/keg.rb
@@ -147,6 +147,7 @@ class Keg
protected :path
def initialize(path)
+ path = path.resolved_path if path.to_s.start_with?("#{HOMEBREW_PREFIX}/opt/")
raise "#{path} is not a valid keg" unless path.parent.parent.realpath == HOMEBREW_CELLAR.realpath
raise "#{path} is not a directory" unless path.directory?
@path = path