Sha256: a819b2fcf7cd33629b7bec1ed4b3aeb7b63f0997af4c058bb995ff10a8ad9012

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

#!/bin/sh
# shellcheck disable=1001,1012,2039,1090
# 1001,1012 stop complaints about '\awk' syntax to bypass aliases.
# 2039 stops complaints about array references not being POSIX.
# 1090 stops complaints about sourcing non-constant paths.

__shell="$(\ps -p $$ | \awk 'NR > 1 { sub(/^-/, "", $4); print $4 }')"
__shellname="$(basename "${__shell}")"

case "${__shellname}" in
  bash)
    __jive_root_dir="$(builtin cd "$(\dirname "${BASH_SOURCE[0]}")" && \pwd)"
    ;;
  zsh)
    __jive_root_dir="$(\dirname "$0:A")"
    ;;
  *)
    >&2 \echo "jive is not compatible with your shell (${__shell})"
    \return 1
    ;;
esac

__jive_exe_dir="${__jive_root_dir}/exe"
__jive_lib_dir="${__jive_root_dir}/lib"
__jive_script="${__jive_root_dir}/jive.sh"

__mtime_of_jive_script="$(\date -r "${__jive_script}" +%s)"
__jive_auto_reload() {
  local current_mtime
  current_mtime="$(\date -r "${__jive_script}" +%s)"

  if [[ "${current_mtime}" != "${__mtime_of_jive_script}" ]]; then
    echo "Reloading... ${__jive_script}"
    . "${__jive_script}"
  fi
}

__jive_exec() {
  /usr/bin/env -S ruby -I "${__jive_lib_dir}" "${__jive_exe_dir}/jive" "$@"
}

__jive_open_pipe() {
  local tmpfile
  tmpfile="$(\mktemp -u)"

  exec 9>"${tmpfile}" # Open the tempfile for writing on FD 9.
  exec 8<"${tmpfile}" # Open the tempfile for reading on FD 8.
  \rm -f "${tmpfile}" # Unlink the tempfile. (we've already opened it).
}

__jive_execute_task() {
  local task=$1

  case "${task}" in
    cd:*)
      # shellcheck disable=SC2164
      cd "${task//cd:/}"
      ;;
    setenv:*)
      export "${task//setenv:/}"
      ;;
    *)
      echo "Woof! ${task}"
      ;;
  esac
}

__jive_flush_tasks() {
  local task
  while \read -r task; do
    __jive_execute_task "${task}"
  done <&8

  __jive_close_pipe
}

__jive_close_pipe() {
  exec 8<&- # close FD 8.
  exec 9<&- # close FD 9.
}

jive() {
  __jive_auto_reload
  __jive_open_pipe
  __jive_exec "$@"
  __jive_flush_tasks
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jive-0.2.1 jive.sh
jive-0.2.0 jive.sh