Skip to content

Instantly share code, notes, and snippets.

@acerosalazar
Created May 18, 2018 12:14
Show Gist options
  • Save acerosalazar/1c414b64fb38ab5bb7ddfae66ace79d8 to your computer and use it in GitHub Desktop.
Save acerosalazar/1c414b64fb38ab5bb7ddfae66ace79d8 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
#
# This script acts as a wrapper of carthage and as such it will handle
# the same commands handled by carthage, doing aditional work in some
# cases (e.g bootstrap) and simply relaying the invokations to carthage
# in the others.
#
# Decorated Commands:
# - bootstrap:
# When invoking for the first time, the script will let Carthage run
# uninterrupted, and once it's done it will create an archive of the
# Carthage directory and will associated with the hash of the
# Cartfile.resolved file. The next time the script is run using the
# same Cartfile.resolved, the archived version of the Carthage folder
# will be used instead of letting carthage re-do all the work.
set -o errexit
set -o pipefail
set -o nounset
function bootstrap() {
local -r CARTFILE_RESOLVED="Cartfile.resolved"
local -r CARTHAGE_DIR="Carthage"
local -r CARTHAGE_CACHE="/tmp"
# Compute the md5 hash of the resolved file
local -r HASH="$(shasum "$CARTFILE_RESOLVED" | cut -d' ' -f1)"
# Retrieve xcode version
local -r XCODE_VERSION="$(xcodebuild -version | tail -n 1 | cut -d' ' -f3)"
# Compute the name of the archive
local -r ARCHIVE_FILE="${HASH}-${XCODE_VERSION}.tar.gz"
# Compute the name of the backup file
local -r BACKUP_FILE="$CARTHAGE_DIR.tar.gz"
# Check if the cache contains the hash of the resolved file
if [[ ! -f "$CARTHAGE_CACHE/$ARCHIVE_FILE" ]]; then
# Let carthage do its magic
carthage "$@"
# Make sure there is a directory where we can save the cache
if [[ ! -d "$CARTHAGE_CACHE" ]]; then
mkdir -p "$CARTHAGE_CACHE"
fi
# Backup the current carthage folder for future uses
tar -czf "$CARTHAGE_CACHE/$ARCHIVE_FILE" "$CARTHAGE_DIR"
exit 0
else
echo "Found suitable archive $CARTHAGE_CACHE/$ARCHIVE_FILE"
# Check if there is a Carthage dir
if [[ -d "$CARTHAGE_DIR" ]]; then
# Make a backup of the carthage dir
tar -czf "$BACKUP_FILE" "$CARTHAGE_DIR"
# Remove the dir
rm -rf "$CARTHAGE_DIR"
fi
echo "Unpacking archive"
# Unpack (xf) and uncompress (z) the existing cache
if tar -xzf "$CARTHAGE_CACHE/$ARCHIVE_FILE"; then
# Check if we have a backup
if [[ -f "$BACKUP_FILE" ]]; then
rm -rf "$BACKUP_FILE"
fi
echo "Done"
exit 0
else
echo "Could not unpack archive. Letting carthage take over"
# Remove the corrupted archive
local -r ARCHIVE_PATH="$CARTHAGE_CACHE/$ARCHIVE_FILE"
rm -rf "${ARCHIVE_PATH:?}"
# Check if we have a backup
if [[ -f "$BACKUP_FILE" ]]; then
# Restore the backed-up Carthage folder
tar -xzf "$BACKUP_FILE" .
# Remove the back up
rm -rf "${BACKUP_FILE}"
fi
# Let carthage take care of the request
carthage "$@"
fi
fi
}
function main() {
local -r ACTION="$1"
if [[ "$ACTION" == "bootstrap" ]]; then
# Use the cache if possible. Otherwise relay call to carthage
bootstrap "$@"
else
# Run carthage action
carthage "$@"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment