Merge pull request #18010 from apainintheneck/refactor-method-to-remove-extra-tap-requires

This commit is contained in:
Mike McQuaid 2024-08-12 14:21:04 +01:00 committed by GitHub
commit e3f8081f0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 39 additions and 47 deletions

View File

@ -65,10 +65,8 @@ begin
internal_cmd = Commands.valid_internal_cmd?(cmd) || Commands.valid_internal_dev_cmd?(cmd) if cmd
unless internal_cmd
require "tap"
# Add contributed commands to PATH before checking.
homebrew_path.append(Tap.cmd_directories)
homebrew_path.append(Commands.tap_cmd_directories)
# External commands expect a normal PATH
ENV["PATH"] = homebrew_path.to_s

View File

@ -58,7 +58,7 @@ module Homebrew
info += ", #{private_count} private"
info += ", #{Utils.pluralize("formula", formula_count, plural: "e", include_count: true)}"
info += ", #{Utils.pluralize("command", command_count, include_count: true)}"
info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory?
info += ", #{HOMEBREW_TAP_DIRECTORY.dup.abv}" if HOMEBREW_TAP_DIRECTORY.directory?
puts info
else
info = ""

View File

@ -70,23 +70,17 @@ module Commands
# Ruby commands which can be `require`d without being run.
def self.external_ruby_v2_cmd_path(cmd)
require "tap"
path = which("#{cmd}.rb", Tap.cmd_directories)
path = which("#{cmd}.rb", tap_cmd_directories)
path if require?(path)
end
# Ruby commands which are run by being `require`d.
def self.external_ruby_cmd_path(cmd)
require "tap"
which("brew-#{cmd}.rb", PATH.new(ENV.fetch("PATH")).append(Tap.cmd_directories))
which("brew-#{cmd}.rb", PATH.new(ENV.fetch("PATH")).append(tap_cmd_directories))
end
def self.external_cmd_path(cmd)
require "tap"
which("brew-#{cmd}", PATH.new(ENV.fetch("PATH")).append(Tap.cmd_directories))
which("brew-#{cmd}", PATH.new(ENV.fetch("PATH")).append(tap_cmd_directories))
end
def self.path(cmd)
@ -107,6 +101,12 @@ module Commands
cmds.sort
end
# An array of all tap cmd directory {Pathname}s.
sig { returns(T::Array[Pathname]) }
def self.tap_cmd_directories
Pathname.glob HOMEBREW_TAP_DIRECTORY/"*/*/cmd"
end
def self.internal_commands_paths
find_commands HOMEBREW_CMD_PATH
end
@ -144,9 +144,7 @@ module Commands
end
def self.external_commands
require "tap"
Tap.cmd_directories.flat_map do |path|
tap_cmd_directories.flat_map do |path|
find_commands(path).select(&:executable?)
.map { basename_without_extension(_1) }
.map { |p| p.to_s.delete_prefix("brew-").strip }

View File

@ -764,7 +764,7 @@ module Homebrew
end
def check_for_external_cmd_name_conflict
cmds = Tap.cmd_directories.flat_map { |p| Dir["#{p}/brew-*"] }.uniq
cmds = Commands.tap_cmd_directories.flat_map { |p| Dir["#{p}/brew-*"] }.uniq
cmds = cmds.select { |cmd| File.file?(cmd) && File.executable?(cmd) }
cmd_map = {}
cmds.each do |cmd|

View File

