Skip to content

Commit

Permalink
Force overwrite artifacts on --force reinstall
Browse files Browse the repository at this point in the history
This commit changes the behavior of a `Moved` artifact such that if the target already exists, `brew cask install --force` will remove the existing target before moving the staged artifact.

In that case, the warning message will say *overwriting* instead of *not moving*.

The behavior of plain `brew cask install` remains unchanged; the same goes for the warning message for that case.
  • Loading branch information
claui authored and mwean committed May 31, 2016
1 parent 9876d17 commit 6500fa1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lib/hbc/artifact/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def self.me?(cask)
cask.artifacts[self.artifact_dsl_key].any?
end

attr_reader :force

def zap_phase
odebug "Nothing to do. The #{self.class.artifact_name} artifact has no zap phase."
end
Expand Down Expand Up @@ -76,8 +78,9 @@ def summary
{}
end

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)
end
end
19 changes: 17 additions & 2 deletions lib/hbc/artifact/moved.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def install_phase
each_artifact do |artifact|
load_specification(artifact)
next unless preflight_checks
delete if File.exist?(target)
move
end
end
Expand Down Expand Up @@ -64,8 +65,12 @@ def load_specification(artifact_spec)

def preflight_checks
if target.exist?
ohai "It seems there is already #{self.class.artifact_english_article} #{english_name} at '#{target}'; not moving."
return false
if force
ohai(warning_target_exists { |s| s << 'overwriting.' })
else
ohai(warning_target_exists { |s| s << 'not moving.' })
return false
end
end
unless source.exist?
message = "It seems the #{english_name} source is not there: '#{source}'"
Expand All @@ -74,6 +79,16 @@ def preflight_checks
true
end

def warning_target_exists
message_parts = [
"It seems there is already #{
self.class.artifact_english_article} #{english_name
} at '#{target}'"
]
yield(message_parts) if block_given?
message_parts.join('; ')
end

def delete
ohai "Removing #{english_name}: '#{target}'"
target.rmtree
Expand Down
6 changes: 4 additions & 2 deletions lib/hbc/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def install_artifacts
odebug "#{artifacts.length} artifact/s defined", artifacts
artifacts.each do |artifact|
odebug "Installing artifact of class #{artifact}"
artifact.new(@cask, @command).install_phase
options = { command: @command, force: force }
artifact.new(@cask, options).install_phase
end
end

Expand Down Expand Up @@ -293,7 +294,8 @@ def uninstall_artifacts
odebug "#{artifacts.length} artifact/s defined", artifacts
artifacts.each do |artifact|
odebug "Un-installing artifact of class #{artifact}"
artifact.new(@cask, @command).uninstall_phase
options = { command: @command, force: force }
artifact.new(@cask, options).uninstall_phase
end
end

Expand Down
6 changes: 4 additions & 2 deletions test/cask/artifact/pkg_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

describe 'install_phase' do
it 'runs the system installer on the specified pkgs' do
pkg = Hbc::Artifact::Pkg.new(@cask, 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', '/'])

Expand All @@ -22,7 +23,8 @@

describe 'uninstall_phase' do
it 'does nothing, because the uninstall_phase method is a no-op' do
pkg = Hbc::Artifact::Pkg.new(@cask, Hbc::FakeSystemCommand)
pkg = Hbc::Artifact::Pkg.new(@cask,
command: Hbc::FakeSystemCommand)
shutup do
pkg.uninstall_phase
end
Expand Down
5 changes: 4 additions & 1 deletion test/cask/artifact/uninstall_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

describe Hbc::Artifact::Uninstall do
let(:cask) { Hbc.load('with-installable') }
let(:uninstall_artifact) { Hbc::Artifact::Uninstall.new(cask, Hbc::FakeSystemCommand) }

let(:uninstall_artifact) do
Hbc::Artifact::Uninstall.new(cask, command: Hbc::FakeSystemCommand)
end

before {
shutup do
Expand Down
5 changes: 4 additions & 1 deletion test/cask/artifact/zap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
# - test that zap removes an alternate version of the same Cask
describe Hbc::Artifact::Zap do
let(:cask) { Hbc.load('with-installable') }
let(:zap_artifact) { Hbc::Artifact::Zap.new(cask, Hbc::FakeSystemCommand) }

let(:zap_artifact) do
Hbc::Artifact::Zap.new(cask, command: Hbc::FakeSystemCommand)
end

before {
shutup do
Expand Down
2 changes: 1 addition & 1 deletion test/cask/cli/install_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

TestHelper.must_output(self, lambda {
Hbc::CLI::Install.run('local-transmission', '--force')
}, %r{==> Success! local-transmission staged at '#{Hbc.caskroom}/local-transmission/2.61' \(487 files, 11M\)})
}, %r{==> Success! local-transmission staged at '#{Hbc.caskroom}/local-transmission/2.61' \(0B\)})
end

it "properly handles Casks that are not present" do
Expand Down

0 comments on commit 6500fa1

Please sign in to comment.