Last active
August 5, 2016 08:23
-
-
Save snown/ed55fe772208f59e8128 to your computer and use it in GitHub Desktop.
Quick bash script for converting Swift playgrounds between Mac and iOS.
This file contains 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 | |
VERSION=0.1.0 | |
USAGE="$(basename "$0") [--ios|--mac] [-f ./file.playground] [Files...] | |
where: | |
-h Show this help text | |
--ios Tells the command to turn the playground into a iOS compatible playground (default). | |
--mac Tells the command to turn the playground into a iOS compatible playground. | |
-f The path to a playground file (or you can leave out and all trailing arguments are parsed for playgrounds). | |
" | |
iOSFlag=true | |
macFlag=false | |
pathArray=() | |
index=0 | |
if [[ $# -eq 0 ]]; then | |
printf "Missing playground file.\n\n" >&2 | |
echo "$USAGE" >&2 | |
exit 1 | |
fi | |
while [ $# -gt 0 ] | |
do | |
case "$1" in | |
-h) | |
echo "$USAGE" | |
exit;; | |
--version) | |
echo "$(basename $0) Version $VERSION (https://gist.github.com/snown/ed55fe772208f59e8128)" | |
exit;; | |
--ios) | |
macFlag=false | |
iOSFlag=true;; | |
--mac) | |
macFlag=true | |
iOSFlag=false;; | |
-f|--file) | |
if [[ -n "$2" ]]; then | |
if [[ "$2" =~ \.playground$ && -d "$2" ]]; then | |
pathArray[index]="$2" | |
fi | |
shift | |
fi;; | |
--) | |
shift; break;; | |
-*) | |
printf "Illegal option: $1\n\n" >&2 | |
echo "$USAGE" >&2 | |
exit 1;; | |
*) | |
break;; | |
esac | |
shift | |
done | |
for arg in "$@"; do | |
if [[ "$arg" =~ \.playground$ && -d "$arg" ]]; then | |
pathArray[index]="$arg" | |
((index++)) | |
fi | |
done | |
old_sdk= | |
new_sdk= | |
old_baseClass= | |
new_sdk= | |
if [ $iOSFlag == true ]; then | |
old_sdk="macosx" | |
old_baseClass="Foundation" | |
new_sdk="iphonesimulator" | |
new_baseClass="UIKit" | |
elif [ $macFlag == true ]; then | |
new_sdk="macosx" | |
new_baseClass="Foundation" | |
old_sdk="iphonesimulator" | |
old_baseClass="UIKit" | |
fi | |
if [ ${#pathArray[@]} -eq 0 ]; then | |
printf "Missing playground file.\n\n" >&2 | |
echo "$USAGE" >&2 | |
exit 1 | |
fi | |
index=0 | |
for arg in "${pathArray[@]}"; do | |
cd "$arg" | |
# sed -i '' -e "s/^import $old_baseClass\s*$/import $new_baseClass/g" ./*.swift | |
sed -i '' -e "s/\(^<playground.*sdk=\"\)\($old_sdk\)\(\".*>$\)/\1$new_sdk\3/g" ./contents.xcplayground | |
((index++)) | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment