From 3b7908dca1006fac2fd28c5c0bd48f8087092d20 Mon Sep 17 00:00:00 2001 From: Eric Nielsen <4120606+ericbn@users.noreply.github.com> Date: Tue, 25 Jun 2024 09:08:10 -0500 Subject: [PATCH] Try to upgrade zimfw.zsh if it's a symlink Initially the idea was to not allow this so upgrades are disabled when the script is installed using a package manager, but this is an unnecessary complexity. Making it less smart and more predictable. --- CHANGELOG.md | 3 +-- src/stage2/20_guard.zsh.erb | 2 +- src/stage2/29_zimfw_build_init.zsh.erb | 2 +- src/stage2/50_zimfw_upgrade.zsh.erb | 10 +++------- src/stage2/80_zimfw.zsh.erb | 2 +- zimfw.zsh | 18 +++++++----------- 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 895d984..87d390b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 already doing so, hence not considering this is a breaking change. - Allow zimfw.zsh to exist anywhere else, not only inside `ZIM_HOME`. This enables installing the zimfw.zsh script independently with a package manager. -- Don't try to upgrade if sourced zimfw.zsh is not in a writable path or is a - symlink. +- Don't try to upgrade if sourced zimfw.zsh is not in a writable path. ### Fixed diff --git a/src/stage2/20_guard.zsh.erb b/src/stage2/20_guard.zsh.erb index ceda2e2..accde4b 100644 --- a/src/stage2/20_guard.zsh.erb +++ b/src/stage2/20_guard.zsh.erb @@ -9,4 +9,4 @@ if (( ! ${+ZIM_HOME} )); then return 1 fi # Define zimfw location -typeset -g __ZIMFW_PATH=${0:h} +typeset -g __ZIMFW_FILE=${0} diff --git a/src/stage2/29_zimfw_build_init.zsh.erb b/src/stage2/29_zimfw_build_init.zsh.erb index 609c5ed..4b268b1 100644 --- a/src/stage2/29_zimfw_build_init.zsh.erb +++ b/src/stage2/29_zimfw_build_init.zsh.erb @@ -5,7 +5,7 @@ _zimfw_build_init() { command mv -f ${ztarget}{,.old} || return 1 fi _zimfw_mv =( - print -R 'if (( ${+ZIM_HOME} )) zimfw() { source '${${(qqq)__ZIMFW_PATH}/${HOME}/\${HOME}}'/zimfw.zsh "${@}" }' + print -R 'if (( ${+ZIM_HOME} )) zimfw() { source '${${(qqq)__ZIMFW_FILE}/${HOME}/\${HOME}}' "${@}" }' local zroot_dir zpre local -a zif_functions zif_cmds zroot_functions zroot_cmds local -a zfunctions=(${_zfunctions}) zcmds=(${_zcmds}) diff --git a/src/stage2/50_zimfw_upgrade.zsh.erb b/src/stage2/50_zimfw_upgrade.zsh.erb index 96d6df5..ad7286f 100644 --- a/src/stage2/50_zimfw_upgrade.zsh.erb +++ b/src/stage2/50_zimfw_upgrade.zsh.erb @@ -1,11 +1,7 @@ _zimfw_upgrade() { - if [[ ! -w ${__ZIMFW_PATH} ]]; then - print -u2 -R $'<%= red %>No write permission to <%= bold %>'${__ZIMFW_PATH}$'<%= normalred %>. Will not try to upgrade.<%= normal %>' - return 1 - fi - local -r ztarget=${__ZIMFW_PATH}/zimfw.zsh zurl=https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh.gz - if [[ -L ${ztarget} || ! -f ${ztarget} ]]; then - print -u2 -R $'<%= red %><%= bold %>'${ztarget}$'<%= normalred %> is a symlink or not a regular file. Will not try to upgrade.<%= normal %>' + local -r ztarget=${__ZIMFW_FILE:A} zurl=https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh.gz + if [[ ! -w ${ztarget:h} ]]; then + print -u2 -R $'<%= red %>No write permission to <%= bold %>'${ztarget:h}$'<%= normalred %>. Will not try to upgrade.<%= normal %>' return 1 fi { diff --git a/src/stage2/80_zimfw.zsh.erb b/src/stage2/80_zimfw.zsh.erb index a68a7cf..e123a88 100644 --- a/src/stage2/80_zimfw.zsh.erb +++ b/src/stage2/80_zimfw.zsh.erb @@ -48,7 +48,7 @@ Options: local -r _zversion_target=${ZIM_HOME}/.latest_version if ! zstyle -t ':zim' disable-version-check && \ - [[ ${1} != check-version && -w ${ZIM_HOME} && -w ${__ZIMFW_PATH} && ! -L ${__ZIMFW_PATH}/zimfw.zsh && -f ${__ZIMFW_PATH}/zimfw.zsh ]] + [[ ${1} != check-version && -w ${ZIM_HOME} && -w ${__ZIMFW_FILE:A:h} ]] then # If .latest_version does not exist or was not modified in the last 30 days [[ -f ${_zversion_target}(#qNm-30) ]]; local -r zversion_check_force=${?} diff --git a/zimfw.zsh b/zimfw.zsh index db5189d..9f25cc1 100644 --- a/zimfw.zsh +++ b/zimfw.zsh @@ -35,7 +35,7 @@ if (( ! ${+ZIM_HOME} )); then return 1 fi # Define zimfw location -typeset -g __ZIMFW_PATH=${0:h} +typeset -g __ZIMFW_FILE=${0} _zimfw_print() { if (( _zprintlevel > 0 )) print "${@}" @@ -61,7 +61,7 @@ _zimfw_build_init() { command mv -f ${ztarget}{,.old} || return 1 fi _zimfw_mv =( - print -R 'if (( ${+ZIM_HOME} )) zimfw() { source '${${(qqq)__ZIMFW_PATH}/${HOME}/\${HOME}}'/zimfw.zsh "${@}" }' + print -R 'if (( ${+ZIM_HOME} )) zimfw() { source '${${(qqq)__ZIMFW_FILE}/${HOME}/\${HOME}}' "${@}" }' local zroot_dir zpre local -a zif_functions zif_cmds zroot_functions zroot_cmds local -a zfunctions=(${_zfunctions}) zcmds=(${_zcmds}) @@ -462,7 +462,7 @@ _zimfw_compile() { } _zimfw_info() { - print -R 'zimfw version: '${_zversion}' (built at 2024-06-21 20:42:45 UTC, previous commit is eb37844)' + print -R 'zimfw version: '${_zversion}' (built at 2024-06-25 14:06:08 UTC, previous commit is 0785d87)' local zparam for zparam in LANG ${(Mk)parameters:#LC_*} OSTYPE TERM TERM_PROGRAM TERM_PROGRAM_VERSION ZIM_HOME ZSH_VERSION; do print -R ${(r.22....:.)zparam}${(P)zparam} @@ -482,13 +482,9 @@ _zimfw_uninstall() { } _zimfw_upgrade() { - if [[ ! -w ${__ZIMFW_PATH} ]]; then - print -u2 -R $'\E[31mNo write permission to \E[1m'${__ZIMFW_PATH}$'\E[0;31m. Will not try to upgrade.\E[0m' - return 1 - fi - local -r ztarget=${__ZIMFW_PATH}/zimfw.zsh zurl=https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh.gz - if [[ -L ${ztarget} || ! -f ${ztarget} ]]; then - print -u2 -R $'\E[31m\E[1m'${ztarget}$'\E[0;31m is a symlink or not a regular file. Will not try to upgrade.\E[0m' + local -r ztarget=${__ZIMFW_FILE:A} zurl=https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh.gz + if [[ ! -w ${ztarget:h} ]]; then + print -u2 -R $'\E[31mNo write permission to \E[1m'${ztarget:h}$'\E[0;31m. Will not try to upgrade.\E[0m' return 1 fi { @@ -946,7 +942,7 @@ Options: local -r _zversion_target=${ZIM_HOME}/.latest_version if ! zstyle -t ':zim' disable-version-check && \ - [[ ${1} != check-version && -w ${ZIM_HOME} && -w ${__ZIMFW_PATH} && ! -L ${__ZIMFW_PATH}/zimfw.zsh && -f ${__ZIMFW_PATH}/zimfw.zsh ]] + [[ ${1} != check-version && -w ${ZIM_HOME} && -w ${__ZIMFW_FILE:A:h} ]] then # If .latest_version does not exist or was not modified in the last 30 days [[ -f ${_zversion_target}(#qNm-30) ]]; local -r zversion_check_force=${?}