2016-08-18 22:11:42 +03:00
|
|
|
require "set"
|
2016-09-28 00:48:19 +02:00
|
|
|
require "locale"
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
require "hbc/artifact"
|
|
|
|
|
2016-08-18 22:11:42 +03:00
|
|
|
require "hbc/dsl/appcast"
|
|
|
|
require "hbc/dsl/base"
|
|
|
|
require "hbc/dsl/caveats"
|
|
|
|
require "hbc/dsl/conflicts_with"
|
|
|
|
require "hbc/dsl/container"
|
|
|
|
require "hbc/dsl/depends_on"
|
|
|
|
require "hbc/dsl/gpg"
|
|
|
|
require "hbc/dsl/postflight"
|
|
|
|
require "hbc/dsl/preflight"
|
|
|
|
require "hbc/dsl/stanza_proxy"
|
|
|
|
require "hbc/dsl/uninstall_postflight"
|
|
|
|
require "hbc/dsl/uninstall_preflight"
|
|
|
|
require "hbc/dsl/version"
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
class DSL
|
2017-04-06 00:33:31 +02:00
|
|
|
ORDINARY_ARTIFACT_CLASSES = [
|
|
|
|
Artifact::Installer,
|
|
|
|
Artifact::App,
|
|
|
|
Artifact::Artifact,
|
|
|
|
Artifact::AudioUnitPlugin,
|
|
|
|
Artifact::Binary,
|
|
|
|
Artifact::Colorpicker,
|
|
|
|
Artifact::Dictionary,
|
|
|
|
Artifact::Font,
|
|
|
|
Artifact::InputMethod,
|
|
|
|
Artifact::InternetPlugin,
|
|
|
|
Artifact::Pkg,
|
|
|
|
Artifact::Prefpane,
|
|
|
|
Artifact::Qlplugin,
|
|
|
|
Artifact::ScreenSaver,
|
|
|
|
Artifact::Service,
|
|
|
|
Artifact::StageOnly,
|
|
|
|
Artifact::Suite,
|
|
|
|
Artifact::VstPlugin,
|
|
|
|
Artifact::Vst3Plugin,
|
|
|
|
Artifact::Uninstall,
|
|
|
|
Artifact::Zap,
|
2016-10-14 20:33:16 +02:00
|
|
|
].freeze
|
2016-09-24 13:52:43 +02:00
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
ACTIVATABLE_ARTIFACT_TYPES = (ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key) - [:stage_only]).freeze
|
2016-09-24 13:52:43 +02:00
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
ARTIFACT_BLOCK_CLASSES = [
|
|
|
|
Artifact::PreflightBlock,
|
|
|
|
Artifact::PostflightBlock,
|
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
|
|
|
DSL_METHODS = Set.new [
|
2016-10-14 20:33:16 +02:00
|
|
|
:accessibility_access,
|
|
|
|
:appcast,
|
|
|
|
:artifacts,
|
|
|
|
:auto_updates,
|
|
|
|
:caskroom_path,
|
|
|
|
:caveats,
|
|
|
|
:conflicts_with,
|
|
|
|
:container,
|
|
|
|
:depends_on,
|
|
|
|
:gpg,
|
|
|
|
:homepage,
|
|
|
|
:language,
|
|
|
|
:name,
|
|
|
|
:sha256,
|
|
|
|
:staged_path,
|
|
|
|
:url,
|
|
|
|
:version,
|
|
|
|
:appdir,
|
2017-04-06 00:33:31 +02:00
|
|
|
*ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key),
|
2016-10-14 20:33:16 +02:00
|
|
|
*ACTIVATABLE_ARTIFACT_TYPES,
|
2017-04-06 00:33:31 +02:00
|
|
|
*ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] },
|
2016-10-14 20:33:16 +02:00
|
|
|
].freeze
|
2016-09-24 13:52:43 +02:00
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
attr_reader :token, :cask
|
|
|
|
def initialize(cask)
|
|
|
|
@cask = cask
|
|
|
|
@token = cask.token
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def name(*args)
|
|
|
|
@name ||= []
|
|
|
|
return @name if args.empty?
|
|
|
|
@name.concat(args.flatten)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-07-30 16:26:44 +02:00
|
|
|
def set_unique_stanza(stanza, should_return)
|
|
|
|
return instance_variable_get("@#{stanza}") if should_return
|
|
|
|
|
|
|
|
if instance_variable_defined?("@#{stanza}")
|
2017-04-06 00:33:31 +02:00
|
|
|
raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.")
|
2017-07-30 16:26:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
instance_variable_set("@#{stanza}", yield)
|
2017-04-06 00:33:31 +02:00
|
|
|
rescue CaskInvalidError
|
|
|
|
raise
|
2017-07-30 16:26:44 +02:00
|
|
|
rescue StandardError => e
|
2017-04-06 00:33:31 +02:00
|
|
|
raise CaskInvalidError.new(cask, "'#{stanza}' stanza failed with: #{e}")
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def homepage(homepage = nil)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:homepage, homepage.nil?) { homepage }
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-28 00:48:19 +02:00
|
|
|
def language(*args, default: false, &block)
|
2016-09-14 23:11:21 +02:00
|
|
|
if !args.empty? && block_given?
|
2016-09-25 22:13:44 +02:00
|
|
|
@language_blocks ||= {}
|
|
|
|
@language_blocks[args] = block
|
2016-09-28 00:57:19 +02:00
|
|
|
|
|
|
|
return unless default
|
|
|
|
|
|
|
|
unless @language_blocks.default.nil?
|
2017-04-06 00:33:31 +02:00
|
|
|
raise CaskInvalidError.new(cask, "Only one default language may be defined.")
|
2016-09-28 00:57:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@language_blocks.default = block
|
2016-09-14 23:11:21 +02:00
|
|
|
else
|
2016-09-18 04:15:28 +02:00
|
|
|
language_eval
|
2016-09-14 23:11:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-18 04:15:28 +02:00
|
|
|
def language_eval
|
2016-10-03 02:34:32 +02:00
|
|
|
return @language if instance_variable_defined?(:@language)
|
2016-09-25 22:13:44 +02:00
|
|
|
|
2017-05-29 18:24:52 +01:00
|
|
|
return @language = nil if @language_blocks.nil? || @language_blocks.empty?
|
2016-09-25 22:13:44 +02:00
|
|
|
|
2016-10-03 02:34:32 +02:00
|
|
|
MacOS.languages.map(&Locale.method(:parse)).each do |locale|
|
2016-10-23 14:44:14 +02:00
|
|
|
key = @language_blocks.keys.detect do |strings|
|
2016-09-28 00:48:19 +02:00
|
|
|
strings.any? { |string| locale.include?(string) }
|
2016-10-23 14:44:14 +02:00
|
|
|
end
|
2016-09-28 00:48:19 +02:00
|
|
|
|
2016-10-03 02:34:32 +02:00
|
|
|
next if key.nil?
|
|
|
|
|
|
|
|
return @language = @language_blocks[key].call
|
|
|
|
end
|
2016-09-25 22:13:44 +02:00
|
|
|
|
2016-09-28 00:48:19 +02:00
|
|
|
@language = @language_blocks.default.call
|
2016-09-18 04:15:28 +02:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def url(*args, &block)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:url, args.empty? && !block_given?) do
|
|
|
|
begin
|
|
|
|
URL.from(*args, &block)
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def appcast(*args)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:appcast, args.empty?) { DSL::Appcast.new(*args) }
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def gpg(*args)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:gpg, args.empty?) { DSL::Gpg.new(*args) }
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
|
|
|
|
def container(*args)
|
|
|
|
# TODO: remove this constraint, and instead merge multiple container stanzas
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:container, args.empty?) do
|
|
|
|
begin
|
|
|
|
DSL::Container.new(*args).tap do |container|
|
|
|
|
# TODO: remove this backward-compatibility section after removing nested_container
|
2017-09-24 19:24:46 +01:00
|
|
|
if container&.nested
|
2017-04-06 00:33:31 +02:00
|
|
|
artifacts[:nested_container] << Artifact::NestedContainer.new(cask, container.nested)
|
2017-07-30 16:26:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def version(arg = nil)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:version, arg.nil?) do
|
|
|
|
if !arg.is_a?(String) && arg != :latest
|
2017-04-06 00:33:31 +02:00
|
|
|
raise CaskInvalidError.new(cask, "invalid 'version' value: '#{arg.inspect}'")
|
2017-07-30 16:26:44 +02:00
|
|
|
end
|
|
|
|
DSL::Version.new(arg)
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def sha256(arg = nil)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:sha256, arg.nil?) do
|
|
|
|
if !arg.is_a?(String) && arg != :no_check
|
2017-04-06 00:33:31 +02:00
|
|
|
raise CaskInvalidError.new(cask, "invalid 'sha256' value: '#{arg.inspect}'")
|
2017-07-30 16:26:44 +02:00
|
|
|
end
|
|
|
|
arg
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
# depends_on uses a load method so that multiple stanzas can be merged
|
|
|
|
def depends_on(*args)
|
|
|
|
@depends_on ||= DSL::DependsOn.new
|
2017-06-28 17:53:59 +02:00
|
|
|
return @depends_on if args.empty?
|
2016-09-24 13:52:43 +02:00
|
|
|
begin
|
2017-06-28 17:53:59 +02:00
|
|
|
@depends_on.load(*args)
|
2016-09-24 13:52:43 +02:00
|
|
|
rescue RuntimeError => e
|
2017-04-06 00:33:31 +02:00
|
|
|
raise CaskInvalidError.new(cask, e)
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
@depends_on
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def conflicts_with(*args)
|
|
|
|
# TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:conflicts_with, args.empty?) { DSL::ConflictsWith.new(*args) }
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def artifacts
|
|
|
|
@artifacts ||= Hash.new { |hash, key| hash[key] = Set.new }
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def caskroom_path
|
|
|
|
@caskroom_path ||= Hbc.caskroom.join(token)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def staged_path
|
|
|
|
return @staged_path if @staged_path
|
|
|
|
cask_version = version || :unknown
|
|
|
|
@staged_path = caskroom_path.join(cask_version.to_s)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def caveats(*string, &block)
|
|
|
|
@caveats ||= []
|
|
|
|
if block_given?
|
|
|
|
@caveats << Hbc::Caveats.new(block)
|
|
|
|
elsif string.any?
|
2016-10-14 20:03:34 +02:00
|
|
|
@caveats << string.map { |s| s.to_s.sub(/[\r\n \t]*\Z/, "\n\n") }
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
@caveats
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def accessibility_access(accessibility_access = nil)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:accessibility_access, accessibility_access.nil?) { accessibility_access }
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def auto_updates(auto_updates = nil)
|
2017-07-30 16:26:44 +02:00
|
|
|
set_unique_stanza(:auto_updates, auto_updates.nil?) { auto_updates }
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
ORDINARY_ARTIFACT_CLASSES.each do |klass|
|
|
|
|
type = klass.dsl_key
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
define_method(type) do |*args|
|
2017-04-06 00:33:31 +02:00
|
|
|
begin
|
|
|
|
if [*artifacts.keys, type].include?(:stage_only) && (artifacts.keys & ACTIVATABLE_ARTIFACT_TYPES).any?
|
|
|
|
raise CaskInvalidError.new(cask, "'stage_only' must be the only activatable artifact.")
|
2017-02-05 22:40:14 +01:00
|
|
|
end
|
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
artifacts[type].add(klass.from_args(cask, *args))
|
|
|
|
rescue CaskInvalidError
|
|
|
|
raise
|
|
|
|
rescue StandardError => e
|
|
|
|
raise CaskInvalidError.new(cask, "invalid '#{klass.dsl_key}' stanza: #{e}")
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
ARTIFACT_BLOCK_CLASSES.each do |klass|
|
|
|
|
[klass.dsl_key, klass.uninstall_dsl_key].each do |dsl_key|
|
|
|
|
define_method(dsl_key) do |&block|
|
|
|
|
artifacts[dsl_key] << block
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def method_missing(method, *)
|
|
|
|
if method
|
|
|
|
Utils.method_missing_message(method, token)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2016-09-20 15:11:33 +02:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def respond_to_missing?(*)
|
|
|
|
true
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def appdir
|
|
|
|
self.class.appdir
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.appdir
|
|
|
|
Hbc.appdir.sub(%r{\/$}, "")
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|