Merge pull request #1620 from reitermarkus/refactoring

Refactor Cask installer.
This commit is contained in:
Markus Reiter 2016-12-05 09:27:32 +01:00 committed by GitHub
commit 697340ff3b
4 changed files with 55 additions and 36 deletions

View File

@ -54,8 +54,27 @@ module Hbc
output output
end end
def fetch
odebug "Hbc::Installer#fetch"
satisfy_dependencies
verify_has_sha if @require_sha && !@force
download
verify
end
def stage
odebug "Hbc::Installer#stage"
extract_primary_container
save_caskfile
rescue StandardError => e
purge_versioned_files
raise e
end
def install def install
odebug "Hbc::Installer.install" odebug "Hbc::Installer#install"
if @cask.installed? && !force if @cask.installed? && !force
raise CaskAlreadyInstalledAutoUpdatesError, @cask if @cask.auto_updates raise CaskAlreadyInstalledAutoUpdatesError, @cask if @cask.auto_updates
@ -63,37 +82,23 @@ module Hbc
end end
print_caveats print_caveats
fetch
begin stage
satisfy_dependencies install_artifacts
verify_has_sha if @require_sha && !@force enable_accessibility_access
download
verify
extract_primary_container
install_artifacts
save_caskfile
enable_accessibility_access
rescue StandardError => e
purge_versioned_files
raise e
end
puts summary puts summary
end end
def summary def summary
s = if MacOS.version >= :lion && !ENV["HOMEBREW_NO_EMOJI"] s = ""
(ENV["HOMEBREW_INSTALL_BADGE"] || "\xf0\x9f\x8d\xba") + " " s << "#{Emoji.install_badge} " if Emoji.enabled?
else
Formatter.headline("Success! ", color: :blue)
end
s << "#{@cask} was successfully installed!" s << "#{@cask} was successfully installed!"
end end
def download def download
odebug "Downloading" odebug "Downloading"
download = Download.new(@cask, force: false) @downloaded_path = Download.new(@cask, force: false).perform
@downloaded_path = download.perform
odebug "Downloaded to -> #{@downloaded_path}" odebug "Downloaded to -> #{@downloaded_path}"
@downloaded_path @downloaded_path
end end
@ -110,28 +115,46 @@ module Hbc
def extract_primary_container def extract_primary_container
odebug "Extracting primary container" odebug "Extracting primary container"
FileUtils.mkdir_p @cask.staged_path FileUtils.mkdir_p @cask.staged_path
container = if @cask.container && @cask.container.type container = if @cask.container && @cask.container.type
Container.from_type(@cask.container.type) Container.from_type(@cask.container.type)
else else
Container.for_path(@downloaded_path, @command) Container.for_path(@downloaded_path, @command)
end end
unless container unless container
raise CaskError, "Uh oh, could not figure out how to unpack '#{@downloaded_path}'" raise CaskError, "Uh oh, could not figure out how to unpack '#{@downloaded_path}'"
end end
odebug "Using container class #{container} for #{@downloaded_path}" odebug "Using container class #{container} for #{@downloaded_path}"
container.new(@cask, @downloaded_path, @command).extract container.new(@cask, @downloaded_path, @command).extract
end end
def install_artifacts def install_artifacts
already_installed_artifacts = []
options = { command: @command, force: force }
odebug "Installing artifacts" odebug "Installing artifacts"
artifacts = Artifact.for_cask(@cask) artifacts = Artifact.for_cask(@cask)
odebug "#{artifacts.length} artifact/s defined", artifacts odebug "#{artifacts.length} artifact/s defined", artifacts
artifacts.each do |artifact| artifacts.each do |artifact|
odebug "Installing artifact of class #{artifact}" odebug "Installing artifact of class #{artifact}"
options = { command: @command, force: force } already_installed_artifacts.unshift(artifact)
artifact.new(@cask, options).install_phase artifact.new(@cask, options).install_phase
end end
rescue StandardError => e
begin
ofail e.message
already_installed_artifacts.each do |artifact|
odebug "Reverting installation of artifact of class #{artifact}"
artifact.new(@cask, options).uninstall_phase
end
ensure
purge_versioned_files
raise e.class, "An error occured during installation of Cask #{@cask}: #{e.message}"
end
end end
# TODO: move dependencies to a separate class # TODO: move dependencies to a separate class
@ -179,7 +202,7 @@ module Hbc
def x11_dependencies def x11_dependencies
return unless @cask.depends_on.x11 return unless @cask.depends_on.x11
raise CaskX11DependencyError, @cask.token if Hbc.x11_libpng.select(&:exist?).empty? raise CaskX11DependencyError, @cask.token unless MacOS::X11.installed?
end end
def formula_dependencies def formula_dependencies
@ -248,6 +271,9 @@ module Hbc
See System Preferences to enable it manually. See System Preferences to enable it manually.
EOS EOS
end end
rescue StandardError => e
purge_versioned_files
raise e
end end
def disable_accessibility_access def disable_accessibility_access
@ -282,7 +308,7 @@ module Hbc
end end
def uninstall def uninstall
odebug "Hbc::Installer.uninstall" odebug "Hbc::Installer#uninstall"
disable_accessibility_access disable_accessibility_access
uninstall_artifacts uninstall_artifacts
purge_versioned_files purge_versioned_files

