105 lines
2.7 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "cask/artifact/abstract_artifact"
2016-08-18 22:11:42 +03:00
2018-07-16 22:46:02 +02:00
require "extend/hash_validator"
using HashValidator
2018-09-06 08:29:14 +02:00
module Cask
2016-09-24 13:52:43 +02:00
module Artifact
2020-08-19 10:23:41 +02:00
# Artifact corresponding to the `installer` stanza.
#
# @api private
class Installer < AbstractArtifact
2019-04-19 21:46:20 +09:00
VALID_KEYS = Set.new([
:manual,
:script,
]).freeze
2020-08-19 10:23:41 +02:00
# Extension module for manual installers.
module ManualInstaller
def install_phase(**)
2017-10-15 02:28:32 +02:00
puts <<~EOS
To complete the installation of Cask #{cask}, you must also
2019-04-08 12:47:15 -04:00
run the installer at:
'#{cask.staged_path.join(path)}'
EOS
end
end
2020-08-19 10:23:41 +02:00
# Extension module for script installers.
module ScriptInstaller
def install_phase(command: nil, **_)
ohai "Running #{self.class.dsl_key} script '#{path}'"
executable_path = staged_path_join_executable(path)
command.run!(
executable_path,
**args,
env: { "PATH" => PATH.new(
HOMEBREW_PREFIX/"bin", HOMEBREW_PREFIX/"sbin", ENV["PATH"]
) },
)
end
end
def self.from_args(cask, **args)
raise CaskInvalidError.new(cask, "'installer' stanza requires an argument.") if args.empty?
if args.key?(:script) && !args[:script].respond_to?(:key?)
if args.key?(:executable)
raise CaskInvalidError.new(cask, "'installer' stanza gave arguments for both :script and :executable.")
2016-09-24 13:52:43 +02:00
end
args[:executable] = args[:script]
args.delete(:script)
args = { script: args }
end
unless args.keys.count == 1
raise CaskInvalidError.new(
cask,
"invalid 'installer' stanza: Only one of #{VALID_KEYS.inspect} is permitted.",
)
2016-09-24 13:52:43 +02:00
end
2018-07-16 22:46:02 +02:00
args.assert_valid_keys!(*VALID_KEYS)
new(cask, **args)
end
attr_reader :path, :args
def initialize(cask, **args)
super(cask)
if args.key?(:manual)
@path = Pathname(args[:manual])
@args = []
extend(ManualInstaller)
return
end
path, @args = self.class.read_script_arguments(
args[:script], self.class.dsl_key.to_s, { must_succeed: true, sudo: false }, print_stdout: true
)
raise CaskInvalidError.new(cask, "#{self.class.dsl_key} missing executable") if path.nil?
@path = Pathname(path)
extend(ScriptInstaller)
end
def summarize
path.to_s
2016-08-18 22:11:42 +03:00
end
2017-08-04 14:59:18 +02:00
def to_h
{ path: path }.tap do |h|
2017-08-04 14:59:18 +02:00
h[:args] = args unless is_a?(ManualInstaller)
end
end
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
end