mirror of
https://github.com/jimeh/dotfiles.git
synced 2026-02-19 13:46:41 +00:00
fix(shell/cached-eval): find usable md5 command and improve reliability
This commit is contained in:
32
zshenv
32
zshenv
@@ -85,8 +85,7 @@ source-if-exists() {
|
|||||||
# $1 - source_file: The path to the source file that the command depends on.
|
# $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
|
# If this file is newer than the cache, the command is
|
||||||
# re-executed and the cache is updated.
|
# re-executed and the cache is updated.
|
||||||
# $2 - cmd: The command to execute.
|
# $@ - script: The command to execute and cache the output of.
|
||||||
# $@ - args: Additional arguments to pass to the command.
|
|
||||||
#
|
#
|
||||||
# Example usage:
|
# Example usage:
|
||||||
#
|
#
|
||||||
@@ -98,25 +97,30 @@ source-if-exists() {
|
|||||||
# command is re-executed and cache is updated.
|
# command is re-executed and cache is updated.
|
||||||
cached-eval() {
|
cached-eval() {
|
||||||
local source_file="$1"
|
local source_file="$1"
|
||||||
local cmd="$2"
|
shift 1
|
||||||
shift 2
|
local script="$@"
|
||||||
local args="$@"
|
|
||||||
local full_cmd="$cmd $args"
|
|
||||||
local cache_dir="${ZSH_CACHED_EVAL_DIR:-$HOME/.local/share/zsh/cached-eval}"
|
local cache_dir="${ZSH_CACHED_EVAL_DIR:-$HOME/.local/share/zsh/cached-eval}"
|
||||||
|
|
||||||
if [[ -z "$(command -v "$cmd")" ]]; then
|
if [[ ! -f "$source_file" ]]; then
|
||||||
echo "cached-eval: Command not found: $cmd" >&2
|
echo "cached-eval: Source file not found: $source_file" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local cache_hash="$(echo -n "$full_cmd" | md5sum | awk '{print $1}')"
|
local md5_cmd="$(command -v md5 || command -v md5sum)"
|
||||||
local cache_file="${cache_dir}/$(basename "$cmd")_${cache_hash}.zsh"
|
local cache_hash="$(echo -n "$script" | "$md5_cmd" | awk '{print $1}')"
|
||||||
|
local cache_file="${cache_dir}/${cache_hash}.cache.zsh"
|
||||||
|
|
||||||
|
if [ -z "$cache_hash" ]; then
|
||||||
|
echo "cached-eval: Failed to compute cache hash for: $script" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ ! -f "$cache_file" || "$source_file" -nt "$cache_file" ]]; then
|
if [[ ! -f "$cache_file" || "$source_file" -nt "$cache_file" ]]; then
|
||||||
mkdir -p "$cache_dir"
|
mkdir -p "$cache_dir"
|
||||||
echo "cached-eval: Updating cache for: $full_cmd --> $cache_file" >&2
|
echo "cached-eval: Updating cache for: $script --> $cache_file" >&2
|
||||||
echo -e "#\n# Generated by cached-eval: $full_cmd\n#\n" >| "$cache_file"
|
echo -e "#\n# Generated by cached-eval: $script\n#\n" >| "$cache_file"
|
||||||
eval "$full_cmd" >>| "$cache_file"
|
eval "$script" >>| "$cache_file"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
source "$cache_file"
|
source "$cache_file"
|
||||||
@@ -178,7 +182,7 @@ fi
|
|||||||
|
|
||||||
# Homebrew on Apple Silicon
|
# Homebrew on Apple Silicon
|
||||||
if [ -f "/opt/homebrew/bin/brew" ]; then
|
if [ -f "/opt/homebrew/bin/brew" ]; then
|
||||||
cached-eval /opt/homebrew/bin/brew /opt/homebrew/bin/brew shellenv
|
cached-eval "/opt/homebrew/bin/brew" /opt/homebrew/bin/brew shellenv
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if command-exists brew; then
|
if command-exists brew; then
|
||||||
|
|||||||
4
zshrc
4
zshrc
@@ -114,8 +114,8 @@ fi
|
|||||||
if command-exists mise; then
|
if command-exists mise; then
|
||||||
alias mi="mise"
|
alias mi="mise"
|
||||||
|
|
||||||
cached-eval "$MISE_INSTALL_PATH" mise activate zsh
|
|
||||||
setup-completions mise "$MISE_INSTALL_PATH" mise completions zsh
|
setup-completions mise "$MISE_INSTALL_PATH" mise completions zsh
|
||||||
|
cached-eval "$MISE_INSTALL_PATH" mise activate zsh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
@@ -128,8 +128,8 @@ if ! command-exists starship && [ -f "$MISE_INSTALL_PATH" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if command-exists starship; then
|
if command-exists starship; then
|
||||||
cached-eval "$(command -v starship)" starship init zsh --print-full-init
|
|
||||||
setup-completions starship "$(command -v starship)" starship completions zsh
|
setup-completions starship "$(command -v starship)" starship completions zsh
|
||||||
|
cached-eval "$(command -v starship)" starship init zsh --print-full-init
|
||||||
else
|
else
|
||||||
echo "WARN: starship not found, skipping prompt setup" >&2
|
echo "WARN: starship not found, skipping prompt setup" >&2
|
||||||
echo " install with: mise install starship" >&2
|
echo " install with: mise install starship" >&2
|
||||||
|
|||||||
Reference in New Issue
Block a user