Move RMDIR_SH into separate file.
This commit is contained in:
parent
23376bad75
commit
5aa0dbe1f9
@ -107,54 +107,14 @@ module Cask
|
||||
|
||||
# Helper script to delete empty directories after deleting `.DS_Store` files and broken symlinks.
|
||||
# Needed in order to execute all file operations with `sudo`.
|
||||
RMDIR_SH = <<~'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
|
||||
BASH
|
||||
RMDIR_SH = (HOMEBREW_LIBRARY_PATH/"cask/utils/rmdir.sh").freeze
|
||||
private_constant :RMDIR_SH
|
||||
|
||||
sig { params(path: T.any(Pathname, T::Array[Pathname])).void }
|
||||
def rmdir(path)
|
||||
@command.run!(
|
||||
"/usr/bin/xargs",
|
||||
args: ["-0", "--", "/bin/bash", "-c", RMDIR_SH, "--"],
|
||||
args: ["-0", "--", RMDIR_SH.to_s],
|
||||
input: Array(path).join("\0"),
|
||||
sudo: true,
|
||||
)
|
||||
|
||||
41
Library/Homebrew/cask/utils/rmdir.sh
Executable file
41
Library/Homebrew/cask/utils/rmdir.sh
Executable 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
|
||||
@ -97,7 +97,7 @@ describe Cask::Pkg, :cask do
|
||||
allow(fake_system_command).to receive(:run!).and_call_original
|
||||
expect(fake_system_command).to receive(:run!).with(
|
||||
"/usr/bin/xargs",
|
||||
args: ["-0", "--", "/bin/bash", "-c", a_string_including("/bin/rmdir"), "--"],
|
||||
args: ["-0", "--", a_string_including("rmdir")],
|
||||
input: [fake_dir].join("\0"),
|
||||
sudo: true,
|
||||
).and_return(instance_double(SystemCommand::Result, stdout: ""))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user