Last active
August 14, 2016 12:09
-
-
Save WeZZard/d0e2a7fc4bcd66755697856cfa239d0f to your computer and use it in GitHub Desktop.
A shell script may improve your gyb experience a lot.
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/sh | |
# Scans all your *.swift.gyb files in your project folder and generates | |
# your boilerplates. | |
# Known bugs: | |
# 1) Timestamp of files with the name contains +, - would conflict each | |
# other. This is caused by Bash 3.2 doesn't support associative array, | |
# which enforces it to use dynamic variable name to store timestamps for | |
# difference file names, and variable name in Bash script cannot | |
# contain characters like "+" and "-", so "+" and "-" are all converted | |
# into "_". | |
# Usage: | |
# 1) Copy the gyb and gyb.py from the Swift's repository and put them in | |
# Your_Project_Root_Folder/Utilities. | |
# 2) Add this shell script to your Xcode's build time phases. | |
# 3) Command + Shift + K(Clean build) would clean the boilerplates' | |
# creation timestamp, and you can use this key combination to enforce | |
# the shell script to re-generate all the boilerplates. | |
SOURCE_DIR=$PROJECT_DIR | |
GYB="$PROJECT_DIR/Utilities/gyb" | |
TIMESTAMPS_LOC="$DERIVED_FILES_DIR/gyb_timestamps" | |
TIMESTAMP_KEYS=() | |
if [ -f $TIMESTAMPS_LOC ]; then | |
# Read timestamps | |
while read -r LINE; do | |
IFS=':' read -ra LINE_COMPONENTS <<< "$LINE" | |
KEY="${LINE_COMPONENTS[0]}" | |
TIMESTAMP="${LINE_COMPONENTS[1]}" | |
declare TIMESTAMPS__${KEY}="$TIMESTAMP" | |
done < "$TIMESTAMPS_LOC" | |
fi | |
IFS=$'\n'; for SOURCE_FILE in $(find /$SOURCE_DIR -name '*.gyb'); do | |
if [[ "$SOURCE_FILE" =~ ^[^\0]+.swift.gyb$ ]]; then | |
SOURCE_TIMESTAMP=`stat -f %m $SOURCE_FILE` | |
KEY=${SOURCE_FILE//[.\/+\-\\]/_} | |
TIMESTAMP_KEYS+=($KEY) | |
TIMESTAMP_NAME=TIMESTAMPS__${KEY} | |
DESTINATION_TIMESTAMP=${!TIMESTAMP_NAME} | |
if [ -z "$DESTINATION_TIMESTAMP" ]; then | |
declare DESTINATION_TIMESTAMP=0 | |
fi | |
if [ $SOURCE_TIMESTAMP -ge $DESTINATION_TIMESTAMP ]; then | |
DESTINATION_FILE=$(expr $SOURCE_FILE \ | |
| egrep -o "[^\0]+.swift") | |
echo "Generate your biolerplate from $SOURCE_FILE to \ | |
$DESTINATION_FILE" | |
$GYB $SOURCE_FILE -o $DESTINATION_FILE --line-directive "" | |
NEW_TIMESTAMP=`stat -f %m $DESTINATION_FILE` | |
declare TIMESTAMPS__${KEY}="$NEW_TIMESTAMP" | |
EXIT_CODE=$? | |
if [[ $EXIT_CODE != 0 ]]; then | |
exit $EXIT_CODE | |
fi | |
else | |
echo "Generated biolerplate found: $DESTINATION. \ | |
Re-generation was skipped due to the template's last modified \ | |
timestamp is ealier than the generated biolerplate's." | |
fi | |
fi | |
done | |
> "$TIMESTAMPS_LOC" | |
for KEY in "${TIMESTAMP_KEYS[@]}"; do | |
TIMESTAMP_NAME=TIMESTAMPS__${KEY} | |
echo "$KEY:${!TIMESTAMP_NAME}" >> "$TIMESTAMPS_LOC" | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment