Skip to content

Instantly share code, notes, and snippets.

@jkereako
Last active November 8, 2023 15:09
Show Gist options
  • Save jkereako/bee838e0bba611b6f1d3a5cc1ba03a16 to your computer and use it in GitHub Desktop.
Save jkereako/bee838e0bba611b6f1d3a5cc1ba03a16 to your computer and use it in GitHub Desktop.
Reset Xcode
#!/usr/bin/env bash
#
# resetxc
#
# Kills Xcode, deletes all caches and build artifacts and re-opens Xcode
set -Eeuo pipefail
# Print out error messages to STDERR.
function err() {
echo -e "\033[0;31mERROR: $@\033[0m" >&2;
}
function check_git() {
if ! git rev-parse --git-dir 1> /dev/null; then
err "Current directory is not tracked with Git"; exit 2;
fi;
}
function kill() {
killall Xcode
}
function clean() {
# xcodebuild -alltargets clean
xcrun --kill-cache
git clean -xfd
rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang/ModuleCache"
rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang.$(whoami)/ModuleCache"
rm -rf ~/Library/Developer/Xcode/DerivedData/*
rm -rf ~/Library/Caches/com.apple.dt.Xcode/*
}
function resolve_dependencies() {
root_dir=$(git rev-parse --show-toplevel)
xcode_proj=$(find . -name "*.xcodeproj" | head -n 1)
dir=$(dirname "${xcode_proj}")
cd "${dir}"
xcodebuild -resolvePackageDependencies -verbose
cd "${root_dir}"
}
function purge_swiftpm_cache() {
root_dir=$(git rev-parse --show-toplevel)
# cd into the directory that has the main Package.swift
cd "$1"
swift package purge-cache
swift package reset
cd "${root_dir}"
}
function open_xcode_project() {
root_dir=$(git rev-parse --show-toplevel)
xcode_proj=$(find . -name "*.xcodeproj" | head -n 1)
# Trim the leading "./"
proj_url="${root_dir}/${xcode_proj#"./"}"
# xed – Xcode text editor invocation tool.
# xed was introduced in Mac OS X 10.5 with Xcode 3.0.u
xed -b "${proj_url}"
}
function rm_package_resolved() {
root_dir=$(git rev-parse --show-toplevel)
xcode_proj=$(find . -name "*.xcodeproj" | head -n 1)
package_resolved="/project.xcworkspace/xcshareddata/swiftpm/Package.resolved"
# Trim the leading "./"
package_resolved_path="${root_dir}/${xcode_proj#"./"}${package_resolved}"
rm -fv "${package_resolved_path}"
}
#-- Main
function main() {
check_git
# Kill Xcode if it's running
if pgrep Xcode 1> /dev/null; then
echo "💀 Killing Xcode..."
kill
fi
if xcrun simctl list | grep booted; then
# Shutdown all the simulators
echo "🔑 Clearing the simluator Keychain..."
xcrun simctl keychain booted reset
xcrun simctl shutdown booted
fi
# Clean
echo "🧹 Cleaning targets and caches..."
clean
# Remove Package.resolved
echo "🧹 Removing Package.resolved..."
rm_package_resolved
echo "📦 Resolving package dependencies..."
resolve_dependencies
# Open
open_xcode_project
echo "✅ Done!"
}
main "$@"
@jkereako
Copy link
Author

jkereako commented Jul 20, 2022

Usage

$ resetxc

Features

  • Kills Xcode
  • Shuts down simulators
  • Clears the Keychain from running simulators
  • Removes all build artificats
  • Removes the project's Package.resolved file
  • Restores packages

Credit

This was adapted from resetXcode.sh written by maciekish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment