Move RMDIR_SH into separate file.

This commit is contained in:
Markus Reiter 2021-03-30 01:09:01 +02:00
parent 23376bad75
commit 5aa0dbe1f9
No known key found for this signature in database
GPG Key ID: 245293B51702655B
3 changed files with 44 additions and 43 deletions

View File

@ -107,54 +107,14 @@ module Cask
# Helper script to delete empty directories after deleting `.DS_Store` files and broken symlinks. # Helper script to delete empty directories after deleting `.DS_Store` files and broken symlinks.
# Needed in order to execute all file operations with `sudo`. # Needed in order to execute all file operations with `sudo`.
RMDIR_SH = <<~'BASH' RMDIR_SH = (HOMEBREW_LIBRARY_PATH/"cask/utils/rmdir.sh").freeze
set -euo pipefail
# Try removing as many empty directories as possible with a single
# `rmdir` call to avoid or at least speed up the loop below.
if /bin/rmdir -- "${@}" &>/dev/null; then
exit
fi
for path in "${@}"; do
symlink=true
[[ -L "${path}" ]] || symlink=false
directory=false
if [[ -d "${path}" ]]; then
directory=true
if [[ -e "${path}/.DS_Store" ]]; then
/bin/rm -- "${path}/.DS_Store"
fi
# Some packages leave broken symlinks around; we clean them out before
# attempting to `rmdir` to prevent extra cruft from accumulating.
/usr/bin/find -f "${path}" -- -mindepth 1 -maxdepth 1 -type l ! -exec /bin/test -e {} \; -delete
elif ! ${symlink} && [[ ! -e "${path}" ]]; then
# Skip paths that don't exists and aren't a broken symlink.
continue
fi
if ${symlink}; then
# Delete directory symlink.
/bin/rm -- "${path}"
elif ${directory}; then
# Delete directory if empty.
/usr/bin/find -f "${path}" -- -maxdepth 0 -type d -empty -exec /bin/rmdir -- {} \;
else
# Try `rmdir` anyways to show a proper error.
/bin/rmdir -- "${path}"
fi
done
BASH
private_constant :RMDIR_SH private_constant :RMDIR_SH
sig { params(path: T.any(Pathname, T::Array[Pathname])).void } sig { params(path: T.any(Pathname, T::Array[Pathname])).void }
def rmdir(path) def rmdir(path)
@command.run!( @command.run!(
"/usr/bin/xargs", "/usr/bin/xargs",
args: ["-0", "--", "/bin/bash", "-c", RMDIR_SH, "--"], args: ["-0", "--", RMDIR_SH.to_s],
input: Array(path).join("\0"), input: Array(path).join("\0"),
sudo: true, sudo: true,
) )

View File

@ -0,0 +1,41 @@
#!/bin/bash
set -euo pipefail
# Try removing as many empty directories as possible with a single
# `rmdir` call to avoid or at least speed up the loop below.
if /bin/rmdir -- "${@}" &>/dev/null; then
exit
fi
for path in "${@}"; do
symlink=true
[[ -L "${path}" ]] || symlink=false
directory=false
if [[ -d "${path}" ]]; then
directory=true
if [[ -e "${path}/.DS_Store" ]]; then
/bin/rm -- "${path}/.DS_Store"
fi
# Some packages leave broken symlinks around; we clean them out before
# attempting to `rmdir` to prevent extra cruft from accumulating.
/usr/bin/find -f "${path}" -- -mindepth 1 -maxdepth 1 -type l ! -exec /bin/test -e {} \; -delete
elif ! ${symlink} && [[ ! -e "${path}" ]]; then
# Skip paths that don't exists and aren't a broken symlink.
continue
fi
if ${symlink}; then
# Delete directory symlink.
/bin/rm -- "${path}"
elif ${directory}; then
# Delete directory if empty.
/usr/bin/find -f "${path}" -- -maxdepth 0 -type d -empty -exec /bin/rmdir -- {} \;
else
# Try `rmdir` anyways to show a proper error.
/bin/rmdir -- "${path}"
fi
done

View File

@ -97,7 +97,7 @@ describe Cask::Pkg, :cask do
allow(fake_system_command).to receive(:run!).and_call_original allow(fake_system_command).to receive(:run!).and_call_original
expect(fake_system_command).to receive(:run!).with( expect(fake_system_command).to receive(:run!).with(
"/usr/bin/xargs", "/usr/bin/xargs",
args: ["-0", "--", "/bin/bash", "-c", a_string_including("/bin/rmdir"), "--"], args: ["-0", "--", a_string_including("rmdir")],
input: [fake_dir].join("\0"), input: [fake_dir].join("\0"),
sudo: true, sudo: true,
).and_return(instance_double(SystemCommand::Result, stdout: "")) ).and_return(instance_double(SystemCommand::Result, stdout: ""))