Skip to content

Commit

Permalink
Replace removable constants with overridable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Oct 8, 2024
1 parent 6975eaa commit 2d16333
Showing 7 changed files with 88 additions and 68 deletions.
6 changes: 3 additions & 3 deletions Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
@@ -555,7 +555,7 @@ def cleanup_cache_db(rack = nil)
end

def rm_ds_store(dirs = nil)
dirs ||= Keg::MUST_EXIST_DIRECTORIES + [
dirs ||= Keg.must_exist_directories + [
HOMEBREW_PREFIX/"Caskroom",
]
dirs.select(&:directory?)
@@ -623,7 +623,7 @@ def prune_prefix_symlinks_and_directories
dirs = []
children_count = {}

Keg::MUST_EXIST_SUBDIRECTORIES.each do |dir|
Keg.must_exist_subdirectories.each do |dir|
next unless dir.directory?

dir.find do |path|
@@ -639,7 +639,7 @@ def prune_prefix_symlinks_and_directories
path.unlink
end
end
elsif path.directory? && Keg::MUST_EXIST_SUBDIRECTORIES.exclude?(path)
elsif path.directory? && Keg.must_exist_subdirectories.exclude?(path)
dirs << path
children_count[path] = path.children.length if dry_run?
end
8 changes: 4 additions & 4 deletions Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
@@ -315,7 +315,7 @@ def check_for_stray_headers
def check_for_broken_symlinks
broken_symlinks = []

Keg::MUST_EXIST_SUBDIRECTORIES.each do |d|
Keg.must_exist_subdirectories.each do |d|
next unless d.directory?

d.find do |path|
@@ -344,7 +344,7 @@ def check_tmpdir_sticky_bit
def check_exist_directories
return if HOMEBREW_PREFIX.writable?

not_exist_dirs = Keg::MUST_EXIST_DIRECTORIES.reject(&:exist?)
not_exist_dirs = Keg.must_exist_directories.reject(&:exist?)
return if not_exist_dirs.empty?

<<~EOS
@@ -359,8 +359,8 @@ def check_exist_directories

def check_access_directories
not_writable_dirs =
Keg::MUST_BE_WRITABLE_DIRECTORIES.select(&:exist?)
.reject(&:writable?)
Keg.must_be_writable_directories.select(&:exist?)
.reject(&:writable?)
return if not_writable_dirs.empty?

<<~EOS
53 changes: 30 additions & 23 deletions Library/Homebrew/extend/os/mac/keg.rb
Original file line number Diff line number Diff line change
@@ -3,32 +3,38 @@

require "system_command"

class Keg
include SystemCommand::Mixin

# TODO: re-implement these as functions, so that we aren't modifying constants:
GENERIC_KEG_LINK_DIRECTORIES = (remove_const :KEG_LINK_DIRECTORIES).freeze
KEG_LINK_DIRECTORIES = (GENERIC_KEG_LINK_DIRECTORIES + ["Frameworks"]).freeze
GENERIC_MUST_EXIST_SUBDIRECTORIES = (remove_const :MUST_EXIST_SUBDIRECTORIES).freeze
MUST_EXIST_SUBDIRECTORIES = (
GENERIC_MUST_EXIST_SUBDIRECTORIES +
[HOMEBREW_PREFIX/"Frameworks"]
).sort.uniq.freeze
GENERIC_MUST_EXIST_DIRECTORIES = (remove_const :MUST_EXIST_DIRECTORIES).freeze
MUST_EXIST_DIRECTORIES = (
GENERIC_MUST_EXIST_DIRECTORIES +
[HOMEBREW_PREFIX/"Frameworks"]
).sort.uniq.freeze
GENERIC_MUST_BE_WRITABLE_DIRECTORIES = (remove_const :MUST_BE_WRITABLE_DIRECTORIES).freeze
MUST_BE_WRITABLE_DIRECTORIES = (
GENERIC_MUST_BE_WRITABLE_DIRECTORIES +
[HOMEBREW_PREFIX/"Frameworks"]
).sort.uniq.freeze
end

module OS
module Mac
module Keg
include SystemCommand::Mixin

module ClassMethods
def keg_link_directories
@keg_link_directories ||= (super + ["Frameworks"]).freeze
end

def must_exist_subdirectories
@must_exist_subdirectories ||= (
super +
[HOMEBREW_PREFIX/"Frameworks"]
).sort.uniq.freeze
end

def must_exist_directories
@must_exist_directories = (
super +
[HOMEBREW_PREFIX/"Frameworks"]
).sort.uniq.freeze
end

def must_be_writable_directories
@must_be_writable_directories ||= (
super +
[HOMEBREW_PREFIX/"Frameworks"]
).sort.uniq.freeze
end
end

def binary_executable_or_library_files = mach_o_files

def codesign_patched_binary(file)
@@ -121,4 +127,5 @@ def consistent_reproducible_symlink_permissions!
end
end

Keg.singleton_class.prepend(OS::Mac::Keg::ClassMethods)
Keg.prepend(OS::Mac::Keg)
2 changes: 1 addition & 1 deletion Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
@@ -1229,7 +1229,7 @@ def post_install
sandbox.deny_write_homebrew_repository
sandbox.allow_write_cellar(formula)
sandbox.deny_all_network unless formula.network_access_allowed?(:postinstall)
Keg::KEG_LINK_DIRECTORIES.each do |dir|
Keg.keg_link_directories.each do |dir|
sandbox.allow_write_path "#{HOMEBREW_PREFIX}/#{dir}"
end
sandbox.run(*args)
2 changes: 1 addition & 1 deletion Library/Homebrew/install.rb
Original file line number Diff line number Diff line change
@@ -325,7 +325,7 @@ def check_cc_argv(cc)
end

def attempt_directory_creation
Keg::MUST_EXIST_DIRECTORIES.each do |dir|
Keg.must_exist_directories.each do |dir|
FileUtils.mkdir_p(dir) unless dir.exist?
rescue
nil
83 changes: 48 additions & 35 deletions Library/Homebrew/keg.rb
Original file line number Diff line number Diff line change
@@ -78,38 +78,6 @@ def to_s
# Locale-specific directories have the form `language[_territory][.codeset][@modifier]`
LOCALEDIR_RX = %r{(locale|man)/([a-z]{2}|C|POSIX)(_[A-Z]{2})?(\.[a-zA-Z\-0-9]+(@.+)?)?}
INFOFILE_RX = %r{info/([^.].*?\.info(\.gz)?|dir)$}
KEG_LINK_DIRECTORIES = %w[
bin etc include lib sbin share var
].freeze
MUST_EXIST_SUBDIRECTORIES = (
KEG_LINK_DIRECTORIES - %w[var] + %w[
opt
var/homebrew/linked
]
).map { |dir| HOMEBREW_PREFIX/dir }.sort.uniq.freeze

# Keep relatively in sync with
# {https://github.com/Homebrew/install/blob/HEAD/install.sh}
MUST_EXIST_DIRECTORIES = (MUST_EXIST_SUBDIRECTORIES + [
HOMEBREW_CELLAR,
].sort.uniq).freeze
MUST_BE_WRITABLE_DIRECTORIES = (
%w[
etc/bash_completion.d lib/pkgconfig
share/aclocal share/doc share/info share/locale share/man
share/man/man1 share/man/man2 share/man/man3 share/man/man4
share/man/man5 share/man/man6 share/man/man7 share/man/man8
share/zsh share/zsh/site-functions
var/log
].map { |dir| HOMEBREW_PREFIX/dir } + MUST_EXIST_SUBDIRECTORIES + [
HOMEBREW_CACHE,
HOMEBREW_CELLAR,
HOMEBREW_LOCKS,
HOMEBREW_LOGS,
HOMEBREW_REPOSITORY,
Language::Python.homebrew_site_packages,
]
).sort.uniq.freeze

# These paths relative to the keg's share directory should always be real
# directories in the prefix, never symlinks.
@@ -146,6 +114,51 @@ def self.all
Formula.racks.flat_map(&:subdirs).map { |d| new(d) }
end

def self.keg_link_directories
@keg_link_directories ||= %w[
bin etc include lib sbin share var
].freeze
end

def self.must_exist_subdirectories
@must_exist_subdirectories ||= (
keg_link_directories - %w[var] + %w[
opt
var/homebrew/linked
]
).map { |dir| HOMEBREW_PREFIX/dir }.sort.uniq.freeze
end

# Keep relatively in sync with
# {https://github.com/Homebrew/install/blob/HEAD/install.sh}
def self.must_exist_directories
@must_exist_directories ||= (must_exist_subdirectories + [
HOMEBREW_CELLAR,
].sort.uniq).freeze
end

# Keep relatively in sync with
# {https://github.com/Homebrew/install/blob/HEAD/install.sh}
def self.must_be_writable_directories
@must_be_writable_directories ||= (
%w[
etc/bash_completion.d lib/pkgconfig
share/aclocal share/doc share/info share/locale share/man
share/man/man1 share/man/man2 share/man/man3 share/man/man4
share/man/man5 share/man/man6 share/man/man7 share/man/man8
share/zsh share/zsh/site-functions
var/log
].map { |dir| HOMEBREW_PREFIX/dir } + must_exist_subdirectories + [
HOMEBREW_CACHE,
HOMEBREW_CELLAR,
HOMEBREW_LOCKS,
HOMEBREW_LOGS,
HOMEBREW_REPOSITORY,
Language::Python.homebrew_site_packages,
]
).sort.uniq.freeze
end

attr_reader :path, :name, :linked_keg_record, :opt_record

protected :path
@@ -288,8 +301,8 @@ def unlink(verbose: false, dry_run: false)

dirs = []

keg_directories = KEG_LINK_DIRECTORIES.map { |d| path/d }
.select(&:exist?)
keg_directories = self.class.keg_link_directories.map { |d| path/d }
.select(&:exist?)
keg_directories.each do |dir|
dir.find do |src|
dst = HOMEBREW_PREFIX + src.relative_path_from(path)
@@ -316,7 +329,7 @@ def unlink(verbose: false, dry_run: false)
unless dry_run
remove_old_aliases
remove_linked_keg_record if linked?
(dirs - MUST_EXIST_SUBDIRECTORIES).reverse_each(&:rmdir_if_possible)
(dirs - self.class.must_exist_subdirectories).reverse_each(&:rmdir_if_possible)
end

ObserverPathnameExtension.n
2 changes: 1 addition & 1 deletion Library/Homebrew/test/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -272,7 +272,7 @@

FileUtils.rm_rf [
*TEST_DIRECTORIES,
*Keg::MUST_EXIST_SUBDIRECTORIES,
*Keg.must_exist_subdirectories,
HOMEBREW_LINKED_KEGS,
HOMEBREW_PINNED_KEGS,
HOMEBREW_PREFIX/"var",

0 comments on commit 2d16333

Please sign in to comment.