@ -51,6 +51,9 @@ HOMEBREW_TEMP = Pathname(ENV.fetch("HOMEBREW_TEMP")).then do |tmp|
tmp.realpath
end.freeze
# Where installed taps live
HOMEBREW_TAP_DIRECTORY = (HOMEBREW_LIBRARY/"Taps").freeze
# The Ruby path and args to use for forked Ruby calls
HOMEBREW_RUBY_EXEC_ARGS = [
RUBY_PATH,

View File

@ -14,8 +14,6 @@ require "settings"
class Tap
extend Cachable
TAP_DIRECTORY = (HOMEBREW_LIBRARY/"Taps").freeze
HOMEBREW_TAP_CASK_RENAMES_FILE = "cask_renames.json"
private_constant :HOMEBREW_TAP_CASK_RENAMES_FILE
HOMEBREW_TAP_FORMULA_RENAMES_FILE = "formula_renames.json"
@ -201,7 +199,7 @@ class Tap
@repository = repository
@name = "#{@user}/#{@repository}".downcase
@full_name = "#{@user}/homebrew-#{@repository}"
@path = TAP_DIRECTORY/@full_name.downcase
@path = HOMEBREW_TAP_DIRECTORY/@full_name.downcase
@git_repository = GitRepository.new(@path)
end
@ -286,7 +284,7 @@ class Tap
sig { returns(String) }
def repository_var_suffix
@repository_var_suffix ||= path.to_s
.delete_prefix(TAP_DIRECTORY.to_s)
.delete_prefix(HOMEBREW_TAP_DIRECTORY.to_s)
.tr("^A-Za-z0-9", "_")
.upcase
end
@ -1021,8 +1019,8 @@ class Tap
# @api public
sig { returns(T::Array[Tap]) }
def self.installed
cache[:installed] ||= if TAP_DIRECTORY.directory?
TAP_DIRECTORY.subdirs.flat_map(&:subdirs).map { from_path(_1) }
cache[:installed] ||= if HOMEBREW_TAP_DIRECTORY.directory?
HOMEBREW_TAP_DIRECTORY.subdirs.flat_map(&:subdirs).map { from_path(_1) }
else
[]
end
@ -1060,12 +1058,6 @@ class Tap
map(&:name).sort
end
# An array of all tap cmd directory {Pathname}s.
sig { returns(T::Array[Pathname]) }
def self.cmd_directories
Pathname.glob TAP_DIRECTORY/"*/*/cmd"
end
# An array of official taps that have been manually untapped
sig { returns(T::Array[String]) }
def self.untapped_official_taps

View File

@ -8,10 +8,10 @@ RSpec.describe "Internal Tap JSON -- Formula", type: :system do
context "when generating JSON", :needs_macos do
before do
cp_r(TEST_FIXTURE_DIR/"internal_tap_json/homebrew-core", Tap::TAP_DIRECTORY/"homebrew")
cp_r(TEST_FIXTURE_DIR/"internal_tap_json/homebrew-core", HOMEBREW_TAP_DIRECTORY/"homebrew")
# NOTE: Symlinks can't be copied recursively so we create them manually here.
(Tap::TAP_DIRECTORY/"homebrew/homebrew-core").tap do |core_tap|
(HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-core").tap do |core_tap|
mkdir(core_tap/"Aliases")
ln_s(core_tap/"Formula/f/fennel.rb", core_tap/"Aliases/fennel-lang")
ln_s(core_tap/"Formula/p/ponyc.rb", core_tap/"Aliases/ponyc-lang")

View File

@ -53,7 +53,7 @@ RSpec.describe Commands do
FileUtils.touch "#{dir}/brew-t4"
allow(Tap).to receive(:cmd_directories).and_return([dir])
allow(described_class).to receive(:tap_cmd_directories).and_return([dir])
cmds = described_class.external_commands

View File

@ -8,7 +8,7 @@ RSpec.describe Homebrew::DevCmd::Extract do
context "when extracting a formula" do
let!(:target) do
path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
path = HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo"
(path/"Formula").mkpath
target = Tap.from_path(path)
core_tap = CoreTap.instance

View File

@ -80,7 +80,7 @@ RSpec.describe Homebrew::DevCmd::PrPull do
let(:tap) { Tap.fetch("Homebrew", "foo") }
let(:formula_file) { tap.path/"Formula/foo.rb" }
let(:cask_file) { tap.cask_dir/"food.rb" }
let(:path) { Pathname(Tap::TAP_DIRECTORY/"homebrew/homebrew-foo") }
let(:path) { Pathname(HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo") }
it_behaves_like "parseable arguments"

View File

@ -101,7 +101,7 @@ RSpec.describe Homebrew::Diagnostic::Checks do
FileUtils.chmod 0755, cmd
end
allow(Tap).to receive(:cmd_directories).and_return([path1, path2])
allow(Commands).to receive(:tap_cmd_directories).and_return([path1, path2])
expect(checks.check_for_external_cmd_name_conflict)
.to match("brew-foo")

View File

@ -11,9 +11,9 @@ RSpec.describe Homebrew::FormulaAuditor do
@count += 1
end
let(:formula_subpath) { "Formula/foo#{foo_version}.rb" }
let(:origin_tap_path) { Tap::TAP_DIRECTORY/"homebrew/homebrew-foo" }
let(:origin_tap_path) { HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo" }
let(:origin_formula_path) { origin_tap_path/formula_subpath }
let(:tap_path) { Tap::TAP_DIRECTORY/"homebrew/homebrew-bar" }
let(:tap_path) { HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-bar" }
let(:formula_path) { tap_path/formula_subpath }
def formula_auditor(name, text, options = {})

View File

@ -751,7 +751,7 @@ RSpec.describe Formulary do
end
after do
FileUtils.rm_rf Tap::TAP_DIRECTORY/"another"
FileUtils.rm_rf HOMEBREW_TAP_DIRECTORY/"another"
end
# FIXME

View File

@ -34,7 +34,7 @@ RSpec.describe Homebrew::MissingFormula do
subject { described_class.tap_migration_reason(formula) }
before do
tap_path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
tap_path = HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo"
tap_path.mkpath
(tap_path/"tap_migrations.json").write <<~JSON
{ "migrated-formula": "homebrew/bar" }
@ -58,7 +58,7 @@ RSpec.describe Homebrew::MissingFormula do
subject { described_class.deleted_reason(formula, silent: true) }
before do
tap_path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
tap_path = HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo"
(tap_path/"Formula").mkpath
(tap_path/"Formula/deleted-formula.rb").write "placeholder"
ENV.delete "GIT_AUTHOR_DATE"

View File

@ -5,7 +5,7 @@ require "rubocops/lines"
RSpec.describe RuboCop::Cop::FormulaAuditStrict::MakeCheck do
subject(:cop) { described_class.new }
let(:path) { Tap::TAP_DIRECTORY/"homebrew/homebrew-core" }
let(:path) { HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-core" }
before do
path.mkpath

View File

@ -210,7 +210,7 @@ RSpec.shared_context "integration test" do # rubocop:disable RSpec/ContextWordin
end
def setup_test_tap
path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
path = HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo"
path.mkpath
path.cd do
system "git", "init"

View File

@ -33,6 +33,7 @@ HOMEBREW_LOCKS = (HOMEBREW_PREFIX.parent/"locks").freeze
HOMEBREW_CELLAR = (HOMEBREW_PREFIX.parent/"cellar").freeze
HOMEBREW_LOGS = (HOMEBREW_PREFIX.parent/"logs").freeze
HOMEBREW_TEMP = (HOMEBREW_PREFIX.parent/"temp").freeze
HOMEBREW_TAP_DIRECTORY = (HOMEBREW_LIBRARY/"Taps").freeze
HOMEBREW_RUBY_EXEC_ARGS = [
RUBY_PATH,
ENV.fetch("HOMEBREW_RUBY_WARNINGS"),

View File

@ -8,7 +8,7 @@ RSpec.describe Tap do
subject(:homebrew_foo_tap) { described_class.fetch("Homebrew", "foo") }
let(:path) { Tap::TAP_DIRECTORY/"homebrew/homebrew-foo" }
let(:path) { HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-foo" }
let(:formula_file) { path/"Formula/foo.rb" }
let(:alias_file) { path/"Aliases/bar" }
let(:cmd_file) { path/"cmd/brew-tap-cmd.rb" }
@ -172,7 +172,7 @@ RSpec.describe Tap do
specify "#issues_url" do
t = described_class.fetch("someone", "foo")
path = Tap::TAP_DIRECTORY/"someone/homebrew-foo"
path = HOMEBREW_TAP_DIRECTORY/"someone/homebrew-foo"
path.mkpath
cd path do
system "git", "init"
@ -182,7 +182,7 @@ RSpec.describe Tap do
expect(t.issues_url).to eq("https://github.com/someone/homebrew-foo/issues")
expect(homebrew_foo_tap.issues_url).to eq("https://github.com/Homebrew/homebrew-foo/issues")
(Tap::TAP_DIRECTORY/"someone/homebrew-no-git").mkpath
(HOMEBREW_TAP_DIRECTORY/"someone/homebrew-no-git").mkpath
expect(described_class.fetch("someone", "no-git").issues_url).to be_nil
ensure
path.parent.rmtree
@ -378,7 +378,7 @@ RSpec.describe Tap do
end.to raise_error(ErrorDuringExecution)
expect(tap).not_to be_installed
expect(Tap::TAP_DIRECTORY/"user").not_to exist
expect(HOMEBREW_TAP_DIRECTORY/"user").not_to exist
end
end
@ -652,7 +652,7 @@ RSpec.describe Tap do
end
specify "files" do
path = Tap::TAP_DIRECTORY/"homebrew/homebrew-core"
path = HOMEBREW_TAP_DIRECTORY/"homebrew/homebrew-core"
formula_file = core_tap.formula_dir/"foo.rb"
core_tap.formula_dir.mkpath
formula_file.write <<~RUBY