Permalink
Find file
0f8843a Dec 23, 2016
executable file 57 lines (44 sloc) 1.16 KB
#!/usr/bin/env bash
set -e
if [[ ("$#" -lt 1) || !(-e "$1") ]]; then
(>&2 echo "Usage: $0 file arg...")
exit 1
fi
MKGO_CACHE="${MKGO_CACHE:=${HOME}/.mkgo/cache}" && mkdir -p "${MKGO_CACHE}"
compile() {
cc "$1" -o "$2"
}
shebang() {
read -n2 first2 < "${1}"
[[ "${first2}" == "#!" ]]
}
_mktemp() {
case $(uname -s) in
Darwin) mktemp -t temp ;;
Linux) mktemp ;;
*) (>&2 echo "Unknown system '$(uname -s)', don't now how to _mktemp");
exit 1 ;;
esac
}
_realpath() {
[[ "${1}" = /* ]] && RET="${1}" || RET="${PWD}/${1#./}"
}
strip_shebang() {
local tmp="${1}"
if shebang "${1}"; then
tmp="$(_mktemp).c"
tail -n+2 "${1}" > $tmp
fi
RET="${tmp}"
}
_realpath "${1}"; SRC_FILE="${RET}"
SRC_BASENAME="${SRC_FILE##*/}"
SRC_BASENAME_NOEXT="${SRC_BASENAME%.*}"
strip_shebang "${SRC_FILE}"; STRIPPED_FILE="${RET}"
CACHED_BIN="${MKGO_CACHE}/${SRC_FILE}/${SRC_BASENAME_NOEXT}"
if [[ !(-f "${CACHED_BIN}") || ("${CACHED_BIN}" -ot "${SRC_FILE}") ]]; then
rm -rf "${MKGO_CACHE}/${SRC_FILE}"
mkdir -p "${MKGO_CACHE}/${SRC_FILE}"
compile "${STRIPPED_FILE}" "${CACHED_BIN}"
fi
shift && exec -a "${SRC_BASENAME_NOEXT}" "${CACHED_BIN}" "$@"