| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Manage the bin/ directory.
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   devtools/bin.sh <function name>
 | 
| 7 | 
 | 
| 8 | set -o nounset
 | 
| 9 | set -o pipefail
 | 
| 10 | set -o errexit
 | 
| 11 | 
 | 
| 12 | # New name is ysh!
 | 
| 13 | # TODO:
 | 
| 14 | # - remove the 'oil' everywhere
 | 
| 15 | # - translation should be 'ysh-translate'.  Later 'ysh-format'
 | 
| 16 | readonly OIL_OVM_NAMES=(oil ysh osh sh true false readlink)
 | 
| 17 | 
 | 
| 18 | # TODO: probably delete this
 | 
| 19 | # For osh-dbg.
 | 
| 20 | ovm-snippet() {
 | 
| 21 |   local name=$1
 | 
| 22 |   echo '#!/bin/sh'
 | 
| 23 |   echo 'exec _bin/oil.ovm-dbg '$name' "$@"'
 | 
| 24 | }
 | 
| 25 | 
 | 
| 26 | # For running spec tests quickly.
 | 
| 27 | make-osh-dbg() {
 | 
| 28 |   local out=_bin/osh-dbg
 | 
| 29 |   ovm-snippet osh > $out
 | 
| 30 |   chmod +x $out
 | 
| 31 | }
 | 
| 32 | 
 | 
| 33 | sh-prefix() {
 | 
| 34 |   cat << 'EOF'
 | 
| 35 | #!/bin/sh
 | 
| 36 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
 | 
| 37 | EOF
 | 
| 38 | }
 | 
| 39 | 
 | 
| 40 | make-oils-for-unix() {
 | 
| 41 |   local out=bin/oils-for-unix
 | 
| 42 |   { sh-prefix
 | 
| 43 |     echo 'PYTHONPATH=$REPO_ROOT:$REPO_ROOT/vendor exec $REPO_ROOT/bin/oils_for_unix.py "$@"'
 | 
| 44 |   } > $out
 | 
| 45 |   chmod +x $out
 | 
| 46 |   echo "Wrote $out"
 | 
| 47 | }
 | 
| 48 | 
 | 
| 49 | #
 | 
| 50 | # Shell Stubs
 | 
| 51 | #
 | 
| 52 | 
 | 
| 53 | sh-snippet() {
 | 
| 54 |   local wrapped=$1  # e.g. oil.py
 | 
| 55 |   local action=$2  # e.g. osh
 | 
| 56 | 
 | 
| 57 |   sh-prefix
 | 
| 58 |   echo 'PYTHONPATH=$REPO_ROOT:$REPO_ROOT/vendor exec $REPO_ROOT/bin/'$wrapped' '$action' "$@"'
 | 
| 59 | }
 | 
| 60 | 
 | 
| 61 | # A snippet that sets PYTHONPATH for bin/oil.py and runs it with the right
 | 
| 62 | # action.
 | 
| 63 | oil-dev-snippet() {
 | 
| 64 |   local action=$1
 | 
| 65 |   sh-snippet oils_for_unix.py $action
 | 
| 66 | }
 | 
| 67 | 
 | 
| 68 | opy-dev-snippet() {
 | 
| 69 |   local action=$1
 | 
| 70 |   sh-snippet opy_.py $action
 | 
| 71 | }
 | 
| 72 | 
 | 
| 73 | make-src-stubs() {
 | 
| 74 |   ### bin/ is for running with the Python interpreter.
 | 
| 75 |   mkdir -p bin
 | 
| 76 | 
 | 
| 77 |   for link in "${OIL_OVM_NAMES[@]}"; do
 | 
| 78 |     # bin/ shell wrapper
 | 
| 79 |     oil-dev-snippet $link > bin/$link
 | 
| 80 |     chmod +x bin/$link
 | 
| 81 |     echo "Wrote bin/$link"
 | 
| 82 |   done
 | 
| 83 | 
 | 
| 84 |   make-osh-dbg
 | 
| 85 | 
 | 
| 86 |   make-oils-for-unix
 | 
| 87 | }
 | 
| 88 | 
 | 
| 89 | make-ovm-links() {
 | 
| 90 |   ### _bin/ is for running with OVM app bundles.
 | 
| 91 | 
 | 
| 92 |   mkdir -p _bin
 | 
| 93 | 
 | 
| 94 |   for link in "${OIL_OVM_NAMES[@]}"; do
 | 
| 95 |     # _bin/ symlink
 | 
| 96 |     ln -s -f --verbose oil.ovm _bin/$link
 | 
| 97 |   done
 | 
| 98 | }
 | 
| 99 | 
 | 
| 100 | "$@"
 |