main
 1#!/bin/sh
 2# shellcheck disable=1001,1012,2039,1090
 3# 1001,1012 stop complaints about '\awk' syntax to bypass aliases.
 4# 2039 stops complaints about array references not being POSIX.
 5# 1090 stops complaints about sourcing non-constant paths.
 6
 7__shell="$(\ps -p $$ | \awk 'NR > 1 { sub(/^-/, "", $4); print $4 }')"
 8__shellname="$(basename "${__shell}")"
 9
10case "${__shellname}" in
11  bash)
12    __jive_root_dir="$(builtin cd "$(\dirname "${BASH_SOURCE[0]}")" && \pwd)"
13    ;;
14  zsh)
15    __jive_root_dir="$(\dirname "$0:A")"
16    ;;
17  *)
18    >&2 \echo "jive is not compatible with your shell (${__shell})"
19    \return 1
20    ;;
21esac
22
23__jive_exe_dir="${__jive_root_dir}/exe"
24__jive_lib_dir="${__jive_root_dir}/lib"
25__jive_script="${__jive_root_dir}/jive.sh"
26
27__mtime_of_jive_script="$(\date -r "${__jive_script}" +%s)"
28__jive_auto_reload() {
29  local current_mtime
30  current_mtime="$(\date -r "${__jive_script}" +%s)"
31
32  if [[ "${current_mtime}" != "${__mtime_of_jive_script}" ]]; then
33    echo "Reloading... ${__jive_script}"
34    . "${__jive_script}"
35  fi
36}
37
38__jive_exec() {
39  /usr/bin/env -S ruby -I "${__jive_lib_dir}" "${__jive_exe_dir}/jive" "$@"
40}
41
42__jive_open_pipe() {
43  local tmpfile
44  tmpfile="$(\mktemp -u)"
45
46  exec 42>"${tmpfile}" # Open the tempfile for writing on FD 42.
47  exec 8<"${tmpfile}" # Open the tempfile for reading on FD 8.
48  \rm -f "${tmpfile}" # Unlink the tempfile. (we've already opened it).
49}
50
51__jive_execute_task() {
52  local task=$1
53
54  case "${task}" in
55    cd:*)
56      # shellcheck disable=SC2164
57      cd "${task//cd:/}"
58      ;;
59    ctags:*)
60      # shellcheck disable=SC2164
61      ctags -R "${task//ctags:/}"
62      ;;
63    setenv:*)
64      export "${task//setenv:/}"
65      ;;
66    *)
67      echo "Woof! ${task}"
68      ;;
69  esac
70}
71
72__jive_flush_tasks() {
73  local task
74  while \read -r task; do
75    __jive_execute_task "${task}"
76  done <&8
77
78  __jive_close_pipe
79}
80
81__jive_close_pipe() {
82  exec 8<&- # close FD 8.
83  exec 42<&- # close FD 42.
84}
85
86jive() {
87  __jive_auto_reload
88  __jive_open_pipe
89  __jive_exec "$@"
90  __jive_flush_tasks
91}