Add Homebrew::DEFAULT_PREFIX for Linux

The default prefix is /usr/local on macOS
and /home/linuxbrew/.linuxbrew on Linux.
This commit is contained in:
Shaun Jackman 2018-09-06 16:58:17 -07:00
parent ca86f07039
commit f6093961ef
16 changed files with 56 additions and 26 deletions

View File

@ -1,5 +1,5 @@
#: * `--prefix`: #: * `--prefix`:
#: Display Homebrew's install path. *Default:* `/usr/local` #: Display Homebrew's install path. *Default:* `/usr/local` on macOS and `/home/linuxbrew/.linuxbrew` on Linux
#: #:
#: * `--prefix` <formula>: #: * `--prefix` <formula>:
#: Display the location in the cellar where <formula> is or would be installed. #: Display the location in the cellar where <formula> is or would be installed.

View File

@ -52,7 +52,7 @@ module Homebrew
end end
if keg_only if keg_only
if HOMEBREW_PREFIX.to_s == "/usr/local" if Homebrew.default_prefix?
f = keg.to_formula f = keg.to_formula
caveats = Caveats.new(f) caveats = Caveats.new(f)

View File

@ -285,7 +285,7 @@ module Homebrew
ohai "Detecting if #{filename} is relocatable..." ohai "Detecting if #{filename} is relocatable..."
end end
if prefix == "/usr/local" if Homebrew.default_prefix?(prefix)
prefix_check = File.join(prefix, "opt") prefix_check = File.join(prefix, "opt")
else else
prefix_check = prefix prefix_check = prefix

View File

@ -56,7 +56,7 @@ module Homebrew
osx_image: xcode9.2 osx_image: xcode9.2
cache: cache:
directories: directories:
- /usr/local/Homebrew/Library/Homebrew/vendor/bundle - #{Homebrew::DEFAULT_PREFIX}/Homebrew/Library/Homebrew/vendor/bundle
branches: branches:
only: only:
- master - master

View File

@ -0,0 +1 @@
require "extend/os/mac/utils/analytics" if OS.mac?

View File

@ -0,0 +1,9 @@
module Utils
module Analytics
class << self
def custom_prefix_label
"non-/usr/local".freeze
end
end
end
end

View File

