Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions Library/Homebrew/cask/artifact/abstract_uninstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,20 +385,37 @@ def trash_paths(*paths, command: nil, **_)
[trashed, untrashable]
end

def uninstall_rmdir(*directories, command: nil, **_)
return if directories.empty?
def all_dirs?(*directories)
directories.all?(&:directory?)
end

def recursive_rmdir(*directories, command: nil, **_)
success = true
each_resolved_path(:rmdir, directories) do |_path, resolved_paths|
resolved_paths.select(&method(:all_dirs?)).each do |resolved_path|
puts resolved_path.sub(Dir.home, "~")

ohai "Removing directories if empty:"
each_resolved_path(:rmdir, directories) do |path, resolved_paths|
puts path
resolved_paths.select(&:directory?).each do |resolved_path|
if (ds_store = resolved_path.join(".DS_Store")).exist?
command.run!("/bin/rm", args: ["-f", "--", ds_store], sudo: true, print_stderr: false)
end

command.run("/bin/rmdir", args: ["--", resolved_path], sudo: true, print_stderr: false)
unless recursive_rmdir(*resolved_path.children, command: command)
success = false
next
end

status = command.run("/bin/rmdir", args: ["--", resolved_path], sudo: true, print_stderr: false).success?
success &= status
end
end
Comment thread
danielbayley marked this conversation as resolved.
Outdated
success
end

def uninstall_rmdir(*args)
return if args.empty?

ohai "Removing directories if empty:"
recursive_rmdir(*args)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions Library/Homebrew/test/cask/artifact/uninstall_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
let(:fake_system_command) { NeverSudoSystemCommand }
let(:cask) { Cask::CaskLoader.load(cask_path("with-uninstall-rmdir")) }
let(:empty_directory) { Pathname.new("#{TEST_TMPDIR}/empty_directory_path") }
let(:empty_directory_tree) { empty_directory.join("nested", "empty_directory_path") }
let(:ds_store) { empty_directory.join(".DS_Store") }

before do
empty_directory.mkdir
empty_directory_tree.mkpath
FileUtils.touch ds_store
end

Expand All @@ -26,7 +27,7 @@
end

it "is supported" do
expect(empty_directory).to exist
expect(empty_directory_tree).to exist
expect(ds_store).to exist

artifact.post_uninstall_phase(command: fake_system_command)
Expand Down
5 changes: 3 additions & 2 deletions Library/Homebrew/test/cask/artifact/zap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
let(:fake_system_command) { NeverSudoSystemCommand }
let(:cask) { Cask::CaskLoader.load(cask_path("with-zap-rmdir")) }
let(:empty_directory) { Pathname.new("#{TEST_TMPDIR}/empty_directory_path") }
let(:empty_directory_tree) { empty_directory.join("nested", "empty_directory_path") }
let(:ds_store) { empty_directory.join(".DS_Store") }

before do
empty_directory.mkdir
empty_directory_tree.mkpath
FileUtils.touch ds_store
end

Expand All @@ -24,7 +25,7 @@
end

it "is supported" do
expect(empty_directory).to exist
expect(empty_directory_tree).to exist
expect(ds_store).to exist

artifact.zap_phase(command: fake_system_command)
Expand Down