David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 1 | #!/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 | |
| 16 | set -eu |
| 17 | |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 18 | readonly WORKING_DIR=$(mktemp -d) |
Klaus Aehlig | 3be6417 | 2017-12-05 08:40:29 -0800 | [diff] [blame] | 19 | : ${HOST:=localhost} |
| 20 | : ${PORT:=12345} |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 21 | TARGET= |
| 22 | SERVING_PREFIX= |
| 23 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 24 | usage() { |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 25 | cat <<EOF |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 26 | Usage: $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 Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 39 | EOF |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 40 | } |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 41 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 42 | build_tree() { |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 43 | bazel build //site:jekyll-tree.tar |
Androbin | ef381e5 | 2017-12-14 07:24:27 -0800 | [diff] [blame] | 44 | rm -rf ${WORKING_DIR:-sentinel}/* |
Klaus Aehlig | 5bd448c | 2017-03-01 17:10:42 +0000 | [diff] [blame] | 45 | tar -xf "$(bazel info bazel-genfiles)/site/jekyll-tree.tar" -C $WORKING_DIR |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 48 | build_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 | |
| 60 | build_and_serve() { |
| 61 | build_tree |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 62 | echo "Serving docs.bazel.build site at $HOST:$PORT" |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 63 | jekyll serve --host "$HOST" --detach --quiet --port "$PORT" --source "$WORKING_DIR" |
| 64 | } |
| 65 | |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 66 | check() { |
| 67 | which $1 > /dev/null || (echo "$1 not installed. Please install $1."; exit 1) |
| 68 | } |
| 69 | |
| 70 | kill_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 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 80 | main() { |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 81 | check jekyll |
| 82 | |
Kristina Chodorow | bfffc50 | 2016-06-21 14:42:21 +0000 | [diff] [blame] | 83 | old_version="Jekyll 0.11.2" |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 84 | if expr match "$(jekyll --version)" "$old_version" > /dev/null; then |
Kristina Chodorow | bfffc50 | 2016-06-21 14:42:21 +0000 | [diff] [blame] | 85 | # The ancient version that apt-get has. |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 86 | 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 Chodorow | bfffc50 | 2016-06-21 14:42:21 +0000 | [diff] [blame] | 89 | fi |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 90 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 91 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 92 | |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 93 | while true; do |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 94 | build_and_serve |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 95 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 96 | echo "Type q to quit, r to rebuild docs and restart jekyll" |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 97 | read -n 1 -s user_input |
| 98 | if [ "$user_input" == "q" ]; then |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 99 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 100 | echo "Quitting" |
| 101 | exit 0 |
| 102 | elif [ "$user_input" == "r" ]; then |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 103 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 104 | echo "Rebuilding docs and restarting jekyll" |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 105 | fi |
| 106 | done |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 107 | } |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 108 | |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 109 | while [[ $# -gt 0 ]] |
| 110 | do |
| 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 |
| 136 | done |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 137 | |
| 138 | cleanup() { |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 139 | rm -rf $WORKING_DIR |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 140 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 141 | } |
| 142 | trap cleanup EXIT |
| 143 | |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 144 | main |