-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-auto-update-plugins.plugin.zsh
executable file
·146 lines (128 loc) · 5.32 KB
/
zsh-auto-update-plugins.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env zsh
#####################
# COMMONS
#####################
autoload colors
zmodload zsh/datetime
#########################
# CONSTANT
#########################
BOLD="bold"
NONE="NONE"
#########################
# Functions
#########################
_zsh_auto_update_log() {
local font=$1
local color=$2
local msg=$3
if [ $font = $BOLD ]; then
echo $fg_bold[$color] "[zsh-auto-update-plugin] $msg" "$reset_color"
else
echo $fg[$color] "[zsh-auto-update-plugin] $msg" "$reset_color"
fi
}
# Current day
function _current_date() {
echo $(($EPOCHSECONDS / 60 / 60 / 24))
}
# temp file to store date
function _update_zsh_custom_update() {
echo "LAST_DATE=$(_current_date)" >|"${ZSH_CACHE_DIR}/.zsh-custom-update"
}
#########################
# PLUGIN MAIN
#########################
# Define update time
date_target=$UPDATE_ZSH_DAYS
if [[ -z "$date_target" ]]; then
# Default value for updates
date_target=15
fi
# Upgrade all plugins and call all custom update functions
function upgrade_oh_my_zsh_custom() {
if [[ -z "$ZSH_AUTOUPDATE_PLUGINS_SILENT" ]]; then
_zsh_auto_update_log $NONE "blue" "#############################################"
_zsh_auto_update_log $BOLD "blue" "-> Upgrading all custom plugins..."
fi
find "${ZSH_CUSTOM}" -type d -name .git | while read d; do
p=$(dirname "$d")
pushd -q "${p}"
if git pull --rebase --autostash; then
_zsh_auto_update_log $NONE "blue" "Plugin $d has been updated and/or is at the current version."
else
_zsh_auto_update_log $NONE "red" 'There was an error updating. Try again later?'
fi
popd -q
done
if [[ -z "$ZSH_AUTOUPDATE_PLUGINS_SILENT" ]]; then
_zsh_auto_update_log $BOLD "blue" "-> Calling update custom functions..."
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_DIRENV" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_direnv &>/dev/null && update_zsh_direnv || _zsh_auto_update_log $NONE "blue" "-> direnv plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_KUBECTX" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_kubectx &>/dev/null && update_zsh_kubectx || _zsh_auto_update_log $NONE "blue" "-> kubectx plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_PKENV" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_pkenv &>/dev/null && update_zsh_pkenv || _zsh_auto_update_log $NONE "blue" "-> pkenv plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_SDKMAN" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_sdkman &>/dev/null && update_zsh_sdkman || _zsh_auto_update_log $NONE "blue" "-> sdkman plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_TFENV" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_tfenv &>/dev/null && update_zsh_tfenv || _zsh_auto_update_log $NONE "blue" "-> tfenv plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_TGENV" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_tgenv &>/dev/null && update_zsh_tgenv || _zsh_auto_update_log $NONE "blue" "-> tgenv plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_EXA" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_exa &>/dev/null && update_zsh_exa || _zsh_auto_update_log $NONE "blue" "-> exa plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_Z" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_z &>/dev/null && update_zsh_z || _zsh_auto_update_log $NONE "blue" "-> z plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_TFSWITCH" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_tfswitch &>/dev/null && update_zsh_tfswitch || _zsh_auto_update_log $NONE "blue" "-> tfswitch plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_TGSWITCH" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_tgswitch &>/dev/null && update_zsh_tgswitch || _zsh_auto_update_log $NONE "blue" "-> tgswitch plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_IGNORE_TF_TOOLS" ]] || [[ -z "$ZSH_AUTOUPDATE_IGNORE_ALL" ]]; then
type update_zsh_terraform &>/dev/null && update_zsh_terraform || _zsh_auto_update_log $NONE "blue" "-> terrafrom plugin will not be updated"
fi
if [[ -z "$ZSH_AUTOUPDATE_PLUGINS_SILENT" ]]; then
_zsh_auto_update_log $NONE "blue" "#############################################"
fi
}
# Source zsh-custom-update to load var as env
if [ -f "${ZSH_CACHE_DIR}/.zsh-custom-update" ]
then
. "${ZSH_CACHE_DIR}/.zsh-custom-update"
# Check if update is needed (by date)
if [[ -z "$LAST_DATE" ]]
then
LAST_DATE=0
fi
date_diff=$(($(_current_date) - $LAST_DATE))
if [ $date_diff -gt $date_target ]
then
if [ "$DISABLE_UPDATE_PROMPT" = "true" ]
then
(upgrade_oh_my_zsh_custom)
else
_zsh_auto_update_log $BOLD "blue" "[Oh My Zsh] Would you like to check for custom plugin updates? [Y/n]: \c"
read -r line
if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]
then
(upgrade_oh_my_zsh_custom)
fi
fi
_update_zsh_custom_update
fi
else
_update_zsh_custom_update
fi
# Alias to call upgrade_oh_my_zsh before calling upgrade_oh_my_zsh_custom
alias upgrade_all_oh_my_zsh='omz update && upgrade_oh_my_zsh_custom'
unset -f _update_zsh_custom_update _current_date