This repository has been archived by the owner on Apr 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·264 lines (227 loc) · 7.22 KB
/
build.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
#!/bin/sh
# LICENSE: MIT license, see LICENSE.md
#
# Creates a new release of xenforo-threema-gateway.
#
CURR_DIR="$( pwd )"
SOURCE_DIR="$CURR_DIR/src"
PHP_SDK_SOURCE_DIR="$CURR_DIR/src/library/ThreemaGateway/threema-msgapi-sdk-php"
SCRIPT_DIR="$CURR_DIR/scripts"
DOC_DIR="$CURR_DIR/docs"
LANG_DIR="$CURR_DIR/languages"
BUILD_DIR="$CURR_DIR/build"
RELEASE_DIR="$CURR_DIR/release"
JS_DIR="upload/js/ThreemaGateway"
DEBUG_MARKER=" /* BUILD ADJUST */"
DEBUG_MARKED=" /* ADJUSTED AT BUILD TIME */"
# functions
show_help() {
echo "This is the built script for xenforo-threema-gateway.
$0 [-h|-?|--help]
$0 [[-p|--sdkPhar] 0/1] [[-m|--copydoc] 0/1] [[-d|--debug] 0/1]
[[-l|--languages] language list] [[-g|--genArchives] 0/1]
[[-o|--addHashes] 0/1] [[-t|--date] date]
-h|-?|--help Show this help.
-p|--sdkPhar Build the PHP-SDK phar.
-m|--copydoc Additionally copy the doc files.
-d|--debug Build a debug version instead of a productive one.
-l|--languages After this parmeter specify the languages, which should be
included, separated by spaces. So you need to quote the
string. (e.g. 'en de' for English and German)
-g|--genArchives Generate zip and tar.gz files (default: 1)
-j|--minimizeJs Minimize JS files using UglifyJs (default: 1)
If a debug version is created, the generated files are not
used by default.
-o|--addHashes Add file hashes to built, so health checker of XenForo
can be used. (default: 1, requires XenForo to be installed)
-t|--date Pass the current date, which should be replaced by in the
files. Useful for reproducible builts. (default: current date)
"
return;
}
# default parameters
languages=''
genPhpSdkPhar=1
copyDoc=0
debugMode=0
addHashes=1
genArchives=1
minimizeJs=1
varDate="$( date +%F )"
# parse parameters
while true; do
param="$1"
paramValue='1' # by default: assume true
# if next param is not a new param it is a param value
if [ "$2" != "" ] && [ "$( echo $2 | sed 's/^-//g' )" = "$2" ]; then
paramValue="$2"
shift
fi
# whitelist of available parameters
case "${param}" in
1)
# always ignore true parameters
;;
-h|-\?|--help)
show_help
exit 0
;;
-p|--sdkPhar)
genPhpSdkPhar="${paramValue}"
;;
-m|--copydoc)
copyDoc="${paramValue}"
;;
-d|--debug)
debugMode="${paramValue}"
;;
-l|--languages)
languages="${paramValue}"
;;
-a|--genArchives)
genArchives="${paramValue}"
;;
-o|--addHashes)
addHashes="${paramValue}"
;;
-j|--minimizeJs)
minimizeJs="${paramValue}"
;;
-t|--date)
varDate="${paramValue}"
;;
--|'') # end processing
break
;;
*) # unknown command
# show warning (currently even if file path was passed as we do not
# accept anyone)
echo "Warning: The parameter '${param}' is unknown and will be ignored."
echo "Warning: Any further parameters will be ignored."
break;
;;
esac
# shift to test next param
shift
done
# check params
if [ "${languages}" = "1" ]; then
echo "Invalid langauge parameter value. This cannot be true."
exit 1
fi
if [ "${varDate}" = "1" ]; then
echo "Invalid date parameter value. This cannot be true."
exit 1
fi
# get user input
if [ "$XENFORO_DIR" = "" ]; then
read -p "Please enter the dir to XenForo: " XENFORO_DIR
if [ "${XENFORO_DIR}" = "" ]; then
exit 1
fi
# make sure to export the variable
export XENFORO_DIR
fi
# get version number
versionDefault=$( git describe --abbrev=0 --tags )
read -p "Version number [${versionDefault}]: " version
version=${version:-$versionDefault}
# copy files
echo "Clean & create dir…"
if [ -d "$BUILD_DIR" ]; then
rm -rf "$BUILD_DIR"
fi
mkdir -p "$BUILD_DIR"
echo "Copy add-on XML…"
cp -a "addon-ThreemaGateway.xml" "$BUILD_DIR"
echo "Copy README.txt…"
cp -a "$DOC_DIR/templates/ArchiveReadme.txt" "$BUILD_DIR/README.txt"
echo "Copy LICENSE.md…"
cp -a "LICENSE.md" "$BUILD_DIR"
if [ ${genPhpSdkPhar} = 1 ]; then
echo "Generate PHP-SDK phar…"
cd "$PHP_SDK_SOURCE_DIR" || exit
scripts/buildPhar.php
cd "$CURR_DIR"
fi
echo "Copy source files…"
mkdir -p "$BUILD_DIR/upload"
rsync -a "$SOURCE_DIR/" "$BUILD_DIR/upload/"
if [ "$languages" ]; then
mkdir -p "$BUILD_DIR/languages"
fi
if [ $copyDoc = 1 ]; then
echo "Copy doc files…"
mkdir -p "$BUILD_DIR/docs"
rsync -a "$DOC_DIR/" "$BUILD_DIR/docs/"
fi
# complete language attributions
langFiles=''
for lang in $languages; do
case $lang in
en)
lang="en_US"
;;
de)
lang="de_DE_du de_DE_Sie"
;;
esac
for langVariant in $lang; do
# convert to full file names
case $langVariant in
en_US) langFiles="${langFiles} language-English-(US)";;
de_DE_du) langFiles="${langFiles} language-Deutsch-[Du]";;
de_DE_Sie) langFiles="${langFiles} language-Deutsch-[Sie]";;
*) langFiles="${langFiles} ${langVariant}";;
esac
done
done
for langFile in $langFiles; do
cp -a "$LANG_DIR/$langFile.xml" "$BUILD_DIR/languages"
done
# replace variables
echo "Replace variables…"
# for Readme or so…
rpl -q -R -d '{{CURR_VERSION}}' "${version}" "$BUILD_DIR"
rpl -q -R -d '{{CURR_GIT_HASH}}' "$( git rev-parse HEAD )" "$BUILD_DIR"
rpl -q -R -d '{{CURR_DATE}}' "${varDate}" "$BUILD_DIR"
# for source code debug
if [ $debugMode = 1 ]; then
rpl -q -R -d "DEBUG = false$DEBUG_MARKER" "DEBUG = true$DEBUG_MARKED" "$BUILD_DIR"
else
rpl -q -R -d "DEBUG = true$DEBUG_MARKER" "DEBUG = false$DEBUG_MARKED" "$BUILD_DIR"
fi
# minify JS files
if [ $minimizeJs = 1 ]; then
for jsFile in "$BUILD_DIR"/"$JS_DIR"/*; do
jsFilename="${jsFile%%.*}"
# RegExp for comments: https://regex101.com/r/sP0bU3/2
uglifyjs \
--compress \
--mangle \
--screw-ie8 \
--output "${jsFilename}.min.js" \
--comments "/MIT license|Copyright|@preserve|@license|@source/i" \
-- "${jsFile}"
# Note: "define" does not work for some reason (so we could do define DEBUG=true/false)...
# rename files to make the minimized version the default one if debug mode is disabled
if [ $debugMode = 0 ]; then
mv "${jsFile}" "${jsFilename}.full.js"
mv "${jsFilename}.min.js" "${jsFile}"
fi
done
fi
# finalize files
if [ $addHashes = 1 ]; then
echo "Generating file hashes…"
php "$SCRIPT_DIR/GenFileHashes.php" "$BUILD_DIR/upload"
fi
#generate archives
if [ ${genArchives} = 1 ]; then
mkdir -p "$RELEASE_DIR"
echo "Generating archives…"
cd "$BUILD_DIR" || exit
7z a -mx=9 "$RELEASE_DIR/xenforo-threema-gateway_v${version}.zip" "./*" > /dev/null
tar -caz --owner=rugk -f "$RELEASE_DIR/xenforo-threema-gateway_v${version}.tar.gz" -- *
cd ..
fi