brew/Library/Homebrew/cask/dsl/depends_on.rb

100 lines
2.8 KiB
Ruby
Raw Normal View History

2016-08-18 22:11:42 +03:00
require "rubygems"
2018-09-06 08:29:14 +02:00
module Cask
2016-09-24 13:52:43 +02:00
class DSL
class DependsOn < DelegateClass(Hash)
2019-04-19 21:46:20 +09:00
VALID_KEYS = Set.new([
:formula,
:cask,
:macos,
:arch,
:x11,
:java,
]).freeze
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
VALID_ARCHES = {
2018-11-02 17:18:07 +00:00
intel: { type: :intel, bits: 64 },
2016-10-14 20:33:16 +02:00
# specific
2018-11-02 17:18:07 +00:00
x86_64: { type: :intel, bits: 64 },
2016-10-14 20:33:16 +02:00
}.freeze
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
attr_accessor :java
attr_reader :arch, :cask, :formula, :macos, :x11
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def initialize
super({})
2017-06-28 17:53:59 +02:00
@cask ||= []
@formula ||= []
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
def load(**pairs)
2016-09-24 13:52:43 +02:00
pairs.each do |key, value|
raise "invalid depends_on key: '#{key.inspect}'" unless VALID_KEYS.include?(key)
2018-09-17 02:45:00 +02:00
self[key] = send(:"#{key}=", *value)
2016-09-24 13:52:43 +02:00
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.coerce_os_release(arg)
@macos_symbols ||= MacOS::Version::SYMBOLS
@inverted_macos_symbols ||= @macos_symbols.invert
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
begin
if arg.is_a?(Symbol)
Gem::Version.new(@macos_symbols.fetch(arg))
elsif arg =~ /^\s*:?([a-z]\S+)\s*$/i
2016-09-24 13:52:43 +02:00
Gem::Version.new(@macos_symbols.fetch(Regexp.last_match[1].downcase.to_sym))
elsif @inverted_macos_symbols.key?(arg)
Gem::Version.new(arg)
else
raise
end
rescue
2016-09-24 13:52:43 +02:00
raise "invalid 'depends_on macos' value: #{arg.inspect}"
end
2016-08-18 22:11:42 +03:00
end
2016-10-23 19:04:17 +02:00
def formula=(*args)
@formula.concat(args)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-10-23 19:04:17 +02:00
def cask=(*args)
@cask.concat(args)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-10-23 19:04:17 +02:00
def macos=(*args)
2016-09-24 13:52:43 +02:00
@macos ||= []
2016-10-23 19:04:17 +02:00
macos = if args.count == 1 && args.first =~ /^\s*(<|>|[=<>]=)\s*(\S+)\s*$/
raise "'depends_on macos' comparison expressions cannot be combined" unless @macos.empty?
2018-09-17 02:45:00 +02:00
operator = Regexp.last_match[1].to_sym
release = self.class.coerce_os_release(Regexp.last_match[2])
[[operator, release]]
else
raise "'depends_on macos' comparison expressions cannot be combined" if @macos.first.is_a?(Symbol)
2018-09-17 02:45:00 +02:00
2016-10-23 19:04:17 +02:00
args.map(&self.class.method(:coerce_os_release)).sort
end
2016-09-24 13:52:43 +02:00
@macos.concat(macos)
end
2016-08-18 22:11:42 +03:00
2016-10-23 19:04:17 +02:00
def arch=(*args)
2016-09-24 13:52:43 +02:00
@arch ||= []
2016-10-23 19:04:17 +02:00
arches = args.map do |elt|
elt.to_s.downcase.sub(/^:/, "").tr("-", "_").to_sym
2016-10-23 14:44:14 +02:00
end
2016-09-24 13:52:43 +02:00
invalid_arches = arches - VALID_ARCHES.keys
raise "invalid 'depends_on arch' values: #{invalid_arches.inspect}" unless invalid_arches.empty?
2018-09-17 02:45:00 +02:00
2016-09-24 13:52:43 +02:00
@arch.concat(arches.map { |arch| VALID_ARCHES[arch] })
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def x11=(arg)
raise "invalid 'depends_on x11' value: #{arg.inspect}" unless [true, false].include?(arg)
2018-09-17 02:45:00 +02:00
2016-09-24 13:52:43 +02:00
@x11 = arg
end
end
2016-08-18 22:11:42 +03:00
end
end