Last active
November 8, 2023 15:09
-
-
Save jkereako/bee838e0bba611b6f1d3a5cc1ba03a16 to your computer and use it in GitHub Desktop.
Reset Xcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Features
Credit
This was adapted from resetXcode.sh written by maciekish.