Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace removed constants with overridable methods #18517

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def refresh
@dsl.language_eval
end

DSL::DSL_METHODS.each do |method_name|
::Cask::DSL::DSL_METHODS.each do |method_name|
define_method(method_name) { |&block| @dsl.send(method_name, &block) }
end

Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down Expand Up @@ -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|
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
def check_for_broken_symlinks
broken_symlinks = []

Keg::MUST_EXIST_SUBDIRECTORIES.each do |d|
Keg.must_exist_subdirectories.each do |d|

Check warning on line 318 in Library/Homebrew/diagnostic.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/diagnostic.rb#L318

Added line #L318 was not covered by tests
next unless d.directory?

d.find do |path|
Expand Down Expand Up @@ -344,7 +344,7 @@
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?)

Check warning on line 347 in Library/Homebrew/diagnostic.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/diagnostic.rb#L347

Added line #L347 was not covered by tests
return if not_exist_dirs.empty?

<<~EOS
Expand All @@ -359,8 +359,8 @@

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
Expand Down
37 changes: 30 additions & 7 deletions Library/Homebrew/extend/os/mac/keg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
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
Expand All @@ -14,11 +12,6 @@ class Keg
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 +
Expand All @@ -29,6 +22,35 @@ class Keg
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)
Expand Down Expand Up @@ -121,4 +143,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
Expand Up @@ -1229,7 +1229,7 @@
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|

Check warning on line 1232 in Library/Homebrew/formula_installer.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_installer.rb#L1232

Added line #L1232 was not covered by tests
sandbox.allow_write_path "#{HOMEBREW_PREFIX}/#{dir}"
end
sandbox.run(*args)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 48 additions & 9 deletions Library/Homebrew/keg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ def to_s
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
Expand Down Expand Up @@ -146,6 +140,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
Expand Down Expand Up @@ -288,8 +327,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)
Expand All @@ -316,7 +355,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
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading