Skip to content

Commit

Permalink
wip: cache environment
Browse files Browse the repository at this point in the history
  • Loading branch information
molleweide committed Feb 20, 2025
1 parent 90d8c44 commit 9d337d5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 23 deletions.
42 changes: 37 additions & 5 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,42 @@ if [ "${DOROTHY_LOAD-}" = 'yes' ]; then
fi
fi

# init dorothy's environment for the login shell
# set the active shell as the detected POSIX (.sh) shell
# do not export
if [ -n "${BASH_VERSION-}" ]; then
ACTIVE_POSIX_SHELL='bash'
elif [ -n "${ZSH_VERSION-}" ]; then
ACTIVE_POSIX_SHELL='zsh'
elif [ "$0" = '-dash' ] || [ "$0" = 'dash' ]; then
# dash does not define DASH_VERSION
ACTIVE_POSIX_SHELL='dash'
elif [ -n "${KSH_VERSION-}" ]; then
ACTIVE_POSIX_SHELL='ksh'
else
ACTIVE_POSIX_SHELL='sh'
fi

PATH_ENV_CACHE=~/.cache/dorothy/env_cache
DISABLE_CACHE="yes"
LOAD_EXISTING_CACHE_SUCCESS='no'

if [ "$DISABLE_CACHE" = 'no' ] && [ -f "$PATH_ENV_CACHE" ]; then
PREV_ENV_KEY_VALUES="$(env)"
if eval "$(cat "$PATH_ENV_CACHE")"; then
LOAD_EXISTING_CACHE_SUCCESS='yes'
echo "Env loaded successfully via cache."
case $- in *i*) . "$DOROTHY/sources/interactive.sh" ;; esac
fi
fi

# Init dorothy's environment for the login shell
# Needs to be loaded in order to compare loaded cache with prev env.
. "$DOROTHY/sources/login.sh"

# if the login shell is also interactive, then init dorothy for the interactive login shell
# [-t 0] and [-s] are true despite [env -i bash -lc ...]
case $- in *i*) . "$DOROTHY/sources/interactive.sh" ;; esac
fi
if [ "$DISABLE_CACHE" = 'yes' ] || [ "$LOAD_EXISTING_CACHE_SUCCESS" = 'no' ]; then
echo "Load interactive LAST"
# if the login shell is also interactive, then init dorothy for the interactive login shell
# [-t 0] and [-s] are true despite [env -i bash -lc ...]
case $- in *i*) . "$DOROTHY/sources/interactive.sh" ;; esac
fi
fi
78 changes: 60 additions & 18 deletions sources/environment.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
#!/usr/bin/env sh

# set the active shell as the detected POSIX (.sh) shell
# do not export
if [ -n "${BASH_VERSION-}" ]; then
ACTIVE_POSIX_SHELL='bash'
elif [ -n "${ZSH_VERSION-}" ]; then
ACTIVE_POSIX_SHELL='zsh'
elif [ "$0" = '-dash' ] || [ "$0" = 'dash' ]; then
# dash does not define DASH_VERSION
ACTIVE_POSIX_SHELL='dash'
elif [ -n "${KSH_VERSION-}" ]; then
ACTIVE_POSIX_SHELL='ksh'
else
ACTIVE_POSIX_SHELL='sh'
# # set the active shell as the detected POSIX (.sh) shell
# # do not export
# if [ -n "${BASH_VERSION-}" ]; then
# ACTIVE_POSIX_SHELL='bash'
# elif [ -n "${ZSH_VERSION-}" ]; then
# ACTIVE_POSIX_SHELL='zsh'
# elif [ "$0" = '-dash' ] || [ "$0" = 'dash' ]; then
# # dash does not define DASH_VERSION
# ACTIVE_POSIX_SHELL='dash'
# elif [ -n "${KSH_VERSION-}" ]; then
# ACTIVE_POSIX_SHELL='ksh'
# else
# ACTIVE_POSIX_SHELL='sh'
# fi

# setup new env
if [ "$LOAD_EXISTING_CACHE_SUCCESS" = 'no' ]; then
echo "load "
eval "$(
"$DOROTHY/commands/setup-environment-commands" "$ACTIVE_POSIX_SHELL" || {
echo "DOROTHY FAILED TO SETUP ENVIRONMENT, RUN THIS TO DEBUG: bash -x '$DOROTHY/commands/setup-environment-commands' '$ACTIVE_POSIX_SHELL'" >/dev/stderr
return 1
}
)"
fi

if [ "$LOAD_EXISTING_CACHE_SUCCESS" = 'yes' ]; then
# process previous env
pairs_formatted="$(echo "$PREV_ENV_KEY_VALUES" | tr '\n' ' ')"
# echo "$pairs_formatted"
eval "$(
env "$pairs_formatted" \
"$DOROTHY/commands/setup-environment-commands" "$ACTIVE_POSIX_SHELL" || {
echo "DOROTHY FAILED TO SETUP ENVIRONMENT, RUN THIS TO DEBUG: bash -x '$DOROTHY/commands/setup-environment-commands' '$ACTIVE_POSIX_SHELL'" >/dev/stderr
return 1
}
)"
fi

# set the environment variables
eval "$("$DOROTHY/commands/setup-environment-commands" "$ACTIVE_POSIX_SHELL" || {
echo "DOROTHY FAILED TO SETUP ENVIRONMENT, RUN THIS TO DEBUG: bash -x '$DOROTHY/commands/setup-environment-commands' '$ACTIVE_POSIX_SHELL'" >/dev/stderr
return 1
})"
# # env '' should be completely redundant with no unforseen consequences, so we
# # can achieve the conditional loading of setup-environment-commands based
# # on the cache with the following
# [ "$LOAD_EXISTING_CACHE_SUCCESS" = 'yes' ] &&
# pairs_formatted="$(echo "$PREV_ENV_KEY_VALUES" | tr '\n' ' ')" || pairs_formatted=''
# # if [ "$LOAD_EXISTING_CACHE_SUCCESS" = 'yes' ]; then
# # process previous env
# # pairs_formatted="$(echo "$PREV_ENV_KEY_VALUES" | tr '\n' ' ')"
# # echo "$pairs_formatted"
# eval "$(
# env "$pairs_formatted" \
# "$DOROTHY/commands/setup-environment-commands" "$ACTIVE_POSIX_SHELL" || {
# echo "DOROTHY FAILED TO SETUP ENVIRONMENT, RUN THIS TO DEBUG: bash -x '$DOROTHY/commands/setup-environment-commands' '$ACTIVE_POSIX_SHELL'" >/dev/stderr
# return 1
# }
# )"
# # fi

# save cache
if [ ! -f "$PATH_ENV_CACHE" ]; then
touch "$PATH_ENV_CACHE"
fi
export -p >"$PATH_ENV_CACHE"

0 comments on commit 9d337d5

Please sign in to comment.