|
| 1 | +#!/bin/bash -i |
| 2 | + |
| 3 | +### |
| 4 | +# This is called from the extension SwanTerminal and it requires the project path and stacks path as arguments. |
| 5 | +# The project path is used to read the .swanproject file and the stacks path is required to load the proper enviroment. |
| 6 | +# When the bash session is started we modify the prompt indicating in which project the terminal is located. |
| 7 | +# Additionally, the bash session runs completely isolated from other possible environments, |
| 8 | +# so we need to define some basic environment variables such as: |
| 9 | +# |
| 10 | +# * HOME with path to the user home |
| 11 | +# * PATH with default paths for the system |
| 12 | +# * OAUTH2_TOKEN required by EOS storage |
| 13 | +# |
| 14 | +# swan_env will load the other variables from the stack, inside the isolated enviroment. |
| 15 | +### |
| 16 | + |
| 17 | +clear |
| 18 | +if ! [ -x "$(command -v jq)" ]; then |
| 19 | + echo 'Error: jq is not installed.' >&2 |
| 20 | + sleep 60 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | +if [[ $# -gt 1 ]] ; then |
| 24 | + PROJECT_PATH=$1 |
| 25 | + PROJECT_NAME=`IFS='/'; ARR=($PROJECT_PATH);echo "${ARR[-1]}"` |
| 26 | + PROJECT_FILE="$PROJECT_PATH/.swanproject" |
| 27 | + |
| 28 | + STACKS_PATH="$2" |
| 29 | + |
| 30 | + if [ -d "$PROJECT_PATH" ] |
| 31 | + then |
| 32 | + STACK=`jq '.stack' "$PROJECT_FILE"` |
| 33 | + RELEASE=`jq '.release' "$PROJECT_FILE"` |
| 34 | + PLATFORM=`jq '.platform' "$PROJECT_FILE"` |
| 35 | + USER_SCRIPT="$PROJECT_PATH/.userscript" |
| 36 | + echo "Loading $RELEASE with plafortm $PLATFORM " |
| 37 | + # FIXME: this have to be removed when environment isolation is not needed anymore, it's only temporary |
| 38 | + env -i HOME=$HOME \ |
| 39 | + OAUTH2_TOKEN=$OAUTH2_TOKEN \ |
| 40 | + PROJECT="$PROJECT_NAME" \ |
| 41 | + PROJECT_PATH="$PROJECT_PATH" PS1="$PS1" \ |
| 42 | + bash -c "swan_env \"$PROJECT_PATH\" \"$STACKS_PATH\" \"$PROJECT_PATH\" bash --rcfile <(echo 'PS1=\"($PROJECT_NAME) $PS1 \"') " |
| 43 | + else |
| 44 | + echo "Error: project $PROJECT_PATH doesn't exist" >&2 |
| 45 | + # JupyterLab closes the terminal window immediately after the process ends |
| 46 | + # this sleep is to allow the user to see the message |
| 47 | + sleep 60 |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +fi |
0 commit comments