Created
September 11, 2024 14:58
-
-
Save chienpm304/1d4c25e3e8317acfa82da55759a49a95 to your computer and use it in GitHub Desktop.
π Automating CocoaPods Cache Cleanup with Bash π
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
#!/bin/bash | |
# Function to print colored messages | |
print_message() { | |
local color="$1" | |
local message="$2" | |
echo -e "${color}${message}\033[0m" | |
} | |
# Define colors | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[0;33m' | |
# Sample Pod directory structure (replace with actual paths) | |
PODS_CACHE_PATH="${HOME}/Library/Caches/CocoaPods/Pods/Release" | |
# Iterate over each pod directory | |
for pod in "$PODS_CACHE_PATH"/*; do | |
if [ -d "$pod" ]; then | |
pod_name=$(basename "$pod") | |
all_versions=("$pod"/*) | |
# Sorting versions (assuming versions are stored in directory names) | |
sorted_versions=($(printf "%s\n" "${all_versions[@]}" | sort -V)) | |
# Keep the latest version | |
keep_version="${sorted_versions[-1]}" | |
delete_versions=("${sorted_versions[@]:0:${#sorted_versions[@]}-1}") | |
# Log the pod name | |
print_message "$YELLOW" "Pod $pod_name:" | |
# Log deleted versions | |
if [ ${#delete_versions[@]} -gt 0 ]; then | |
for version in "${delete_versions[@]}"; do | |
rm -rf "$version" | |
version_name=$(basename "$version") | |
deleted_versions+=("$version_name") | |
done | |
print_message "$RED" "- Deleted: ${deleted_versions[*]}" | |
else | |
print_message "$RED" "- Deleted: None" | |
fi | |
# Log the kept version | |
keep_version_name=$(basename "$keep_version") | |
print_message "$GREEN" "- Keep: $keep_version_name" | |
# Reset deleted_versions array for the next pod | |
deleted_versions=() | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment