Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zgenom clean #39

Merged
merged 4 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ Pulls updates on every plugin repository and removes the init script.
zgenom selfupdate
```

#### Clean zgenom plugins

```zsh
zgenom clean
```

#### Watch files for modifications

You can automate the process of running `zgenom reset` by specifying a list of
Expand Down Expand Up @@ -322,6 +328,7 @@ be disabled if you've already called `compinit` yourself before sourcing
- compinit with custom flags wasn't working properly.
- Update to `ohmyzsh/ohmyzsh`.
- Implement the [Zsh Plugin Standard](https://zdharma.org/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html).
- Add `zgenom clean` to remove all unused plugins.

## Example .zshrc

Expand Down
3 changes: 2 additions & 1 deletion functions/_zgen
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

local -a _zgen_commands
_zgen_commands=(
"bin:clone and add files to PATH"
"clean:remove all unused repositories"
"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"
"bin:clone and add files to PATH"
"ohmyzsh:load ohmyzsh base"
"prezto:load prezto base"
"reset:delete the init.zsh script"
Expand Down
39 changes: 25 additions & 14 deletions zgen.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ if [[ -z "${ZGENOM_LOADED}" ]]; then
ZGENOM_LOADED=()
fi

if [[ -z "${ZGENOM_PLUGINS}" ]]; then
ZGENOM_PLUGINS=()
fi

if [[ -z "${zsh_loaded_plugins}" ]]; then
typeset -ga zsh_loaded_plugins
fi
Expand Down Expand Up @@ -146,6 +150,8 @@ zgen-clone() {
local url="$(-zgen-get-clone-url ${repo})"
local dir="$(-zgen-get-clone-dir ${repo} ${branch})"

ZGENOM_PLUGINS+=("${repo}/${branch:-___}")

if [[ -d "${dir}" ]]; then
return # Everything is fine!
elif [[ -z "$branch" ]] && [[ -d "${dir%-___}-master" ]]; then
Expand Down Expand Up @@ -336,6 +342,8 @@ zgen-save() {
-zginit "export PMSPEC=$PMSPEC"
-zginit "export ZPFX=$ZPFX"
-zginit ""
-zginit "ZGENOM_PLUGINS=(${(@qOa)ZGENOM_PLUGINS})"
-zginit ""
-zginit "ZSH=$(-zgen-get-zsh)"
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
-zginit ""
Expand Down Expand Up @@ -510,10 +518,7 @@ zgen-load() {
local location="${dir}/${file}"
location=${location%/}

# clone repo if not present
if [[ ! -d "${dir}" ]]; then
zgen-clone "${repo}" "${branch}"
fi
zgen-clone "${repo}" "${branch}"
fi

# source the file
Expand Down Expand Up @@ -581,7 +586,7 @@ zgen-loadall() {
}

-zgen-bin-dir() {
echo "$ZGEN_SOURCE/bin"
-zgputs "$ZGEN_SOURCE/bin"
}
-zgen-bin() {
local file="${1}"
Expand All @@ -607,10 +612,7 @@ zgen-bin() {
local name="${4}"
local dir="$(-zgen-get-clone-dir ${repo} ${branch})"

# clone repo if not present
if [[ ! -d "${dir}" ]]; then
zgen-clone "${repo}" "${branch}"
fi
zgen-clone "${repo}" "${branch}"

if [[ ! -d $(-zgen-bin-dir) ]]; then
mkdir -p $(-zgen-bin-dir)
Expand Down Expand Up @@ -657,6 +659,18 @@ zgen-selfupdate() {
fi
}

zgen-clean() {
local repo_dir
local repo
setopt localoptions nullglob
for repo_dir in $ZGEN_DIR/**/*/.git; do
repo="${${repo_dir#$ZGEN_DIR/}%/.git}"
if [[ ! ${ZGENOM_PLUGINS[@]} =~ $repo ]]; then
rm -drf "$repo_dir" && -zgputs "Removing $repo."
fi
done
}

# Backwards compatibilty for zgen
zgen-oh-my-zsh() { zgen-ohmyzsh $@ }

Expand Down Expand Up @@ -702,10 +716,7 @@ zgen-pmodule() {

local dir="$(-zgen-get-clone-dir ${repo} ${branch})"

# clone repo if not present
if [[ ! -d "${dir}" ]]; then
zgen-clone "${repo}" "${branch}"
fi
zgen-clone "${repo}" "${branch}"

local module="${repo:t}"
-zgen-prezto-load "'${module}'"
Expand All @@ -715,7 +726,7 @@ zgenom() {
local cmd="${1}"
if [[ -z "${cmd}" ]]; then
-zgputs 'usage: `zgenom [command | instruction] [options]`'
-zgputs " commands: list, saved, reset, clone, update, selfupdate, compile"
-zgputs " commands: list, saved, reset, clone, update, selfupdate, clean, compile"
-zgputs " instructions: load, bin, ohmyzsh, pmodule, prezto, save, apply"
return 1
fi
Expand Down