blob: 24d10e703cb212f291fefeace48a5f1bb7abd0f8 [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.
jingwenc6244b22020-03-02 10:00:05 -080015#
16# Prerequisites:
17#
18# - ruby version >= 2.4
19# - rubygem dependencies: cd into script/docs and run
20#
21# 'gem install -g --no-rdoc --no-ri'
22#
David Chen3f697512016-05-09 08:46:21 +000023
24set -eu
25
hlopkoa1fb6f22017-05-30 16:51:44 +020026readonly WORKING_DIR=$(mktemp -d)
Klaus Aehlig3be64172017-12-05 08:40:29 -080027: ${HOST:=localhost}
28: ${PORT:=12345}
hlopkoa1fb6f22017-05-30 16:51:44 +020029TARGET=
30SERVING_PREFIX=
31
kchodorow410147a2017-04-21 19:16:17 +020032usage() {
Dmitry Lomovc19737c2016-06-24 14:24:28 +000033 cat <<EOF
kchodorow410147a2017-04-21 19:16:17 +020034Usage: $0 [--port 12345] [--target DIR [PREFIX]] [--share]
35 --port [port]
36 Builds docs and starts a web server serving docs on localhost:port. Default
37 port is 12345.
38 --target <target directory> [<serving prefix>]
39 Builds docs as static web pages in <target directory>. Replaces absolute
40 paths in the resulting HTML with <serving prefix>, or, if it is not
41 specified, with <target directory>.
42 --share
43 Binds jekyll to the machine's hostname, instead of localhost (useful for
44 review).
45 --help
46 This message.
Dmitry Lomovc19737c2016-06-24 14:24:28 +000047EOF
kchodorow410147a2017-04-21 19:16:17 +020048}
Dmitry Lomovc19737c2016-06-24 14:24:28 +000049
kchodorow410147a2017-04-21 19:16:17 +020050build_tree() {
Alex Humesky6105e242016-06-22 19:14:13 +000051 bazel build //site:jekyll-tree.tar
Androbinef381e52017-12-14 07:24:27 -080052 rm -rf ${WORKING_DIR:-sentinel}/*
Klaus Aehlig5bd448c2017-03-01 17:10:42 +000053 tar -xf "$(bazel info bazel-genfiles)/site/jekyll-tree.tar" -C $WORKING_DIR
Alex Humesky6105e242016-06-22 19:14:13 +000054}
55
kchodorow410147a2017-04-21 19:16:17 +020056build_static() {
57 build_tree
58 TMP_TARGET=$(mktemp -d)
59 jekyll build --source $WORKING_DIR --destination "$TMP_TARGET"
60 REPLACEMENT=$(echo $SERVING_PREFIX | sed s/\\//\\\\\\//g)
61 find $TMP_TARGET -name '*.html' | xargs sed -i s/href=\\\"\\//href=\"$REPLACEMENT\\//g
62 find $TMP_TARGET -name '*.html' | xargs sed -i s/src=\\\"\\//src=\"$REPLACEMENT\\//g
63 cp -R $TMP_TARGET/* $TARGET
64 echo "Static pages copied to $TARGET"
65 echo "Should be served from $SERVING_PREFIX"
66}
67
68build_and_serve() {
69 build_tree
dzc22b85a22017-05-31 20:37:50 +020070 echo "Serving docs.bazel.build site at $HOST:$PORT"
kchodorow410147a2017-04-21 19:16:17 +020071 jekyll serve --host "$HOST" --detach --quiet --port "$PORT" --source "$WORKING_DIR"
72}
73
jingwenc6244b22020-03-02 10:00:05 -080074check_jekyll() {
gregce0e1100f2020-03-02 11:08:10 -080075 which jekyll > /dev/null || \
jingwenc6244b22020-03-02 10:00:05 -080076 (
77 cat <<EOF
78jekyll not installed. Please install jekyll and rubygem dependencies by going
79into the scripts/docs/ directory and running 'gem install -g --no-rdoc --no-ri'.
80EOF
81 exit 1
82 )
hlopkoa1fb6f22017-05-30 16:51:44 +020083}
84
85kill_jekyll() {
86 pid="$(lsof "-tiTCP:$PORT" -sTCP:LISTEN)" || true
87 if [ ! -z "$pid" ]; then
88 kill "$pid"
89 fi
90 # I found I got bind errors sometimes if I didn't wait a second for the server to
91 # actually shut down.
92 sleep 2
93}
94
kchodorow410147a2017-04-21 19:16:17 +020095main() {
jingwenc6244b22020-03-02 10:00:05 -080096 check_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +000097
kchodorow410147a2017-04-21 19:16:17 +020098 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +000099
Alex Humesky6105e242016-06-22 19:14:13 +0000100 while true; do
kchodorow410147a2017-04-21 19:16:17 +0200101 build_and_serve
Alex Humesky6105e242016-06-22 19:14:13 +0000102
kchodorow410147a2017-04-21 19:16:17 +0200103 echo "Type q to quit, r to rebuild docs and restart jekyll"
Alex Humesky6105e242016-06-22 19:14:13 +0000104 read -n 1 -s user_input
105 if [ "$user_input" == "q" ]; then
kchodorow410147a2017-04-21 19:16:17 +0200106 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +0000107 echo "Quitting"
108 exit 0
109 elif [ "$user_input" == "r" ]; then
kchodorow410147a2017-04-21 19:16:17 +0200110 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +0000111 echo "Rebuilding docs and restarting jekyll"
Alex Humesky6105e242016-06-22 19:14:13 +0000112 fi
113 done
David Chen3f697512016-05-09 08:46:21 +0000114}
Alex Humesky6105e242016-06-22 19:14:13 +0000115
hlopkoa1fb6f22017-05-30 16:51:44 +0200116while [[ $# -gt 0 ]]
117do
118 key="$1"
119 case $key in
120 --port)
121 PORT="$2"
122 shift
123 ;;
124 --share)
125 HOST="$HOSTNAME"
126 ;;
127 --target)
128 TARGET="$2"
129 shift
130 SERVING_PREFIX="${2:-}"
131 build_static
132 exit 0
133 ;;
134 --help|help)
135 usage
136 exit 0
137 ;;
138 *)
139 usage
140 exit 1
141 esac
142 shift
143done
kchodorow410147a2017-04-21 19:16:17 +0200144
145cleanup() {
Alex Humesky6105e242016-06-22 19:14:13 +0000146 rm -rf $WORKING_DIR
kchodorow410147a2017-04-21 19:16:17 +0200147 kill_jekyll
Alex Humesky6105e242016-06-22 19:14:13 +0000148}
149trap cleanup EXIT
150
David Chen3f697512016-05-09 08:46:21 +0000151main