Skip to content

Commit

Permalink
cleaning up install/uninstall implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phinze committed Jul 4, 2013
1 parent ad86c3b commit ddb1fea
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Casks/virtualbox.rb
Expand Up @@ -4,5 +4,5 @@ class Virtualbox < Cask
version '4.2.14-86644'
sha1 '93c36db799d53acaed7bb18c1402c6f11671f40a'
install 'VirtualBox.pkg'
uninstall 'Virtualbox_Uninstall.tool', :args => %w[--unattended]
uninstall 'VirtualBox_Uninstall.tool', :args => %w[--unattended]
end
2 changes: 1 addition & 1 deletion lib/cask.rb
Expand Up @@ -104,7 +104,7 @@ def installers
end

def uninstallers
uninstallables.map { |pkg| Pathname.glob("#{destination_path}/**/#{pkg}").first }
uninstallables.map { |file, options| [ Pathname.glob("#{destination_path}/**/#{file}").first, options ] }
end

def to_s
Expand Down
4 changes: 2 additions & 2 deletions lib/cask/cli/alfred.rb
Expand Up @@ -105,9 +105,9 @@ def self.alfred_scopes

def self.alfred_preference(key, value=nil)
if value
@system_command.run(%Q(defaults write #{DOMAIN} #{key} "#{value}"))
@system_command.run(%Q(defaults write #{DOMAIN} #{key} "#{value}"), :print => false)
else
@system_command.run("defaults read #{DOMAIN} #{key}")
@system_command.run("defaults read #{DOMAIN} #{key}", :print => false)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/cask/dsl.rb
Expand Up @@ -58,8 +58,8 @@ def uninstallables
@uninstallables ||= Set.new
end

def uninstall(*files)
uninstallables.merge files
def uninstall(file, options={})
uninstallables.merge [[file, options]]
end

attr_reader :sums
Expand Down
9 changes: 6 additions & 3 deletions lib/cask/pkg_installer.rb
Expand Up @@ -7,14 +7,17 @@ def initialize(cask, command=Cask::SystemCommand)
def install
@cask.installers.each do |installer|
ohai "Running installer for #{@cask}; your password may be necessary."
@command.run("sudo installer -pkg '#{installer}' -target /")
@command.run("installer", {
:sudo => true,
:args => %W[-pkg #{installer} -target /]
})
end
end

def uninstall
@cask.uninstallers.each do |uninstaller|
@cask.uninstallers.each do |uninstaller, options|
ohai "Running uninstaller for #{@cask}; your password may be necessary."
@command.run("sudo '#{uninstaller}'")
@command.run(uninstaller, options.merge(:sudo => true))
end
end
end
26 changes: 23 additions & 3 deletions lib/cask/system_command.rb
@@ -1,11 +1,31 @@
class Cask::SystemCommand
def self.run(command, input='')
def self.run(command, options={})
command = _process_options(command, options)
output = ''
IO.popen("#{command} 2>&1", 'r+') do |pipe|
pipe.puts input
if options[:input]
options[:input].each { |line| pipe.puts line }
end
pipe.close_write
while line = pipe.gets
ohai line.chomp
output << line
ohai line.chomp if options.fetch(:print, true)
end
end
output
end

def self._process_options(command, options)
if options[:sudo]
command = "sudo #{_quote(command)}"
end
if options[:args]
command = "#{command} #{options[:args].map { |arg| _quote(arg) }.join(' ')}"
end
command
end

def self._quote(string)
%Q('#{string}')
end
end
17 changes: 16 additions & 1 deletion test/cask/pkg_installer_test.rb
Expand Up @@ -18,7 +18,7 @@
it 'runs the system installer on the specified pkgs' do
pkg_installer = Cask::PkgInstaller.new(@cask, Cask::FakeSystemCommand)

expected_command = "sudo installer -pkg '#{@cask.destination_path/'MyFancyPkg'/'Fancy.pkg'}' -target /"
expected_command = "sudo 'installer' '-pkg' '#{@cask.destination_path/'MyFancyPkg'/'Fancy.pkg'}' '-target' '/'"
Cask::FakeSystemCommand.fake_response_for(expected_command)

shutup do
Expand All @@ -28,4 +28,19 @@
Cask::FakeSystemCommand.system_calls[expected_command].must_equal 1
end
end

describe 'uninstall' do
it 'runs the specified uninstaller for the cask' do
pkg_installer = Cask::PkgInstaller.new(@cask, Cask::FakeSystemCommand)

expected_command = "sudo '#{@cask.destination_path/'MyFancyPkg'/'FancyUninstaller.tool'}' '--please'"
Cask::FakeSystemCommand.fake_response_for(expected_command)

shutup do
pkg_installer.uninstall
end

Cask::FakeSystemCommand.system_calls[expected_command].must_equal 1
end
end
end
3 changes: 2 additions & 1 deletion test/support/Casks/with-installable.rb
Expand Up @@ -2,6 +2,7 @@ class WithInstallable < TestCask
url TestHelper.local_binary('MyFancyPkg.zip')
homepage 'http://example.com/fancy-pkg'
version '1.2.3'
sha1 'd3540d05a48518cc808ad854a8ab64da94b635b7'
sha1 '8588bd8175a54b8e0a1310cc18e6567d520ab7c4'
install 'Fancy.pkg'
uninstall 'FancyUninstaller.tool', :args => %w[--please]
end
Binary file modified test/support/binaries/MyFancyPkg.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion test/support/fake_system_command.rb
Expand Up @@ -17,7 +17,8 @@ def self.clear
@system_calls = Hash.new(0)
end

def self.run(command)
def self.run(command, options={})
command = Cask::SystemCommand._process_options(command, options)
@responses ||= {}
@system_calls ||= Hash.new(0)
unless @responses.key?(command)
Expand Down

0 comments on commit ddb1fea

Please sign in to comment.