Streamlining iOS Development: Automated Derived Data and Pod Management

Yannis Alexiou
3 min readDec 22, 2023
https://unsplash.com/photos/people-sitting-down-near-table-with-assorted-laptop-computers-SYTO3xs06fU

Introduction

In the dynamic world of iOS development, we’re all too familiar with the pesky issues of local pods and derived data. These can lead to project inconsistencies and extended debugging sessions. To combat this, I present an automated script for post-Git operations, ensuring our local environment is as seamless as a swipe on a freshly unboxed iPhone.

The Usual Suspects: Derived Data and Local Pods

Derived Data and Local Pods, while essential, can be a thorn in a developer’s side. Derived Data, is like that cluttered drawer everyone has at home. It’s supposed to help, storing intermediate build results, but over time it just becomes a chaotic pile you’d rather not open. Similarly, Local Pods, our project-specific dependencies, are great until they’re not, causing version mismatches and other fun surprises.

Proposed Solution: The Hero Script

Enter the hero of our story: a git hook script. This tool is designed to leap into action after merge and checkout operations. It's like having a virtual janitor for your codebase. When it detects changes in Local Pods:

  • Clears Derived Data: It waves goodbye to the old Derived Data. 👋
  • Runs pod install: Ensures everything is current and in harmony. 👌

Simply replace <PROJECT_NAME> with the name of your project and <DevPods_Folder> with the directory where your pods lives.

```bash
GREEN='\033[0;32m'
NC='\033[0m'

info() {
echo "${GREEN}▶ $1${NC}"
}

loader() {
printf "${GREEN}"
while kill -0 $1 2>/dev/null; do
printf "▓"
sleep 0.1
done
printf "${NC}\n"
}

podInstall() {
info "🌍 Running pod install"
export COCOAPODS_DISABLE_STATS=1 # Prevent sending stats, which slows down checkout unbearably
export LANG=en_US.UTF-8 # Cocoapods complain without this
unset GIT_DIR
exec pod install
true
}

# Check for files changed in local Pods
git diff --stat --name-only --quiet HEAD^ HEAD -- <DevPods_Folder>/; LOCALPODSUPDATED=$?

info "📚 Checking pods"
# If Podfile exists and `pod install` does something
if [ -f Podfile ] && command -v pod install >/dev/null
then
# If the Podfile has changed
if git diff --name-only $old $2 | egrep -q '^Podfile$'
then
info "🗑️ Clearing Derived Data"
rm -rf ~/Library/Developer/Xcode/DerivedData/<PROJECT_NAME>-* 2> /dev/null & DELETEAPP=$!
rm -rf ~/Library/Developer/Xcode/DerivedData/ModuleCache.noindex 2> /dev/null & DELETEMODULE=$!
rm -rf ~/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex 2> /dev/null & DELETESDKSTAT=$!
rm -rf ~/Library/Developer/Xcode/DerivedData/SymbolCache.noindex 2> /dev/null & DELETESYMBOL=$!
loader $DELETEAPP
loader $DELETEMODULE
loader $DELETESDKSTAT
loader $DELETESYMBOL
podInstall
else
if [ $LOCALPODSUPDATED -gt 0 ]
then
info "🕵️‍♂️ Local pod diffs found"
podInstall
fi
fi
fi

info "🤝 All done!"
```

Using the Script as a Git Checkout Hook

To utilize this script as a Git checkout hook, simply place it in the .git/hooks directory of your project and name it post-checkout. Ensure the file is executable (chmod +x post-checkout). This setup will automatically execute the script after each git checkout operation, keeping your development environment clean and synchronized. You can read more about git hooks here and here.

Examples of Errors and Why We Need This Script

The Persistent Phantom Errors

Errors that linger despite multiple builds are often due to outdated Derived Data. This script ensures such errors are sent packing.

The Inconsistent Pods Dilemma

When Local Pods are not in sync across the team, it’s like trying to cook a meal with everyone using different recipes. Chaos. This script ensures everyone’s ingredients (or code) are the same.

⚠️ A Word of Caution for Intel-Based Macs

While this script is a boon for maintaining a clean and efficient development environment, those using Intel-based Macs might notice an increase in build time during the initial build post-script execution. This is due to the script clearing out Derived Data, requiring a fresh build. But fear not, this is a small price for the long-term benefits of having a consistent and error-free development environment.

Conclusion

In the fast-paced world of iOS development, any tool that can save us time and maintain sanity is worth its weight in gold (or code). This script isn’t just a time-saver; it’s a mood-saver. Ready to give your development process a little magic touch? Let’s dive in and make our coding lives a bit easier, one automated script at a time.

--

--

Yannis Alexiou

This is me. A passionate software engineer, with a good taste of design driven by enchanted quality.