231 lines
10 KiB
Plaintext
231 lines
10 KiB
Plaintext
zmodule() {
|
|
local -r ztarget=${ZIM_CONFIG_FILE:-<%= home %>/.zimrc}
|
|
local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [%B-r%b|%B--root%b <path>] [options]
|
|
|
|
Add %Bzmodule%b calls to your %B${ztarget}%b file to define the modules to be initialized.
|
|
The initialization will be done in the same order it's defined.
|
|
|
|
<url> Module absolute path or repository URL. The following URL formats
|
|
are equivalent: %Bfoo%b, %Bzimfw/foo%b, %Bhttps://github.com/zimfw/foo.git%b.
|
|
If an absolute path is given, the module is considered externally
|
|
installed, and won't be installed or updated by zimfw.
|
|
%B-n%b|%B--name%b <module_name> Set a custom module name. Default: the last component in <url>.
|
|
Slashes can be used inside the name to organize the module into
|
|
subdirectories. The module will be installed at
|
|
%B${ZIM_HOME}/%b<module_name>.
|
|
%B-r%b|%B--root%b <path> Relative path to the module root.
|
|
|
|
Per-module options:
|
|
%B-b%b|%B--branch%b <branch_name> Use specified branch when installing and updating the module.
|
|
Overrides the tag option. Default: the repository default branch.
|
|
%B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module. Over-
|
|
rides the branch option.
|
|
%B-u%b|%B--use%b <%%Bgit%b|%Bdegit%b> Install and update the module using the defined tool. Default is
|
|
either defined by %Bzstyle ':zim:zmodule' use '%b<%%Bgit%b|%Bdegit%b>%B'%b, or %Bgit%b
|
|
if none is provided.
|
|
%Bgit%b requires git itself. Local changes are preserved on updates.
|
|
%Bdegit%b requires curl or wget, and currently only works with GitHub
|
|
URLs. Modules install faster and take less disk space. Local
|
|
changes are lost on updates. Git submodules are not supported.
|
|
%B--no-submodules%b Don't install or update git submodules.
|
|
%B-z%b|%B--frozen%b Don't install or update the module.
|
|
|
|
The per-module options above are carried over multiple zmodule calls for the same module.
|
|
Modules are uniquely identified by their name.
|
|
|
|
Per-module-root options:
|
|
%B--if%b <test> Will only initialize module root if specified test returns a zero
|
|
exit status. The test is evaluated at every new terminal startup.
|
|
%B--on-pull%b <command> Execute command after installing or updating the module. The com-
|
|
mand is executed in the module root directory.
|
|
%B-d%b|%B--disabled%b Don't initialize the module root or uninstall the module.
|
|
|
|
The per-module-root options above are carried over multiple zmodule calls for the same mod-
|
|
ule root.
|
|
|
|
Per-call initialization options:
|
|
%B-f%b|%B--fpath%b <path> Will add specified path to fpath. The path is relative to the
|
|
module root directory. Default: %Bfunctions%b, if the subdirectory
|
|
exists and is non-empty.
|
|
%B-a%b|%B--autoload%b <func_name> Will autoload specified function. Default: all valid names inside
|
|
the %Bfunctions%b subdirectory, if any.
|
|
%B-s%b|%B--source%b <file_path> Will source specified file. The path is relative to the module
|
|
root directory. Default: %Binit.zsh%b, if a non-empty %Bfunctions%b sub-
|
|
directory exists, else the largest of the files matching the glob
|
|
%B(init.zsh|%b<name>%B.(zsh|plugin.zsh|zsh-theme|sh))%b, if any.
|
|
<name> in the glob is resolved to the last component of the mod-
|
|
ule name, or the last component of the path to the module root.
|
|
%B-c%b|%B--cmd%b <command> Will execute specified command. Occurrences of the %B{}%b placeholder
|
|
in the command are substituted by the module root directory path.
|
|
I.e., %B-s 'foo.zsh'%b and %B-c 'source {}/foo.zsh'%b are equivalent.
|
|
|
|
Setting any per-call initialization option above will disable the default values from the
|
|
other per-call initialization options, so only your provided values will be used. I.e. these
|
|
values are either all automatic, or all manual in each zmodule call. To use default values
|
|
and also provided values, use separate zmodule calls."
|
|
if [[ ${${funcfiletrace[1]%:*}:A} != ${ztarget:A} ]]; then
|
|
print -u2 -PlR "%F{red}${0}: Must be called from %B${ztarget}%b%f" '' ${zusage}
|
|
return 2
|
|
fi
|
|
if (( ! # )); then
|
|
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}: Missing zmodule url%f" '' ${zusage}
|
|
_zfailed=1
|
|
return 2
|
|
fi
|
|
local zurl=${1} zname=${1:t} zroot zarg
|
|
local -a zfpaths zfunctions zcmds
|
|
if [[ ${zurl} =~ ^[^:/]+: ]]; then
|
|
zname=${zname%.git}
|
|
elif [[ ${zurl} != /* ]]; then
|
|
# Count number of slashes
|
|
case ${#zurl//[^\/]/} in
|
|
0) zurl=https://github.com/zimfw/${zurl}.git ;;
|
|
1) zurl=https://github.com/${zurl}.git ;;
|
|
esac
|
|
fi
|
|
shift
|
|
while [[ ${1} == (-n|--name|-r|--root) ]]; do
|
|
if (( # < 2 )); then
|
|
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage}
|
|
_zfailed=1
|
|
return 2
|
|
fi
|
|
case ${1} in
|
|
-n|--name)
|
|
shift
|
|
zname=${${1%%/##}##/##}
|
|
;;
|
|
-r|--root)
|
|
shift
|
|
zroot=${${1%%/##}##/##}
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
if [[ ${zurl} == /* ]]; then
|
|
_zdirs[${zname}]=${zurl%%/##}
|
|
zurl=
|
|
else
|
|
_zdirs[${zname}]=${ZIM_HOME}/modules/${zname}
|
|
fi
|
|
if [[ ${+_zurls[${zname}]} -ne 0 && ${_zurls[${zname}]} != ${zurl} ]]; then
|
|
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Module already defined with a different URL. Expected %B${_zurls[${zname}]}%b%f" '' ${zusage}
|
|
_zfailed=1
|
|
return 2
|
|
fi
|
|
_zurls[${zname}]=${zurl}
|
|
local -r zroot_dir=${_zdirs[${zname}]}${zroot:+/${zroot}}
|
|
_zroot_dirs+=(${zroot_dir})
|
|
# Set default values
|
|
if (( ! ${+_ztools[${zname}]} )); then
|
|
zstyle -s ':zim:zmodule' use "_ztools[${zname}]" || _ztools[${zname}]=git
|
|
fi
|
|
if (( ! ${+_ztypes[${zname}]} )) _ztypes[${zname}]=branch
|
|
if (( ! ${+_zsubmodules[${zname}]} )) _zsubmodules[${zname}]=1
|
|
# Set values from options
|
|
while (( # > 0 )); do
|
|
case ${1} in
|
|
-b|--branch|-t|--tag|-u|--use|--on-pull|--if|-f|--fpath|-a|--autoload|-s|--source|-c|--cmd)
|
|
if (( # < 2 )); then
|
|
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage}
|
|
_zfailed=1
|
|
return 2
|
|
fi
|
|
;;
|
|
esac
|
|
case ${1} in
|
|
-b|--branch|-t|--tag|-u|--use|--no-submodules)
|
|
if [[ -z ${zurl} ]] _zimfw_print -u2 -PR "%F{yellow}<%= warn %>${funcfiletrace[1]}:%B${zname}:%b The zmodule option %B${1}%b has no effect for external modules%f"
|
|
;;
|
|
esac
|
|
case ${1} in
|
|
-b|--branch)
|
|
shift
|
|
_ztypes[${zname}]=branch
|
|
_zrevs[${zname}]=${1}
|
|
;;
|
|
-t|--tag)
|
|
shift
|
|
_ztypes[${zname}]=tag
|
|
_zrevs[${zname}]=${1}
|
|
;;
|
|
-u|--use)
|
|
shift
|
|
_ztools[${zname}]=${1}
|
|
;;
|
|
--no-submodules) _zsubmodules[${zname}]=0 ;;
|
|
-z|--frozen) _zfrozens[${zname}]=1 ;;
|
|
--on-pull)
|
|
shift
|
|
zarg=${1}
|
|
if [[ -n ${zroot} ]] zarg="(builtin cd -q ${zroot}; ${zarg})"
|
|
_zonpulls[${zname}]="${_zonpulls[${zname}]+${_zonpulls[${zname}]}; }${zarg}"
|
|
;;
|
|
--if)
|
|
shift
|
|
_zifs[${zroot_dir}]=${1}
|
|
;;
|
|
-f|--fpath)
|
|
shift
|
|
zarg=${1}
|
|
if [[ ${zarg} != /* ]] zarg=${zroot_dir}/${zarg}
|
|
zfpaths+=(${zarg})
|
|
;;
|
|
-a|--autoload)
|
|
shift
|
|
zfunctions+=(${1})
|
|
;;
|
|
-s|--source)
|
|
shift
|
|
zarg=${1}
|
|
if [[ ${zarg} != /* ]] zarg=${zroot_dir}/${zarg}
|
|
zcmds+=("source ${zarg:A}")
|
|
;;
|
|
-c|--cmd)
|
|
shift
|
|
zcmds+=(${1//{}/${zroot_dir:A}})
|
|
;;
|
|
-d|--disabled) _zdisabled_root_dirs+=(${zroot_dir}) ;;
|
|
*)
|
|
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Unknown zmodule option %B${1}%b%f" '' ${zusage}
|
|
_zfailed=1
|
|
return 2
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
if (( _zflags & 1 )); then
|
|
_znames+=(${zname})
|
|
fi
|
|
if (( _zflags & 2 )); then
|
|
if [[ ! -e ${zroot_dir} ]]; then
|
|
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}: ${zroot_dir}%b not found%f"
|
|
_zfailed=1
|
|
return 1
|
|
fi
|
|
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
|
|
zfpaths=(${zroot_dir}/functions(NF))
|
|
# _* functions are autoloaded by compinit
|
|
# prompt_*_setup functions are autoloaded by promptinit
|
|
zfunctions=(${^zfpaths}/^(*~|*.zwc(|.old)|_*|prompt_*_setup)(N-.:t))
|
|
local -ra prezto_scripts=(${zroot_dir}/init.zsh(N))
|
|
if (( ${#zfpaths} && ${#prezto_scripts} )); then
|
|
# this follows the prezto module format, no need to check for other scripts
|
|
zcmds=('source '${^prezto_scripts:A})
|
|
else
|
|
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
|
|
local -ra zscripts=(${zroot_dir}/(init.zsh|(${zname:t}|${zroot_dir:t}).(zsh|plugin.zsh|zsh-theme|sh))(NOL[1]))
|
|
zcmds=('source '${^zscripts:A})
|
|
fi
|
|
fi
|
|
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
|
|
_zimfw_print -u2 -PlR "%F{yellow}<%= warn %>${funcfiletrace[1]}:%B${zname}:%b Nothing found to be initialized. Customize the module name, root or initialization with %Bzmodule%b options.%f" '' ${zusage}
|
|
fi
|
|
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating root dir
|
|
local -r zpre=${zroot_dir}$'\0'
|
|
_zfpaths+=(${zpre}${^zfpaths})
|
|
_zfunctions+=(${zpre}${^zfunctions})
|
|
_zcmds+=(${zpre}${^zcmds})
|
|
fi
|
|
}
|