venv.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # This script is used to setup a virtual environment for QMK use, and subsequently allows running `qmk` commands from the virtual environment.
  3. # Exit on error
  4. set -eEuo pipefail
  5. umask 022
  6. # Find the QMK firmware directory
  7. this_script=$(realpath "${BASH_SOURCE[0]}")
  8. this_dir=$(dirname "$this_script")
  9. qmk_firmware_dir=$(realpath "$this_dir/..")
  10. # Allow overriding the virtual environment directory from the environment, if necessary
  11. if [ -z "${QMK_VENV_DIR:-}" ]; then
  12. QMK_VENV_DIR="$qmk_firmware_dir/.venv"
  13. fi
  14. # Switch to the QMK firmware directory
  15. cd "$qmk_firmware_dir"
  16. # Check if the virtual environment exists
  17. if [ ! -d "$QMK_VENV_DIR" ]; then
  18. echo "Creating virtual environment..."
  19. python3 -m venv "$QMK_VENV_DIR"
  20. fi
  21. # Activate the virtual environment
  22. source "$QMK_VENV_DIR/bin/activate"
  23. # Install qmk into the venv if it's not already installed
  24. if [[ ! -e "$QMK_VENV_DIR/bin/qmk" ]]; then
  25. echo "Installing QMK CLI..."
  26. pip install --require-virtualenv --quiet --no-input --upgrade pip qmk
  27. pip install --require-virtualenv --quiet --no-input --upgrade -r requirements-dev.txt
  28. fi
  29. # Run the arguments as if it were a `qmk` command
  30. exec "$@"