Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rmdir: recursive #6922

Merged
merged 1 commit into from Feb 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions Library/Homebrew/cask/artifact/abstract_uninstall.rb
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
danielbayley marked this conversation as resolved.
Show resolved Hide resolved
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
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
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