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

129 lines
3.5 KiB
Ruby
Raw Normal View History

2016-08-18 22:11:42 +03:00
require "rubygems"
2016-09-24 13:52:43 +02:00
module Hbc
class DSL
class DependsOn
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-10-14 20:33:16 +02:00
intel: { type: :intel, bits: [32, 64] },
ppc: { type: :ppc, bits: [32, 64] },
# specific
i386: { type: :intel, bits: 32 },
x86_64: { type: :intel, bits: 64 },
ppc_7400: { type: :ppc, bits: 32 },
ppc_64: { type: :ppc, bits: 64 },
}.freeze
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
# Intentionally undocumented: catch variant spellings.
ARCH_SYNONYMS = {
2016-10-14 20:33:16 +02:00
x86_32: :i386,
x8632: :i386,
x8664: :x86_64,
intel_32: :i386,
intel32: :i386,
intel_64: :x86_64,
intel64: :x86_64,
amd_64: :x86_64,
amd64: :x86_64,
ppc7400: :ppc_7400,
ppc_32: :ppc_7400,
ppc32: :ppc_7400,
ppc64: :ppc_64,
}.freeze
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
attr_accessor :java
attr_accessor :pairs
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
@pairs ||= {}
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def load(pairs = {})
pairs.each do |key, value|
raise "invalid depends_on key: '#{key.inspect}'" unless VALID_KEYS.include?(key)
writer_method = "#{key}=".to_sym
@pairs[key] = send(writer_method, value)
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 StandardError
raise "invalid 'depends_on macos' value: #{arg.inspect}"
end
2016-08-18 22:11:42 +03:00
end
2016-09-24 13:52:43 +02:00
def formula=(*arg)
@formula ||= []
@formula.concat(Array(*arg))
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def cask=(*arg)
@cask ||= []
@cask.concat(Array(*arg))
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def macos=(*arg)
@macos ||= []
macos = if arg.count == 1 && arg.first =~ /^\s*(<|>|[=<>]=)\s*(\S+)\s*$/
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)
Array(*arg).map { |elt|
self.class.coerce_os_release(elt)
}.sort
end
2016-09-24 13:52:43 +02:00
@macos.concat(macos)
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def arch=(*arg)
@arch ||= []
arches = Array(*arg).map { |elt|
elt = elt.to_s.downcase.sub(/^:/, "").tr("-", "_").to_sym
2016-09-24 13:52:43 +02:00
ARCH_SYNONYMS.key?(elt) ? ARCH_SYNONYMS[elt] : elt
}
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
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def to_yaml
@pairs.to_yaml
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def to_s
@pairs.inspect
end
end
2016-08-18 22:11:42 +03:00
end
end