79 lines
2.2 KiB
Ruby
Raw Normal View History

2018-07-16 22:46:02 +02:00
require "vendor/plist/plist"
2016-08-18 22:11:42 +03:00
require "cask/artifact/abstract_artifact"
2016-10-04 15:24:58 +02:00
2018-07-16 22:46:02 +02:00
require "extend/hash_validator"
using HashValidator
2016-09-24 13:52:43 +02:00
module Hbc
module Artifact
class Pkg < AbstractArtifact
2016-09-24 13:52:43 +02:00
attr_reader :pkg_relative_path
2016-08-18 22:11:42 +03:00
def self.from_args(cask, path, **stanza_options)
2018-07-16 22:46:02 +02:00
stanza_options.assert_valid_keys!(:allow_untrusted, :choices)
new(cask, path, **stanza_options)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
attr_reader :path, :stanza_options
def initialize(cask, path, **stanza_options)
super(cask)
@path = cask.staged_path.join(path)
@stanza_options = stanza_options
2016-08-18 22:11:42 +03:00
end
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
def install_phase(**options)
run_installer(**options)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
private
def run_installer(command: nil, verbose: false, **_options)
ohai "Running installer for #{cask}; your password may be necessary."
2016-09-24 13:52:43 +02:00
ohai "Package installers may write to any location; options such as --appdir are ignored."
unless path.exist?
raise CaskError, "pkg source file not found: '#{path.relative_path_from(cask.staged_path)}'"
2016-09-24 13:52:43 +02:00
end
args = [
"-pkg", path,
2016-10-14 20:33:16 +02:00
"-target", "/"
]
args << "-verboseR" if verbose
if stanza_options.fetch(:allow_untrusted, false)
args << "-allowUntrusted"
end
2016-11-21 04:13:29 +09:00
with_choices_file do |choices_path|
args << "-applyChoiceChangesXML" << choices_path if choices_path
logged_in_user = Utils.current_user
env = {
"LOGNAME" => logged_in_user,
"USER" => logged_in_user,
"USERNAME" => logged_in_user,
}
command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true, env: env)
end
2016-09-24 13:52:43 +02:00
end
2016-11-21 04:13:29 +09:00
def with_choices_file
choices = stanza_options.fetch(:choices, {})
return yield nil if choices.empty?
2016-11-21 04:13:29 +09:00
Tempfile.open(["choices", ".xml"]) do |file|
begin
file.write Plist::Emit.dump(choices)
2016-11-21 04:13:29 +09:00
file.close
yield file.path
ensure
file.unlink
end
end
end
2016-08-18 22:11:42 +03:00
end
end
end