blob: c4541a84e096c5a319a03ef4961c6eb4f0a60b80 [file] [log] [blame]
David Chen3f697512016-05-09 08:46:21 +00001#!/bin/bash
2# Copyright 2016 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -eu
17
hlopkoa1fb6f22017-05-30 16:51:44 +020018readonly WORKING_DIR=$(mktemp -d)
Klaus Aehlig3be64172017-12-05 08:40:29 -080019: ${HOST:=localhost}
20: ${PORT:=12345}
hlopkoa1fb6f22017-05-30 16:51:44 +020021TARGET=
22SERVING_PREFIX=
23
kchodorow410147a2017-04-21 19:16:17 +020024usage() {
Dmitry Lomovc19737c2016-06-24 14:24:28 +000025 cat <<EOF
kchodorow410147a2017-04-21 19:16:17 +020026Usage: $0 [--port 12345] [--target DIR [PREFIX]] [--share]
27 --port [port]
28 Builds docs and starts a web server serving docs on localhost:port. Default
29 port is 12345.
30 --target <target directory> [<serving prefix>]
31 Builds docs as static web pages in <target directory>. Replaces absolute
32 paths in the resulting HTML with <serving prefix>, or, if it is not
33 specified, with <target directory>.
34 --share
35 Binds jekyll to the machine's hostname, instead of localhost (useful for
36 review).
37 --help
38 This message.
Dmitry Lomovc19737c2016-06-24 14:24:28 +000039EOF
kchodorow410147a2017-04-21 19:16:17 +020040}
Dmitry Lomovc19737c2016-06-24 14:24:28 +000041
kchodorow410147a2017-04-21 19:16:17 +020042build_tree() {
Alex Humesky6105e242016-06-22 19:14:13 +000043 bazel build //site:jekyll-tree.tar
Androbinef381e52017-12-14 07:24:27 -080044 rm -rf ${WORKING_DIR:-sentinel}/*
Klaus Aehlig5bd448c2017-03-01 17:10:42 +000045 tar -xf "$(bazel info bazel-genfiles)/site/jekyll-tree.tar" -C $WORKING_DIR
Alex Humesky6105e242016-06-22 19:14:13 +000046}
47
kchodorow410147a2017-04-21 19:16:17 +020048build_static() {
49 build_tree
50 TMP_TARGET=$(mktemp -d)
51 jekyll build --source $WORKING_DIR --destination "$TMP_TARGET"
52 REPLACEMENT=$(echo $SERVING_PREFIX | sed s/\\//\\\\\\//g)
53 find $TMP_TARGET -name '*.html' | xargs sed -i s/href=\\\"\\//href=\"$REPLACEMENT\\//g
54 find $TMP_TARGET -name '*.html' | xargs sed -i s/src=\\\"\\//src=\"$REPLACEMENT\\//g
55 cp -R $TMP_TARGET/* $TARGET
56 echo "Static pages copied to $TARGET"
57 echo "Should be served from $SERVING_PREFIX"
58}
59
60build_and_serve() {
61 build_tree
dzc22b85a22017-05-31 20:37:50 +020062 echo "Serving docs.bazel.build site at $HOST:$PORT"
kchodorow410147a2017-04-21 19:16:17 +020063 jekyll serve --host "$HOST" --detach --quiet --port "$PORT" --source "$WORKING_DIR"
64}
65
hlopkoa1fb6f22017-05-30 16:51:44 +020066check() {
67 which $1 > /dev/null || (echo "$1 not installed. Please install $1."; exit 1)
68}
69
70kill_jekyll() {
71 pid="$(lsof "-tiTCP:$PORT" -sTCP:LISTEN)" || true
72 if [ ! -z "$pid" ]; then
73 kill "$pid"
74 fi
75 # I found I got bind errors sometimes if I didn't wait a second for the server to
76 # actually shut down.
77 sleep 2
78}
79
kchodorow410147a2017-04-21 19:16:17 +020080main() {
David Chen3f697512016-05-09 08:46:21 +000081 check jekyll
82
Kristina Chodorowbfffc502016-06-21 14:42:21 +000083 old_version="Jekyll 0.11.2"
kchodorow410147a2017-04-21 19:16:17 +020084 if expr match "$(jekyll --version)" "$old_version" > /dev/null; then
Kristina Chodorowbfffc502016-06-21 14:42:21 +000085 # The ancient version that apt-get has.
Alex Humesky6105e242016-06-22 19:14:13 +000086 echo "ERROR: Running with an old version of Jekyll, update " \
87 "to 2.5.3 with \`sudo gem install jekyll -v 2.5.3\`"
88 exit 1
Kristina Chodorowbfffc502016-06-21 14:42:21 +000089 fi
Alex Humesky6105e242016-06-22 19:14:13 +000090
kchodorow410147a2017-04-21 19:16:17 +020091 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +000092
Alex Humesky6105e242016-06-22 19:14:13 +000093 while true; do
kchodorow410147a2017-04-21 19:16:17 +020094 build_and_serve
Alex Humesky6105e242016-06-22 19:14:13 +000095
kchodorow410147a2017-04-21 19:16:17 +020096 echo "Type q to quit, r to rebuild docs and restart jekyll"
Alex Humesky6105e242016-06-22 19:14:13 +000097 read -n 1 -s user_input
98 if [ "$user_input" == "q" ]; then
kchodorow410147a2017-04-21 19:16:17 +020099 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +0000100 echo "Quitting"
101 exit 0
102 elif [ "$user_input" == "r" ]; then
kchodorow410147a2017-04-21 19:16:17 +0200103 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +0000104 echo "Rebuilding docs and restarting jekyll"
Alex Humesky6105e242016-06-22 19:14:13 +0000105 fi
106 done
David Chen3f697512016-05-09 08:46:21 +0000107}
Alex Humesky6105e242016-06-22 19:14:13 +0000108
hlopkoa1fb6f22017-05-30 16:51:44 +0200109while [[ $# -gt 0 ]]
110do
111 key="$1"
112 case $key in
113 --port)
114 PORT="$2"
115 shift
116 ;;
117 --share)
118 HOST="$HOSTNAME"
119 ;;
120 --target)
121 TARGET="$2"
122 shift
123 SERVING_PREFIX="${2:-}"
124 build_static
125 exit 0
126 ;;
127 --help|help)
128 usage
129 exit 0
130 ;;
131 *)
132 usage
133 exit 1
134 esac
135 shift
136done
kchodorow410147a2017-04-21 19:16:17 +0200137
138cleanup() {
Alex Humesky6105e242016-06-22 19:14:13 +0000139 rm -rf $WORKING_DIR
kchodorow410147a2017-04-21 19:16:17 +0200140 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +0000141}
142trap cleanup EXIT
143
David Chen3f697512016-05-09 08:46:21 +0000144main