mirror of
https://github.com/jimeh/dotfiles.git
synced 2026-02-19 03:06:40 +00:00
feat(shell): create cached-eval helper to improve shell startup speed.
This commit is contained in:
51
zshenv
51
zshenv
@@ -74,6 +74,54 @@ source-if-exists() {
|
||||
fi
|
||||
}
|
||||
|
||||
# cached-eval executes a command with arguments and caches the output. On
|
||||
# subsequent calls, if the source file has not changed, the output is sourced
|
||||
# from the cache instead of re-executing the command. This optimizes performance
|
||||
# for commands that are costly to execute but result in the same output unless
|
||||
# their source files change.
|
||||
#
|
||||
# Arguments:
|
||||
#
|
||||
# $1 - source_file: The path to the source file that the command depends on.
|
||||
# If this file is newer than the cache, the command is
|
||||
# re-executed and the cache is updated.
|
||||
# $2 - cmd: The command to execute.
|
||||
# $@ - args: Additional arguments to pass to the command.
|
||||
#
|
||||
# Example usage:
|
||||
#
|
||||
# cached-eval "$(command -v direnv)" direnv hook zsh
|
||||
# cached-eval "$(command -v mise)" mise activate zsh
|
||||
#
|
||||
# The above commands will cache the output of `direnv hook zsh` and `mise
|
||||
# activate zsh` respectively. If the source file is newer than the cache, the
|
||||
# command is re-executed and cache is updated.
|
||||
cached-eval() {
|
||||
local source_file="$1"
|
||||
local cmd="$2"
|
||||
shift 2
|
||||
local args="$@"
|
||||
local full_cmd="$cmd $args"
|
||||
local cache_dir="${ZSH_CACHED_EVAL_DIR:-$HOME/.local/share/zsh/cached-eval}"
|
||||
|
||||
if [[ -z "$(command -v "$cmd")" ]]; then
|
||||
echo "cached-eval: Command not found: $cmd" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local cache_hash="$(echo -n "$full_cmd" | md5sum | awk '{print $1}')"
|
||||
local cache_file="${cache_dir}/$(basename "$cmd")_${cache_hash}.zsh"
|
||||
|
||||
if [[ ! -f "$cache_file" || "$source_file" -nt "$cache_file" ]]; then
|
||||
mkdir -p "$cache_dir"
|
||||
echo "cached-eval: Updating cache for: $full_cmd --> $cache_file" >&2
|
||||
echo -e "#\n# Generated by cached-eval: $full_cmd\n#\n" >| "$cache_file"
|
||||
eval "$full_cmd" >>| "$cache_file"
|
||||
fi
|
||||
|
||||
source "$cache_file"
|
||||
}
|
||||
|
||||
# ==============================================================================
|
||||
# System Environment Setup
|
||||
# ==============================================================================
|
||||
@@ -98,6 +146,7 @@ fi
|
||||
|
||||
export DOTZSH_SITEFUNS="$DOTZSH/site-functions"
|
||||
export ZSH_COMPLETIONS="$HOME/.local/share/zsh/completions"
|
||||
export ZSH_CACHED_EVAL_DIR="$HOME/.local/share/zsh/cached-eval"
|
||||
|
||||
# Ensure basic systems paths are in desired order
|
||||
path_prepend "/bin"
|
||||
@@ -129,7 +178,7 @@ fi
|
||||
|
||||
# Homebrew on Apple Silicon
|
||||
if [ -f "/opt/homebrew/bin/brew" ]; then
|
||||
eval "$(/opt/homebrew/bin/brew shellenv)"
|
||||
cached-eval /opt/homebrew/bin/brew /opt/homebrew/bin/brew shellenv
|
||||
fi
|
||||
|
||||
if command-exists brew; then
|
||||
|
||||
Reference in New Issue
Block a user