blob: 0ab17dc0f3372ec506367dfd4a32ea317fffa802 [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
Dmitry Lomovc19737c2016-06-24 14:24:28 +000018if [ "${1-}" == "help" ]; then
19 cat <<EOF
20Usage:
21$0 [port]
22 Builds docs and starts a web server serving docs on localhost:port
23 Default port is 12345.
24$0 <target directory> [<serving prefix>]
25 Builds docs as static web pages in <target directory>.
26 Replaces absolute paths in the resulting HTML with <serving prefix>,
27 or, if it is not specified, with <target directory>.
28EOF
29 exit 0
30fi
31
32if [[ "${1-}" == [0-9]* ]]; then
33 readonly PORT=$1
34 readonly TARGET=''
35else
36 readonly PORT=${1-12345}
37 readonly TARGET=${1-}
38fi
David Chen3f697512016-05-09 08:46:21 +000039
40readonly WORKING_DIR=$(mktemp -d)
Dmitry Lomovc19737c2016-06-24 14:24:28 +000041readonly SERVING_PREFIX=${2-$TARGET}
David Chen3f697512016-05-09 08:46:21 +000042
43function check {
44 which $1 > /dev/null || (echo "$1 not installed. Please install $1."; exit 1)
45}
46
Alex Humesky6105e242016-06-22 19:14:13 +000047function build_and_serve {
48 bazel build //site:jekyll-tree.tar
49 rm -rf $WORKING_DIR/*
50 tar -xf bazel-genfiles/site/jekyll-tree.tar -C $WORKING_DIR
51
52 pkill -9 jekyll || true
Dmitry Lomovc19737c2016-06-24 14:24:28 +000053
54 if [ -z "$TARGET" ]; then
Damien Martin-Guillerez4885eef2016-10-28 12:02:50 +000055 echo "Serving bazel.build site at port $PORT"
Dmitry Lomovc19737c2016-06-24 14:24:28 +000056 jekyll serve --detach --quiet --port $PORT --source $WORKING_DIR
57 else
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 fi
Alex Humesky6105e242016-06-22 19:14:13 +000067}
68
David Chen3f697512016-05-09 08:46:21 +000069function main {
70 check jekyll
71
Kristina Chodorowbfffc502016-06-21 14:42:21 +000072 old_version="Jekyll 0.11.2"
73 if expr match "$(jekyll --version)" "$old_version"; then
74 # The ancient version that apt-get has.
Alex Humesky6105e242016-06-22 19:14:13 +000075 echo "ERROR: Running with an old version of Jekyll, update " \
76 "to 2.5.3 with \`sudo gem install jekyll -v 2.5.3\`"
77 exit 1
Kristina Chodorowbfffc502016-06-21 14:42:21 +000078 fi
Alex Humesky6105e242016-06-22 19:14:13 +000079
80 build_and_serve
81
82 echo "Type q to quit, r to rebuild docs and restart jekyll"
83 while true; do
84
85 read -n 1 -s user_input
86 if [ "$user_input" == "q" ]; then
87 echo "Quitting"
88 exit 0
89 elif [ "$user_input" == "r" ]; then
90 echo "Rebuilding docs and restarting jekyll"
91 build_and_serve
92 echo "Rebuilt docs and restarted jekyll"
93 fi
94 done
David Chen3f697512016-05-09 08:46:21 +000095}
Alex Humesky6105e242016-06-22 19:14:13 +000096
97function cleanup {
98 rm -rf $WORKING_DIR
99 pkill -9 jekyll
100}
101trap cleanup EXIT
102
David Chen3f697512016-05-09 08:46:21 +0000103main