Skip to content

Commit

Permalink
DSL: add conflicts_with stanza
Browse files Browse the repository at this point in the history
Forgotten in Homebrew#4688, but should be considered part of Cask DSL 1.0.
A `depends_on` stanza is much less useful without the corresponding
`conflicts_with`.

References: Homebrew#4896
  • Loading branch information
rolandwalker committed Jul 29, 2014
1 parent eb2c22d commit 44f1a37
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Cask; end
require 'cask/checkable'
require 'cask/cli'
require 'cask/caveats'
require 'cask/conflicts_with'
require 'cask/container'
require 'cask/depends_on'
require 'cask/download'
Expand Down
31 changes: 31 additions & 0 deletions lib/cask/conflicts_with.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Cask::ConflictsWith

VALID_KEYS = Set.new [
:formula,
:cask,
:macos,
:arch,
:x11,
:java,
]

attr_accessor *VALID_KEYS
attr_accessor :pairs

def initialize(pairs={})
@pairs = pairs
pairs.each do |key, value|
raise "invalid conflicts_with key: '#{key.inspect}'" unless VALID_KEYS.include?(key)
writer_method = "#{key}=".to_sym
send(writer_method, value)
end
end

def to_yaml
@pairs.to_yaml
end

def to_s
@pairs.inspect
end
end
14 changes: 14 additions & 0 deletions lib/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def depends_on_formula; self.class.depends_on_formula; end

def depends_on; self.class.depends_on; end

def conflicts_with; self.class.conflicts_with; end

def container_type; self.class.container_type; end

def tags; self.class.tags; end
Expand Down Expand Up @@ -137,6 +139,18 @@ def depends_on(*args)
@depends_on
end

def conflicts_with(*args)
if @conflicts_with and !args.empty?
# todo: remove this constraint, and instead merge multiple conflicts_with stanzas
raise CaskInvalidError.new(self.title, "'conflicts_with' stanza may only appear once")
end
@conflicts_with ||= begin
Cask::ConflictsWith.new(*args) unless args.empty?
rescue StandardError => e
raise CaskInvalidError.new(self.title, e)
end
end

def artifacts
@artifacts ||= Hash.new { |hash, key| hash[key] = Set.new }
end
Expand Down
1 change: 1 addition & 0 deletions lib/cask/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def dumpcask
:artifacts,
:caveats,
:depends_on_formula,
:conflicts_with,
:container_type,
:gpg,
].each do |method|
Expand Down
13 changes: 13 additions & 0 deletions test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ def caveats; <<-EOS.undent
end
end

describe "conflicts_with stanza" do
it "allows conflicts_with stanza to be specified" do
cask = Cask.load('with-conflicts-with')
cask.conflicts_with.formula.wont_be_nil
end

it "refuses to load invalid conflicts_with key" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-conflicts-with-key')
}.must_raise(CaskInvalidError)
end
end

describe "license stanza" do
it "allows the license to be specified" do
cask = Cask.load('with-license')
Expand Down
8 changes: 8 additions & 0 deletions test/support/Casks/invalid/invalid-conflicts-with-key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class InvalidConflictsWithKey < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-conflicts-with-key'
conflicts_with :no_such_key => 'unar'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
8 changes: 8 additions & 0 deletions test/support/Casks/with-conflicts-with.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class WithConflictsWith < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/with-conflicts-with'
conflicts_with :formula => 'unar'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end

0 comments on commit 44f1a37

Please sign in to comment.