跳到主要内容

🌀 Bin Gem Node

z-shell/z-a-bin-gem-node

An annex provides the following functionality:

  1. Run programs and scripts without adding anything to $PATH,
  2. Install: Ruby Gems, Node, and Python modules, with automatically set:
  3. Run programs, scripts, and functions with automatic cd into the plugin or snippet directory, plus also with automatic standard output & standard error redirecting.
  4. Source scripts through an automatically created function with the above $GEM_HOME, $NODE_PATH, $VIRTUALENV, and cd features available,
  5. Create the so-called shims known from rbenv – the same feature as the first item of this enumeration – of running a program without adding anything to $PATH with all of the above features, however through an automatic script created in $ZPFX/bin, not a function (the first item uses a function-based mechanism),
  6. Automatic updates of Ruby gems and Node modules during regular plugin and snippet updates with zi update ….

Install bin-gem-node

Simply load like a regular plugin, i.e.:

zi light z-shell/z-a-bin-gem-node

After executing this command you can then use the dl'…' and patch'…' ice-modifiers.

Synopsis of the bin-gem-node annex

The sbin'…' ice that creates forwarder-scripts instead of forwarder-functions created by the fbin'…' ice) turned out to be the proper, best method for exposing binary programs and scripts. This way there is no need to add anything to $PATHz-a-bin-gem-node will automatically create a function that will wrap the binary and provide it on the command line as if it was being placed in the $PATH.

As previously mentioned, the function can automatically export $GEM_HOME, $NODE_PATH, $VIRTUALENV shell variables and also automatically cd into the plugin or snippet directory right before executing the binary and then cd back to the original directory after the execution is finished. As previously mentioned, instead of the function an automatically created script – the so-called shim – can be used for the same purpose and with the same functionality, so that the command is accessible practically fully normally – not only in the live Zsh session, only within which the functions created by fbin'…' exist, but also from any Zsh script.

Suppose that we want to install the junegunn/fzf plugin from GitHub Releases, which contains only a single file – the fzf binary for the selected architecture. It is possible to do it in the standard way – by adding the plugin's directory to the $PATH.

zi ice as'program' from'gh-r'
zi load junegunn/fzf

After this command, the $PATH variable will contain e.g.:

print $PATH
/home/sall/.zi/plugins/junegunn---fzf:/bin:/usr/bin:/usr/sbin:/sbin

For many such programs loaded as plugins, the PATH can become quite cluttered. I've had 26 entries before switching to z-a-bin-gem-node. To solve this, load with the use of sbin'…' ice provided and handled by z-a-bin-gem-node:

zi ice as'program' from'gh-r' sbin'fzf'zi load junegunn/fzf

The $PATH will remain unchanged and a forwarder-script of fzf shim will be created in $ZPFX/bin (~/.zi/polaris/bin by default), which is being already added to the $PATH by Zi when it is being sourced:

cat $ZPFX/bin/fzf
#!/usr/bin/env zshfunction fzf {    local bindir="/home/sall/.zi/plugins/junegunn---fzf"    "$bindir"/"fzf" "$@"}fzf "$@"

Running the script will forward the call to the program accessed through an embedded path to it. Thus, no $PATH changes are needed!

The Ice Modifiers provided by the annex

There are 7 ice modifiers provided and handled by the annex:

Ice modifierDescription
sbinCreates shims for binaries and scripts.
fbinCreates functions for binaries and scripts.
gemInstalls and updates gems + creates functions for gems binaries.
nodeInstalls and updates node_modules + creates functions for binaries of the modules.
pipInstalls and updates python packages into a virtualenv + creates functions for binaries of the packages.
fmodCreates wrapping functions for other functions.
fsrcCreates functions that source given scripts.
fercThe same as fsrc, but using an alternate script-loading method.

Function wrappers for binaries, scripts, gems, node_modules, python packages, etc:

FlagDescription
gSet $GEM_HOME variable to {plugin-dir}.
nSet $NODE_PATH variable to {plugin-dir}/node_modules.
pSet $VIRTUALENV variable to {plugin-dir}/venv.
ccd to the plugin's directory before running the program and then cd back after it has been run.
NAppend &>/dev/null to the call of the binary, i.e. redirect both standard output and standard error to /dev/null.
EAppend 2>/dev/null to the call of the binary, i.e. redirect standard error to /dev/null.
OAppend >/dev/null to the call of the binary, i.e. redirect standard output to /dev/null.

SBIN''

sbin'[{g|n|c|N|E|O}:]{path-to-binary}[ -> {name-of-the-script}]; …'

It creates the so-called shim known from rbenv – a wrapper script that forwards the call to the actual binary. The script is created always under the same, standard, and single $PATH entry: $ZPFX/bin (which is ~/.zi/polaris/bin by default). The flags have the same meaning as with fbin'…' ice.

Example:

zi ice as'program' from'gh-r' sbin'fzf'zi load junegunn/fzfcat $ZPFX/bin/fzf#!/usr/bin/env zshfunction fzf {  local bindir="/home/sall/.zi/plugins/junegunn---fzf"  local -xU PATH="$bindir":"$PATH"      "$bindir"/"fzf" "$@"  }fzf "$@"
  1. as'program' (an alias: as'command') - used for the plugin to be added to $PATH when a plugin is not a file for sourcing.

The sbin ice can be empty. It will then try to create the shim for the trailing component of the id_as ice, e.g.:

  • id_as'exts/git-my' → it'll check if a file git-my exists and if yes, will create the function git-my.
  • paulirish/git-open it'll check if a file git-open exists and if yes, will create the function git-open.

The same trailing component would be set for the snippet URL, for any alphabetically first and executable file.

FBIN''

fbin'[{g|n|c|N|E|O}:]{path-to-binary}[ -> {name-of-the-function}]; …'

Creates a wrapper function of the name the same as the last segment of the path or as {name-of-the-function}.

Example:

zi ice from"gh-r" fbin"g:fzf -> myfzf" nocompilezi load junegunn/fzfwhich myfzfmyfzf () {        local bindir="/home/sall/.zi/plugins/junegunn---fzf"        local -x GEM_HOME="/home/sall/.zi/plugins/junegunn---fzf"        local -xU PATH="/home/sall/.zi/plugins/junegunn---fzf"/bin:"$bindir":"$PATH"        "$bindir"/"fzf" "$@"}
  1. nocompile - used to skip file compilation when it is not required.

The ice can be empty. It will then try to create the function for the trailing component of the id_as ice, e.g.:

  • id_as'exts/git-my' → it'll check if a file git-my exists and if yes, will create the function git-my.

  • paulirish/git-open it'll check if a file git-open exists and if yes, will create the function git-open.

The same trailing component would be set for the snippet URL, for any alphabetically first and executable file.

GEM''

gem'{gem-name}; …'

gem'[{path-to-binary} <-] !{gem-name} [-> {name-of-the-function}]; …'

Installs the gem of name {gem-name} with $GEM_HOME set to the plugin's or snippet's directory. In other words, the gem and its dependencies will be installed locally in that directory. In the second form, it also creates a wrapper function identical to the one created with fbin'…' ice.

Example:

zi ice gem'!asciidoctor' id-as'asciidoctor' nocompilezi load z-shell/0which asciidoctorasciidoctor () {        local bindir="/home/sall/.zi/plugins/asciidoctor/bin"        local -x GEM_HOME="/home/sall/.zi/plugins/asciidoctor"        local -xU PATH="/home/sall/.zi/plugins/asciidoctor"/bin:"$bindir":"$PATH"        "$bindir"/"asciidoctor" "$@"}
  1. z-shell/0 - an empty repository to aid Zi's hooks, in this case, used to store the asciidoctor gem.
  2. id-as'asciidoctor' - used to assign a name instead of the z-shell/0.
  3. nocompile - used to skip file compilation when it is not required.

NODE''

node'{node-module}; …'

node'[{path-to-binary} <-] !{node-module} [-> {name-of-the-function}]; …'

Installs the node module of name {node-module} inside the plugin's or snippet's directory. In the second form, it also creates a wrapper function identical to the one created with fbin'…' ice.

Example:

zi ice node'remark <- !remark-cli -> remark; remark-man' id-as'remark' nocompilezi load z-shell/0which remarkremark () {        local bindir="/home/sall/.zi/plugins/remark/node_modules/.bin"        local -x NODE_PATH="/home/sall/.zi/plugins/remark"/node_modules        local -xU PATH="/home/sall/.zi/plugins/remark"/node_modules/.bin:"$bindir":"$PATH"        "$bindir"/"remark" "$@"}
  1. z-shell/0 - an empty repository to aid Zi's hooks, in this case, used to store the remark Node module.
  2. id-as'remark' - used to assign a name instead of the z-shell/0.
  3. nocompile - used to skip file compilation when it is not required.

In this case, the name of the binary program provided by the node module is different from its name, hence the second form with the b <- a -> c syntax has been used.

PIP''

pip'{pip-package}; …'

pip'[{path-to-binary} <-] !{pip-package} [-> {name-of-the-function}]; …'

Installs the node module of name {pip-package} inside the plugin's or snippet's directory. In the second form, it also creates a wrapper function identical to the one created with fbin'…' ice.

