From aade4704b151fb0653ae1fa13aaab6cf8c27f3b9 Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Sat, 14 Dec 2019 22:21:34 -0500 Subject: [PATCH] Generate login_init.zsh file So we can have the following code in the zlogin template: source ${ZIM_HOME}/login_init.zsh -q &! instead of depending on the zimfw function there. This allows fixing the issue were a non-interactive login shell currently yields: command not found: zimfw. To fully fix the issue, we also need a new zshenv template containing: ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim Templates will be updated in the install script. --- src/stage1/30_init.zsh.erb | 2 - src/stage1/80_zimfw.zsh.erb | 4 - src/{stage1 => stage2}/20_guard.zsh.erb | 3 + src/stage2/28_zimfw_mv.zsh.erb | 15 ++ src/stage2/29_zimfw_build_init.zsh.erb | 13 ++ src/stage2/29_zimfw_build_login_init.zsh.erb | 6 + src/stage2/30_zimfw_build.zsh.erb | 24 +--- src/stage2/50_zimfw_clean_dumpfile.zsh.erb | 2 +- src/stage2/50_zimfw_compile.zsh.erb | 3 + src/stage2/50_zimfw_upgrade.zsh.erb | 25 ++-- src/stage2/80_zimfw.zsh.erb | 10 +- .../login_init.zsh.erb} | 6 +- src/zimfw.zsh.erb | 1 - zimfw.zsh | 136 +++++++++--------- 14 files changed, 126 insertions(+), 124 deletions(-) delete mode 100644 src/stage1/30_init.zsh.erb delete mode 100644 src/stage1/80_zimfw.zsh.erb rename src/{stage1 => stage2}/20_guard.zsh.erb (84%) create mode 100644 src/stage2/28_zimfw_mv.zsh.erb create mode 100644 src/stage2/29_zimfw_build_init.zsh.erb create mode 100644 src/stage2/29_zimfw_build_login_init.zsh.erb create mode 100644 src/stage2/50_zimfw_compile.zsh.erb rename src/{stage1/50_zimfw_compile.zsh.erb => templates/login_init.zsh.erb} (95%) diff --git a/src/stage1/30_init.zsh.erb b/src/stage1/30_init.zsh.erb deleted file mode 100644 index 0e62c46..0000000 --- a/src/stage1/30_init.zsh.erb +++ /dev/null @@ -1,2 +0,0 @@ -# Define Zim location -: ${ZIM_HOME=${0:A:h}} diff --git a/src/stage1/80_zimfw.zsh.erb b/src/stage1/80_zimfw.zsh.erb deleted file mode 100644 index 64ea658..0000000 --- a/src/stage1/80_zimfw.zsh.erb +++ /dev/null @@ -1,4 +0,0 @@ -if [[ ${1} == (compile|login-init) && ${2} == (|-q) ]]; then - _zimfw_compile ${2} - return -fi diff --git a/src/stage1/20_guard.zsh.erb b/src/stage2/20_guard.zsh.erb similarity index 84% rename from src/stage1/20_guard.zsh.erb rename to src/stage2/20_guard.zsh.erb index d46518d..fff3721 100644 --- a/src/stage1/20_guard.zsh.erb +++ b/src/stage2/20_guard.zsh.erb @@ -2,3 +2,6 @@ autoload -Uz is-at-least && if ! is-at-least <%= min_zsh_version %>; then print -u2 -R "${0}: Error starting Zim. You're using Zsh version ${ZSH_VERSION} and versions < <%= min_zsh_version %> are not supported. Update your Zsh." return 1 fi + +# Define Zim location +: ${ZIM_HOME=${0:A:h}} diff --git a/src/stage2/28_zimfw_mv.zsh.erb b/src/stage2/28_zimfw_mv.zsh.erb new file mode 100644 index 0000000..8da341c --- /dev/null +++ b/src/stage2/28_zimfw_mv.zsh.erb @@ -0,0 +1,15 @@ +_zimfw_mv() { + if command cmp -s ${2} ${1}; then + if (( ! _zquiet )); then + print -PR "<%= okay %>%B${2}:%b Already up to date" + fi + else + if [[ -e ${2} ]]; then + command mv -f ${2}{,.old} || return 1 + fi + command mv -f ${1} ${2} && \ + if (( ! _zquiet )); then + print -PR "<%= okay %>%B${2}:%b Updated. Restart your terminal for changes to take effect." + fi + fi +} diff --git a/src/stage2/29_zimfw_build_init.zsh.erb b/src/stage2/29_zimfw_build_init.zsh.erb new file mode 100644 index 0000000..c3a4c7e --- /dev/null +++ b/src/stage2/29_zimfw_build_init.zsh.erb @@ -0,0 +1,13 @@ +_zimfw_build_init() { + local -r ztarget=${ZIM_HOME}/init.zsh + # Force update of init.zsh if it's older than .zimrc + if [[ ${ztarget} -ot <%= home %>/.zimrc ]]; then + command mv -f ${ztarget}{,.old} || return 1 + fi + _zimfw_mv =( + print -R "zimfw() { source ${ZIM_HOME}/<%= script_filename %> \"\${@}\" }" + (( ${#_zfpaths} )) && print -R 'fpath=('${_zfpaths:P}' ${fpath})' + (( ${#_zfunctions} )) && print -R 'autoload -Uz '${_zfunctions} + print -Rn ${(F):-source ${^_zscripts:P}} + ) ${ztarget} +} diff --git a/src/stage2/29_zimfw_build_login_init.zsh.erb b/src/stage2/29_zimfw_build_login_init.zsh.erb new file mode 100644 index 0000000..89eb2f1 --- /dev/null +++ b/src/stage2/29_zimfw_build_login_init.zsh.erb @@ -0,0 +1,6 @@ +_zimfw_build_login_init() { + local -r ztarget=${ZIM_HOME}/login_init.zsh + _zimfw_mv =( + print -Rn "<%= render_escaped("src/templates/login_init.zsh.erb") %>" + ) ${ztarget} +} diff --git a/src/stage2/30_zimfw_build.zsh.erb b/src/stage2/30_zimfw_build.zsh.erb index 12f71af..8226784 100644 --- a/src/stage2/30_zimfw_build.zsh.erb +++ b/src/stage2/30_zimfw_build.zsh.erb @@ -1,26 +1,6 @@ _zimfw_build() { - () { - local -r ztarget=${ZIM_HOME}/init.zsh - if [[ ${ztarget} -nt <%= home %>/.zimrc ]] && command cmp -s ${ztarget} ${1}; then + _zimfw_build_init && _zimfw_build_login_init && \ if (( ! _zquiet )); then - print -PR "<%= okay %>%B${ztarget}:%b Already up to date" + print -P '<%= done %>Done with build.' fi - else - if [[ -e ${ztarget} ]]; then - command mv -f ${ztarget}{,.old} || return 1 - fi - command mv -f ${1} ${ztarget} && \ - if (( ! _zquiet )); then - print -PR "<%= okay %>%B${ztarget}:%b Updated. Restart your terminal for changes to take effect." - fi - fi - if (( ! _zquiet )); then - print -P '<%= done %>Done with build.' - fi - } =( - print -R "zimfw() { source ${ZIM_HOME}/<%= script_filename %> \"\${@}\" }" - (( ${#_zfpaths} )) && print -R 'fpath=('${_zfpaths:P}' ${fpath})' - (( ${#_zfunctions} )) && print -R 'autoload -Uz '${_zfunctions} - print -Rn ${(F):-source ${^_zscripts:P}} - ) } diff --git a/src/stage2/50_zimfw_clean_dumpfile.zsh.erb b/src/stage2/50_zimfw_clean_dumpfile.zsh.erb index aa8d637..4f7efa6 100644 --- a/src/stage2/50_zimfw_clean_dumpfile.zsh.erb +++ b/src/stage2/50_zimfw_clean_dumpfile.zsh.erb @@ -1,6 +1,6 @@ _zimfw_clean_dumpfile() { local zdumpfile zopt - zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="<%= home %>/.zcompdump" + zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile=<%= home %>/.zcompdump (( ! _zquiet )) && zopt='-v' command rm -f ${zopt} ${zdumpfile}{,.zwc{,.old}} || return 1 if (( ! _zquiet )); then diff --git a/src/stage2/50_zimfw_compile.zsh.erb b/src/stage2/50_zimfw_compile.zsh.erb new file mode 100644 index 0000000..3d77b5d --- /dev/null +++ b/src/stage2/50_zimfw_compile.zsh.erb @@ -0,0 +1,3 @@ +_zimfw_compile() { + source ${ZIM_HOME}/login_init.zsh "${@}" +} diff --git a/src/stage2/50_zimfw_upgrade.zsh.erb b/src/stage2/50_zimfw_upgrade.zsh.erb index 4b14a6f..d3b1c8a 100644 --- a/src/stage2/50_zimfw_upgrade.zsh.erb +++ b/src/stage2/50_zimfw_upgrade.zsh.erb @@ -1,26 +1,17 @@ _zimfw_upgrade() { - local -r zscript=${ZIM_HOME}/<%= script_filename %> + local -r ztarget=${ZIM_HOME}/<%= script_filename %> local -r zurl=https://raw.githubusercontent.com/zimfw/zimfw/develop/<%= script_filename %> { if (( ${+commands[wget]} )); then - command wget -nv ${1} -O ${zscript}.new ${zurl} || return 1 + command wget -nv -O ${ztarget}.new ${zurl} || return 1 else - command curl -fsSL -o ${zscript}.new ${zurl} || return 1 - fi - if command cmp -s ${zscript}{,.new}; then - if (( ! _zquiet )); then - print -P '<%= okay %>%B<%= script_filename %>:%b Already up to date' - fi - else - command mv -f ${zscript}{,.old} && command mv -f ${zscript}{.new,} && \ - if (( ! _zquiet )); then - print -P '<%= okay %>%B<%= script_filename %>:%b Upgraded. Restart your terminal for changes to take effect.' - fi - fi - if (( ! _zquiet )); then - print -P '<%= done %>Done with upgrade.' + command curl -fsSL -o ${ztarget}.new ${zurl} || return 1 fi + _zimfw_mv ${ztarget}{.new,} && \ + if (( ! _zquiet )); then + print -P '<%= done %>Done with upgrade.' + fi } always { - command rm -f ${zscript}.new + command rm -f ${ztarget}.new } } diff --git a/src/stage2/80_zimfw.zsh.erb b/src/stage2/80_zimfw.zsh.erb index 12a7989..8e943c3 100644 --- a/src/stage2/80_zimfw.zsh.erb +++ b/src/stage2/80_zimfw.zsh.erb @@ -45,14 +45,10 @@ Options: case ${1} in build) _zimfw_source_zimrc && _zimfw_build && _zimfw_compile ${2} ;; init) _zimfw_source_zimrc && _zimfw_build ;; - clean) - _zimfw_source_zimrc && \ - _zimfw_clean_compiled && \ - _zimfw_clean_dumpfile - ;; + clean) _zimfw_clean_compiled && _zimfw_clean_dumpfile ;; clean-compiled) _zimfw_clean_compiled ;; clean-dumpfile) _zimfw_clean_dumpfile ;; - compile|login-init) _zimfw_source_zimrc && _zimfw_compile ${2} ;; + compile) _zimfw_build_login_init && _zimfw_compile ${2} ;; info) _zimfw_info ;; install|update) _zimfw_source_zimrc 1 || return 1 @@ -63,7 +59,7 @@ Options: _zimfw_source_zimrc && _zimfw_build && _zimfw_compile ${2} ;; uninstall) _zimfw_source_zimrc && _zimfw_uninstall ;; - upgrade) _zimfw_upgrade && _zimfw_compile ;; + upgrade) _zimfw_upgrade && _zimfw_build_login_init && _zimfw_compile ${2} ;; *) print -u2 -PR "%F{red}${0}: Unknown action ${1}%f"$'\n'${zusage} return 1 diff --git a/src/stage1/50_zimfw_compile.zsh.erb b/src/templates/login_init.zsh.erb similarity index 95% rename from src/stage1/50_zimfw_compile.zsh.erb rename to src/templates/login_init.zsh.erb index 71e2384..30f43a9 100644 --- a/src/stage1/50_zimfw_compile.zsh.erb +++ b/src/templates/login_init.zsh.erb @@ -1,10 +1,10 @@ -_zimfw_compile() { +() { setopt LOCAL_OPTIONS CASE_GLOB EXTENDED_GLOB autoload -U zrecompile local zdumpfile zdir zfile # Compile the completion cache; significant speedup - zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="<%= home %>/.zcompdump" + zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile=<%= home %>/.zcompdump if [[ -f ${zdumpfile} ]]; then zrecompile -p ${1} ${zdumpfile} || return 1 fi @@ -30,4 +30,4 @@ _zimfw_compile() { if [[ ${1} != -q ]]; then print -P '<%= done %>Done with compile.' fi -} +} "${@}" diff --git a/src/zimfw.zsh.erb b/src/zimfw.zsh.erb index 29cb8d0..3314711 100644 --- a/src/zimfw.zsh.erb +++ b/src/zimfw.zsh.erb @@ -35,6 +35,5 @@ zim = Zim.new # DO NOT DIRECTLY EDIT THIS FILE! <%= zim.render_commented("LICENSE") %> -<%= zim.render_all("src/stage1/*.erb") %> <%= zim.render_all("src/stage2/*.erb") %> zimfw "${@}" diff --git a/zimfw.zsh b/zimfw.zsh index 565ac7e..752d031 100644 --- a/zimfw.zsh +++ b/zimfw.zsh @@ -32,70 +32,81 @@ fi # Define Zim location : ${ZIM_HOME=${0:A:h}} -_zimfw_compile() { +_zimfw_mv() { + if command cmp -s ${2} ${1}; then + if (( ! _zquiet )); then + print -PR "%F{green})%f %B${2}:%b Already up to date" + fi + else + if [[ -e ${2} ]]; then + command mv -f ${2}{,.old} || return 1 + fi + command mv -f ${1} ${2} && \ + if (( ! _zquiet )); then + print -PR "%F{green})%f %B${2}:%b Updated. Restart your terminal for changes to take effect." + fi + fi +} + +_zimfw_build_init() { + local -r ztarget=${ZIM_HOME}/init.zsh + # Force update of init.zsh if it's older than .zimrc + if [[ ${ztarget} -ot ${ZDOTDIR:-${HOME}}/.zimrc ]]; then + command mv -f ${ztarget}{,.old} || return 1 + fi + _zimfw_mv =( + print -R "zimfw() { source ${ZIM_HOME}/zimfw.zsh \"\${@}\" }" + (( ${#_zfpaths} )) && print -R 'fpath=('${_zfpaths:P}' ${fpath})' + (( ${#_zfunctions} )) && print -R 'autoload -Uz '${_zfunctions} + print -Rn ${(F):-source ${^_zscripts:P}} + ) ${ztarget} +} + +_zimfw_build_login_init() { + local -r ztarget=${ZIM_HOME}/login_init.zsh + _zimfw_mv =( + print -Rn "() { setopt LOCAL_OPTIONS CASE_GLOB EXTENDED_GLOB autoload -U zrecompile local zdumpfile zdir zfile # Compile the completion cache; significant speedup - zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="${ZDOTDIR:-${HOME}}/.zcompdump" - if [[ -f ${zdumpfile} ]]; then - zrecompile -p ${1} ${zdumpfile} || return 1 + zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile=\${ZDOTDIR:-\${HOME}}/.zcompdump + if [[ -f \${zdumpfile} ]]; then + zrecompile -p \${1} \${zdumpfile} || return 1 fi # Compile .zshrc - zrecompile -p ${1} ${ZDOTDIR:-${HOME}}/.zshrc || return 1 + zrecompile -p \${1} \${ZDOTDIR:-\${HOME}}/.zshrc || return 1 # Compile autoloaded functions, taken from zrecompile doc in zshcontrib(1) - for zdir in ${fpath}; do - [[ ${zdir} == (.|..) || ${zdir} == (.|..)/* ]] && continue - zfile=(${zdir}/^(*.*)(N-.)) - if [[ -w ${zdir:h} && -n ${zfile} ]]; then - zfile=(${${(M)zfile%/*/*}#/}) - (builtin cd -q ${zdir:h} && zrecompile -p ${1} ${zdir:t}.zwc ${zfile}) || return 1 + for zdir in \${fpath}; do + [[ \${zdir} == (.|..) || \${zdir} == (.|..)/* ]] && continue + zfile=(\${zdir}/^(*.*)(N-.)) + if [[ -w \${zdir:h} && -n \${zfile} ]]; then + zfile=(\${\${(M)zfile%/*/*}#/}) + (builtin cd -q \${zdir:h} && zrecompile -p \${1} \${zdir:t}.zwc \${zfile}) || return 1 fi done # Compile scripts - for zfile in ${ZIM_HOME}/(^*test*/)#*.zsh{,-theme}(N-.); do - zrecompile -p ${1} ${zfile} || return 1 + for zfile in \${ZIM_HOME}/(^*test*/)#*.zsh{,-theme}(N-.); do + zrecompile -p \${1} \${zfile} || return 1 done - if [[ ${1} != -q ]]; then + if [[ \${1} != -q ]]; then print -P 'Done with compile.' fi +} \"\${@}\" +" + ) ${ztarget} } -if [[ ${1} == (compile|login-init) && ${2} == (|-q) ]]; then - _zimfw_compile ${2} - return -fi - _zimfw_build() { - () { - local -r ztarget=${ZIM_HOME}/init.zsh - if [[ ${ztarget} -nt ${ZDOTDIR:-${HOME}}/.zimrc ]] && command cmp -s ${ztarget} ${1}; then + _zimfw_build_init && _zimfw_build_login_init && \ if (( ! _zquiet )); then - print -PR "%F{green})%f %B${ztarget}:%b Already up to date" + print -P 'Done with build.' fi - else - if [[ -e ${ztarget} ]]; then - command mv -f ${ztarget}{,.old} || return 1 - fi - command mv -f ${1} ${ztarget} && \ - if (( ! _zquiet )); then - print -PR "%F{green})%f %B${ztarget}:%b Updated. Restart your terminal for changes to take effect." - fi - fi - if (( ! _zquiet )); then - print -P 'Done with build.' - fi - } =( - print -R "zimfw() { source ${ZIM_HOME}/zimfw.zsh \"\${@}\" }" - (( ${#_zfpaths} )) && print -R 'fpath=('${_zfpaths:P}' ${fpath})' - (( ${#_zfunctions} )) && print -R 'autoload -Uz '${_zfunctions} - print -Rn ${(F):-source ${^_zscripts:P}} - ) } zmodule() { @@ -256,7 +267,7 @@ _zimfw_clean_compiled() { _zimfw_clean_dumpfile() { local zdumpfile zopt - zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="${ZDOTDIR:-${HOME}}/.zcompdump" + zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile=${ZDOTDIR:-${HOME}}/.zcompdump (( ! _zquiet )) && zopt='-v' command rm -f ${zopt} ${zdumpfile}{,.zwc{,.old}} || return 1 if (( ! _zquiet )); then @@ -264,8 +275,12 @@ _zimfw_clean_dumpfile() { fi } +_zimfw_compile() { + source ${ZIM_HOME}/login_init.zsh "${@}" +} + _zimfw_info() { - print 'Zim version: 1.0.0-SNAPSHOT (previous commit is f9dc5ea)' + print 'Zim version: 1.0.0-SNAPSHOT (previous commit is 86f177a)' print -R 'ZIM_HOME: '${ZIM_HOME} print -R 'Zsh version: '${ZSH_VERSION} print -R 'System info: '$(command uname -a) @@ -287,29 +302,20 @@ _zimfw_uninstall() { } _zimfw_upgrade() { - local -r zscript=${ZIM_HOME}/zimfw.zsh + local -r ztarget=${ZIM_HOME}/zimfw.zsh local -r zurl=https://raw.githubusercontent.com/zimfw/zimfw/develop/zimfw.zsh { if (( ${+commands[wget]} )); then - command wget -nv ${1} -O ${zscript}.new ${zurl} || return 1 + command wget -nv -O ${ztarget}.new ${zurl} || return 1 else - command curl -fsSL -o ${zscript}.new ${zurl} || return 1 - fi - if command cmp -s ${zscript}{,.new}; then - if (( ! _zquiet )); then - print -P '%F{green})%f %Bzimfw.zsh:%b Already up to date' - fi - else - command mv -f ${zscript}{,.old} && command mv -f ${zscript}{.new,} && \ - if (( ! _zquiet )); then - print -P '%F{green})%f %Bzimfw.zsh:%b Upgraded. Restart your terminal for changes to take effect.' - fi - fi - if (( ! _zquiet )); then - print -P 'Done with upgrade.' + command curl -fsSL -o ${ztarget}.new ${zurl} || return 1 fi + _zimfw_mv ${ztarget}{.new,} && \ + if (( ! _zquiet )); then + print -P 'Done with upgrade.' + fi } always { - command rm -f ${zscript}.new + command rm -f ${ztarget}.new } } @@ -440,14 +446,10 @@ fi case ${1} in build) _zimfw_source_zimrc && _zimfw_build && _zimfw_compile ${2} ;; init) _zimfw_source_zimrc && _zimfw_build ;; - clean) - _zimfw_source_zimrc && \ - _zimfw_clean_compiled && \ - _zimfw_clean_dumpfile - ;; + clean) _zimfw_clean_compiled && _zimfw_clean_dumpfile ;; clean-compiled) _zimfw_clean_compiled ;; clean-dumpfile) _zimfw_clean_dumpfile ;; - compile|login-init) _zimfw_source_zimrc && _zimfw_compile ${2} ;; + compile) _zimfw_build_login_init && _zimfw_compile ${2} ;; info) _zimfw_info ;; install|update) _zimfw_source_zimrc 1 || return 1 @@ -458,7 +460,7 @@ fi _zimfw_source_zimrc && _zimfw_build && _zimfw_compile ${2} ;; uninstall) _zimfw_source_zimrc && _zimfw_uninstall ;; - upgrade) _zimfw_upgrade && _zimfw_compile ;; + upgrade) _zimfw_upgrade && _zimfw_build_login_init && _zimfw_compile ${2} ;; *) print -u2 -PR "%F{red}${0}: Unknown action ${1}%f"$'\n'${zusage} return 1