Files
computer-playbook/scripts/meta/env/load.sh
T
kevinveenbirkenbachandClaude Opus 4.8 4181d2194d fix(env): reload .env when it changed since the last source
load.sh short-circuits on INFINITO_ENV_LOADED, so a shell that already
sourced the env kept a stale copy after 'make dotenv' regenerated .env
(make runs in a subshell and cannot reset the parent's guard), and a
freshly added key was missing until a new shell. Gate the short-circuit
on the .env mtime: re-source when it changed, still a fast no-op when it
did not. A stat failure falls back to the previous idempotent behaviour.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 20:01:32 +02:00

93 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Sources `.env` (auto-generates via `python -m cli.meta.env` if missing)
# with setdefault semantics: caller-set values win over `.env` defaults.
# Idempotent (INFINITO_ENV_LOADED). INFINITO_ENV_GENERATING short-circuits
# re-entry from generator subshells (BASH_ENV would otherwise recurse).
# PYTHON/PATH are re-resolved on every source so a venv created mid-chain wins;
# caller-set values are snapshotted and restored after the bulk .env source.
# zsh cannot run this loader's bash-isms (BASH_SOURCE, ${!indirect}, assoc-array
# key expansion). When sourced from zsh, delegate to bash and import the
# resulting INFINITO_*/PYTHON/VENV/PATH back into the current shell. The zsh-only
# ${(%):-%x} (this file's path) is hidden behind eval so bash still parses fine.
if [ -n "${ZSH_VERSION:-}" ]; then
_infinito_zsh_self=""
eval '_infinito_zsh_self=${(%):-%x}'
_infinito_zsh_root="$(cd "$(dirname "${_infinito_zsh_self}")/../../.." && pwd)"
while IFS= read -r _infinito_zsh_line; do
case "${_infinito_zsh_line}" in
INFINITO_*=* | PYTHON=* | VENV=* | PATH=*)
_infinito_zsh_key="${_infinito_zsh_line%%=*}"
_infinito_zsh_val="${_infinito_zsh_line#*=}"
export "${_infinito_zsh_key}=${_infinito_zsh_val}"
;;
esac
done < <(bash -c "source '${_infinito_zsh_root}/scripts/meta/env/load.sh' >/dev/null 2>&1; env") # nocheck: self-path-reference
unset _infinito_zsh_self _infinito_zsh_root _infinito_zsh_line \
_infinito_zsh_key _infinito_zsh_val
return 0
fi
_infinito_env_repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
_infinito_env_dotenv="${_infinito_env_repo_root}/.env"
# shellcheck source=scripts/meta/env/python.sh
source "${_infinito_env_repo_root}/scripts/meta/env/python.sh"
if [[ -n "${VENV:-}" && -d "${VENV%/}/bin" ]]; then
case ":${PATH:-}:" in
*:"${VENV%/}/bin":*) ;;
*) export PATH="${VENV%/}/bin${PATH:+:${PATH}}" ;;
esac
fi
_infinito_env_mtime="$(stat -c %Y "${_infinito_env_dotenv}" 2>/dev/null || echo 0)"
if [[ "${INFINITO_ENV_LOADED:-}" == "1" && "${INFINITO_ENV_LOADED_MTIME:-}" == "${_infinito_env_mtime}" ]]; then # nocheck: env-loader-internal-guard
unset _infinito_env_repo_root _infinito_env_dotenv _infinito_env_mtime
return 0
fi
if [[ "${INFINITO_ENV_GENERATING:-}" == "1" ]]; then # nocheck: env-loader-internal-guard
unset _infinito_env_repo_root _infinito_env_dotenv _infinito_env_mtime
return 0
fi
if [[ ! -f "${_infinito_env_dotenv}" ]]; then
(
cd "${_infinito_env_repo_root}" || exit 1
export INFINITO_ENV_GENERATING=1
"${PYTHON}" -m cli.meta.env
) >&2
fi
declare -A _infinito_env_preserved=()
while IFS= read -r _infinito_env_line; do
case "${_infinito_env_line}" in
"" | "#"*) continue ;;
esac
_infinito_env_key="${_infinito_env_line%%=*}"
if [[ "${_infinito_env_key}" == "${_infinito_env_line}" ]]; then
continue
fi
if [[ -n "${!_infinito_env_key:-}" ]]; then
_infinito_env_preserved["${_infinito_env_key}"]="${!_infinito_env_key}"
fi
done <"${_infinito_env_dotenv}"
unset _infinito_env_line _infinito_env_key
set -a
# shellcheck disable=SC1090
source "${_infinito_env_dotenv}"
set +a
for _infinito_env_key in "${!_infinito_env_preserved[@]}"; do
declare -gx "${_infinito_env_key}=${_infinito_env_preserved[${_infinito_env_key}]}"
done
unset _infinito_env_key _infinito_env_preserved
export INFINITO_ENV_LOADED="1"
_infinito_env_mtime="$(stat -c %Y "${_infinito_env_dotenv}" 2>/dev/null || echo 0)"
export INFINITO_ENV_LOADED_MTIME="${_infinito_env_mtime}"
unset _infinito_env_repo_root _infinito_env_dotenv _infinito_env_mtime