Make rmdir: recursive

This commit is contained in:
Daniel Bayley 2020-01-10 18:14:53 +00:00
parent f262e6f3b4
commit f3a0fcfb6d
3 changed files with 30 additions and 11 deletions

View File

@ -385,20 +385,37 @@ module Cask
[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
success
end
def uninstall_rmdir(*args)
return if args.empty?
ohai "Removing directories if empty:"
recursive_rmdir(*args)
end
end
end

View File

@ -14,10 +14,11 @@ describe Cask::Artifact::Uninstall, :cask do
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
@ -26,7 +27,7 @@ describe Cask::Artifact::Uninstall, :cask do
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)

View File

@ -12,10 +12,11 @@ describe Cask::Artifact::Zap, :cask do
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
@ -24,7 +25,7 @@ describe Cask::Artifact::Zap, :cask do
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)