2022-10-11 00:52:32 +01:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-13 15:24:18 +01:00
|
|
|
require "plist"
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2018-10-01 12:29:21 +02:00
|
|
|
require "utils/user"
|
2018-09-03 19:39:07 +01:00
|
|
|
require "cask/artifact/abstract_artifact"
|
2024-01-12 09:38:49 -08:00
|
|
|
require "extend/hash/keys"
|
2016-10-04 15:24:58 +02:00
|
|
|
|
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 `pkg` stanza.
|
2017-04-06 00:33:31 +02:00
|
|
|
class Pkg < AbstractArtifact
|
2020-12-06 07:04:47 +01:00
|
|
|
attr_reader :path, :stanza_options
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-10-02 00:26:58 +02:00
|
|
|
def self.from_args(cask, path, **stanza_options)
|
2023-03-19 17:31:51 -07:00
|
|
|
stanza_options.assert_valid_keys(:allow_untrusted, :choices)
|
2017-10-02 00:26:58 +02:00
|
|
|
new(cask, path, **stanza_options)
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-10-02 00:26:58 +02:00
|
|
|
def initialize(cask, path, **stanza_options)
|
2024-05-23 17:08:41 +01:00
|
|
|
super
|
2017-04-06 00:33:31 +02:00
|
|
|
@path = cask.staged_path.join(path)
|
2017-10-02 00:26:58 +02:00
|
|
|
@stanza_options = stanza_options
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2017-04-06 00:33:31 +02:00
|
|
|
def summarize
|
|
|
|
path.relative_path_from(cask.staged_path).to_s
|
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
|
|
|
def install_phase(**options)
|
|
|
|
run_installer(**options)
|
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
|
|
|
private
|
|
|
|
|
2017-10-02 00:26:58 +02:00
|
|
|
def run_installer(command: nil, verbose: false, **_options)
|
2023-09-29 18:49:44 +01:00
|
|
|
ohai "Running installer for #{cask} with sudo; the password may be necessary."
|
2017-04-06 00:33:31 +02:00
|
|
|
unless path.exist?
|
2020-12-21 15:45:18 +01:00
|
|
|
pkg = path.relative_path_from(cask.staged_path)
|
|
|
|
pkgs = Pathname.glob(cask.staged_path/"**"/"*.pkg").map { |path| path.relative_path_from(cask.staged_path) }
|
|
|
|
|
|
|
|
message = "Could not find PKG source file '#{pkg}'"
|
|
|
|
message += ", found #{pkgs.map { |path| "'#{path}'" }.to_sentence} instead" if pkgs.any?
|
|
|
|
message += "."
|
|
|
|
|
|
|
|
raise CaskError, message
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
args = [
|
2017-04-06 00:33:31 +02:00
|
|
|
"-pkg", path,
|
2016-10-14 20:33:16 +02:00
|
|
|
"-target", "/"
|
|
|
|
]
|
2017-04-06 00:33:31 +02:00
|
|
|
args << "-verboseR" if verbose
|
2019-02-19 13:11:32 +00:00
|
|
|
args << "-allowUntrusted" if stanza_options.fetch(:allow_untrusted, false)
|
2016-11-21 04:13:29 +09:00
|
|
|
with_choices_file do |choices_path|
|
2016-11-20 14:09:16 +09:00
|
|
|
args << "-applyChoiceChangesXML" << choices_path if choices_path
|
2018-06-01 23:26:12 +02:00
|
|
|
env = {
|
2018-11-02 17:18:07 +00:00
|
|
|
"LOGNAME" => User.current,
|
|
|
|
"USER" => User.current,
|
2018-10-01 12:29:21 +02:00
|
|
|
"USERNAME" => User.current,
|
2018-06-01 23:26:12 +02:00
|
|
|
}
|
2023-03-27 14:06:07 -07:00
|
|
|
command.run!(
|
|
|
|
"/usr/sbin/installer",
|
|
|
|
sudo: true,
|
|
|
|
sudo_as_root: true,
|
2024-03-07 16:20:20 +00:00
|
|
|
args:,
|
2023-03-27 14:06:07 -07:00
|
|
|
print_stdout: true,
|
2024-03-07 16:20:20 +00:00
|
|
|
env:,
|
2023-03-27 14:06:07 -07:00
|
|
|
)
|
2016-11-10 11:56:00 +09:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-11-10 11:56:00 +09:00
|
|
|
|
2016-11-21 04:13:29 +09:00
|
|
|
def with_choices_file
|
2017-10-02 00:26:58 +02:00
|
|
|
choices = stanza_options.fetch(:choices, {})
|
2017-04-06 00:33:31 +02:00
|
|
|
return yield nil if choices.empty?
|
2016-11-20 14:09:16 +09:00
|
|
|
|
2016-11-21 04:13:29 +09:00
|
|
|
Tempfile.open(["choices", ".xml"]) do |file|
|
2019-10-13 10:03:26 +01:00
|
|
|
file.write Plist::Emit.dump(choices)
|
|
|
|
file.close
|
|
|
|
yield file.path
|
|
|
|
ensure
|
|
|
|
file.unlink
|
2016-11-20 14:09:16 +09:00
|
|
|
end
|
2016-11-10 11:56:00 +09:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|