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

Expose uses_from_macos list in formula API #6467

Merged
merged 2 commits into from
Oct 11, 2019
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
12 changes: 9 additions & 3 deletions Library/Homebrew/extend/os/mac/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ class SoftwareSpec
undef uses_from_macos

def uses_from_macos(deps, **args)
@uses_from_macos_elements ||= []

if deps.is_a?(Hash)
args = deps
deps = Hash[*args.shift]
end

depends_on(deps) if add_mac_dependency?(args)
if add_mac_dependency?(args)
depends_on(deps)
else
@uses_from_macos_elements << deps
end
end

private

def add_mac_dependency?(args)
args.each { |key, version| args[key] = OS::Mac::Version.from_symbol(version) }

return false if args[:after] && OS::Mac.version < args[:after]
return false if args[:after] && OS::Mac.version >= args[:after]

return false if args[:before] && OS::Mac.version >= args[:before]
return false if args[:before] && OS::Mac.version < args[:before]

args.present?
end
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ def aliases
# The {Dependency}s for the currently active {SoftwareSpec}.
delegate deps: :active_spec

# Dependencies provided by macOS for the currently active {SoftwareSpec}.
delegate uses_from_macos_elements: :active_spec

# The {Requirement}s for the currently active {SoftwareSpec}.
delegate requirements: :active_spec

Expand Down Expand Up @@ -1589,6 +1592,7 @@ def missing_dependencies(hide: nil)
# @private
def to_hash
dependencies = deps
uses_from_macos = uses_from_macos_elements || []

hsh = {
"name" => name,
Expand Down Expand Up @@ -1624,6 +1628,7 @@ def to_hash
"optional_dependencies" => dependencies.select(&:optional?)
.map(&:name)
.uniq,
"uses_from_macos" => uses_from_macos.uniq,
"requirements" => [],
"conflicts_with" => conflicts.map(&:name),
"caveats" => caveats&.gsub(HOMEBREW_PREFIX, "$(brew --prefix)"),
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SoftwareSpec
attr_reader :dependency_collector
attr_reader :bottle_specification
attr_reader :compiler_failures
attr_reader :uses_from_macos_elements

def_delegators :@resource, :stage, :fetch, :verify_download_integrity, :source_modified_time
def_delegators :@resource, :download_name, :cached_download, :clear_cache
Expand Down
21 changes: 12 additions & 9 deletions Library/Homebrew/test/os/mac/software_spec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,46 @@
allow(OS::Mac).to receive(:version).and_return(OS::Mac::Version.new(sierra_os_version))
end

it "allows specifying dependencies before certain version" do
it "allows specifying macOS dependencies before a certain version" do
spec.uses_from_macos("foo", before: :high_sierra)

expect(spec.deps.first.name).to eq("foo")
expect(spec.deps).to be_empty
expect(spec.uses_from_macos_elements.first).to eq("foo")
end

it "allows specifying dependencies after certain version" do
it "allows specifying macOS dependencies after a certain version" do
spec.uses_from_macos("foo", after: :el_capitan)

expect(spec.deps.first.name).to eq("foo")
expect(spec.deps).to be_empty
expect(spec.uses_from_macos_elements.first).to eq("foo")
end

it "doesn't adds a dependency if it doesn't meet OS version requirements" do
it "doesn't add a macOS dependency if the OS version doesn't meet requirements" do
spec.uses_from_macos("foo", after: :high_sierra)
spec.uses_from_macos("bar", before: :el_capitan)

expect(spec.deps).to be_empty
expect(spec.deps.first.name).to eq("foo")
expect(spec.uses_from_macos_elements).to be_empty
end

it "works with tags" do
spec.uses_from_macos("foo" => :head, :after => :el_capitan)
spec.uses_from_macos("foo" => :head, :after => :high_sierra)

dep = spec.deps.first

expect(dep.name).to eq("foo")
expect(dep.tags).to include(:head)
end

it "doesn't adds the dependency without OS version requirements" do
it "doesn't add a dependency if no OS version is specified" do
spec.uses_from_macos("foo")
spec.uses_from_macos("bar" => :head)

expect(spec.deps).to be_empty
end

it "respects OS version requirements with tags" do
spec.uses_from_macos("foo" => :head, :after => :mojave)
spec.uses_from_macos("foo" => :head, :before => :mojave)

expect(spec.deps).to be_empty
end
Expand Down