View File

@ -171,14 +171,6 @@ module Hbc
def pre_mavericks_accessibility_dotfile def pre_mavericks_accessibility_dotfile
@pre_mavericks_accessibility_dotfile ||= Pathname.new("/private/var/db/.AccessibilityAPIEnabled") @pre_mavericks_accessibility_dotfile ||= Pathname.new("/private/var/db/.AccessibilityAPIEnabled")
end end
def x11_executable
@x11_executable ||= Pathname.new("/usr/X11/bin/X")
end
def x11_libpng
@x11_libpng ||= [Pathname.new("/opt/X11/lib/libpng.dylib"), Pathname.new("/usr/X11/lib/libpng.dylib")]
end
end end
end end
end end

View File

@ -39,7 +39,7 @@ describe Hbc::CLI::Install do
lambda { lambda {
Hbc::CLI::Install.run("local-transmission", "--force") Hbc::CLI::Install.run("local-transmission", "--force")
}.must_output(/==> Success! local-transmission was successfully installed!/) }.must_output(/local-transmission was successfully installed!/)
end end
it "skips dependencies with --skip-cask-deps" do it "skips dependencies with --skip-cask-deps" do

View File

@ -93,6 +93,7 @@ describe "Satisfy Dependencies and Requirements" do
describe "depends_on x11" do describe "depends_on x11" do
it "succeeds when depends_on x11 is satisfied" do it "succeeds when depends_on x11 is satisfied" do
x11_cask = Hbc.load("with-depends-on-x11") x11_cask = Hbc.load("with-depends-on-x11")
MacOS::X11.stubs(:installed?).returns(true)
shutup do shutup do
Hbc::Installer.new(x11_cask).install Hbc::Installer.new(x11_cask).install
end end
@ -100,7 +101,7 @@ describe "Satisfy Dependencies and Requirements" do
it "raises an exception when depends_on x11 is not satisfied" do it "raises an exception when depends_on x11 is not satisfied" do
x11_cask = Hbc.load("with-depends-on-x11") x11_cask = Hbc.load("with-depends-on-x11")
Hbc.stubs(:x11_libpng).returns([Pathname.new("/usr/path/does/not/exist")]) MacOS::X11.stubs(:installed?).returns(false)
lambda { lambda {
shutup do shutup do
Hbc::Installer.new(x11_cask).install Hbc::Installer.new(x11_cask).install
@ -110,7 +111,7 @@ describe "Satisfy Dependencies and Requirements" do
it "never raises when depends_on x11: false" do it "never raises when depends_on x11: false" do
x11_cask = Hbc.load("with-depends-on-x11-false") x11_cask = Hbc.load("with-depends-on-x11-false")
Hbc.stubs(:x11_executable).returns(Pathname.new("/usr/path/does/not/exist")) MacOS::X11.stubs(:installed?).returns(false)
lambda do lambda do
shutup do shutup do
Hbc::Installer.new(x11_cask).install Hbc::Installer.new(x11_cask).install