From f4a3bc100b4b98a3d06222c15934abb36bf16594 Mon Sep 17 00:00:00 2001 From: Kaito Udagawa Date: Thu, 10 Nov 2016 11:56:00 +0900 Subject: [PATCH 1/4] cask: add the choices option to pkg stanza installer command accepts -applyChoiceChangesXML option to change customize options on the GUI installer from the commandline. (`man installer` for more detailed information) The introduced option `choice` enables the choice changes to be supplied via pkg stanza without tricks in preflight code. --- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 17 ++++++++- .../cask/test/cask/artifact/pkg_test.rb | 37 +++++++++++++++++++ .../cask/test/support/Casks/with-choices.rb | 16 ++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 Library/Homebrew/cask/test/support/Casks/with-choices.rb diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index d5e63d8efc..2f34814aba 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -2,6 +2,8 @@ require "hbc/artifact/base" require "hbc/utils/hash_validator" +require "vendor/plist/plist" + module Hbc module Artifact class Pkg < Base @@ -16,7 +18,7 @@ module Hbc @pkg_install_opts = pkg_description.shift begin if @pkg_install_opts.respond_to?(:keys) - @pkg_install_opts.extend(HashValidator).assert_valid_keys(:allow_untrusted) + @pkg_install_opts.extend(HashValidator).assert_valid_keys(:allow_untrusted, :choices) elsif @pkg_install_opts raise end @@ -52,8 +54,21 @@ module Hbc ] args << "-verboseR" if Hbc.verbose args << "-allowUntrusted" if pkg_install_opts :allow_untrusted + if pkg_install_opts :choices + args << "-applyChoiceChangesXML" + args << choices_xml + end @command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true) end + + def choices_xml + path = @cask.staged_path.join("Choices.xml") + unless File.exist? path + choices = pkg_install_opts :choices + IO.write path, Plist::Emit.dump(choices) + end + path + end end end end diff --git a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb index e87db7a7ab..8ee4e0a3c4 100644 --- a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb +++ b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb @@ -30,4 +30,41 @@ describe Hbc::Artifact::Pkg do end end end + + describe "choices" do + before do + @cask = Hbc.load("with-choices") + shutup do + TestHelper.install_without_artifacts(@cask) + end + end + + it "passes the choice changes xml to the system installer" do + pkg = Hbc::Artifact::Pkg.new(@cask, + command: Hbc::FakeSystemCommand) + + Hbc::FakeSystemCommand.expects_command(["/usr/bin/sudo", "-E", "--", "/usr/sbin/installer", "-pkg", @cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/", "-applyChoiceChangesXML", @cask.staged_path.join("Choices.xml")]) + + shutup do + pkg.install_phase + end + + IO.read(@cask.staged_path.join("Choices.xml")).must_equal <<-EOS.undent + + + + + + attributeSetting + 1 + choiceAttribute + selected + choiceIdentifier + choice1 + + + + EOS + end + end end diff --git a/Library/Homebrew/cask/test/support/Casks/with-choices.rb b/Library/Homebrew/cask/test/support/Casks/with-choices.rb new file mode 100644 index 0000000000..1871efab30 --- /dev/null +++ b/Library/Homebrew/cask/test/support/Casks/with-choices.rb @@ -0,0 +1,16 @@ +test_cask 'with-choices' do + version '1.2.3' + sha256 '8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b' + + url "file://#{TEST_FIXTURE_DIR}/cask/MyFancyPkg.zip" + homepage 'http://example.com/fancy-pkg' + + pkg 'MyFancyPkg/Fancy.pkg', + choices: [ + { + 'choiceIdentifier' => 'choice1', + 'choiceAttribute' => 'selected', + 'attributeSetting' => 1, + }, + ] +end From 28072021031836937a01e9fd995b03995fe49443 Mon Sep 17 00:00:00 2001 From: Kaito Udagawa Date: Sat, 19 Nov 2016 13:05:55 +0900 Subject: [PATCH 2/4] cask: use Tempfile and some style fixes --- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 13 +++---- .../cask/test/cask/artifact/pkg_test.rb | 35 ++++++++++--------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index 2f34814aba..cede9f4d53 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -55,19 +55,16 @@ module Hbc args << "-verboseR" if Hbc.verbose args << "-allowUntrusted" if pkg_install_opts :allow_untrusted if pkg_install_opts :choices - args << "-applyChoiceChangesXML" - args << choices_xml + choices_file = choices_xml + args << "-applyChoiceChangesXML" << choices_file.path end @command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true) end def choices_xml - path = @cask.staged_path.join("Choices.xml") - unless File.exist? path - choices = pkg_install_opts :choices - IO.write path, Plist::Emit.dump(choices) - end - path + file = Tempfile.open(["", ".xml"]) + file.write Plist::Emit.dump(pkg_install_opts(:choices)) + file end end end diff --git a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb index 8ee4e0a3c4..cb30c4a0af 100644 --- a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb +++ b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb @@ -40,31 +40,32 @@ describe Hbc::Artifact::Pkg do end it "passes the choice changes xml to the system installer" do - pkg = Hbc::Artifact::Pkg.new(@cask, - command: Hbc::FakeSystemCommand) + pkg = Hbc::Artifact::Pkg.new(@cask, command: Hbc::FakeSystemCommand) - Hbc::FakeSystemCommand.expects_command(["/usr/bin/sudo", "-E", "--", "/usr/sbin/installer", "-pkg", @cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/", "-applyChoiceChangesXML", @cask.staged_path.join("Choices.xml")]) - - shutup do - pkg.install_phase - end - - IO.read(@cask.staged_path.join("Choices.xml")).must_equal <<-EOS.undent + file = mock + file.expects(:write).with <<-EOS.undent - - attributeSetting - 1 - choiceAttribute - selected - choiceIdentifier - choice1 - + \t + \t\tattributeSetting + \t\t1 + \t\tchoiceAttribute + \t\tselected + \t\tchoiceIdentifier + \t\tchoice1 + \t EOS + file.stubs path: Pathname.new("/tmp/choices.xml") + Tempfile.expects(:open).returns(file) + Hbc::FakeSystemCommand.expects_command(["/usr/bin/sudo", "-E", "--", "/usr/sbin/installer", "-pkg", @cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/", "-applyChoiceChangesXML", @cask.staged_path.join("/tmp/choices.xml")]) + + shutup do + pkg.install_phase + end end end end From 0c22cc45e6fe7da64f037fc2fc1556c0e967d55f Mon Sep 17 00:00:00 2001 From: Kaito Udagawa Date: Sun, 20 Nov 2016 14:09:16 +0900 Subject: [PATCH 3/4] cask: Call Tempfile#close(true) with the choices file to ensure it deleted --- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 24 ++++++++++++------- .../cask/test/cask/artifact/pkg_test.rb | 4 +++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index cede9f4d53..fffb10cae7 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -54,17 +54,25 @@ module Hbc ] args << "-verboseR" if Hbc.verbose args << "-allowUntrusted" if pkg_install_opts :allow_untrusted - if pkg_install_opts :choices - choices_file = choices_xml - args << "-applyChoiceChangesXML" << choices_file.path + with_choices_file pkg_install_opts(:choices) do |choices_path| + args << "-applyChoiceChangesXML" << choices_path if choices_path + @command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true) end - @command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true) end - def choices_xml - file = Tempfile.open(["", ".xml"]) - file.write Plist::Emit.dump(pkg_install_opts(:choices)) - file + def with_choices_file(choices) + unless choices + yield nil + return + end + + begin + file = Tempfile.new(["choices", ".xml"]) + file.write Plist::Emit.dump(choices) + yield file.path + ensure + file.close(true) + end end end end diff --git a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb index cb30c4a0af..6e10177d12 100644 --- a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb +++ b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb @@ -60,7 +60,9 @@ describe Hbc::Artifact::Pkg do EOS file.stubs path: Pathname.new("/tmp/choices.xml") - Tempfile.expects(:open).returns(file) + file.expects(:close).with true + Tempfile.expects(:new).returns file + Hbc::FakeSystemCommand.expects_command(["/usr/bin/sudo", "-E", "--", "/usr/sbin/installer", "-pkg", @cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/", "-applyChoiceChangesXML", @cask.staged_path.join("/tmp/choices.xml")]) shutup do From df635c8259dffca891424352765088a3b88a87d2 Mon Sep 17 00:00:00 2001 From: Kaito Udagawa Date: Mon, 21 Nov 2016 04:13:29 +0900 Subject: [PATCH 4/4] cask: compact the code --- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 23 +++++++++---------- .../cask/test/cask/artifact/pkg_test.rb | 5 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index fffb10cae7..0569d2a86e 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -54,24 +54,23 @@ module Hbc ] args << "-verboseR" if Hbc.verbose args << "-allowUntrusted" if pkg_install_opts :allow_untrusted - with_choices_file pkg_install_opts(:choices) do |choices_path| + with_choices_file do |choices_path| args << "-applyChoiceChangesXML" << choices_path if choices_path @command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true) end end - def with_choices_file(choices) - unless choices - yield nil - return - end + def with_choices_file + return yield nil unless pkg_install_opts(:choices) - begin - file = Tempfile.new(["choices", ".xml"]) - file.write Plist::Emit.dump(choices) - yield file.path - ensure - file.close(true) + Tempfile.open(["choices", ".xml"]) do |file| + begin + file.write Plist::Emit.dump(pkg_install_opts(:choices)) + file.close + yield file.path + ensure + file.unlink + end end end end diff --git a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb index 6e10177d12..3ed427763b 100644 --- a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb +++ b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb @@ -60,8 +60,9 @@ describe Hbc::Artifact::Pkg do EOS file.stubs path: Pathname.new("/tmp/choices.xml") - file.expects(:close).with true - Tempfile.expects(:new).returns file + file.expects(:close) + file.expects(:unlink) + Tempfile.expects(:open).yields file Hbc::FakeSystemCommand.expects_command(["/usr/bin/sudo", "-E", "--", "/usr/sbin/installer", "-pkg", @cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/", "-applyChoiceChangesXML", @cask.staged_path.join("/tmp/choices.xml")])