Example:

zi ice pip'youtube-dl <- !youtube-dl -> youtube-dl' id-as'youtube-dl' nocompilezi load z-shell/0which youtube-dlyoutube-dl () {        local bindir="/home/sall/.zi/plugins/youtube-dl/venv/bin"        local -x VIRTUALENV="/home/sall/.zi/plugins/youtube-dl"/venv        local -xU PATH="/home/sall/.zi/plugins/youtube-dl"/venv/bin:"$bindir":"$PATH"        "$bindir"/"youtube-dl" "$@"}
  1. z-shell/0 - an empty repository to aid Zi's hooks, in this case, used to store the youtube-dl pip package.
  2. id-as'youtube-dl' - used to assign a name instead of the z-shell/0.
  3. nocompile - used to skip file compilation when it is not required.

In this case, the name of the binary program provided by the node module is different from its name, hence the second form with the b <- a -> c syntax has been used.

FMOD''

fmod'[{g|n|c|N|E|O}:]{function-name}; …'

fmod'[{g|n|c|N|E|O}:]{function-name} -> {wrapping-function-name}; …'

It wraps the given function with the ability to set $GEM_HOME, etc. – the meaning of the g,n and c flags is the same as in the fbin'…' ice.

Example:

myfunc() { pwd; ls -1 }; zi ice fmod'cgn:myfunc' id-as'myfunc' nocompilezi load z-shell/0which myfuncmyfunc () {        local -x GEM_HOME="/home/sall/.zi/plugins/myfunc"        local -x NODE_PATH="/home/sall/.zi/plugins/myfunc"/node_modules        local oldpwd="/home/sall"        () {                setopt localoptions noautopushd                builtin cd -q "/home/sall/.zi/plugins/myfunc"        }        "myfunc--za-bgn-orig" "$@"        () {                builtin setopt localoptions noautopushd                builtin cd -q "$oldpwd"        }}myfun/home/sall/.zi/plugins/z-shell---0docs/LICENSEREADME.md
  1. z-shell/0 - an empty repository to aid Zi's hooks, in this case, used to store the myfunc function files.
  2. id-as'myfunc' - used to assign a name instead of the z-shell/0.
  3. nocompile - used to skip file compilation when it is not required.

FSCR''

fsrc'[{g|n|c|N|E|O}:]{path-to-script}[ -> {name-of-the-function}]; …'

FERC''

ferc'[{g|n|c|N|E|O}:]{path-to-script}[ -> {name-of-the-function}]; …'

Creates a wrapper function that at each invocation sources the given file. The second ice, ferc'…' works the same with the single difference that it uses eval "$(<{path-to-script})" instead of source "{path-to-script}" to load the script.

Example:

zi ice fsrc"myscript -> myfunc" ferc"myscript" nocompilezi load z-shell/0which myfuncmyfunc () {        local bindir="/home/sall/.zi/plugins/z-shell---0"        local -xU PATH="$bindir":"$PATH"        () {                source "$bindir"/"myscript"        } "$@"}➜  ~ which myscriptmyscript () {        local bindir="/home/sall/.zi/plugins/z-shell---0"        local -xU PATH="$bindir":"$PATH"        () {                eval "$(<"$bindir"/"myscript")"        } "$@"}
  1. nocompile - used to skip file compilation when it is not required.

The ices can be empty. They will then try to create the function for the trailing component of the id-as'…' ice and the other cases, in the same way as with the fbin'…' ice.

Additional subcommands

信息

To view subcommands registered by annexes run: zi subcmds.

There's an additional Zi subcommand that is provided by annex –shim-list. It searches for and displays any shims that are currently stored under $ZPFX/bin:

shim-list invocation

Available flags are:

zi shim-list [ -t | -i | -o | -s | -h ]
FlagDescription
-t --this-dirInstructs Zi to look for shims in the current directory instead of $ZPFX/bin.
-i --from-icesNormally the code looks for the shim files by examining their contents (more info 1).
-o --one-lineDisplay the list of shim files without line breaks, in a single line, after spaces.
-s --shortDon't show the plugin/snippet that the shim belongs to.
-h --helpShows usage information.

Cygwin Support

The sbin'…' ice has an explicit Cygwin support – it creates additional, extra shim files – Windows batch scripts that allow running the shielded applications from e.g.: Windows run dialog – if the ~/.zi/polaris/bin directory is being added to the Windows PATH environment variable, for example (it is a good idea to do so, IMHO). The Windows shims have the same name as the standard ones (which are also being created, normally) plus the .cmd extension. You can test the feature by e.g.: installing Firefox from the Zi package via:

zi pack=bgn for firefox