Skip to content

Commit

Permalink
Refactor: add options, ivars to Installer, Download
Browse files Browse the repository at this point in the history
In preparation for upcoming changes, this commit cleans up some code. The commit includes:

- In order to reduce unnecessary object passing, make both the `force` and `skip_cask_deps` option into instance variables of the `Installer` class

- Introduce options hashes to initializers of both the `Installer` and `Download` class

- When the `install --force` command enters the fetch phase, make it explicit in the code that fetching is never enforced in that case.

- Update tests
  • Loading branch information
claui authored and mwean committed May 31, 2016
1 parent 91f19b1 commit 9876d17
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/hbc/cli/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.run(*args)
cask_tokens.each do |cask_token|
ohai "Downloading external files for Cask #{cask_token}"
cask = Hbc.load(cask_token)
downloaded_path = Hbc::Download.new(cask, force).perform
downloaded_path = Hbc::Download.new(cask, force: force).perform
Hbc::Verify.all(cask, downloaded_path)
ohai "Success! Downloaded to -> #{downloaded_path}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hbc/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.install_casks(cask_tokens, force)
cask_tokens.each do |cask_token|
begin
cask = Hbc.load(cask_token)
Hbc::Installer.new(cask).install(force)
Hbc::Installer.new(cask, force: force).install
count += 1
rescue Hbc::CaskAlreadyInstalledError => e
opoo e.message
Expand Down
6 changes: 4 additions & 2 deletions lib/hbc/cli/uninstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ def self.run(*args)
cask_tokens.each do |cask_token|
odebug "Uninstalling Cask #{cask_token}"
cask = Hbc.load(cask_token)
raise Hbc::CaskNotInstalledError.new(cask) unless cask.installed? or force
Hbc::Installer.new(cask).uninstall(force)
unless cask.installed? || force
raise Hbc::CaskNotInstalledError.new(cask)
end
Hbc::Installer.new(cask, force: force).uninstall
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/hbc/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class Hbc::Download
attr_reader :cask

def initialize(cask, force=false)
def initialize(cask, options={})
@cask = cask
@force = force
@force = options.fetch(:force, false)
end

def perform
Expand Down
25 changes: 15 additions & 10 deletions lib/hbc/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class Hbc::Installer
include Hbc::Staged
include Hbc::Verify

attr_reader :force, :skip_cask_deps

PERSISTENT_METADATA_SUBDIRS = [ 'gpg' ]

def initialize(cask, command=Hbc::SystemCommand)
def initialize(cask, options={})
@cask = cask
@command = command
@command = options.fetch(:command, Hbc::SystemCommand)
@force = options.fetch(:force, false)
@skip_cask_deps = options.fetch(:skip_cask_deps, false)
end

def self.print_caveats(cask)
Expand Down Expand Up @@ -50,7 +54,7 @@ def self.capture_output(&block)
output
end

def install(force=false, skip_cask_deps=false)
def install
odebug "Hbc::Installer.install"

if @cask.installed? && @cask.auto_updates && !force
Expand All @@ -64,12 +68,12 @@ def install(force=false, skip_cask_deps=false)
print_caveats

begin
satisfy_dependencies(skip_cask_deps)
satisfy_dependencies
download
verify
extract_primary_container
install_artifacts
save_caskfile force
save_caskfile
enable_accessibility_access
rescue StandardError => e
purge_versioned_files
Expand All @@ -90,7 +94,7 @@ def summary

def download
odebug "Downloading"
download = Hbc::Download.new(@cask)
download = Hbc::Download.new(@cask, force: false)
@downloaded_path = download.perform
odebug "Downloaded to -> #{@downloaded_path}"
@downloaded_path
Expand Down Expand Up @@ -128,7 +132,7 @@ def install_artifacts
# todo move dependencies to a separate class
# dependencies should also apply for "brew cask stage"
# override dependencies with --force or perhaps --force-deps
def satisfy_dependencies(skip_cask_deps=false)
def satisfy_dependencies
if @cask.depends_on
ohai 'Satisfying dependencies'
macos_dependencies
Expand Down Expand Up @@ -205,7 +209,8 @@ def cask_dependencies
if dep.installed?
puts "already installed"
else
Hbc::Installer.new(dep).install(false, true)
Hbc::Installer.new(dep,
force: false, skip_cask_deps: true).install
puts "done"
end
end
Expand Down Expand Up @@ -258,7 +263,7 @@ def disable_accessibility_access
end
end

def save_caskfile(force=false)
def save_caskfile
timestamp = :now
create = true
savedir = @cask.metadata_subdir('Casks', timestamp, create)
Expand All @@ -274,7 +279,7 @@ def save_caskfile(force=false)
FileUtils.copy(@cask.sourcefile_path, savedir) if @cask.sourcefile_path
end

def uninstall(force=false)
def uninstall
odebug "Hbc::Installer.uninstall"
disable_accessibility_access
uninstall_artifacts
Expand Down
3 changes: 2 additions & 1 deletion test/cask/accessibility_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
describe "Accessibility Access" do
before do
cask = Hbc.load('with-accessibility-access')
@installer = Hbc::Installer.new(cask, Hbc::FakeSystemCommand)
with_fake_command = { command: Hbc::FakeSystemCommand }
@installer = Hbc::Installer.new(cask, with_fake_command)
end

describe "install" do
Expand Down
17 changes: 8 additions & 9 deletions test/cask/installer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@
it "allows already-installed Casks which auto-update to be installed if force is provided" do
auto_updates = Hbc.load('auto-updates')
auto_updates.installed?.must_equal false
installer = Hbc::Installer.new(auto_updates)

shutup { installer.install }
shutup { Hbc::Installer.new(auto_updates).install }

shutup {
installer.install(true)
Hbc::Installer.new(auto_updates, force: true).install
} # wont_raise
end

Expand All @@ -243,11 +243,11 @@
it "allows already-installed Casks to be installed if force is provided" do
transmission = Hbc.load('local-transmission')
transmission.installed?.must_equal false
installer = Hbc::Installer.new(transmission)

shutup { installer.install }
shutup { Hbc::Installer.new(transmission).install }

shutup {
installer.install(true)
Hbc::Installer.new(transmission, force: true).install
} # wont_raise
end

Expand Down Expand Up @@ -329,11 +329,10 @@

it "uninstalls all versions if force is set" do
caffeine = Hbc.load('local-caffeine')
installer = Hbc::Installer.new(caffeine)
mutated_version = caffeine.version + '.1'

shutup do
installer.install
Hbc::Installer.new(caffeine).install
end

Hbc.caskroom.join('local-caffeine',caffeine.version).must_be :directory?
Expand All @@ -343,7 +342,7 @@
Hbc.caskroom.join('local-caffeine',mutated_version).must_be :directory?

shutup do
installer.uninstall(true)
Hbc::Installer.new(caffeine, force: true).uninstall
end

Hbc.caskroom.join('local-caffeine',caffeine.version).wont_be :directory?
Expand Down

0 comments on commit 9876d17

Please sign in to comment.