-
Notifications
You must be signed in to change notification settings - Fork 4
/
autoupdate-zgen.plugin.zsh
executable file
·101 lines (91 loc) · 3.5 KB
/
autoupdate-zgen.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
# Copyright 2014-2021 Joe Block <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
_zgen-check-interval() {
now=$(date +%s)
if [[ ! -f ~/"${1}" ]]; then
# We've never run, set the last run time to the dawn of time, or at
# least the dawn of posix time.
echo 0 > ~/"${1}"
fi
last_update=$(cat ~/"${1}")
interval=$(expr ${now} - ${last_update})
echo "${interval}"
}
_zgen-check-for-updates() {
if [ -z "${ZGEN_PLUGIN_UPDATE_DAYS}" ]; then
ZGEN_PLUGIN_UPDATE_DAYS=7
fi
if [ -z "${ZGEN_SYSTEM_UPDATE_DAYS}" ]; then
ZGEN_SYSTEM_UPDATE_DAYS=7
fi
if [ -z "${ZGEN_SYSTEM_RECEIPT_F}" ]; then
if [ -n "${ZGEN_DIR}" ]; then
# Since the $ZGEN_{SYSTEM|PLUGIN}_RECEIPT_F variables are with
# respect to the home directory but $ZGEN_DIR is an absolute path,
# $ZGEN_{SYSTEM|PLUGIN}_RECEIPT_F is set to $ZGEN_DIR (if it is defined)
# with the $HOME directory as the prefix removed
# (That's what the "${${ZGEN_DIR}#${HOME}}" syntax does: it removes the
# "$HOME" prefix from "$ZGEN_DIR")
ZGEN_SYSTEM_RECEIPT_F="${${ZGEN_DIR}#${HOME}}/.zgen_system_lastupdate"
else
ZGEN_SYSTEM_RECEIPT_F='.zgen_system_lastupdate'
fi
fi
if [ -z "${ZGEN_PLUGIN_RECEIPT_F}" ]; then
if [ -n "${ZGEN_DIR}" ]; then
ZGEN_PLUGIN_RECEIPT_F="${${ZGEN_DIR}#${HOME}}/.zgen_plugin_lastupdate"
else
ZGEN_PLUGIN_RECEIPT_F='.zgen_plugin_lastupdate'
fi
fi
local day_seconds=$(expr 24 \* 60 \* 60)
local system_seconds=$(expr "${day_seconds}" \* "${ZGEN_SYSTEM_UPDATE_DAYS}")
local plugins_seconds=$(expr ${day_seconds} \* ${ZGEN_PLUGIN_UPDATE_DAYS})
local last_plugin=$(_zgen-check-interval ${ZGEN_PLUGIN_RECEIPT_F})
local last_system=$(_zgen-check-interval ${ZGEN_SYSTEM_RECEIPT_F})
if [[ ${last_plugin} -gt ${plugins_seconds} ]]; then
if [[ ! -z "${ZGEN_AUTOUPDATE_VERBOSE}" ]]; then
echo "It has been $(expr ${last_plugin} / $day_seconds) days since your zgen plugins were updated"
echo "Updating plugins"
fi
zgen update
date +%s >! ~/${ZGEN_PLUGIN_RECEIPT_F}
fi
if [[ ${last_system} -gt ${system_seconds} ]]; then
if [[ ! -z "${ZGEN_AUTOUPDATE_VERBOSE}" ]]; then
echo "It has been $(expr ${last_plugin} / ${day_seconds}) days since your zgen was updated"
echo "Updating zgen..."
fi
zgen selfupdate
date +%s >! ~/${ZGEN_SYSTEM_RECEIPT_F}
fi
}
# Don't update if we're running as different user than whoever
# owns ~/.zgen. This prevents sudo runs from leaving root-owned
# files & directories in ~/.zgen that will break future updates
# by the user.
#
# Use ls and awk instead of stat because stat has incompatible arguments
# on linux, macOS and FreeBSD.
local zgen_owner=$(ls -ld ${ZGEN_DIR:-$HOME/.zgen} | awk '{print $3}')
if [[ "$zgen_owner" == "$USER" ]]; then
zmodload zsh/system
lockfile=~/.zgen_autoupdate_lock
touch "$lockfile"
if ! which zsystem &> /dev/null || zsystem flock -t 1 "$lockfile"; then
_zgen-check-for-updates
command rm -f "$lockfile"
fi
unset lockfile
fi