/ bin / doc
doc
  1  #!/usr/bin/env bash
  2  
  3  ## \brief Documentation for your shell scripts!
  4  
  5  if [ $# -eq 0 ]; then
  6    doc usage "$0"
  7    exit 1
  8  fi
  9  
 10  data_dir="/tmp/shellm_doc"
 11  mkdir -p "${data_dir}" &>/dev/null
 12  
 13  resolve_0() {
 14    if [ ! -f "$1" ]; then
 15      command -v "$1" 2>/dev/null
 16    else
 17      echo "$1"
 18    fi
 19  }
 20  
 21  cache_get_key() {
 22    shasum "$1" | cut -d' ' -f1
 23  }
 24  
 25  cache_has() {
 26    [ -f "${data_dir}/$1" ]
 27  }
 28  
 29  cache_set_help() {
 30    shellman -thelptext "$1" -o "${data_dir}/$2"
 31  }
 32  
 33  cache_set_usage() {
 34    shellman -tusagetext "$1" -o "${data_dir}/$2"
 35  }
 36  
 37  cache_set() {
 38    cache_set_"${type}" "$1" "$2"
 39  }
 40  
 41  cache_get() {
 42    cat "${data_dir}/$1"
 43  }
 44  
 45  cache_clear() {
 46    rm "${data_dir}/$1"
 47  }
 48  
 49  caches_clear() {
 50    local arg
 51    local cache_key
 52    if [ $# -eq 0 ]; then
 53      rm "${data_dir}"/* &>/dev/null
 54    else
 55      for arg; do
 56        arg="$(resolve_0 "${arg}")" || continue
 57        cache_key="$(cache_get_key "${arg}")"
 58        cache_clear "usage-${cache_key}"
 59        cache_clear "help-${cache_key}"
 60      done
 61    fi
 62  }
 63  
 64  get() {
 65    local type="$1"
 66    local script
 67    local cache_key
 68  
 69    if ! script="$(resolve_0 "$2")"; then
 70      echo "doc: no such file: $2" >&2
 71      return 1
 72    fi
 73  
 74    cache_key="${type}-$(cache_get_key "${script}")"
 75    if ! cache_has "${cache_key}"; then
 76      cache_set "${script}" "${cache_key}" || return 1
 77    fi
 78    cache_get "${cache_key}"
 79  }
 80  
 81  caches_set() {
 82    local arg
 83    local cache_key
 84    local scripts
 85    local basenames
 86    local shasums
 87  
 88    for arg; do
 89      arg="$(resolve_0 "${arg}")" || continue
 90      scripts+=(${arg})
 91      basenames+=($(basename "${arg}"))
 92      shasums+=($(cache_get_key "${arg}"))
 93    done
 94  
 95    shellman "${scripts[@]}" -t helptext -o "${data_dir}/_help-{filename}"
 96    shellman "${scripts[@]}" -t usagetext -o "${data_dir}/_usage-{filename}"
 97  
 98    i=0
 99    for arg in "${basenames[@]}"; do
100      ln -s "${data_dir}/_help-${arg}" "${data_dir}/help-${shasums[$i]}"
101      ln -s "${data_dir}/_usage-${arg}" "${data_dir}/usage-${shasums[$i]}"
102      (( i++ ))
103    done
104  }
105  
106  main() {
107    case "$1" in
108      ## \option -h, --help
109      ## Print this help and exit.
110      -h|--help) doc "$0" ;;
111  
112      ## \option -c, --cache SCRIPT...
113      ## Cache the help and usage text for the given scripts.
114      -c|--cache) shift; caches_set "$@" ;;
115  
116      ## \option -d, --delete-cache [SCRIPT...]
117      ## Clear the cache for the given scripts, or clear all the cache.
118      -d|--delete-cache) shift; caches_clear "$@" ;;
119  
120      usage) get usage "$2" ;;
121      help) get help "$2" ;;
122      *) get help "$1" ;;
123    esac
124  }
125  
126  ## \usage doc [help] SCRIPT
127  ## \usage doc usage SCRIPT
128  ## \usage doc -c SCRIPT...
129  ## \usage doc -d [SCRIPT...]
130  main "$@"