Skip to content

Commit

Permalink
Autodetect config files (#501)
Browse files Browse the repository at this point in the history
* Only generate resources list on non-interactive mode
* Should always use `antigen` wrapper
* Default enable ANTIGE_AUTO_CONFIG
* Update README.mkd
  • Loading branch information
desyncr committed May 14, 2017
1 parent 58ebc55 commit 1d81f7e
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 61 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ revert-info
# Ignore timestamp file for zcache auto-reset config
*.timestamp

# cache init
# Cache init
/init.zsh
.antigenrc
.zshrc
.zcomp*

# Config files
.resources
11 changes: 7 additions & 4 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,13 @@ to `$ADOTDIR/bundles`.

### Optimizations

`ANTIGEN_CHECK_FILES` — Use this configuration to define if Antigen should
check for configuration changes, this is to avoid doing `antigen-reset` manually.
Use `typeset -a ANTIGEN_CHECK_FILES=(/path/to/.antigenrc /path/to/.zshrc /path/to/other)`
to activate it.
`ANTIGEN_AUTO_CONFIG` — This configuration determines if Antigen should auto
detect configuration files to check for changes (see `ANTIGEN_CHECK_FILES`). Default `true`.

`ANTIGEN_CHECK_FILES` — Use this configuration to tell Antigen which files
to check for configuration changes, this is to avoid doing `antigen-reset` manually.
It's auto-determined if `ANTIGEN_AUTO_CONFIG` is enabled (default), otherwise use
`typeset -a ANTIGEN_CHECK_FILES=(/path/to/.antigenrc /path/to/.zshrc /path/to/other)`.

`ANTIGEN_CLONE_OPTS`, `ANTIGEN_SUBMODULE_OPTS` — Use to control how Antigen
perform cloning repositories and fetching submodules from bundles. These are flags
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.2
develop
63 changes: 34 additions & 29 deletions bin/antigen.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@
# and Contributors <https://github.com/zsh-users/antigen/contributors>
# Homepage: http://antigen.sharats.me
# License: MIT License <mitl.sharats.me>
zmodload zsh/parameter

if [[ $ANTIGEN_CACHE != false ]]; then
ANTIGEN_CACHE="${ANTIGEN_CACHE:-${ADOTDIR:-$HOME/.antigen}/init.zsh}"
ANTIGEN_RSRC="${ADOTDIR:-$HOME/.antigen}/.resources"

for config in $ANTIGEN_CHECK_FILES; do
if [[ "$config" -nt "$config.zwc" ]]; then
{ zcompile "$config" } &!
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
fi
done
if [[ $ANTIGEN_AUTO_CONFIG != false && -f $ANTIGEN_RSRC ]]; then
ANTIGEN_CHECK_FILES=$(cat $ANTIGEN_RSRC 2> /dev/null)
ANTIGEN_CHECK_FILES=(${(@f)ANTIGEN_CHECK_FILES})

for config in $ANTIGEN_CHECK_FILES; do
if [[ "$config" -nt "$config.zwc" ]]; then
{ zcompile "$config" } &!
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
fi
done
fi

[[ -f $ANTIGEN_CACHE && ! $_ANTIGEN_CACHE_LOADED == true ]] && source "$ANTIGEN_CACHE" && return 0;
fi


[[ -z "$_ANTIGEN_INSTALL_DIR" ]] && _ANTIGEN_INSTALL_DIR=${0:A:h}

# Each line in this string has the following entries separated by a space
Expand Down Expand Up @@ -226,31 +232,15 @@ antigen () {
return 0
}

# Updates _ANTIGEN_INTERACTIVE environment variable to reflect
# if antigen is running in an interactive shell or from sourcing.
#
# This function check ZSH_EVAL_CONTEXT if available or functrace otherwise.
# If _ANTIGEN_INTERACTIVE is set to true it won't re-check again.
# This function check ZSH_EVAL_CONTEXT to determine if running in interactive shell.
#
# Usage
# -antigen-interactive-mode
#
# Returns
# Either true or false depending if we are running in interactive mode
-antigen-interactive-mode () {
# Check if we are in any way running in interactive mode
if [[ $_ANTIGEN_INTERACTIVE == false ]]; then
if [[ "$ZSH_EVAL_CONTEXT" =~ "toplevel:*" ]]; then
_ANTIGEN_INTERACTIVE=true
elif [[ -z "$ZSH_EVAL_CONTEXT" ]]; then
zmodload zsh/parameter
if [[ "${functrace[$#functrace]%:*}" == "zsh" ]]; then
_ANTIGEN_INTERACTIVE=true
fi
fi
fi

return _ANTIGEN_INTERACTIVE
return [[ "$ZSH_EVAL_CONTEXT" =~ "toplevel:*" || "$ZSH_EVAL_CONTEXT" =~ "cmdarg:*" ]];
}

# Parses and retrieves a remote branch given a branch name.
Expand Down Expand Up @@ -482,6 +472,8 @@ antigen () {

-set-default ANTIGEN_LOG /dev/null

-set-default ANTIGEN_AUTO_CONFIG true

# CLONE_OPTS uses ${=CLONE_OPTS} expansion so don't use spaces
# for arguments that can be passed as `--key=value`.
-set-default ANTIGEN_CLONE_OPTS "--single-branch --recursive --depth=1"
Expand Down Expand Up @@ -826,7 +818,7 @@ ANTIGEN_CACHE="${ANTIGEN_CACHE:-$ADOTDIR/init.zsh}"

_payload="#-- START ZCACHE GENERATED FILE
#-- GENERATED: $(date)
#-- ANTIGEN v2.0.2
#-- ANTIGEN develop
$(functions -- _antigen)
antigen () {
[[ \"\$ZSH_EVAL_CONTEXT\" =~ \"toplevel:*\" || \"\$ZSH_EVAL_CONTEXT\" =~ \"cmdarg:*\" ]] && source \""$_ANTIGEN_INSTALL_DIR/antigen.zsh"\" && eval antigen \$@;
Expand All @@ -849,22 +841,34 @@ compdef () {}\NL"

_payload+="typeset -aU _ANTIGEN_BUNDLE_RECORD;\
_ANTIGEN_BUNDLE_RECORD=("$(print ${(qq)_ANTIGEN_BUNDLE_RECORD})")\NL"
_payload+="_ANTIGEN_CACHE_LOADED=true ANTIGEN_CACHE_VERSION='v2.0.2'\NL"
_payload+="_ANTIGEN_CACHE_LOADED=true ANTIGEN_CACHE_VERSION='develop'\NL"

_payload+="#-- END ZCACHE GENERATED FILE\NL"

echo -E $_payload | sed 's/\\NL/\'$'\n/g' >! "$ANTIGEN_CACHE"
{ zcompile "$ANTIGEN_CACHE" } &!

# Compile config files, if any
[[ -n $ANTIGEN_CHECK_FILES ]] && { zcompile "$ANTIGEN_CHECK_FILES" } &!
[[ $ANTIGEN_AUTO_CONFIG == true && -n $ANTIGEN_CHECK_FILES ]] && {
echo "$ANTIGEN_CHECK_FILES" >! "$ANTIGEN_RSRC"
zcompile "$ANTIGEN_CHECK_FILES"
} &!

return true
}
# Initialize completion
antigen-apply () {
\rm -f $ANTIGEN_COMPDUMP

# Auto determine check_files
if (( ! -antigen-interactive-mode )); then
# There always should be 2 steps from original source as the recommended way is to use
# `antigen` wrapper not `antigen-apply` directly.
if [[ $ANTIGEN_AUTO_CONFIG == true && -z "$ANTIGEN_CHECK_FILES" && $#funcfiletrace -ge 2 ]]; then
ANTIGEN_CHECK_FILES+=("${${funcfiletrace[2]%:*}##* }")
fi
fi

# Load the compinit module. This will readefine the `compdef` function to
# the one that actually initializes completions.
autoload -Uz compinit
Expand Down Expand Up @@ -1165,6 +1169,7 @@ antigen-purge () {
# Nothing
antigen-reset () {
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
[[ -f "$ANTIGEN_RSRC" ]] && rm -f "$ANTIGEN_RSRC"
echo 'Done. Please open a new shell to see the changes.'
}
antigen-restore () {
Expand Down Expand Up @@ -1434,7 +1439,7 @@ antigen-version () {
revision=" ($(git --git-dir=$_ANTIGEN_INSTALL_DIR/.git rev-parse --short '@'))"
fi
echo "Antigen v2.0.2$revision"
echo "Antigen develop$revision"
}
#compdef _antigen
Expand Down
20 changes: 13 additions & 7 deletions src/boot.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
# and Contributors <https://github.com/zsh-users/antigen/contributors>
# Homepage: http://antigen.sharats.me
# License: MIT License <mitl.sharats.me>
zmodload zsh/parameter

if [[ $ANTIGEN_CACHE != false ]]; then
ANTIGEN_CACHE="${ANTIGEN_CACHE:-${ADOTDIR:-$HOME/.antigen}/init.zsh}"
ANTIGEN_RSRC="${ADOTDIR:-$HOME/.antigen}/.resources"

for config in $ANTIGEN_CHECK_FILES; do
if [[ "$config" -nt "$config.zwc" ]]; then
{ zcompile "$config" } &!
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
fi
done
if [[ $ANTIGEN_AUTO_CONFIG != false && -f $ANTIGEN_RSRC ]]; then
ANTIGEN_CHECK_FILES=$(cat $ANTIGEN_RSRC 2> /dev/null)
ANTIGEN_CHECK_FILES=(${(@f)ANTIGEN_CHECK_FILES})

for config in $ANTIGEN_CHECK_FILES; do
if [[ "$config" -nt "$config.zwc" ]]; then
{ zcompile "$config" } &!
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
fi
done
fi

[[ -f $ANTIGEN_CACHE && ! $_ANTIGEN_CACHE_LOADED == true ]] && source "$ANTIGEN_CACHE" && return 0;
fi


9 changes: 9 additions & 0 deletions src/commands/apply.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
antigen-apply () {
\rm -f $ANTIGEN_COMPDUMP

# Auto determine check_files
if (( ! -antigen-interactive-mode )); then
# There always should be 2 steps from original source as the recommended way is to use
# `antigen` wrapper not `antigen-apply` directly.
if [[ $ANTIGEN_AUTO_CONFIG == true && -z "$ANTIGEN_CHECK_FILES" && $#funcfiletrace -ge 2 ]]; then
ANTIGEN_CHECK_FILES+=("${${funcfiletrace[2]%:*}##* }")
fi
fi

# Load the compinit module. This will readefine the `compdef` function to
# the one that actually initializes completions.
autoload -Uz compinit
Expand Down
1 change: 1 addition & 0 deletions src/commands/reset.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
# Nothing
antigen-reset () {
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
[[ -f "$ANTIGEN_RSRC" ]] && rm -f "$ANTIGEN_RSRC"
echo 'Done. Please open a new shell to see the changes.'
}
20 changes: 2 additions & 18 deletions src/helpers/interactive-mode.zsh
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
# Updates _ANTIGEN_INTERACTIVE environment variable to reflect
# if antigen is running in an interactive shell or from sourcing.
#
# This function check ZSH_EVAL_CONTEXT if available or functrace otherwise.
# If _ANTIGEN_INTERACTIVE is set to true it won't re-check again.
# This function check ZSH_EVAL_CONTEXT to determine if running in interactive shell.
#
# Usage
# -antigen-interactive-mode
#
# Returns
# Either true or false depending if we are running in interactive mode
-antigen-interactive-mode () {
# Check if we are in any way running in interactive mode
if [[ $_ANTIGEN_INTERACTIVE == false ]]; then
if [[ "$ZSH_EVAL_CONTEXT" =~ "toplevel:*" ]]; then
_ANTIGEN_INTERACTIVE=true
elif [[ -z "$ZSH_EVAL_CONTEXT" ]]; then
zmodload zsh/parameter
if [[ "${functrace[$#functrace]%:*}" == "zsh" ]]; then
_ANTIGEN_INTERACTIVE=true
fi
fi
fi

return _ANTIGEN_INTERACTIVE
return [[ "$ZSH_EVAL_CONTEXT" =~ "toplevel:*" || "$ZSH_EVAL_CONTEXT" =~ "cmdarg:*" ]];
}

2 changes: 2 additions & 0 deletions src/lib/env-setup.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

-set-default ANTIGEN_LOG /dev/null

-set-default ANTIGEN_AUTO_CONFIG true

# CLONE_OPTS uses ${=CLONE_OPTS} expansion so don't use spaces
# for arguments that can be passed as `--key=value`.
-set-default ANTIGEN_CLONE_OPTS "--single-branch --recursive --depth=1"
Expand Down
5 changes: 4 additions & 1 deletion src/lib/zcache.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ compdef () {}\NL"
{ zcompile "$ANTIGEN_CACHE" } &!

# Compile config files, if any
[[ -n $ANTIGEN_CHECK_FILES ]] && { zcompile "$ANTIGEN_CHECK_FILES" } &!
[[ $ANTIGEN_AUTO_CONFIG == true && -n $ANTIGEN_CHECK_FILES ]] && {
echo "$ANTIGEN_CHECK_FILES" >! "$ANTIGEN_RSRC"
zcompile "$ANTIGEN_CHECK_FILES"
} &!

return true
}
1 change: 1 addition & 0 deletions tests/.zshenv
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export ADOTDIR=$(mktemp -du "/tmp/dot-antigen-tmp-XXXXX")
[[ ! -d "$ADOTDIR" ]] && mkdir -p "$ADOTDIR"

export ANTIGEN=${ANTIGEN:-"/antigen"}
export ANTIGEN_AUTO_CONFIG=false
export TESTDIR=$(mktemp -d "/tmp/cram-testdir-XXXXX" || /tmp/cram-testdir)
test -f "$TESTDIR/.zcompdump" && rm "$TESTDIR/.zcompdump"
source "$ANTIGEN/antigen.zsh"
Expand Down

0 comments on commit 1d81f7e

Please sign in to comment.