-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-plugin
executable file
·259 lines (220 loc) · 6.15 KB
/
install-plugin
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
#!/usr/bin/env bash
pluginDir="./dist"
bdConfigDir="$HOME/.config/BetterDiscord/plugins/"
red=`tput setaf 1`
green=`tput setaf 2`
orange=`tput setaf 3`
bold=`tput bold`
reset=`tput sgr0`
# Dependency check
if ! command -v jq &> /dev/null
then
echo "${bold}${red}ERROR: I need jq to work properly!"
fi
# First build all the plugins
function buildPlugins {
echo "${bold}${orange}Building plugins...${reset}"
if command -v yarn &> /dev/null
then
yarn
yarn build
else
if command -v npm &> /dev/null
then
npm install
npm run-script build
else
echo "${bold}${red}ERROR: Either yarn or npm must be installed!"
exit 1
fi
fi
}
buildPlugins
# Ensure that BetterDiscord is installed
if [ ! -d $bdConfigDir ]
then
echo "${bold}${red}ERROR: ${reset}BetterDiscord does not seem to be installed."
echo "Please install BetterDiscord, or make sure that ${bold}$bdConfigDir${reset} exists."
exit 1
fi
function printUsage {
echo
echo "${orange}USAGE:${reset}"
echo -e "\t$0 [FLAGS] <PLUGINS...>"
echo
echo "${orange}FLAGS:${reset}"
echo -e "\t${green}-h${reset}, ${green}--help${reset}\tPrints help information"
echo -e "\t${green}-l${reset}, ${green}--list${reset}\tLists available plugins"
echo -e "\t${green}-a${reset}, ${green}--all${reset}\tInstalls all available plugins"
echo -e "\t${green}-f${reset}, ${green}--force${reset}\tForce recompilation of plugins"
echo -e "\t${green}-r${reset}, ${green}--remove${reset}\tUninstalls plugins instead of installing"
}
function printHelp {
echo "${green}Tmpim BD-Plugin Installer${reset}"
echo "Installer script for the tmpim collection of BetterDiscord plugins."
printUsage
}
function printNoPlugins {
echo "${red}${bold}ERROR:${reset} No plugins specified for action."
echo "Try ${bold}$0 --help${reset} for more information"
}
function getPluginInfo {
filename=$1
pluginName=$(cat $filename | grep '* @name' | head -n1 | awk '{print $3}')
pluginDesc=$(cat $filename | grep '* @description' | head -n1 | awk '{for (i=3; i<=NF; i++) printf("%s ", $i)}')
if [ -z "$pluginName" ]
then
# Maybe it uses the META comment syntax
meta=$(cat $filename | grep '//META' | head -n1 | sed 's;//META;;' | sed 's;*//\s*$;;')
pluginName=$(echo "$meta" | jq '.name')
pluginDesc=$(echo "$meta" | jq '.description')
pluginName="${pluginName:1:-1}"
pluginDesc="${pluginDesc:1:-1}"
fi
if [ -z $pluginDesc ] || [ "$pluginDesc" == "ul" ]
then
pluginDesc=$(cat $filename | grep -Eo 'getDescription\s*\(\).+' | grep -Po '\\?"\K(.*?)(?=\\?")' | head -n1)
fi
}
function listPlugins {
echo "${bold}${green}Plugins:${reset}"
plugins=$(find $pluginDir -name "*plugin.js")
while read -r line; do
getPluginInfo $line
echo "${bold}$pluginName${reset} - $pluginDesc"
done <<< "$plugins"
}
function pluginFindRule {
if [ -z "$filename" ]
then
if command -v rg &> /dev/null
then
filename=$(rg "$1" $pluginDir)
else
filename=$(grep -rnw $pluginDir -e "$1")
fi
fi
}
function findPlugin {
pluginName=$1
unset filename
pluginFindRule "//META\\{\"name\":\"$pluginName\""
pluginFindRule "class $pluginName"
if [ -z "$filename" ]
then
echo "${bold}${red}ERROR:${reset} Cannot find plugin ${bold}$pluginName${reset}."
installFailed=1
return
fi
filename=$(echo "$filename" | sed 's/:.*//')
}
removePlugins=0
function installPlugin {
getPluginInfo $1
if [ $removePlugins -eq 0 ]
then
echo -n "${orange}Installing Plugin: ${reset}${bold}$pluginName${reset}... "
cp $1 $bdConfigDir 2> /dev/null
else
echo -n "${red}Removing Plugin: ${reset}${bold}$pluginName... "
rm $bdConfigDir/$(basename $1) 2> /dev/null
fi
if [ $? -eq 0 ]
then
echo "${bold}${green}OK${reset}"
else
echo "${bold}${red}ERROR${reset}"
installFailed=1
return 1
fi
pluginNameLower=$(echo "$pluginName" | tr '[:upper:]' '[:lower:]')
if [ -d "./assets/$pluginNameLower" ]
then
if [ $removePlugins -eq 0 ]
then
echo -n "${orange}Installing${reset} ${bold}$pluginName ${reset}${orange}Assets:${reset}... "
mkdir -p $bdConfigDir/assets
cp -r ./assets/$pluginNameLower $bdConfigDir/assets/ 2> /dev/null
else
echo -n "${red}Removing${reset} ${bold}$pluginName ${reset}${red}Assets:${reset}... "
rm -r $bdConfigDir/assets/$pluginNameLower/ 2> /dev/null
fi
if [ $? -eq 0 ]
then
echo "${bold}${green}OK${reset}"
else
echo "${bold}${red}ERROR${reset}"
installFailed=1
return 1
fi
fi
}
if [ -z $1 ]
then
printNoPlugins
echo
exit 1
fi
options=$(getopt -o hlarf --long help --long list --long all --long remove --long force -- "$@")
[ $? -eq 0 ] || {
printUsage
exit 1
}
installFailed=0
eval set -- "$options"
while true; do
case "$1" in
-h | --help)
printHelp
exit 0
;;
-l | --list)
listPlugins
exit 0
;;
-a | --all)
plugins=$(find $pluginDir -name "*plugin.js")
while read -r line; do
installPlugin $line
done <<< "$plugins"
if [ $installFailed -ne 0 ]
then
echo "Some plugins failed to install/remove."
fi
echo
exit $installFailed
;;
-r | --remove)
removePlugins=1
echo "Install mode set to ${red}REMOVE${reset}."
echo
;;
-f | --force)
rm -rf $pluginDir
buildPlugins
rebuilt=1
;;
--)
shift
break
;;
esac
shift
done
if [ -z $1 ] && [ -z $rebuilt ]
then
printNoPlugins
fi
for plugin in "$@"
do
findPlugin "$plugin"
if [ ! -z "$filename" ]
then
installPlugin "$filename"
fi
done
if [ $installFailed -ne 0 ]
then
echo "Some plugins failed to install."
fi
echo # Trailing newline