Skip to content

Commit

Permalink
Merge pull request #16421 from Bo98/dependency-cleanup
Browse files Browse the repository at this point in the history
dependency: cleanup unused code
  • Loading branch information
MikeMcQuaid authored Jan 2, 2024
2 parents 3e027de + e466417 commit 734123f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 45 deletions.
38 changes: 9 additions & 29 deletions Library/Homebrew/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ class Dependency
include Dependable
extend Cachable

attr_reader :name, :env_proc, :option_names, :tap
attr_reader :name, :tap

DEFAULT_ENV_PROC = proc {}.freeze
private_constant :DEFAULT_ENV_PROC

def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name&.split("/")&.last])
def initialize(name, tags = [])
raise ArgumentError, "Dependency must have a name!" unless name

@name = name
@tags = tags
@env_proc = env_proc
@option_names = option_names

@tap = Tap.fetch(Regexp.last_match(1), Regexp.last_match(2)) if name =~ HOMEBREW_TAP_FORMULA_REGEX
end
Expand Down Expand Up @@ -90,8 +85,8 @@ def missing_options(inherited_options)
required
end

def modify_build_environment
env_proc&.call
def option_names
[name.split("/").last].freeze
end

sig { overridable.returns(T::Boolean) }
Expand All @@ -104,18 +99,9 @@ def inspect
"#<#{self.class.name}: #{name.inspect} #{tags.inspect}>"
end

# Define marshaling semantics because we cannot serialize @env_proc.
def _dump(*)
Marshal.dump([name, tags])
end

def self._load(marshaled)
new(*Marshal.load(marshaled)) # rubocop:disable Security/MarshalLoad
end

sig { params(formula: Formula).returns(T.self_type) }
def dup_with_formula_name(formula)
self.class.new(formula.full_name.to_s, tags, env_proc, option_names)
self.class.new(formula.full_name.to_s, tags)
end

class << self
Expand Down Expand Up @@ -202,15 +188,9 @@ def merge_repeats(all)
deps = grouped.fetch(name)
dep = deps.first
tags = merge_tags(deps)
option_names = deps.flat_map(&:option_names).uniq
kwargs = {}
kwargs[:bounds] = dep.bounds if dep.uses_from_macos?
# TODO: simpify to just **kwargs when we require Ruby >= 2.7
if kwargs.empty?
dep.class.new(name, tags, dep.env_proc, option_names)
else
dep.class.new(name, tags, dep.env_proc, option_names, **kwargs)
end
dep.class.new(name, tags, **kwargs)
end
end

Expand Down Expand Up @@ -250,8 +230,8 @@ def merge_temporality(deps)
class UsesFromMacOSDependency < Dependency
attr_reader :bounds

def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name], bounds:)
super(name, tags, env_proc, option_names)
def initialize(name, tags = [], bounds:)
super(name, tags)

@bounds = bounds
end
Expand Down Expand Up @@ -293,7 +273,7 @@ def uses_from_macos?

sig { override.params(formula: Formula).returns(T.self_type) }
def dup_with_formula_name(formula)
self.class.new(formula.full_name.to_s, tags, env_proc, option_names, bounds: bounds)
self.class.new(formula.full_name.to_s, tags, bounds: bounds)
end

sig { returns(String) }
Expand Down
9 changes: 0 additions & 9 deletions Library/Homebrew/test/dependency_expansion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ def build_dep(name, tags = [], deps = [])
end
end

it "merges dependencies and preserves env_proc" do
env_proc = double
dep = described_class.new("foo", [], env_proc)
allow(dep).to receive(:to_formula).and_return \
instance_double(Formula, deps: [], name: "foo", full_name: "foo")
deps.replace([dep])
expect(described_class.expand(formula).first.env_proc).to eq(env_proc)
end

it "merges tags without duplicating them" do
foo2 = build_dep(:foo, ["option"])
foo3 = build_dep(:foo, ["option"])
Expand Down
10 changes: 3 additions & 7 deletions Library/Homebrew/test/dependency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@

describe "::merge_repeats" do
it "merges duplicate dependencies" do
dep = described_class.new("foo", [:build], nil, "foo")
dep2 = described_class.new("foo", ["bar"], nil, "foo2")
dep3 = described_class.new("xyz", ["abc"], nil, "foo")
dep = described_class.new("foo", [:build])
dep2 = described_class.new("foo", ["bar"])
dep3 = described_class.new("xyz", ["abc"])
merged = described_class.merge_repeats([dep, dep2, dep3])
expect(merged.count).to eq(2)
expect(merged.first).to be_a described_class

foo_named_dep = merged.find { |d| d.name == "foo" }
expect(foo_named_dep.tags).to eq(["bar"])
expect(foo_named_dep.option_names).to include("foo")
expect(foo_named_dep.option_names).to include("foo2")

xyz_named_dep = merged.find { |d| d.name == "xyz" }
expect(xyz_named_dep.tags).to eq(["abc"])
expect(xyz_named_dep.option_names).to include("foo")
expect(xyz_named_dep.option_names).not_to include("foo2")
end

it "merges necessity tags" do
Expand Down

0 comments on commit 734123f

Please sign in to comment.