Skip to content

Instantly share code, notes, and snippets.

@monkeydom
Last active October 24, 2015 23:50
Show Gist options
  • Save monkeydom/e6b38e8c34f3147a08c6 to your computer and use it in GitHub Desktop.
Save monkeydom/e6b38e8c34f3147a08c6 to your computer and use it in GitHub Desktop.
Script to be used in a hashbang to conditionally compile and then run the swift script referenced. usage #!/usr/bin/env swiftc.sh in your swift file, and put in path
#!/bin/sh
SWIFT=$(/usr/bin/env xcrun -f swift)
SCRIPTPATH=$1
COMPILEDPATH="$SCRIPTPATH.o"
SDKPATH=$(/usr/bin/env xcrun --show-sdk-path --sdk macosx)
#compile if necessary
if [ $SCRIPTPATH -nt $COMPILEDPATH ]; then
$SWIFT -o "$COMPILEDPATH" -sdk "$SDKPATH" "$SCRIPTPATH"
if [ $? -ne 0 ]; then
exit $?
fi
fi
# ${@:n} passes on all arguments, sarting with the nth, "${@:n}" makes it work iwth escaped arguments as well (spaces, etc)
$COMPILEDPATH "${@:2}"
@mpw
Copy link

mpw commented Jun 11, 2014

./$COMPILEDPATH ${@:2}

For those of us who don't put "." in the $PATH...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment