Skip to content

Instantly share code, notes, and snippets.

@kevinmbeaulieu
Last active July 6, 2022 16:54
Show Gist options
  • Select an option

  • Save kevinmbeaulieu/d86413907523a6ccc3a52fdddd647566 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmbeaulieu/d86413907523a6ccc3a52fdddd647566 to your computer and use it in GitHub Desktop.
Delete derived data
#!/bin/sh
# To install, run:
# sudo install ddd /usr/local/bin
set -euo pipefail
print_usage() {
cat <<EOF
usage: ddd [<options>] [<project>]
-f, --force Deelte derived data without asking for confirmation
-h, --help Print usage information
-n, --dry-run Don't actually delete anything, just show what would be deleted
-q, --quiet Suppress non-essential output
EOF
}
dry_run=0
force=0
quiet=0
project_name=''
for arg in "$@"
do
case "$arg" in
-h | --help)
print_usage
exit 0
;;
-n | --dry-run)
dry_run=1
;;
-f | --force)
force=1
;;
-q | --quiet)
quiet=1
;;
-*)
echo "Unrecognized flag \"$arg\"\n" 1>&2
print_usage
exit 1
;;
*)
project_name="$arg"
;;
esac
done
derived_data_dir=''
if [[ -z $project_name ]]; then
derived_data_dir='~/Library/Developer/Xcode/DerivedData'
else
[[ $quiet -eq 0 ]] && echo "Getting derived data folder for '$project_name'..."
# path/to/DerivedData/project-12345/Build/Products
build_dir=$(xcodebuild -project "$project_name" -showBuildSettings 2>/dev/null | grep -m 1 "BUILD_DIR" | grep -oEi "\/.*")
# path/to/DerivedData/project-12345
derived_data_dir=$(echo "${build_dir}" | rev | cut -d'/' -f3- | rev)
if [[ -z "${derived_data_dir}" ]]; then
[[ $quiet -eq 0 ]] && echo "Derived data already deleted"
exit 0
fi
fi
if [[ $dry_run -eq 1 ]]; then
echo "Would delete ${derived_data_dir}"
exit 0
fi
if [[ ! $force -eq 1 ]]; then
while true; do
read -rp "Delete ${derived_data_dir} [y/n]? "
case "$REPLY" in
[Yy][Ee]Ss]|[Yy])
break
;;
[Nn][Oo]|[Nn])
[[ $quiet -eq 0 ]] && echo "Canceling without deleting anything."
exit 1
;;
*)
;;
esac
done
fi
rm -rf $derived_data_dir
[[ $quiet -eq 0 ]] && echo "Deleted ${derived_data_dir}"
MIT License
Copyright (c) 2022 Kevin Beaulieu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment