-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu
executable file
·207 lines (185 loc) · 5.63 KB
/
u
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
# update system on arch, calls aur sep w extra util for aux programs (date-based) at end
# requires informant, pacman-contrib and either paru or yay
set -o pipefail
# set this to paru or yay
aur_helper="yay"
# counter for if pacdiff has been run yet
pacdiff_run=0 # dont change this
# get the current user to execute commands as
user="${SUDO_USER}"
# find the user's home dir for script locations
user_home="$(getent passwd "${SUDO_USER}" | cut -d':' -f6)"
# if you want extra updates to run on a timer, add a file to this line
last_date="${user_home}/bin/.last_date"
update_system() {
# run the main update
set -e
informant check
pacman -Syu
pacdiff -o >/tmp/pacnew.out
get_user_input \
":: Also update AUR packages? [Y/n] " \
"sudo -u ${user} ${aur_helper} -Syua" \
"Skipping AUR packages"
}
update_extras() {
# update extra programs and non-arch package managers
# if you don't want to use any of these, simply delete or comment-out the line
if [[ -s "${user_home}/bin/upgrade-zinit" ]]; then
sudo -u "${user}" zsh "${user_home}/bin/upgrade-zinit"
fi
if hash tldr >/dev/null; then
sudo -u "${user}" /usr/bin/env tldr --update |
sed 's/ cache/ tldr cache/'
fi
update_neovim
update_neovim_plugins
update_luarocks
update_pip
remove_orphans
clear_paccache
"${user_home}/bin/update-mirrors"
"${user_home}/bin/get-installed"
if [[ "$#" ]]; then
echo "$1" >"${user_home}/bin/.last_date"
printf -- '\nNext full update will run after %s.\n' "$2"
fi
}
update_luarocks() {
# update luarocks packages
if (($(luarocks list --outdated | wc -l) <= 4)); then
printf -- 'No luarocks package to upgrade.\n'
return
fi
printf -- 'Updating luarocks packages...\n'
for pkg in $(luarocks list --outdated | tail -n+5 | sed '/^\s*$/d' | sed -n 'p;n'); do
luarocks install "$pkg"
done
printf -- 'Luarocks packages upgraded.\n'
}
update_neovim() {
# update neovim
printf -- 'Updating neovim...\n'
(
cd "${user_home}/build/neovim/" && rm -rf "${user_home}/build/neovim/build"
make CMAKE_BUILD_TYPE=RelWithDebInfo && make install
)
printf -- 'Neovim successfully updated\n'
}
update_pip() {
# get the python version
python_ver="$(env pip --version | grep -Eo 'python[2-3]\.\b([0-9]|1[0-2])\b')"
# set the output location for installed packages at runtime
output_loc="${HOME}/.local/lib/${python_ver}/installed/$(date +'%Y-%m-%dT%H:%M:%S')"
# checks if output folder exists by removing the filename with substring extraction
if [[ ! -d "${output_loc:0:-20}" ]]; then
mkdir "${output_loc:0:-20}"
fi
# upgrade pip packages by listing and filtering outdated packages, then
# feeding them to pip with xargs
printf -- '%s\n' "Upgrading pip packages..."
env pip list --user -o |
grep -v '\^-e' |
tee "${output_loc} " |
grep -Ev 'sdist|--|Latest\s' |
cut -d' ' -f1 |
xargs -r -n1 env pip install --user -U &&
printf -- '\n%s\n' "Pip packages upgraded."
return 0
}
update_neovim_plugins() {
# set your vim plugin manager's commands here
printf -- 'Updating neovim plugins...\n'
sudo -u "${user}" nvim --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync'
printf -- 'Neovim plugins updated.\n'
}
remove_orphans() {
# remove orphaned pacman packages
if pacman -Qdtq >/dev/null; then
printf -- '\nOrphaned packages are:\n'
pacman -Qdt
get_user_input \
":: Remove orphan packages? [Y/n] " \
"pacman -Rns --noconfirm $(pacman -Qdtq)" \
"Leaving orphaned packages."
else
printf -- 'No orphaned packages.\n'
fi
}
clear_paccache() {
# get_user_input isn't in use because it borks the cache-clearing
printf -- 'Checking pacman cache for old packages...\n'
paccache -dvk2
read -rp ":: Clear pacman cache? [Y/n] " confirm
case "$confirm" in
y | Y)
paccache -rvk2 && printf -- 'Pacman cache cleared.\n'
;;
*)
printf -- 'Skipping pacman cache.\n'
;;
esac
}
check_last_date() {
# check if it's time to run update_extras
if [[ -z "${last_date}" ]]; then
# if the user doesn't want to make last_date file
get_user_input \
":: Update extra programs? " \
"update_extras" \
"Skipping extra updates."
elif [[ -n "${last_date}" ]] && [[ ! -s "${last_date}" ]]; then
# if variable is set but file doesn't exist, create it
echo '0000-00-00' >"${last_date}"
elif [[ -s "${last_date}" ]]; then
# check date to see if it's been a week since the last update
# TODO: swap last_date and last_date_file
local last_date_file current_date next_date future_date
last_date_file="$(cat "${last_date}")"
current_date="$(date "+%Y-%m-%d")"
next_date="$(env python3 "${user_home}/bin/week_from.py" "${last_date_file}")"
future_date="$(env python3 "${user_home}/bin/week_from.py" "${current_date}")"
if (("${current_date//-/}" >= "${next_date//-/}")); then
update_extras "${current_date}" "${future_date}"
fi
fi
}
check_root() {
# exit program if not run as root
set -o pipefail
if (("$(id -u)" != 0)); then
tput setaf 1
printf -- 'Error! Run as root.\nCancelling...\n' >&2
tput sgr0
exit 1
fi
}
get_user_input() {
# get user confirmation for an action
# args are: 1 - prompt, 2 - action if yes, 3 - action if no
read -rp "$1" confirm
case "$confirm" in
y | Y)
$2
;;
*)
printf -- '%s\n' "$3"
;;
esac
}
print_pacdiff() {
# print out any pacnew files
if ((pacdiff_run == 0)) && [[ -s "/tmp/pacnew.out" ]]; then
printf -- '\nPacnew files:\n%s\n' "$(cat /tmp/pacnew.out)"
pacdiff_run=1
fi
}
main() {
check_root
update_system
check_last_date
print_pacdiff
}
main
exit 0