From 863fad057acf7c88e3b261c95f0c973c6d9d0e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dammsh=C3=A4user?= Date: Thu, 17 Sep 2020 21:51:57 +0200 Subject: [PATCH] Compile files when zgen save is called It compiles *.zsh, *.sh and zcomdump*, as well as all files with zsh shebang (`#!...zsh`). In these paths: - zgen source files - plugin files - zcompdump (if custom path or ~/.zcompdump*) This also adds `zgen compile ` to the api. Closes https://github.com/tarjoilija/zgen/issues/92 --- _zgen | 1 + zgen.zsh | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/_zgen b/_zgen index 716556b..9eac64c 100644 --- a/_zgen +++ b/_zgen @@ -16,6 +16,7 @@ local -a _zgen_commands _zgen_commands=( "clone:clone plugin from repository" + "compile:compile files the given path" "completions:deprecated, please use load instead" "list:print init.zsh" "load:clone and load plugin" diff --git a/zgen.zsh b/zgen.zsh index 05651de..9e93a85 100755 --- a/zgen.zsh +++ b/zgen.zsh @@ -289,6 +289,23 @@ zgen-save() { -zginit "# }}}" zgen-apply + + -zgpute "Compiling files ..." + zgen-compile $ZGEN_DIR + if [[ $ZGEN_DIR != $ZGEN_SOURCE ]]; then + zgen-compile $ZGEN_SOURCE + fi + if [[ -n $ZGEN_CUSTOM_COMPDUMP ]]; then + -zgen-compile $ZGEN_CUSTOM_COMPDUMP + else + set -o nullglob + for compdump in $HOME/.zcompdump*; do + if [ $compdump = *.zwc ]; then + continue + fi + -zgen-compile $compdump + done + fi } zgen-apply() { @@ -311,6 +328,42 @@ zgen-apply() { -zgputs "$(-zgen-get-clone-dir "$ZGEN_OH_MY_ZSH_REPO" "$ZGEN_OH_MY_ZSH_BRANCH")" } +-zgen-compile() { + local file=$1 + [ ! $file.zwc -nt $file ] && zcompile $file +} + +zgen-compile() { + local inp=$1 + if [ -z $inp ]; then + -zgpute '`compile` requires one parameter:' + -zgpute '`zgen compile `' + elif [ -f $inp ]; then + -zgen-compile $inp + else + set -o nullglob + for file in $inp/**/* + do + # only files and ignore compiled files + if [ ! -f $file ] || [[ $file = *.zwc ]]; then + continue + + # Check for shebang if not: + # - *.zsh + # - *.sh + # - zcompdump* + elif [[ $file != *.zsh ]] && [[ $file != *.sh ]] && [[ $file != *zcompdump* ]]; then + read -r firstline < $file + if [[ ! $firstline =~ '^#!.*zsh' ]] 2>/dev/null; then + continue + fi + fi + + -zgen-compile $file + done + fi +} + zgen-load() { if [[ "$#" == 0 ]]; then -zgpute '`load` requires at least one parameter:' @@ -465,7 +518,7 @@ zgen() { local cmd="${1}" if [[ -z "${cmd}" ]]; then -zgputs 'usage: `zgen [command | instruction] [options]`' - -zgputs " commands: list, saved, reset, clone, update, selfupdate" + -zgputs " commands: list, saved, reset, clone, update, selfupdate, compile" -zgputs " instructions: load, oh-my-zsh, pmodule, prezto, save, apply" return 1 fi