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. |
jingwen | c6244b2 | 2020-03-02 10:00:05 -0800 | [diff] [blame] | 15 | # |
| 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 Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 23 | |
| 24 | set -eu |
| 25 | |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 26 | readonly WORKING_DIR=$(mktemp -d) |
Klaus Aehlig | 3be6417 | 2017-12-05 08:40:29 -0800 | [diff] [blame] | 27 | : ${HOST:=localhost} |
| 28 | : ${PORT:=12345} |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 29 | TARGET= |
| 30 | SERVING_PREFIX= |
| 31 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 32 | usage() { |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 33 | cat <<EOF |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 34 | Usage: $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 Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 47 | EOF |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 48 | } |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 49 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 50 | build_tree() { |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 51 | bazel build //site:jekyll-tree.tar |
Androbin | ef381e5 | 2017-12-14 07:24:27 -0800 | [diff] [blame] | 52 | rm -rf ${WORKING_DIR:-sentinel}/* |
Klaus Aehlig | 5bd448c | 2017-03-01 17:10:42 +0000 | [diff] [blame] | 53 | 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] | 54 | } |
| 55 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 56 | build_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 | |
| 68 | build_and_serve() { |
| 69 | build_tree |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 70 | echo "Serving docs.bazel.build site at $HOST:$PORT" |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 71 | jekyll serve --host "$HOST" --detach --quiet --port "$PORT" --source "$WORKING_DIR" |
| 72 | } |
| 73 | |
jingwen | c6244b2 | 2020-03-02 10:00:05 -0800 | [diff] [blame] | 74 | check_jekyll() { |
gregce | 0e1100f | 2020-03-02 11:08:10 -0800 | [diff] [blame] | 75 | which jekyll > /dev/null || \ |
jingwen | c6244b2 | 2020-03-02 10:00:05 -0800 | [diff] [blame] | 76 | ( |
| 77 | cat <<EOF |
| 78 | jekyll not installed. Please install jekyll and rubygem dependencies by going |
| 79 | into the scripts/docs/ directory and running 'gem install -g --no-rdoc --no-ri'. |
| 80 | EOF |
| 81 | exit 1 |
| 82 | ) |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | kill_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 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 95 | main() { |
jingwen | c6244b2 | 2020-03-02 10:00:05 -0800 | [diff] [blame] | 96 | check_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 97 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 98 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 99 | |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 100 | while true; do |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 101 | build_and_serve |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 102 | |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 103 | echo "Type q to quit, r to rebuild docs and restart jekyll" |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 104 | read -n 1 -s user_input |
| 105 | if [ "$user_input" == "q" ]; then |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 106 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 107 | echo "Quitting" |
| 108 | exit 0 |
| 109 | elif [ "$user_input" == "r" ]; then |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 110 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 111 | echo "Rebuilding docs and restarting jekyll" |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 112 | fi |
| 113 | done |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 114 | } |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 115 | |
hlopko | a1fb6f2 | 2017-05-30 16:51:44 +0200 | [diff] [blame] | 116 | while [[ $# -gt 0 ]] |
| 117 | do |
| 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 |
| 143 | done |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 144 | |
| 145 | cleanup() { |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 146 | rm -rf $WORKING_DIR |
kchodorow | 410147a | 2017-04-21 19:16:17 +0200 | [diff] [blame] | 147 | kill_jekyll |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 148 | } |
| 149 | trap cleanup EXIT |
| 150 | |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 151 | main |