Skip to content

Commit df0c72b

Browse files
committed
Restructure highlighter server handling
In particular, this only attempts to install and start the server if local Wattsi is in use. As part of this, we no longer check for the existence and version of Wattsi each time before we invoke it, but instead only once.
1 parent f24a008 commit df0c72b

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

build.sh

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ HIGHLIGHT_SERVER_URL="http://127.0.0.1:8080" # this needs to be coordinated with
4141
function main {
4242
processCommandLineArgs "$@"
4343

44+
checkWattsi
45+
ensureHighlighterInstalled
46+
4447
# $SKIP_BUILD_UPDATE_CHECK is set inside the Dockerfile so that we don't check for updates both inside and outside
4548
# the Docker container.
4649
if [[ $DO_UPDATE == "true" && $SKIP_BUILD_UPDATE_CHECK != "true" ]]; then
4750
checkHTMLBuildIsUpToDate
48-
# If we're using Docker then this will be installed inside the container.
49-
if [[ $USE_DOCKER != "true" ]]; then
50-
pip3 install bs-highlighter
51-
fi
5251
fi
5352

5453
findHTMLSource
@@ -178,6 +177,24 @@ function checkHTMLBuildIsUpToDate {
178177
fi
179178
}
180179

180+
# Tries to install the bs-highlighter Python package if necessary
181+
# - Arguments: none
182+
# - Output:
183+
# - Either bs-highlighter-server will be in the $PATH, or a warning will be echoed
184+
function ensureHighlighterInstalled {
185+
# If we're using Docker then this will be installed inside the container.
186+
# If we're not using local Wattsi then we won't use the local highlighter.
187+
if [[ $USE_DOCKER != "true" && $LOCAL_WATTSI == "true" ]]; then
188+
if hash pip3 2>/dev/null; then
189+
if ! hash bs-highlighter-server 2>/dev/null; then
190+
pip3 install bs-highlighter
191+
fi
192+
else
193+
LOCAL_WATTSI="false"
194+
fi
195+
fi
196+
}
197+
181198
# Finds the location of the HTML Standard, and stores it in the HTML_SOURCE variable.
182199
# It either guesses based on directory structure, or interactively prompts the user.
183200
# - Arguments: none
@@ -537,6 +554,25 @@ Disallow: /review-drafts/" > "$HTML_OUTPUT/robots.txt"
537554
fi
538555
}
539556

557+
# Checks if Wattsi is available and up to date
558+
# - Arguments: none
559+
# - Output:
560+
# - Sets $LOCAL_WATTSI to true or false
561+
# - Echoes a warning if Wattsi is out of date according to $WATTSI_LATEST
562+
function checkWattsi {
563+
if hash wattsi 2>/dev/null; then
564+
if [[ "$(wattsi --version | cut -d' ' -f2)" -lt "$WATTSI_LATEST" ]]; then
565+
echo
566+
echo "Warning: Your wattsi version is out of date. You should to rebuild an"
567+
echo "up-to-date wattsi binary from the wattsi sources."
568+
echo
569+
fi
570+
LOCAL_WATTSI=true
571+
else
572+
LOCAL_WATTSI=false
573+
fi
574+
}
575+
540576
# Runs Wattsi on the given file, either locally or using the web service
541577
# - Arguments:
542578
# - $1: the file to run Wattsi on
@@ -547,7 +583,6 @@ Disallow: /review-drafts/" > "$HTML_OUTPUT/robots.txt"
547583
# - $HTML_TEMP/wattsi-output directory will contain the output from Wattsi on success
548584
# - $HTML_TEMP/wattsi-output.txt will contain the output from Wattsi, on both success and failure
549585
function runWattsi {
550-
551586
rm -rf "$2"
552587
mkdir "$2"
553588

@@ -559,19 +594,12 @@ function runWattsi {
559594
"$HTML_CACHE/caniuse.json" \
560595
"$HTML_CACHE/mdn-spec-links-html.json" \
561596
"$HIGHLIGHT_SERVER_URL" )
562-
if hash wattsi 2>/dev/null; then
563-
if [[ "$(wattsi --version | cut -d' ' -f2)" -lt "$WATTSI_LATEST" ]]; then
564-
echo
565-
echo "Warning: Your wattsi version is out of date. You should to rebuild an"
566-
echo "up-to-date wattsi binary from the wattsi sources."
567-
echo
568-
fi
597+
if [[ "$LOCAL_WATTSI" == "true" ]]; then
569598
WATTSI_RESULT="0"
570599
wattsi "${WATTSI_ARGS[@]}" || WATTSI_RESULT=$?
571600
else
572-
LOCAL_WATTSI=false
573601
$QUIET || echo
574-
$QUIET || echo "Local wattsi is not present; trying the build server..."
602+
$QUIET || echo "Local wattsi or pip3 are not present; trying the build server..."
575603

576604
CURL_ARGS=( https://build.whatwg.org/wattsi \
577605
--form "source=@$1" \
@@ -624,19 +652,20 @@ function generateBacklinks {
624652
perl .post-process-partial-backlink-generator.pl "$HTML_TEMP/wattsi-output/index-$1" > "$2/index.html";
625653
}
626654

627-
# Starts the syntax-highlighting Python server
655+
# Starts the syntax-highlighting Python server, when appropriate
628656
# Arguments: none
629-
# Output:
657+
# Output: if the server is necessary, then
630658
# - A server will be running in the background, at $HIGHLIGHT_SERVER_URL
631659
# - $HIGHLIGHT_SERVER_PID will be set for later use by stopHighlightServer
632660
function startHighlightServer {
633-
HIGHLIGHT_SERVER_ARGS=()
634-
$QUIET && HIGHLIGHT_SERVER_ARGS+=( --quiet )
635-
# shellcheck disable=SC2068
636-
bs-highlighter-server ${HIGHLIGHT_SERVER_ARGS[@]+"${HIGHLIGHT_SERVER_ARGS[@]}"} &
637-
HIGHLIGHT_SERVER_PID=$!
661+
if [[ "$LOCAL_WATTSI" == "true" ]]; then
662+
HIGHLIGHT_SERVER_ARGS=()
663+
$QUIET && HIGHLIGHT_SERVER_ARGS+=( --quiet )
664+
bs-highlighter-server ${HIGHLIGHT_SERVER_ARGS[@]+"${HIGHLIGHT_SERVER_ARGS[@]}"} &
665+
HIGHLIGHT_SERVER_PID=$!
638666

639-
trap stopHighlightServer EXIT
667+
trap stopHighlightServer EXIT
668+
fi
640669
}
641670

642671
# Stops the syntax-highlighting Python server

0 commit comments

Comments
 (0)