@ -42,13 +42,21 @@ HOMEBREW_BOTTLE_DOMAIN = ENV["HOMEBREW_BOTTLE_DOMAIN"] ||
HOMEBREW_BOTTLE_DEFAULT_DOMAIN HOMEBREW_BOTTLE_DEFAULT_DOMAIN
require "fileutils" require "fileutils"
require "os"
require "os/global"
module Homebrew module Homebrew
extend FileUtils extend FileUtils
DEFAULT_PREFIX ||= "/usr/local".freeze
class << self class << self
attr_writer :failed, :raise_deprecation_exceptions, :auditing, :args attr_writer :failed, :raise_deprecation_exceptions, :auditing, :args
def Homebrew.default_prefix?(prefix = HOMEBREW_PREFIX)
prefix.to_s == DEFAULT_PREFIX
end
def failed? def failed?
@failed ||= false @failed ||= false
@failed == true @failed == true
@ -108,7 +116,6 @@ HOMEBREW_INTERNAL_COMMAND_ALIASES = {
require "set" require "set"
require "os"
require "extend/pathname" require "extend/pathname"
require "extend/module" require "extend/module"

View File

@ -0,0 +1 @@
require "os/linux/global" if OS.linux?

View File

@ -0,0 +1,3 @@
module Homebrew
DEFAULT_PREFIX = "/home/linuxbrew/.linuxbrew".freeze
end

View File

@ -7,6 +7,7 @@ require "dependency_collector"
require "utils/bottles" require "utils/bottles"
require "patch" require "patch"
require "compilers" require "compilers"
require "global"
require "os/mac/version" require "os/mac/version"
class SoftwareSpec class SoftwareSpec
@ -325,8 +326,8 @@ class Bottle
end end
class BottleSpecification class BottleSpecification
DEFAULT_PREFIX = "/usr/local".freeze DEFAULT_PREFIX = Homebrew::DEFAULT_PREFIX
DEFAULT_CELLAR = "/usr/local/Cellar".freeze DEFAULT_CELLAR = "#{DEFAULT_PREFIX}/Cellar".freeze
attr_rw :prefix, :cellar, :rebuild attr_rw :prefix, :cellar, :rebuild
attr_accessor :tap attr_accessor :tap

View File

@ -112,9 +112,9 @@ class SystemConfig
f.puts "Core tap: N/A" f.puts "Core tap: N/A"
end end
defaults_hash = { defaults_hash = {
HOMEBREW_PREFIX: "/usr/local", HOMEBREW_PREFIX: Homebrew::DEFAULT_PREFIX,
HOMEBREW_REPOSITORY: "/usr/local/Homebrew", HOMEBREW_REPOSITORY: "#{Homebrew::DEFAULT_PREFIX}/Homebrew",
HOMEBREW_CELLAR: "/usr/local/Cellar", HOMEBREW_CELLAR: "#{Homebrew::DEFAULT_PREFIX}/Cellar",
HOMEBREW_CACHE: "#{ENV["HOME"]}/Library/Caches/Homebrew", HOMEBREW_CACHE: "#{ENV["HOME"]}/Library/Caches/Homebrew",
HOMEBREW_RUBY_WARNINGS: "-W0", HOMEBREW_RUBY_WARNINGS: "-W0",
HOMEBREW_TEMP: ENV["HOMEBREW_SYSTEM_TEMP"], HOMEBREW_TEMP: ENV["HOMEBREW_SYSTEM_TEMP"],

View File

@ -8,9 +8,9 @@ describe Utils::Analytics do
described_class.clear_os_prefix_ci described_class.clear_os_prefix_ci
end end
it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is not /usr/local" do it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is a custom prefix" do
stub_const("HOMEBREW_PREFIX", "blah") stub_const("HOMEBREW_PREFIX", "blah")
expect(described_class.os_prefix_ci).to include("#{OS_VERSION}, non-/usr/local") expect(described_class.os_prefix_ci).to include("#{OS_VERSION}, #{Utils::Analytics.custom_prefix_label}")
end end
it "includes CI when ENV['CI'] is set" do it "includes CI when ENV['CI'] is set" do
@ -18,9 +18,9 @@ describe Utils::Analytics do
expect(described_class.os_prefix_ci).to include("CI") expect(described_class.os_prefix_ci).to include("CI")
end end
it "does not include prefix when HOMEBREW_PREFIX is /usr/local" do it "does not include prefix when HOMEBREW_PREFIX is the default prefix" do
stub_const("HOMEBREW_PREFIX", "/usr/local") stub_const("HOMEBREW_PREFIX", Homebrew::DEFAULT_PREFIX)
expect(described_class.os_prefix_ci).not_to include("non-/usr/local") expect(described_class.os_prefix_ci).not_to include(Utils::Analytics.custom_prefix_label)
end end
end end
end end

View File

@ -256,8 +256,8 @@ module UpdateMigrator
end end
def migrate_legacy_repository_if_necessary def migrate_legacy_repository_if_necessary
return unless HOMEBREW_PREFIX.to_s == "/usr/local" return unless Homebrew.default_prefix?
return unless HOMEBREW_REPOSITORY.to_s == "/usr/local" return unless Homebrew.default_prefix?(HOMEBREW_REPOSITORY)
ohai "Migrating HOMEBREW_REPOSITORY (please wait)..." ohai "Migrating HOMEBREW_REPOSITORY (please wait)..."
@ -273,7 +273,7 @@ module UpdateMigrator
return return
end end
new_homebrew_repository = Pathname.new "/usr/local/Homebrew" new_homebrew_repository = Pathname.new "#{HOMEBREW_PREFIX}/Homebrew"
new_homebrew_repository.rmdir_if_possible new_homebrew_repository.rmdir_if_possible
if new_homebrew_repository.exist? if new_homebrew_repository.exist?
ofail <<~EOS ofail <<~EOS
@ -359,11 +359,13 @@ module UpdateMigrator
link_completions_manpages_and_docs(new_homebrew_repository) link_completions_manpages_and_docs(new_homebrew_repository)
ohai "Migrated HOMEBREW_REPOSITORY to #{new_homebrew_repository}!" ohai "Migrated HOMEBREW_REPOSITORY to #{new_homebrew_repository}!"
puts <<~EOS if HOMEBREW_PREFIX == "/usr/local"
Homebrew no longer needs to have ownership of /usr/local. If you wish you can puts <<~EOS
return /usr/local to its default ownership with: Homebrew no longer needs to have ownership of #{HOMEBREW_PREFIX}. If you wish you can
sudo chown root:wheel #{HOMEBREW_PREFIX} return #{HOMEBREW_PREFIX} to its default ownership with:
EOS sudo chown root:wheel #{HOMEBREW_PREFIX}
EOS
end
rescue => e rescue => e
ofail <<~EOS ofail <<~EOS
#{Tty.bold}Failed to migrate HOMEBREW_REPOSITORY to #{new_homebrew_repository}!#{Tty.reset} #{Tty.bold}Failed to migrate HOMEBREW_REPOSITORY to #{new_homebrew_repository}!#{Tty.reset}

View File

@ -3,6 +3,10 @@ require "erb"
module Utils module Utils
module Analytics module Analytics
class << self class << self
def custom_prefix_label
"generic-prefix".freeze
end
def clear_os_prefix_ci def clear_os_prefix_ci
return unless instance_variable_defined?(:@os_prefix_ci) return unless instance_variable_defined?(:@os_prefix_ci)
remove_instance_variable(:@os_prefix_ci) remove_instance_variable(:@os_prefix_ci)
@ -11,7 +15,7 @@ module Utils
def os_prefix_ci def os_prefix_ci
@os_prefix_ci ||= begin @os_prefix_ci ||= begin
os = OS_VERSION os = OS_VERSION
prefix = ", non-/usr/local" if HOMEBREW_PREFIX.to_s != "/usr/local" prefix = ", #{custom_prefix_label}" if HOMEBREW_PREFIX.to_s != Homebrew::DEFAULT_PREFIX
ci = ", CI" if ENV["CI"] ci = ", CI" if ENV["CI"]
"#{os}#{prefix}#{ci}" "#{os}#{prefix}#{ci}"
end end
@ -79,3 +83,5 @@ module Utils
end end
end end
end end
require "extend/os/analytics"

View File

@ -644,7 +644,7 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note
the list is formatted for export to `bash`(1) unless `--plain` is passed. the list is formatted for export to `bash`(1) unless `--plain` is passed.
* `--prefix`: * `--prefix`:
Display Homebrew's install path. *Default:* `/usr/local` Display Homebrew's install path. *Default:* `/usr/local` on macOS and `/home/linuxbrew/.linuxbrew` on Linux
* `--prefix` `formula`: * `--prefix` `formula`:
Display the location in the cellar where `formula` is or would be installed. Display the location in the cellar where `formula` is or would be installed.

View File

@ -581,7 +581,7 @@ Pass \fB\-\-shell=\fR\fIshell\fR to generate a list of environment variables for
If the command\'s output is sent through a pipe and no shell is specified, the list is formatted for export to \fBbash\fR(1) unless \fB\-\-plain\fR is passed\. If the command\'s output is sent through a pipe and no shell is specified, the list is formatted for export to \fBbash\fR(1) unless \fB\-\-plain\fR is passed\.
. .
.IP "\(bu" 4 .IP "\(bu" 4
\fB\-\-prefix\fR: Display Homebrew\'s install path\. \fIDefault:\fR \fB/usr/local\fR \fB\-\-prefix\fR: Display Homebrew\'s install path\. \fIDefault:\fR \fB/usr/local\fR on macOS and \fB/home/linuxbrew/\.linuxbrew\fR on Linux
. .
.IP "\(bu" 4 .IP "\(bu" 4
\fB\-\-prefix\fR \fIformula\fR: Display the location in the cellar where \fIformula\fR is or would be installed\. \fB\-\-prefix\fR \fIformula\fR: Display the location in the cellar where \fIformula\fR is or would be installed\.