forked from cyfrost/install-gnome-extensions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-gnome-extensions.sh
executable file
·307 lines (248 loc) · 9.75 KB
/
install-gnome-extensions.sh
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
#################################################################
# #
# GNOME Shell Extension Installer v1.2.1 #
# #
# A simple (scriptable) way to install GNOME Shell Extensions! #
# #
# Author: Cyrus Frost #
# License: MIT #
# #
# https://github.com/ToasterUwU/install-gnome-extensions #
# #
#################################################################
#vars
script_revision="v1.3.0"
args_count="$#"
dependencies=(wget curl jq tput sed egrep sed awk gnome-shell cut basename)
deps_install_apt="sudo apt install -y wget curl jq sed"
deps_install_dnf="sudo dnf install -y wget curl jq sed"
EXTENSIONS_TO_INSTALL=()
OVERWRITE_EXISTING=false
ENABLE_ALL=false
INSTALLED_EXT_COUNT=''
INSTALLED_EXTs=''
# message colors.
info_text_blue=$(tput setaf 7)
normal_text=$(tput sgr0)
error_text=$(tput setaf 1)
status_text_yellow=$(tput setaf 3)
# Bail immediately if running as root.
# function CheckIfRunningAsRoot() {
# if [ "$(id -u)" = 0 ]; then
# printf "
# ${error_text}There was a error message here, but using sudo works just fine.. so i will comment this part out
# ${normal_text}"
# exit 1
# fi
# }
# CheckIfRunningAsRoot
# Trap SIGINT and SIGTERM.
function _term() {
printf "\n\n${normal_text}"
trap - INT TERM # clear the trap
kill -- -$$
}
# Trap SIGINT and SIGTERM for cleanup.
trap _term INT TERM
# This function can check for binaries/commands to be available in Env PATH and report otherwise.
function CheckDependencies() {
# echo -en "\n${info_text_blue}Checking dependencies...${normal_text}";
dependencies=("$@")
for name in "${dependencies[@]}"; do
command -v "$name" >/dev/null 2>&1 || {
echo -en "${error_text}\n[Error] Command not found: \"$name\"${normal_text}"
deps=1
}
done
[[ $deps -ne 1 ]] || {
echo -en "${error_text}\n\nOne or more dependencies is unavailable. Please make sure the above commands are available and re-run this script.\n\n${status_text_yellow}For Ubuntu and other Debian based Distros, try: $deps_install_apt\n\nFor Fedora and Fedora based Distros, try: $deps_install_dnf\n\n${normal_text}"
exit 1
}
}
# Fail if dependencies unmet.
CheckDependencies "${dependencies[@]}"
function confirm_action() {
while true; do
printf "\n${normal_text}"
read -p "$1" -n 1 yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
*) printf "\nPlease answer with 'y' or 'n'." ;;
esac
done
}
# check if current (active) desktop instance is GNOME.
function IsEnvGNOME() {
if [ "$XDG_CURRENT_DESKTOP" = "" ]; then
desktop=$(echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/')
else
desktop=$XDG_CURRENT_DESKTOP
fi
desktop=${desktop,,}
if [[ $desktop == *"gnome"* ]]; then
return 0
else
return 1
fi
}
function install_extension() {
archive_file_name="$1"
gnome-extensions install "$archive_file_name" -f >/dev/null 2>&1
}
function enable_extension() {
ext_uuid="$1"
gnome-extensions enable "$ext_uuid" >/dev/null 2>&1
}
function disable_extension() {
ext_uuid="$1"
gnome-extensions disable "$ext_uuid" >/dev/null 2>&1
}
function get_installed_extensions_list() {
user_extensions_path="/home/$USER/.local/share/gnome-shell/extensions"
array=($(ls -l $user_extensions_path --time-style=long-iso | egrep '^d' | awk '{print $8}'))
ext_list=$(printf "'%s'," "${array[@]}")
ext_list=${ext_list%,}
INSTALLED_EXT_COUNT="${#array[@]}"
INSTALLED_EXTs=$(printf '%s\n' "${array[@]}")
}
function install_shell_extensions() {
for ext_id in "${EXTENSIONS_TO_INSTALL[@]}"; do
request_url="https://extensions.gnome.org/extension-info/?pk=$ext_id&shell_version=$GNOME_SHELL_VERSION"
http_response="$(curl -s -o /dev/null -I -w "%{http_code}" "$request_url")"
if [ "$http_response" = 404 ]; then
printf "\n${error_text}Error: No extension exists matching the ID: $ext_id and GNOME Shell version $GNOME_SHELL_VERSION (Skipping this).\n"
continue
fi
printf "${normal_text}\n"
ext_info="$(curl -s "$request_url")"
extension_name="$(echo "$ext_info" | jq -r '.name')"
direct_dload_url="$(echo "$ext_info" | jq -r '.download_url')"
ext_uuid="$(echo "$ext_info" | jq -r '.uuid')"
ext_version="$(echo "$ext_info" | jq -r '.version')"
ext_homepage="$(echo "$ext_info" | jq -r '.link')"
ext_description="$(echo "$ext_info" | jq -r '.description')"
download_url="https://extensions.gnome.org"$direct_dload_url
printf "${status_text_yellow}\nDownloading and installing extension \"$extension_name\"${normal_text}"
printf "${info_text_blue}"
printf "\nDescription: $ext_description"
printf "\nExtension ID: $ext_id"
printf "\nExtension Version: v$ext_version"
printf "\nHomepage: https://extensions.gnome.org$ext_homepage"
printf "\nUUID: \"$ext_uuid\""
if [ -d "$target_installation_dir" ] && [ "$OVERWRITE_EXISTING" = "false" ]; then
confirm_action "${normal_text}This extension is already installed. Do you want to overwrite it? (y/n): " || continue
fi
printf "\n${info_text_blue}Please wait...\n"
filename="$(basename "$download_url")"
wget -q "$download_url"
install_extension "$filename"
rm "$filename"
if [ ! "$ENABLE_ALL" = "false" ]; then
enable_extension "$ext_uuid"
fi
printf "${info_text_blue}Done!\n${normal_text}"
done
printf "\n"
}
# Check if arg is number.
function IsNumber() {
re='^[0-9]+$'
if [[ "$1" =~ $re ]]; then
return 0
fi
return 1
}
function print_usage() {
print_banner
printf "
Usage: ./install-gnome-extensions.sh [options] <extension_ids> | [links_file]
Options:
-e, --enable Enable extension after installing it.
-l. --list Lists the UUIDs of installed extensions.
-f, --file Specify a file containing extension links to install.
-h, --help Display this help message.
Example usages:
---------------
1) ./install-gnome-extensions.sh 6 8 19 --enable
Installs and enables extensions with IDs 6, 8, and 19.
2) ./install-gnome-extensions.sh -e --file links.txt
Installs and enables the extensions from the URLs specified in "links.txt" file.
"
}
function print_banner() {
printf "${normal_text}
===========================================================
GNOME Shell Extensions Installer $script_revision
A simple (scriptable) way to install GNOME Shell extensions.
https://github.com/ToasterUwU/install-gnome-extensions
===========================================================\n"
}
function trim_file() {
file="$1"
sed -i '/^[[:blank:]]*$/ d' $file && awk '{$1=$1};1' $file >tmp && mv tmp $file
}
function install_exts_from_links_file() {
file="$1"
if [ ! -f "$file" ] || [ ! -s "$file" ]; then
printf "\n${error_text}Error: Supplied argument (\"$1\") is either not a valid file or is empty.${normal_text}\n\nPlease gather all extension links in a text file (line-by-line) and try again.\n\nSample usage: ./install-gnome-extensions --file links.txt\n\n"
exit 1
fi
trim_file $file
printf "\nParsing file \"$file\" for extension links...\n"
while IFS="" read -r p || [ -n "$p" ]; do
url="$(echo "$p" | sed '/^[[:space:]]*$/d')"
ext_id="$(echo "$url" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | awk '{print $1;}')"
IsNumber "$ext_id" && EXTENSIONS_TO_INSTALL+=($ext_id) || printf "\n${error_text}Error: Invalid URL: $url (Skipping this).${normal_text}"
done <$file
printf "\n"
}
function begin_install() {
exts_list="$(printf '%s, ' "${EXTENSIONS_TO_INSTALL[@]}")"
exts_list=${exts_list%, }
print_banner
printf "\n${info_text_blue}[Info] Detected GNOME Shell version: $GNOME_SHELL_VERSION\n\nInstalling $extensions_count extensions ($exts_list)...\n${normal_text}"
install_shell_extensions
printf "\n${normal_text}Complete!\n\n"
IsEnvGNOME || printf "${normal_text}Please login to GNOME desktop to see the installed/enabled extensions.\n\n"
}
# Obtain GNOME Shell version.
GNOME_SHELL_VERSION="$(gnome-shell --version | cut --delimiter=' ' --fields=3 | cut --delimiter='.' --fields=1,2)"
while test $# -gt 0; do
case "$1" in
-e | --enable)
ENABLE_ALL=true
;;
-u | --update)
UPDATE=true
;;
-o | --overwrite)
OVERWRITE_EXISTING=true
;;
-h | --help)
print_usage
exit 0
;;
-l | --list)
get_installed_extensions_list
printf "\n============================\nInstalled extensions (UUIDs)\n============================\n\n$INSTALLED_EXTs\n\n$INSTALLED_EXT_COUNT extensions are installed.\n\nDone!\n\n"
exit 0
;;
-f | --file)
install_exts_from_links_file "$2"
;;
esac
IsNumber "$1" && EXTENSIONS_TO_INSTALL+=($1)
shift
done
extensions_count="${#EXTENSIONS_TO_INSTALL[@]}"
if [ "$args_count" -eq 0 ]; then
printf "\n${error_text}Invalid usage.\n${normal_text}"
print_usage
elif [ "$extensions_count" -eq 0 ]; then
printf "\n${error_text}Error: Could not find any valid extension IDs or URLs for installation.\n${normal_text}\n"
exit 1
else
begin_install
fi