Skip to content

Instantly share code, notes, and snippets.

@devinus
Created May 3, 2022 05:39
Show Gist options
  • Select an option

  • Save devinus/0da4f20d2e8fd80eae94b779ea30efe6 to your computer and use it in GitHub Desktop.

Select an option

Save devinus/0da4f20d2e8fd80eae94b779ea30efe6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# Caches the output of a binary initialization command, to avoid the time to
# execute it in the future.
#
# Usage: _evalcache <command> <generation args...>
# shellcheck disable=3006,3043
# default cache directory
export ZSH_EVALCACHE_DIR="${ZSH_EVALCACHE_DIR:-${HOME}/.zsh-evalcache}"
zmodload -F zsh/files b:zf_mkdir
_evalcache () {
local cache_file="${ZSH_EVALCACHE_DIR}/init-${1##*/}.sh"
# shellcheck disable=2154
if [ -n "${ZSH_EVALCACHE_DISABLE}" ]; then
local output
output="$("$@")"
eval "${output}"
elif [ -r "${cache_file}" ]; then
# shellcheck source=/dev/null
. "${cache_file}"
else
if (( $+commands[$1] )); then
echo "$1 initialization not cached, caching output of: $*" >&2
zf_mkdir -pm 0700 "${ZSH_EVALCACHE_DIR}"
"$@" > "${cache_file}"
# shellcheck source=/dev/null
. "${cache_file}"
else
echo "evalcache ERROR: $1 is not installed or in PATH"
fi
fi
}
evalcache_clear () {
rm "${ZSH_EVALCACHE_DIR}/init-*.sh"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment