forked from whatwg/html-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·329 lines (301 loc) · 10.1 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/bash
set -e
HTML_GIT_CLONE_OPTIONS=${HTML_GIT_CLONE_OPTIONS:-"--depth 1"}
# cd to the directory containing this script
cd "$( dirname "${BASH_SOURCE[0]}" )"
DIR=$(pwd)
DO_UPDATE=true
VERBOSE=false
QUIET=false
export DO_UPDATE
export VERBOSE
export QUIET
HTML_CACHE=${HTML_CACHE:-$DIR/.cache}
export HTML_CACHE
HTML_TEMP=${HTML_TEMP:-$DIR/.temp}
export HTML_TEMP
HTML_OUTPUT=${HTML_OUTPUT:-$DIR/output}
export HTML_OUTPUT
for arg in "$@"
do
case $arg in
-c|--clean)
rm -rf $HTML_CACHE
exit 0
;;
-h|--help)
echo "Usage: $0 [-c|--clean]"
echo " $0 [-h|--help]"
echo "Usage: $0 [-n|--no-update] [-q|--quiet] [-v|--verbose]"
echo
echo " -c|--clean Remove downloaded dependencies and generated files (then stop)."
echo " -h|--help Show this usage statement."
echo " -n|--no-update Don't update before building; just build."
echo " -q|--quiet Don't emit any messages except errors/warnings."
echo " -v|--verbose Show verbose output from every build step."
exit 0
;;
-n|--no-update|--no-updates)
DO_UPDATE=false
;;
-q|--quiet)
QUIET=true
VERBOSE=false
;;
-v|--verbose)
VERBOSE=true
QUIET=false
set -vx
;;
*)
;;
esac
done
if [ "$DO_UPDATE" == true ]; then
$QUIET || echo "Checking if html-build is up to date..."
# TODO: `git remote get-url origin` is nicer, but new in Git 2.7.
ORIGIN_URL=$(git config --get remote.origin.url)
git fetch $($VERBOSE || echo "-q") $ORIGIN_URL master
NEW_COMMITS=$(git rev-list --count HEAD..FETCH_HEAD)
if [ "$NEW_COMMITS" != "0" ]; then
$QUIET || echo
echo -n "Your local branch is $NEW_COMMITS "
[ "$NEW_COMMITS" == "1" ] && echo -n commit || echo -n commits
echo " behind $ORIGIN_URL:"
git log --oneline HEAD..FETCH_HEAD
echo
echo "To update, run this command:"
echo
echo " git pull --rebase origin master"
echo
echo "This check can be bypassed with the --no-update option."
exit 1
fi
fi
function chooseRepo {
echo
echo "What HTML source would you like to build from?"
echo
echo "1) Use an existing clone on my local filesystem."
echo "2) Create a clone from https://github.com/whatwg/html."
echo "3) Create a clone from an existing fork, by GitHub username."
echo "4) Create a clone from an existing fork, by custom URL."
echo "5) Quit"
echo
read -e -p "Choose 1-5: " choice
if [ "1" = "$choice" ]; then
read -e -p "Path to your existing clone: "
HTML_SOURCE=$(echo "$REPLY" | xargs) # trims leading/trailing space
if [[ "$HTML_SOURCE" = "" ]]; then
chooseRepo
fi
confirmRepo
elif [ "2" = "$choice" ]; then
HTML_REPO=https://github.com/whatwg/html.git
confirmRepo
elif [ "3" = "$choice" ]; then
echo
read -e -p "GitHub username of fork owner: "
GH_USERNAME=$(echo "$REPLY" | xargs) # trims leading/trailing space
if [ -z "$GH_USERNAME" ]; then
chooseRepo
fi
echo
echo "Does a fork already exist at https://github.com/$GH_USERNAME/html?"
echo
read -e -p "Y or N? " yn
if [[ "y" = "$yn" || "Y" = "$yn" ]]; then
HTML_REPO="https://github.com/$GH_USERNAME/html.git"
confirmRepo
else
echo
echo "Before proceeding, first go to https://github.com/whatwg/html and create a fork."
exit
fi
elif [ "4" = "$choice" ]; then
echo
read -e -p "URL: "
REPLY=$(echo "$REPLY" | xargs) # trims leading/trailing space
if [ -z "$REPLY" ]; then
chooseRepo
fi
HTML_REPO=$REPLY
confirmRepo
elif [[ "5" = "$choice" || "q" = "$choice" || "Q" = "$choice" ]]; then
echo
echo "Can't build without a source repo to build from. Quitting..."
exit
else
chooseRepo
fi
}
function confirmRepo {
if [ -n "$HTML_SOURCE" ]; then
if [ -f "$HTML_SOURCE/source" ]; then
echo
echo "OK, build from the $HTML_SOURCE/source file?"
echo
read -e -p "Y or N? " yn
if [[ "y" = "$yn" || "Y" = "$yn" ]]; then
return
else
unset HTML_SOURCE
chooseRepo
fi
else
echo
echo "$HTML_SOURCE/source file doesn't exist. Please choose another option."
unset HTML_SOURCE
chooseRepo
fi
return
fi
HTML_SOURCE=${HTML_SOURCE:-$DIR/html}
echo
echo "OK, clone from $HTML_REPO?"
echo
read -e -p "Y or N? " yn
if [[ "y" = "$yn" || "Y" = "$yn" ]]; then
git clone $HTML_GIT_CLONE_OPTIONS \
$($VERBOSE && echo "--verbose" || $QUIET && echo "--quiet") \
$HTML_REPO $HTML_SOURCE
else
unset HTML_SOURCE
chooseRepo
fi
}
$QUIET || echo "Looking for the HTML source (set HTML_SOURCE to override)..."
if [ -z "$HTML_SOURCE" ]; then
PARENT_DIR=$(dirname $DIR)
if [ -f $PARENT_DIR/html/source ]; then
HTML_SOURCE=$PARENT_DIR/html
$QUIET || echo "Found $HTML_SOURCE (alongside html-build)..."
else
if [ -f $DIR/html/source ]; then
HTML_SOURCE=$DIR/html
$QUIET || echo "Found $HTML_SOURCE (inside html-build)..."
else
$QUIET || echo "Didn't find the HTML source on your system..."
chooseRepo
fi
fi
else
if [ -f "$HTML_SOURCE/source" ]; then
$QUIET || echo "Found $HTML_SOURCE (from HTML_SOURCE)..."
else
$QUIET || echo "Looked in the $HTML_SOURCE directory but didn't find HTML source there..."
unset HTML_SOURCE
chooseRepo
fi
fi
export HTML_SOURCE
$QUIET || echo "Linting the source file..."
./lint.sh $HTML_SOURCE/source || {
echo
echo "There were lint errors. Stopping."
exit 1
}
rm -rf $HTML_TEMP && mkdir -p $HTML_TEMP
rm -rf $HTML_OUTPUT && mkdir -p $HTML_OUTPUT
if [ -d $HTML_CACHE ]; then
PREV_BUILD_SHA=$( cat $HTML_CACHE/last-build-sha.txt 2>/dev/null || echo "" )
CURRENT_BUILD_SHA=$( git rev-parse HEAD )
if [ "$PREV_BUILD_SHA" != "$CURRENT_BUILD_SHA" ]; then
$QUIET || echo "Build tools have been updated since last run; clearing the cache..."
DO_UPDATE=true
rm -rf $HTML_CACHE
mkdir -p $HTML_CACHE
echo $CURRENT_BUILD_SHA > $HTML_CACHE/last-build-sha.txt
fi
else
mkdir -p $HTML_CACHE
fi
if [ "$DO_UPDATE" == true ] || [ ! -f $HTML_CACHE/caniuse.json ]; then
rm -f $HTML_CACHE/caniuse.json
$QUIET || echo "Downloading caniuse data..."
curl $($VERBOSE || echo "-s") \
-o $HTML_CACHE/caniuse.json -k \
https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json
fi
if [ "$DO_UPDATE" == true ] || [ ! -f $HTML_CACHE/w3cbugs.csv ]; then
rm -f $HTML_CACHE/w3cbugs.csv
$QUIET || echo "Downloading list of W3C bugzilla bugs..."
curl $($VERBOSE || echo "-s") \
-o $HTML_CACHE/w3cbugs.csv \
'https://www.w3.org/Bugs/Public/buglist.cgi?columnlist=bug_file_loc,short_desc&query_format=advanced&resolution=---&ctype=csv&status_whiteboard=whatwg-resolved&status_whiteboard_type=notregexp&bug_file_loc=http&bug_file_loc_type=substring&product=WHATWG&product=HTML%20WG&product=CSS&product=WebAppsWG'
fi
$QUIET || echo "Pre-processing the source..."
cp -p entities/out/entities.inc $HTML_CACHE
cp -p entities/out/entities-dtd.url $HTML_CACHE
cp -p quotes/out/cldr.inc $HTML_CACHE
perl .pre-process-main.pl $($VERBOSE && echo "--verbose") < $HTML_SOURCE/source > $HTML_TEMP/source-expanded-1
perl .pre-process-annotate-attributes.pl < $HTML_TEMP/source-expanded-1 > $HTML_TEMP/source-expanded-2 # this one could be merged
perl .pre-process-tag-omission.pl < $HTML_TEMP/source-expanded-2 | perl .pre-process-index-generator.pl > $HTML_TEMP/source-whatwg-complete # this one could be merged
function runWattsi {
# Input arguments: $1 is the file to run wattsi on, $2 is a directory for wattsi to write output to
# Output:
# - Sets global variable $WATTSI_RESULT to an exit code (or equivalent, for HTTP version)
# - $HTML_TEMP/wattsi-output directory will contain the output from wattsi on success
# - $HTML_TEMP/wattsi-output.txt will contain the output from wattsi, on both success and failure
rm -rf $2
mkdir $2
if hash wattsi 2>/dev/null; then
WATTSI_RESULT=$(wattsi $($QUIET && echo "--quiet") $1 $2 \
$HTML_CACHE/caniuse.json $HTML_CACHE/w3cbugs.csv \
> $HTML_TEMP/wattsi-output.txt; echo $?)
else
$QUIET || echo
$QUIET || echo "Local wattsi is not present; trying the build server..."
curl $($VERBOSE && echo "-v") $($QUIET && echo "-s") \
https://build.whatwg.org/wattsi \
--form source=@$1 \
--form caniuse=@$HTML_CACHE/caniuse.json \
--form w3cbugs=@$HTML_CACHE/w3cbugs.csv \
--dump-header $HTML_TEMP/wattsi-headers.txt \
--output $HTML_TEMP/wattsi-output.zip
# read exit code from the Wattsi-Exit-Code header and assume failure if not found
WATTSI_RESULT=1
while IFS=":" read NAME VALUE; do
if [ "$NAME" == "Wattsi-Exit-Code" ]; then
WATTSI_RESULT=$(echo $VALUE | tr -d ' \r\n')
break
fi
done < $HTML_TEMP/wattsi-headers.txt
if [ "$WATTSI_RESULT" != "0" ]; then
mv $HTML_TEMP/wattsi-output.zip $HTML_TEMP/wattsi-output.txt
else
unzip $($VERBOSE && echo "-v" || echo "-qq") $HTML_TEMP/wattsi-output.zip -d $2
mv $2/output.txt $HTML_TEMP/wattsi-output.txt
fi
fi
}
runWattsi $HTML_TEMP/source-whatwg-complete $HTML_TEMP/wattsi-output
if [ "$WATTSI_RESULT" == "0" ]; then
$QUIET || cat $HTML_TEMP/wattsi-output.txt | grep -v '^$' # trim blank lines
else
cat $HTML_TEMP/wattsi-output.txt | grep -v '^$' # trim blank lines
if [ "$WATTSI_RESULT" == "65" ]; then
echo
echo "There were errors. Running again to show the original line numbers."
echo
runWattsi $HTML_SOURCE/source $HTML_TEMP/wattsi-raw-source-output
cat $HTML_TEMP/wattsi-output.txt | grep -v '^$' # trim blank lines
fi
echo
echo "There were errors. Stopping."
exit $WATTSI_RESULT
fi
cat $HTML_TEMP/wattsi-output/index-html | perl .post-process-partial-backlink-generator.pl > $HTML_OUTPUT/index;
cp -p entities/out/entities.json $HTML_OUTPUT
# multipage setup
rm -rf $HTML_OUTPUT/multipage
mv $HTML_TEMP/wattsi-output/multipage-html $HTML_OUTPUT/multipage
rm -rf $HTML_TEMP
cp -p $HTML_SOURCE/.htaccess $HTML_OUTPUT
cp -p $HTML_SOURCE/404.html $HTML_OUTPUT
cp -pR $HTML_SOURCE/fonts $HTML_OUTPUT
cp -pR $HTML_SOURCE/images $HTML_OUTPUT
cp -pR $HTML_SOURCE/demos $HTML_OUTPUT
cp -pR $HTML_SOURCE/link-fixup.js $HTML_OUTPUT
$QUIET || echo
$QUIET || echo "Success!"