|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# |
| 4 | +# Bootstraps a development environment. |
| 5 | +# |
| 6 | +# This includes: |
| 7 | +# * install pre-commit hooks |
| 8 | +# * setup virtualenv |
| 9 | + |
| 10 | +function exit_on_error { |
| 11 | + if [ "${1}" -ne 0 ]; then |
| 12 | + echo "ERROR: ${2}, exiting with code ${1}" 1>&2 |
| 13 | + exit "${1}" |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +# poetry is installed in ~/.local/bin |
| 18 | +PATH=~/.local/bin:$PATH |
| 19 | + |
| 20 | +if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then |
| 21 | + cat << __EOF__ |
| 22 | +Setup development environment (run with no arguments): |
| 23 | +* install pre-commit hooks (set NO_HOOKS=1 to skip) |
| 24 | +__EOF__ |
| 25 | + exit 0 |
| 26 | +fi |
| 27 | + |
| 28 | +if [ ! "${PWD}" == "$(git rev-parse --show-toplevel)" ]; then |
| 29 | + cat >&2 <<__EOF__ |
| 30 | +ERROR: this script must be run at the root of the source tree |
| 31 | +__EOF__ |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +echo "=======================" |
| 36 | +if [ -z "${INSTALL_POETRY}" ]; then |
| 37 | + if ! command -v poetry >/dev/null 2>&1; then |
| 38 | + echo "ERROR: poetry is missing. Please run the following command to install it: |
| 39 | + curl -sSL https://install.python-poetry.org | python3 -" 1>&2 |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +else |
| 43 | + should_install_poetry="yes" |
| 44 | + if command -v poetry >/dev/null 2>&1; then |
| 45 | + if [[ "$(poetry --version)" == "Poetry (version ${POETRY_VERSION})" ]]; then |
| 46 | + echo "Poetry is already installed with the correct version: $(poetry --version)" |
| 47 | + should_install_poetry="no" |
| 48 | + else |
| 49 | + echo "Poetry is already installed with a different version: $(poetry --version), required version: ${POETRY_VERSION}" |
| 50 | + fi |
| 51 | + else |
| 52 | + echo "Poetry isn't installed" |
| 53 | + fi |
| 54 | + if [[ "${should_install_poetry}" == "yes" ]]; then |
| 55 | + echo "Installing Poetry version:${POETRY_VERSION}" |
| 56 | + curl -sSL https://install.python-poetry.org | python3 - --version "${POETRY_VERSION}" |
| 57 | + error_code=$? |
| 58 | + if ! command -v poetry >/dev/null 2>&1; then |
| 59 | + exit_on_error $? "Poetry isn't installed" |
| 60 | + fi |
| 61 | + if [[ "$(poetry --version)" == "Poetry (version ${POETRY_VERSION})" ]]; then |
| 62 | + echo "Poetry version ${POETRY_VERSION} installed successfully" |
| 63 | + else |
| 64 | + exit_on_error 1 "Poetry version $(poetry --version) doesn't match the required version: ${POETRY_VERSION}" |
| 65 | + fi |
| 66 | + if [ -n "${ARTIFACTS_FOLDER}" ] && [ "${error_code}" -ne 0 ]; then |
| 67 | + cp "${PWD}"/poetry-installer-error-*.log "${ARTIFACTS_FOLDER}" |
| 68 | + fi |
| 69 | + exit_on_error $error_code "Failed to install Poetry version:${POETRY_VERSION}" |
| 70 | + fi |
| 71 | +fi |
| 72 | + |
| 73 | +if [ -n "${NO_HOOKS}" ]; then |
| 74 | + echo "Skipping hooks setup as environment variable NO_HOOKS is set" |
| 75 | +else |
| 76 | + GIT_HOOKS_DIR="${PWD}/.git/hooks" |
| 77 | + if [ ! -e "${GIT_HOOKS_DIR}/pre-commit" ]; then |
| 78 | + echo "Installing 'pre-commit' hooks" |
| 79 | + poetry run pre-commit install |
| 80 | + exit_on_error $? "Failed to install pre-commit hook" |
| 81 | + else |
| 82 | + echo "Skipping install of pre-commit hook as it already exists." |
| 83 | + echo "If you want to re-install: 'rm ${GIT_HOOKS_DIR}/pre-commit' and then run this script again." |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | +fi |
| 87 | + |
| 88 | +if [ -n "${CI}" ]; then |
| 89 | + echo "Detected CI environment" |
| 90 | + echo "Checking whether poetry files are valid" |
| 91 | + poetry check --no-interaction |
| 92 | + exit_on_error $? "Failed to check poetry files" |
| 93 | + echo "Installing dependencies..." |
| 94 | + poetry install --no-interaction |
| 95 | + exit_on_error $? "Failed to install dependencies" |
| 96 | +else |
| 97 | + echo "Detected local environment" |
| 98 | + echo "Check if poetry files are valid" |
| 99 | + poetry check |
| 100 | + exit_on_error $? "Failed to check poetry files" |
| 101 | + echo "Installing dependencies..." |
| 102 | + poetry install |
| 103 | + exit_on_error $? "Failed to install dependencies" |
| 104 | +fi |
| 105 | + |
| 106 | +echo "==========================" |
| 107 | +echo "Done setting up virtualenv with poetry" |
| 108 | +echo "Activate the virtualenv by running: poetry shell" |
| 109 | +echo "Deactivate by running: deactivate" |
| 110 | +echo "=======================" |
| 111 | + |
| 112 | +echo "Finished setting up the environment." |
| 113 | +exit 0 |
0 commit comments