From ddde9c8bbc2cdc2b34b3d4e7658f5bf40598c0e2 Mon Sep 17 00:00:00 2001 From: vladislav doster <10052309+vladdoster@users.noreply.github.com> Date: Sun, 26 Jun 2022 16:51:29 -0500 Subject: [PATCH] style: format zsh and markdown to zinit convention (#3) Signed-off-by: Vladislav Doster --- README.md | 44 +++++----- z-a-patch-dl.plugin.zsh | 14 ++-- za-patch-dl-handler | 180 ++++++++++++++++++++-------------------- 3 files changed, 119 insertions(+), 119 deletions(-) diff --git a/README.md b/README.md index 5ec0231..84bb798 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # zinit-annex-patch-dl -## Introduction - -A Zsh-Zinit extension (i.e. an -[annex](https://zdharma-continuum.github.io/zinit/wiki/Annexes/)) that downloads files and +A Zinit extension (i.e., an [annex](https://zdharma-continuum.github.io/zinit/wiki/Annexes/)) that downloads files and applies patches. It adds two ice modifiers: ```zsh @@ -16,43 +13,42 @@ and zinit ice patch'{file-name-with-the-patch-to-apply}; …' … ``` -The Zinit annex (i.e. an extension) will: +The Zinit annex (i.e., an extension) will: -- download the given `{URL}` under the path `{optional-output-file-name}` (if no - file name given, then it is taken from last segment of the URL) in case of the - `dl''` ice-mod, -- apply a patch given by the `{file-name-with-the-patch-to-apply}` in case of - the `patch''` ice-mod. +- Download the given `{URL}` under the path `{optional-output-file-name}` (if no file name is given, then it is taken + from the last segment of the URL) in case of the `dl''` ice-mod, +- Apply a patch given by the `{file-name-with-the-patch-to-apply}` in case of the `patch''` ice-mod. -You can use this functionality to download and apply patches. For example, to -install `fbterm`, two patches are being needed, one to fix the operation, the -other one to fix the build: +You can use this functionality to download and apply patches. For example, to install `fbterm`, two patches are being +needed, one to fix the operation, the other one to fix the build: -```zsn +```zsh zinit ice \ - as"command" pick"$ZPFX/bin/fbterm" \ - dl"https://bugs.archlinux.org/task/46860?getfile=13513 -> ins.patch" \ - dl"https://aur.archlinux.org/cgit/aur.git/plain/0001-Fix-build-with-gcc-6.patch?h=fbterm-git" \ - patch"ins.patch; 0001-Fix-build-with-gcc-6.patch" \ + as"command" \ atclone"./configure --prefix=$ZPFX" \ atpull"%atclone" \ - make"install" reset + dl"https://aur.archlinux.org/cgit/aur.git/plain/0001-Fix-build-with-gcc-6.patch?h=fbterm-git" \ + dl"https://bugs.archlinux.org/task/46860?getfile=13513 -> ins.patch" \ + make"install" \ + patch"ins.patch; 0001-Fix-build-with-gcc-6.patch" \ + pick"$ZPFX/bin/fbterm" \ + reset + zinit load izmntuk/fbterm ``` This command will result in: -![fbterm -example](https://raw.githubusercontent.com/zdharma-continuum/zinit-annex-patch-dl/master/images/fbterm-ex.png) +![fbterm example](https://raw.githubusercontent.com/zdharma-continuum/zinit-annex-patch-dl/master/images/fbterm-ex.png) ## Installation -Simply load like a plugin, i.e. the following will add the annex to Zinit: +Run the following command to add the annex to Zinit: ```zsh zinit light zdharma-continuum/zinit-annex-patch-dl ``` -After executing this command you can then use the `dl''` and `patch''` ice-mods. +After executing this command, you can use the `dl''` and `patch''` ice-mods. - + diff --git a/z-a-patch-dl.plugin.zsh b/z-a-patch-dl.plugin.zsh index f9ea9b5..0c5223b 100644 --- a/z-a-patch-dl.plugin.zsh +++ b/z-a-patch-dl.plugin.zsh @@ -1,4 +1,4 @@ -# According to the Zsh Plugin Standard: +# In accordance with the Zsh Plugin Standard # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}" @@ -11,11 +11,13 @@ za-patch-dl-help-null-handler() { :; } # Register !atclone hook @zinit-register-annex "zinit-annex-patch-dl" hook:\!atclone-20 \ - za-patch-dl-handler \ - za-patch-dl-help-null-handler \ - "dl''|patch''" # register a new ice-mod: test'' + za-patch-dl-handler \ + za-patch-dl-help-null-handler \ + "dl''|patch''" # register a new ice-mod: test'' # Register !atpull hook @zinit-register-annex "zinit-annex-patch-dl" hook:\!atpull-20 \ - za-patch-dl-handler \ - za-patch-dl-help-null-handler + za-patch-dl-handler \ + za-patch-dl-help-null-handler + +# vim:ft=zsh:sw=2:sts=2:et diff --git a/za-patch-dl-handler b/za-patch-dl-handler index 4dc1d84..628bc9a 100644 --- a/za-patch-dl-handler +++ b/za-patch-dl-handler @@ -1,111 +1,113 @@ emulate -RL zsh -setopt extendedglob warncreateglobal typesetsilent noshortloops +setopt extendedglob noshortloops typesetsilent warncreateglobal -[[ "$1" = plugin ]] && \ - local type="$1" user="$2" plugin="$3" id_as="$4" dir="$5" || \ - local type="$1" url="$2" id_as="$3" dir="$4" +if [[ "$1" = plugin ]]; then + local type="$1" user="$2" plugin="$3" id_as="$4" dir="$5" +else + local type="$1" url="$2" id_as="$3" dir="$4" +fi # FUNCTION: .zinit-annex-patch-dl-download-file-stdout {{{ -# Downloads file to stdout. Supports following backend commands: -# curl, wget, lftp, lynx. Used by snippet loading. +# Downloads file to stdout. Used by snippet loading. +# Supports following backend commands: curl, wget, lftp, lynx. .zinit-annex-patch-dl-download-file-stdout() { - local url="$1" restart="$2" + local url="$1" restart="$2" - setopt localoptions localtraps + setopt localoptions localtraps - if (( restart )); then - (( ${path[(I)/usr/local/bin]} )) || \ - { - path+=( "/usr/local/bin" ); - trap "path[-1]=()" EXIT - } - - if (( ${+commands[curl]} )) then - command curl -fsSL "$url" || return 1 - elif (( ${+commands[wget]} )); then - command wget -q "$url" -O - || return 1 - elif (( ${+commands[lftp]} )); then - command lftp -c "cat $url" || return 1 - elif (( ${+commands[lynx]} )) then - command lynx -source "$url" || return 1 - else - return 2 - fi - else - if type curl 2>/dev/null 1>&2; then - command curl -fsSL "$url" || return 1 - elif type wget 2>/dev/null 1>&2; then - command wget -q "$url" -O - || return 1 - elif type lftp 2>/dev/null 1>&2; then - command lftp -c "cat $url" || return 1 - else - .zinit-annex-patch-dl-download-file-stdout "$url" "1" - return $? - fi + if (( restart )); then + if (( !${path[(I)/usr/local/bin]} )); then + path+=( "/usr/local/bin" ); + trap "path[-1]=()" EXIT fi - return 0 + if (( ${+commands[curl]} )) then + command curl -fsSL "$url" || return 1 + elif (( ${+commands[wget]} )); then + command wget -q "$url" -O - || return 1 + elif (( ${+commands[lftp]} )); then + command lftp -c "cat $url" || return 1 + elif (( ${+commands[lynx]} )) then + command lynx -source "$url" || return 1 + else + return 2 + fi + else + if type curl 2>/dev/null 1>&2; then + command curl -fsSL "$url" || return 1 + elif type wget 2>/dev/null 1>&2; then + command wget -q "$url" -O - || return 1 + elif type lftp 2>/dev/null 1>&2; then + command lftp -c "cat $url" || return 1 + else + .zinit-annex-patch-dl-download-file-stdout "$url" "1" + return $? + fi + fi + + return 0 } # }}} -if [[ -n ${ICE[dl]} && ${ICE[dl]} != ink=[-/[:alnum:]]* ]] { - local -a dls srcdst - dls=( "${(s.;.)ICE[dl]}" ) +if [[ -n ${ICE[dl]} && ${ICE[dl]} != ink=[-/[:alnum:]]* ]]; then + local -a dls srcdst + dls=( "${(s.;.)ICE[dl]}" ) - local dl hd tl - for dl ( $dls ) { - srcdst=( ${(@s.->.)dl} ) - srcdst=( "${srcdst[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" ) - - hd="" tl="" - [[ -n ${srcdst[2]} ]] && { - @zinit-substitute 'srcdst[2]' - hd="${srcdst[2]:h}" - tl="${srcdst[2]:t}" - } || { - @zinit-substitute 'srcdst[1]' - hd="." - tl="${${srcdst[1]%%\?*}:t}" - } + local dl hd tl + for dl ( $dls ) { + srcdst=( ${(@s.->.)dl} ) + srcdst=( ${srcdst[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/} ) + hd="" tl="" - local tdir="${${(M)hd:#/*}:-$dir/$hd}" + if [[ -n ${srcdst[2]} ]]; then + @zinit-substitute 'srcdst[2]' + hd="${srcdst[2]:h}" + tl="${srcdst[2]:t}" + else + @zinit-substitute 'srcdst[1]' + hd="." + tl="${${srcdst[1]%%\?*}:t}" + fi - # Create the destination directory - command mkdir -p "$tdir" + local tdir="${${(M)hd:#/*}:-$dir/$hd}" - local ret=0 + # Create the destination directory + command mkdir -p "$tdir" - .zinit-annex-patch-dl-download-file-stdout "${srcdst[1]}" >! "$tdir/$tl" || { - .zinit-annex-patch-dl-download-file-stdout "${srcdst[1]}" 1 >! "$tdir/$tl" || { - print -P -- "%F{38}patch-dl annex: %F{160}Couldn't download" \ - "the url %F{220}${srcdst[1]}%f" - ret=1 - } - } + local ret=0 - (( !ret && !OPTS[opt_-q,--quiet] && ZINIT[annex-multi-flag:pull-active] == 2 )) && \ - print -P -- "%F{38}patch-dl annex: %F{154}File %F{220}$tl%F{154}" \ - "downloaded correctly%f" - } -} + if (( !.zinit-annex-patch-dl-download-file-stdout "${srcdst[1]}" >! "$tdir/$tl" )); then + if (( !.zinit-annex-patch-dl-download-file-stdout "${srcdst[1]}" 1 >! "$tdir/$tl" )); then + print -P -- "%F{38}[patch-dl annex]: %F{160}Failed to download the URL %F{220}${srcdst[1]}%f" + ret=1 + fi + fi -if [[ -n "${ICE[patch]}" ]] { - local -a patches - patches=( "${(s.;.)ICE[patch]}" ) - patches=( "${patches[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" ) + if (( !ret && !OPTS[opt_-q,--quiet] && ZINIT[annex-multi-flag:pull-active] == 2 )); then + print -P -- "%F{38}[patch-dl annex]: %F{154}File %F{220}$tl%F{154} downloaded correctly%f" + fi + } +fi - local pch - for pch ( $patches ) { - @zinit-substitute pch +if [[ -n "${ICE[patch]}" ]]; then + local -a patches + patches=( "${(s.;.)ICE[patch]}" ) + patches=( "${patches[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" ) - (( !OPTS[opt_-q,--quiet] && ZINIT[annex-multi-flag:pull-active] == 2 )) && \ - print -P -- "%F{38}patch-dl annex: %F{154}Applying patch %F{220}$pch...%f" + local pch + for pch ( $patches ) { + @zinit-substitute pch - ( cd -q "$dir" - print -nP "%F{220}" - command patch -s -N -p1 -i "$pch" - print -nP "%f" - ) - } -} + if (( !OPTS[opt_-q,--quiet] && ZINIT[annex-multi-flag:pull-active] == 2 )); then + print -P -- "%F{38}[patch-dl annex]: %F{154}Applying patch %F{220}$pch...%f" + fi -# vim:ft=zsh:sw=4:sts=4:et + ( + cd -q "$dir" + print -nP "%F{220}" + command patch -s -N -p1 -i "$pch" + print -nP "%f" + ) + } +fi + +# vim:ft=zsh:sw=2:sts=2:et