pyenv/libexec/pyenv-version-file-write
native-api dc873cf568
Support missing versions being present and set in a local .python-version (#3134)
Requested in https://github.com/pyenv/pyenv/issues/2680
for deployments with a stock `.pyenv-version` that can use any
of a number of Python versions
and for compatibility with `uv`.

* Support `pyenv local --force`
* Support `pyenv-version-file-write --force`
* Support `pyenv version-name --force`
* Ignore missing versions when searching for executables
* Display "commmand not found" even when there are nonexistent versions

* exec.bats: replace `python` and `rspec` with something that doesn't exist globally, either
in Ubuntu Github CI, `python` exists globally
2024-12-16 02:32:45 +03:00

41 lines
851 B
Bash
Executable File

#!/usr/bin/env bash
# Usage: pyenv version-file-write [-f|--force] <file> <version> [...]
#
# -f/--force Don't verify that the versions exist
set -e
[ -n "$PYENV_DEBUG" ] && set -x
while [[ $# -gt 0 ]]
do
case "$1" in
-f|--force)
FORCE=1
shift
;;
*)
break
;;
esac
done
PYENV_VERSION_FILE="$1"
shift || true
versions=("$@")
if [ -z "$versions" ] || [ -z "$PYENV_VERSION_FILE" ]; then
pyenv-help --usage version-file-write >&2
exit 1
fi
# Make sure the specified version is installed.
[[ -z $FORCE ]] && pyenv-prefix "${versions[@]}" >/dev/null
# Write the version out to disk.
# Create an empty file. Using "rm" might cause a permission error.
> "$PYENV_VERSION_FILE"
for version in "${versions[@]}"; do
echo "$version" >> "$PYENV_VERSION_FILE"
done