refactor(shell): stop managing ~/.zprofile
Always-on and machine hooks already live in .zshenv / local.zsh. Drop the empty stub, remove it on deploy, and leave real .zprofile files to optional migration into local.zsh only.
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
# Install easyzsh shell stubs into a home directory.
|
||||
# Overwrites: ~/.zshenv ~/.zshrc ~/.zprofile
|
||||
# Overwrites: ~/.zshenv ~/.zshrc
|
||||
# Never overwrites: ~/.config/zsh/local.zsh ~/.config/zshrc/** ~/.local/bin/**
|
||||
# Does not manage ~/.zprofile (machine/login hooks belong in local.zsh).
|
||||
#
|
||||
# Usage:
|
||||
# ./deploy.sh
|
||||
# ./deploy.sh --home /path/to/home
|
||||
# ./deploy.sh --migrate-zprofile # fold pre-easyzsh ~/.zprofile into local.zsh
|
||||
# ./deploy.sh --migrate-zprofile # fold real ~/.zprofile into local.zsh, then remove stub
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
@@ -42,6 +43,7 @@ fi
|
||||
|
||||
ZSH_CFG="${TARGET_HOME}/.config/zsh"
|
||||
LOCAL_ZSH="${ZSH_CFG}/local.zsh"
|
||||
ZPROFILE="${TARGET_HOME}/.zprofile"
|
||||
ts=$(date +%Y%m%d-%H%M%S)
|
||||
|
||||
backup_file() {
|
||||
@@ -56,6 +58,16 @@ file_has_content() {
|
||||
[[ -f "$1" ]] && grep -q '[^[:space:]]' "$1"
|
||||
}
|
||||
|
||||
is_easyzsh_zprofile_stub() {
|
||||
local f="$1"
|
||||
[[ -f "$f" ]] || return 1
|
||||
# Comment-only placeholder from earlier easyzsh deploys.
|
||||
if grep -Eqv '^[[:space:]]*(#|$)' "$f"; then
|
||||
return 1
|
||||
fi
|
||||
grep -Eq 'easyzsh|PATH lives in|Leave this file empty|prefer.*local\.zsh' "$f"
|
||||
}
|
||||
|
||||
echo "Deploying easyzsh shell stubs into $TARGET_HOME"
|
||||
|
||||
mkdir -p \
|
||||
@@ -87,11 +99,11 @@ if [[ ! -f "$LOCAL_ZSH" ]]; then
|
||||
rm -f "$tmp"
|
||||
fi
|
||||
|
||||
# Fold a pre-easyzsh ~/.zprofile into local.zsh. Skip easyzsh stub / empty files.
|
||||
if [[ "$MIGRATE_ZPROFILE" -eq 1 && -f "${TARGET_HOME}/.zprofile" ]]; then
|
||||
if grep -Eq 'easyzsh|PATH lives in|Machine-local env' "${TARGET_HOME}/.zprofile" 2>/dev/null \
|
||||
|| grep -Fq 'Leave this file empty' "${TARGET_HOME}/.zprofile" 2>/dev/null; then
|
||||
echo "Skipping zprofile migration: already easyzsh-managed"
|
||||
# Optional: fold a real (non-stub) ~/.zprofile into local.zsh.
|
||||
if [[ "$MIGRATE_ZPROFILE" -eq 1 && -f "$ZPROFILE" ]]; then
|
||||
if is_easyzsh_zprofile_stub "$ZPROFILE"; then
|
||||
echo "Removing easyzsh ~/.zprofile stub (hooks live in local.zsh / .zshenv)"
|
||||
rm -f "$ZPROFILE"
|
||||
elif [[ ! -f "$LOCAL_ZSH" ]]; then
|
||||
zprofile_extract=$(mktemp)
|
||||
awk '
|
||||
@@ -99,22 +111,30 @@ if [[ "$MIGRATE_ZPROFILE" -eq 1 && -f "${TARGET_HOME}/.zprofile" ]]; then
|
||||
skip_pipx && /local\/bin/ { skip_pipx=0; next }
|
||||
skip_pipx { next }
|
||||
{ print }
|
||||
' "${TARGET_HOME}/.zprofile" >"$zprofile_extract"
|
||||
' "$ZPROFILE" >"$zprofile_extract"
|
||||
if file_has_content "$zprofile_extract"; then
|
||||
cp "$zprofile_extract" "$LOCAL_ZSH"
|
||||
echo "Migrated ~/.zprofile body -> ~/.config/zsh/local.zsh"
|
||||
rm -f "$ZPROFILE"
|
||||
echo "Removed ~/.zprofile after migration"
|
||||
fi
|
||||
rm -f "$zprofile_extract"
|
||||
elif is_easyzsh_zprofile_stub "$ZPROFILE"; then
|
||||
rm -f "$ZPROFILE"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Always drop the comment-only easyzsh stub if present (even without --migrate).
|
||||
if is_easyzsh_zprofile_stub "$ZPROFILE"; then
|
||||
rm -f "$ZPROFILE"
|
||||
echo "Removed empty easyzsh ~/.zprofile stub"
|
||||
fi
|
||||
|
||||
backup_file "${TARGET_HOME}/.zshenv"
|
||||
backup_file "${TARGET_HOME}/.zshrc"
|
||||
backup_file "${TARGET_HOME}/.zprofile"
|
||||
|
||||
install -m 644 "${ROOT}/.zshenv" "${TARGET_HOME}/.zshenv"
|
||||
install -m 644 "${ROOT}/.zshrc" "${TARGET_HOME}/.zshrc"
|
||||
install -m 644 "${ROOT}/.zprofile" "${TARGET_HOME}/.zprofile"
|
||||
|
||||
# Remove obsolete managed-tree layout and examples from earlier deploys.
|
||||
/bin/rm -rf \
|
||||
|
||||
Reference in New Issue
Block a user