Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Update script to no longer modify source file, but instead copy to a …
Browse files Browse the repository at this point in the history
…temp source file to work with
  • Loading branch information
Nicholas Omer Chiasson committed Feb 25, 2017
1 parent eec518d commit d829009
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/car.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
CWD="${PWD}"
SRC_FILE="$1"
SRC_FILE_BASENAME=`basename "${SRC_FILE}"`
TEMP_SRC_FILE="${TMPDIR}/${SRC_FILE_BASENAME}"
TEMP_OUT_FILE=$(mktemp -q)
TEMP_OUT_DIR=$(mktemp -qd)
CXX=gcc
Expand Down Expand Up @@ -77,9 +78,8 @@ function Usage()

function Cleanup()
{
# Put original source file contents back in case a shebang was removed
[ -n "${ORIGINAL_SRC_FILE_CONTENTS}" ] && echo "${ORIGINAL_SRC_FILE_CONTENTS}" > "${SRC_FILE}"
# Remove compilation output
# Remove cloned source file and compilation output
rm -rf "${TEMP_SRC_FILE}"
rm -rf "${TEMP_OUT_FILE}"
rm -rf "${TEMP_OUT_DIR}"
}
Expand Down Expand Up @@ -114,10 +114,13 @@ function IsNumber()
# Require source file
([ -n "${SRC_FILE}" ] && [ -f "${SRC_FILE}" ]) || Usage 1 "must provide <source-file>"

# Look for shebang and remove it from original source, caching the original source to put back later
ORIGINAL_SRC_FILE_CONTENTS=`cat "${SRC_FILE}"`
NEW_SRC_FILE_CONTENTS=`sed -e 's/^#!.*$//g' "${SRC_FILE}"` || Error 2 "failed sed operation on '${SRC_FILE}'"
echo "${NEW_SRC_FILE_CONTENTS}" > "${SRC_FILE}"
# Copy source file contents to temp source file
rm -rf "${TEMP_SRC_FILE}"
cp -f "${SRC_FILE}" "${TEMP_SRC_FILE}"

# Look for shebang and remove it from temp source
NEW_SRC_FILE_CONTENTS=`sed -e 's/^#!.*$//g' "${TEMP_SRC_FILE}"` || Error 2 "failed sed operation on '${TEMP_SRC_FILE}'"
echo "${NEW_SRC_FILE_CONTENTS}" > "${TEMP_SRC_FILE}"

# Determine compiler to use based on file extension
case "${SRC_FILE_BASENAME##*.}" in
Expand Down Expand Up @@ -149,8 +152,8 @@ case "${SRC_FILE_BASENAME##*.}" in
CXX=javac
CXX_OUTPUT_FLAG="-d "
VM=java
JAVA_CLASS_NAME=${SRC_FILE%.java}
JAVA_PACKAGE=$(sed -n -e 's/^package \(.*\);$/\1/p' "${SRC_FILE}")
JAVA_CLASS_NAME=${TEMP_SRC_FILE%.java}
JAVA_PACKAGE=$(sed -n -e 's/^package \(.*\);$/\1/p' "${TEMP_SRC_FILE}")
[ -n "${JAVA_PACKAGE}" ] && JAVA_PACKAGE="${JAVA_PACKAGE}."
PRERUN='cd "${TEMP_OUT_DIR}"; TEMP_OUT_FILE="${JAVA_PACKAGE}"$(basename "${JAVA_CLASS_NAME}")'
POSTRUN='cd "${CWD}"'
Expand All @@ -165,13 +168,13 @@ case "${SRC_FILE_BASENAME##*.}" in
esac

# Perform compilation
${CXX} ${CXX_OUTPUT_FLAG}"${TEMP_OUT_FILE}" "${SRC_FILE}" || Error 4 "failed to compile '${SRC_FILE}' using ${CXX}"
${CXX} ${CXX_OUTPUT_FLAG}"${TEMP_OUT_FILE}" "${TEMP_SRC_FILE}" || Error 4 "failed to compile '${TEMP_SRC_FILE}' using ${CXX}"

# Optional pre-run commands
eval "${PRERUN}"

# Execute ouput file passing any extra command-line arguments
${VM} "${TEMP_OUT_FILE}" ${@:2} || Error 5 "'${SRC_FILE}' failed with exit code $?"
${VM} "${TEMP_OUT_FILE}" ${@:2} || Error 5 "'${TEMP_SRC_FILE}' failed with exit code $?"

# Optional post-run commands
eval "${POSTRUN}"
Expand Down

0 comments on commit d829009

Please sign in to comment.