compile.sh (2599B)
1 #!/bin/sh 2 # 3 # compile 4 5 . "${HOME}/.local/bin/symlink.sh" 6 7 main() { 8 # use POSIX-compliant way to follow symlink 9 file="$(follow_link "${1}")" 10 11 # dir, base, ext are good things to know 12 dir="${file%/*}/" 13 base="${file%.*}" 14 ext="${file##*.}" 15 16 # dir does not exist and exist 17 cd "${dir}" || return 1 18 19 # clear screen for neatness 20 clear 21 22 # add some functionality to this basic compiler 23 while getopts 'c:o:' opt; do 24 case "${opt}" in 25 # shell check if called with c flag and shell script 26 c) if [ "${OPTARG##*.}" = 'sh' ]; then 27 if [ -z "$(shellcheck -x "${OPTARG}")" ]; then printf 'No issues detected!' 28 else shellcheck -x "${OPTARG}" 29 fi 30 else printf 'Not a shell script.' 31 fi 32 return 33 ;; 34 # open same file name with different extension if called with o flag 35 o) case "${OPTARG##*.}" in 36 'tex') [ -f "${OPTARG%.*}.pdf" ] && "${HOME:-/home/${USER}/}/.local/bin/open.sh" "${OPTARG%.*}.pdf" >/dev/null 2>&1 & ;; 37 esac 38 return 39 ;; 40 *) break ;; 41 esac 42 done 43 44 # different compile options based on file extension 45 case "${ext}" in 46 # GNU compilers 47 'c') cc "${file}" -o "${base}" && "${base}" ;; 48 'cpp') g++ "${file}" -o "${base}" && "${base}" ;; 49 'java') javac "${file}" && java "${file}" ;; 50 'py') python "${file}" ;; 51 'sh') sh "${file}" ;; 52 'tex') xelatex --output-directory="${dir}" "${base}" 53 if grep -iq 'addbibresource' "${file}"; then 54 biber --input-directory "${dir}" "${base}" 55 fi 56 xelatex --output-directory="${dir}" "${base}" 57 # remove annoying files generated by latex 58 for f in "${base}".*; do 59 case "${f##*.}" in 60 'aux' | \ 61 'bbl' | \ 62 'bcf' | \ 63 'blg' | \ 64 'lof' | \ 65 'log' | \ 66 'out' | \ 67 'toc' | \ 68 'xml') rm -f "${f}" ;; 69 esac 70 done 71 ;; 72 esac 73 } 74 75 main "${@}"