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 | |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 18 | if [ "${1-}" == "help" ]; then |
| 19 | cat <<EOF |
| 20 | Usage: |
| 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>. |
| 28 | EOF |
| 29 | exit 0 |
| 30 | fi |
| 31 | |
| 32 | if [[ "${1-}" == [0-9]* ]]; then |
| 33 | readonly PORT=$1 |
| 34 | readonly TARGET='' |
| 35 | else |
| 36 | readonly PORT=${1-12345} |
| 37 | readonly TARGET=${1-} |
| 38 | fi |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 39 | |
| 40 | readonly WORKING_DIR=$(mktemp -d) |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 41 | readonly SERVING_PREFIX=${2-$TARGET} |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 42 | |
| 43 | function check { |
| 44 | which $1 > /dev/null || (echo "$1 not installed. Please install $1."; exit 1) |
| 45 | } |
| 46 | |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 47 | function 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 Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 53 | |
| 54 | if [ -z "$TARGET" ]; then |
Damien Martin-Guillerez | 4885eef | 2016-10-28 12:02:50 +0000 | [diff] [blame] | 55 | echo "Serving bazel.build site at port $PORT" |
Dmitry Lomov | c19737c | 2016-06-24 14:24:28 +0000 | [diff] [blame] | 56 | 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 Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 67 | } |
| 68 | |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 69 | function main { |
| 70 | check jekyll |
| 71 | |
Kristina Chodorow | bfffc50 | 2016-06-21 14:42:21 +0000 | [diff] [blame] | 72 | old_version="Jekyll 0.11.2" |
| 73 | if expr match "$(jekyll --version)" "$old_version"; then |
| 74 | # The ancient version that apt-get has. |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 75 | 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 Chodorow | bfffc50 | 2016-06-21 14:42:21 +0000 | [diff] [blame] | 78 | fi |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 79 | |
| 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 Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 95 | } |
Alex Humesky | 6105e24 | 2016-06-22 19:14:13 +0000 | [diff] [blame] | 96 | |
| 97 | function cleanup { |
| 98 | rm -rf $WORKING_DIR |
| 99 | pkill -9 jekyll |
| 100 | } |
| 101 | trap cleanup EXIT |
| 102 | |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 103 | main |