-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·277 lines (239 loc) · 8.01 KB
/
build
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
#!/bin/bash -eu
shopt -s extglob
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR"
. "$DIR"/init.sh
if [ ! -d "$BUILD_DIR" ]; then
echo "$BUILD_DIR doesn't exist"
exit 1
fi
function usage {
cat >&2 <<DONE
Usage: $0 -p PLATFORMS [-v VERSION] [-d]
Options
-p platforms (b=browserExt, s=Safari, k=bookmarklet, a=all)
-v version
-d debug build
DONE
exit 1
}
BUILD_BROWSEREXT=0
BUILD_SAFARI=0
BUILD_BOOKMARKLET=0
DEV=0
VERSION=""
while getopts "p:v:d" opt; do
case $opt in
p)
if [ "$OPTARG" = "a" ]; then
BUILD_BROWSEREXT=1
BUILD_SAFARI=1
BUILD_BOOKMARKLET=1
else
for i in `seq 0 1 $((${#OPTARG}-1))`
do
case ${OPTARG:i:1} in
b) BUILD_BROWSEREXT=1;;
s) BUILD_SAFARI=1;;
k) BUILD_BOOKMARKLET=1;;
*)
echo "$0: Invalid platform option ${OPTARG:i:1}"
usage
;;
esac
done
fi
;;
v)
VERSION="$OPTARG"
;;
d)
DEV=1
;;
*)
usage
;;
esac
shift $((OPTIND-1)); OPTIND=1
done
if [[ $BUILD_BROWSEREXT -eq 0 ]] && [[ $BUILD_SAFARI -eq 0 ]] && [[ $BUILD_BOOKMARKLET -eq 0 ]]; then
usage
fi
PROCESS_CMD="browserify -t babelify"
rm -rf "$BUILD_DIR"/*
mkdir -p "$BUILD_DIR/tmp/common"
if [ $BUILD_BROWSEREXT -eq 1 ]; then
mkdir "$BUILD_DIR/tmp/browserExt"
mkdir "$BUILD_DIR/browserExt"
fi
if [ $BUILD_SAFARI -eq 1 ]; then
mkdir "$BUILD_DIR/tmp/safari"
mkdir "$BUILD_DIR/safari"
fi
if [ $BUILD_BOOKMARKLET -eq 1 ]; then
mkdir "$BUILD_DIR/tmp/bookmarklet"
mkdir "$BUILD_DIR/bookmarklet"
fi
if [[ $BUILD_BROWSEREXT -eq 1 ]] || [[ $BUILD_SAFARI -eq 1 ]]; then
BUILD_EXTENSION=1
else
BUILD_EXTENSION=0
fi
# Stage common files used in all versions
rsync -r --include '**/' --exclude 'tooltip.*' --include '*.js' --exclude '*' src/common/ "$BUILD_DIR/tmp/common/"
cp .babelrc package.json "$BUILD_DIR/tmp/common/"
ln -s "$(pwd)/node_modules" "$BUILD_DIR/tmp/common/node_modules"
# Stage common files used only in extensions
if [[ $BUILD_EXTENSION -eq 1 ]]; then
sass --sourcemap=none src/common/content/tooltip.scss "$BUILD_DIR/tmp/common/content/first_mention.css"
fi
# Copy common files to staging
for dest in browserExt safari bookmarklet; do
if [[ $dest = "browserExt" ]] && [[ $BUILD_BROWSEREXT -ne 1 ]]; then continue; fi
if [[ $dest = "safari" ]] && [[ $BUILD_SAFARI -ne 1 ]]; then continue; fi
if [[ $dest = "bookmarklet" ]] && [[ $BUILD_BOOKMARKLET -ne 1 ]]; then continue; fi
rsync -a "$BUILD_DIR/tmp/common/" "$BUILD_DIR/tmp/$dest/"
done
# Copy extension files
for dest in browserExt safari; do
if [[ $dest = "browserExt" ]] && [[ $BUILD_BROWSEREXT -ne 1 ]]; then continue; fi
if [[ $dest = "safari" ]] && [[ $BUILD_SAFARI -ne 1 ]]; then continue; fi
# Copy external files straight to destination
cp deps/xregexp/xregexp-all.js "$BUILD_DIR/$dest/xregexp.js"
cp src/common/content/tooltip.min.js "$BUILD_DIR/$dest/"
cp "$BUILD_DIR/tmp/$dest/content/first_mention.css" "$BUILD_DIR/$dest/"
cp src/extension/installed.html "$BUILD_DIR/$dest/"
cp src/extension/installed.js "$BUILD_DIR/$dest/"
cp src/extension/demo.html "$BUILD_DIR/$dest/"
# Copy extension-only files to staging
rsync -r src/extension/ "$BUILD_DIR/tmp/$dest/background/"
# Overlay version-specific files
cp src/$dest/firstMention.js "$BUILD_DIR/tmp/$dest/background/"
rsync -r --exclude=firstMention.js src/$dest/ "$BUILD_DIR/tmp/$dest/background/"
# Add browserify-shim config to have extension use XRegExp global, which allows use of original,
# unmodified file for review purposes
cat package.json package-extension.json | jq -s add > "$BUILD_DIR/tmp/$dest/package.json"
# Set Babel environment
if [[ $dest = "browserExt" ]]; then
CMD_ENV=modern
IS_BROWSEREXT=true
IS_SAFARI=false
elif [[ $dest = "safari" ]]; then
CMD_ENV=compat
IS_BROWSEREXT=false
IS_SAFARI=true
fi
# Allow code for other environments to be removed with unreachable-branch-transform
patterns=(
"FirstMention\.isExtension/true"
"FirstMention\.isBrowserExt/$IS_BROWSEREXT"
"FirstMention\.isSafari/$IS_SAFARI"
"FirstMention\.isBookmarklet/false"
"FirstMention\.cacheEnabled/false"
"FirstMention\.isiOS/false"
)
# If not dev mode, remove console.log() calls
if [ $DEV -eq 0 ]; then
patterns+=('^\s*(\/\/\s*)?console.log\(.+\n$/')
fi
for pattern in "${patterns[@]}"; do
perl -pi -e "s/$pattern/g" "$BUILD_DIR"/tmp/$dest/+(content|background)/*.js
done
# Build background and content bundles
BABEL_ENV=$CMD_ENV $PROCESS_CMD \
-t unreachable-branch-transform \
-x xregexp \
"$BUILD_DIR/tmp/$dest/background/background.js" \
> "$BUILD_DIR/$dest/background.js"
BABEL_ENV=$CMD_ENV $PROCESS_CMD \
-t unreachable-branch-transform \
"$BUILD_DIR/tmp/$dest/content/content.js" \
> "$BUILD_DIR/$dest/content.js"
# Word list
BABEL_ENV=$CMD_ENV $PROCESS_CMD \
--standalone WordBlacklist \
"$BUILD_DIR/tmp/$dest/background/wordBlacklist.js" \
> "$BUILD_DIR/$dest/wordBlacklist.js"
done
# browserExt-only files
if [ $BUILD_BROWSEREXT -eq 1 ]; then
# Copy images
mkdir "$BUILD_DIR/browserExt/icons"
for size in 32 48 96 128 256; do
cp "images/icon_$size.png" "$BUILD_DIR/browserExt/icons/"
done
mv "$BUILD_DIR/browserExt" "$BUILD_DIR/chrome"
rsync -a "$BUILD_DIR/chrome/" "$BUILD_DIR/firefox/"
cp src/browserExt/manifest.json "$BUILD_DIR/chrome/"
# Add 'applications' key for Firefox
cat src/browserExt/manifest.json src/firefox/manifest-include.json | jq -s add > "$BUILD_DIR/firefox/manifest.json"
# Set version in manifest; defaults to 0.0
if [ -n "$VERSION" ]; then
for dest in chrome firefox; do
cat "$BUILD_DIR/$dest/manifest.json" | jq ".version=\"$VERSION\"" > "$BUILD_DIR/$dest/manifest.json.new"
mv "$BUILD_DIR/$dest/manifest.json.new" "$BUILD_DIR/$dest/manifest.json"
done
fi
fi
# Safari-only files
if [ $BUILD_SAFARI -eq 1 ]; then
# Copy images
cp "images/icon_128.png" "$BUILD_DIR/safari/Icon-64.png"
mkdir "$BUILD_DIR/safari/icons/"
cp "images/icon_256.png" "$BUILD_DIR/safari/icons/icon_256.png"
cp "$DIR/src/safari/Info.plist" "$BUILD_DIR/safari/"
cp "$DIR/src/safari/global.html" "$BUILD_DIR/safari/"
# Set version in Info.plist
if [ -n "$VERSION" ]; then
SAFARI_VERSION="$VERSION"
else
SAFARI_VERSION="0.0"
fi
perl -pi -e "s/\[VERSION]/$SAFARI_VERSION/g" "$BUILD_DIR/safari/Info.plist"
mv "$BUILD_DIR/safari/" "$BUILD_DIR/safari.safariextension"
fi
# Build bookmarklet
if [ $BUILD_BOOKMARKLET -eq 1 ]; then
rsync -r src/bookmarklet/ "$BUILD_DIR/tmp/bookmarklet/background/"
perl -pi -e 's/^.*IF_BOOKMARKLET.*$//g' "$BUILD_DIR/tmp/bookmarklet/content/content.js"
# Create minified CSS file
sass --sourcemap=none --style=compressed src/common/content/tooltip.scss "$BUILD_DIR/bookmarklet/first_mention.css"
# Allow code for other environments to be removed with unreachable-branch-transform
patterns=(
"FirstMention\.isSafari && FirstMention\.isExtension/false"
"FirstMention\.isExtension/false"
"FirstMention\.isBrowserExt/false"
"FirstMention\.isBookmarklet/true"
"FirstMention\.cacheEnabled/false"
)
# If not dev mode, remove console.log() calls
if [ $DEV -eq 0 ]; then
patterns+=('^\s*(\/\/\s*)?console.log\(.+\n$/')
fi
for pattern in "${patterns[@]}"; do
perl -pi -e "s/$pattern/g" "$BUILD_DIR"/tmp/bookmarklet/+(content|background)/*.js
done
# Create bundle
BABEL_ENV=compat $PROCESS_CMD \
-t unreachable-branch-transform \
-r xregexp \
"$BUILD_DIR/tmp/bookmarklet/content/content.js" > "$BUILD_DIR/bookmarklet/tmp.js"
./node_modules/.bin/uglifyjs \
--preamble "/* First Mention [VERSION] | https://danstillman.com/first_mention */" \
"$BUILD_DIR/bookmarklet/tmp.js" | cat - src/common/content/tooltip.min.js \
> "$BUILD_DIR/bookmarklet/first_mention.js"
rm "$BUILD_DIR"/bookmarklet/tmp*.js
# Update version placeholder
if [ -n "$VERSION" ]; then
BOOKMARKLET_VERSION="$VERSION"
else
BOOKMARKLET_VERSION="0.0"
fi
perl -pi -e "s/\[VERSION]/$BOOKMARKLET_VERSION/g" "$BUILD_DIR/bookmarklet/first_mention.js"
# Rename dev version
if [ $DEV -eq 1 ]; then
mv "$BUILD_DIR/bookmarklet" "$BUILD_DIR/bookmarklet-dev"
fi
fi
rm -rf "$BUILD_DIR/tmp"
echo Done