|
1 | 1 | #!/bin/bash |
| 2 | +# Check the current shell |
| 3 | +export SHX_VERSION=0.2.1 |
| 4 | +export INVOKE_DIR=$PWD |
| 5 | +export CURRENT_SHELL=$(ps h -p $$ -o args='' | cut -f1 -d' ') |
| 6 | +if [[ "${NO_SHELL_CHECK}" == "" ]] && |
| 7 | + [[ "${CURRENT_SHELL}" != *"bash" ]] && |
| 8 | + [[ "${CURRENT_SHELL}" != *"ksh" ]] && |
| 9 | + [[ "${CURRENT_SHELL}" != *"zsh" ]]; then |
| 10 | + echo "Please run with bash instead of ${CURRENT_SHELL}." |
| 11 | + exit |
| 12 | +fi |
| 13 | +# Define runner |
| 14 | +runner="bash" |
| 15 | +if [[ "${CURRENT_SHELL}" == *"bash" ]]; then |
| 16 | + runner=$CURRENT_SHELL |
| 17 | +elif [[ "$(bash --version >/dev/null 2>/dev/null;echo $?)" == "0" ]]; then |
| 18 | + printf "" |
| 19 | +else |
| 20 | + echo -e "\033[1;33mWarning\033[0m: Not using Bash. Scripts may not work." |
| 21 | +fi |
| 22 | +# Locate the project folder |
| 23 | +OIFS="$IFS" |
| 24 | +IFS='/' read -r -a paths <<< "$PWD" 2>/dev/null || paths=('' ${(@s:/:)PWD}) |
| 25 | +let fsPointer=$((${#paths[@]} - 1)) |
| 26 | +IFS='/' |
| 27 | +while [ "$fsPointer" -ge 0 ]; do |
| 28 | + fsPath="${paths[@]:0:$((fsPointer + 1))}" |
| 29 | + fsPath=${fsPath:-/} |
| 30 | + if [ -f "$fsPath/shx" ] && [ -d "$fsPath/sh" ] ; then |
| 31 | + #echo "Switched to \"$fsPath\"." |
| 32 | + cd "$fsPath" |
| 33 | + break |
| 34 | + #else |
| 35 | + #echo "No match for \"$fsPath\"." |
| 36 | + fi |
| 37 | + fsPointer=$(($fsPointer - 1)) |
| 38 | +done |
| 39 | +if [[ "$fsPath" == "/" ]] ; then |
| 40 | + echo "No project directory is found. Quitting." |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | +IFS="$OIFS" |
| 44 | +export SOURCE_DIR=$PWD |
| 45 | +export PATH=$PATH:./:./sh |
| 46 | +# Command parsing |
2 | 47 | arg="$@" |
3 | 48 | args=( "$@" ) |
4 | | -if [ "$arg" == "" ] ; then |
| 49 | +if [[ "${arg}" == "" ]] ; then |
| 50 | + echo -e "\033[1;37mshx ${SHX_VERSION}\033[0m" |
5 | 51 | echo "All available actions:" |
6 | 52 | ls -1 sh | while IFS= read -r file; do |
7 | | - echo "${file/.sh/}" |
| 53 | + echo "· ${file/.sh/}" |
8 | 54 | done |
9 | | - exit |
10 | | -fi |
11 | | -if [ -e "sh/$1.sh" ] ; then |
12 | | - export PATH=./:./sh/:$PATH |
| 55 | +elif [ -e "sh/$1.sh" ] ; then |
13 | 56 | bash "sh/$1.sh" "${args[@]:1}" |
14 | 57 | elif [ -e "sh/$1" ] ; then |
15 | | - export PATH=./:./sh/:$PATH |
16 | 58 | bash "sh/$1" "${args[@]:1}" |
17 | 59 | else |
18 | | - echo "No action found as \"$1\". Command: \"${args[@]:1}\"". |
| 60 | + echo -e "\033[1;31mError\033[0m: No action found as \"$1\". Command: shx ${args[@]:1}". |
19 | 61 | fi |
20 | 62 | exit |
0 commit comments