2016-08-18 22:11:42 +03:00
|
|
|
require "rubygems"
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
class DSL
|
2017-11-06 21:13:39 +01:00
|
|
|
class DependsOn < DelegateClass(Hash)
|
2016-09-24 13:52:43 +02:00
|
|
|
VALID_KEYS = Set.new [
|
2016-10-14 20:33:16 +02:00
|
|
|
: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 = {
|
2016-12-29 18:56:19 +00:00
|
|
|
intel: { type: :intel, bits: 64 },
|
2016-10-14 20:33:16 +02:00
|
|
|
# specific
|
|
|
|
x86_64: { type: :intel, bits: 64 },
|
|
|
|
}.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
|
2017-11-06 21:13:39 +01:00
|
|
|
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
|
|
|
|
2017-11-06 21:13:39 +01: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)
|
2017-11-06 21:13:39 +01: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))
|
2016-10-14 20:03:34 +02:00
|
|
|
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
|
2018-09-02 20:14:54 +01:00
|
|
|
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*$/
|
2016-10-14 20:11:33 +02:00
|
|
|
raise "'depends_on macos' comparison expressions cannot be combined" unless @macos.empty?
|
|
|
|
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)
|
2016-10-23 19:04:17 +02:00
|
|
|
args.map(&self.class.method(:coerce_os_release)).sort
|
2016-10-14 20:11:33 +02:00
|
|
|
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|
|
2016-12-29 18:56:19 +00:00
|
|
|
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?
|
|
|
|
@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)
|
|
|
|
@x11 = arg
|
|
|
|
end
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|