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 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 16 | # This script constructs the final Jekyll tree by combining the static Jekyll |
| 17 | # site files with generated documentation, such as the Build Encyclopedia and |
| 18 | # Skylark Library. It then constructs the site directory structure for |
| 19 | # Bazel documentation at HEAD by moving all documentation into the |
| 20 | # /versions/master directory and adding redirects from the root of the site. |
| 21 | # This way, URLs of the form https://docs.bazel.build/foo.html will be |
| 22 | # redirected to https://docs.bazel.build/versions/master/foo.html. |
| 23 | |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 24 | set -eu |
| 25 | |
| 26 | readonly OUTPUT=${PWD}/$1 |
| 27 | shift |
| 28 | readonly JEKYLL_BASE=${PWD}/$1 |
| 29 | shift |
| 30 | readonly SKYLARK_RULE_DOCS=${PWD}/$1 |
| 31 | shift |
| 32 | readonly BE_ZIP=${PWD}/$1 |
| 33 | shift |
| 34 | readonly SL_ZIP=${PWD}/$1 |
Ulf Adams | 9e24ebd | 2016-06-23 09:24:57 +0000 | [diff] [blame] | 35 | shift |
| 36 | readonly CLR_HTML=${PWD}/$1 |
Klaus Aehlig | 6f52fca | 2019-03-18 03:43:40 -0700 | [diff] [blame] | 37 | shift |
Klaus Aehlig | 852c11f | 2019-03-19 06:42:17 -0700 | [diff] [blame] | 38 | readonly REPO_TAR="${PWD}/$1" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 39 | |
| 40 | # Create temporary directory that is removed when this script exits. |
Damien Martin-Guillerez | 7ac7a15 | 2016-05-25 13:30:38 +0000 | [diff] [blame] | 41 | readonly TMP=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXX") |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 42 | readonly OUT_DIR="$TMP/out" |
| 43 | trap "rm -rf ${TMP}" EXIT |
| 44 | |
Jingwen Chen | 186bdcd | 2018-12-14 10:27:23 -0800 | [diff] [blame] | 45 | readonly VERSION="${DOC_VERSION:-master}" |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 46 | readonly VERSION_DIR="$OUT_DIR/versions/$VERSION" |
| 47 | |
Klaus Aehlig | 6f52fca | 2019-03-18 03:43:40 -0700 | [diff] [blame] | 48 | # Unpacks the base Jekyll tree, Build Encyclopedia, etc. |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 49 | function setup { |
| 50 | mkdir -p "$OUT_DIR" |
| 51 | cd "$OUT_DIR" |
| 52 | tar -xf "${JEKYLL_BASE}" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 53 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 54 | mkdir -p "$VERSION_DIR" |
| 55 | mv "$OUT_DIR"/docs/* "$VERSION_DIR" |
| 56 | rm -r "$OUT_DIR"/docs |
| 57 | |
| 58 | # Unpack the Build Encyclopedia into versions/master/be |
| 59 | local be_dir="$VERSION_DIR/be" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 60 | mkdir -p "$be_dir" |
| 61 | unzip -qq "$BE_ZIP" -d "$be_dir" |
| 62 | mv "$be_dir/be-nav.html" "$OUT_DIR/_includes" |
David Chen | 15c09dd | 2016-08-29 08:56:37 +0000 | [diff] [blame] | 63 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 64 | # Unpack the Skylark Library into versions/master/skylark/lib |
| 65 | local sl_dir="$VERSION_DIR/skylark/lib" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 66 | mkdir -p "$sl_dir" |
| 67 | unzip -qq "$SL_ZIP" -d "$sl_dir" |
| 68 | mv "$sl_dir/skylark-nav.html" "$OUT_DIR/_includes" |
David Chen | 15c09dd | 2016-08-29 08:56:37 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | dac096c | 2019-03-22 10:29:40 -0700 | [diff] [blame] | 70 | # Unpack the documentation for the repository rules to repo subdirectory |
Klaus Aehlig | 6f52fca | 2019-03-18 03:43:40 -0700 | [diff] [blame] | 71 | local repo_dir="${VERSION_DIR}/repo" |
| 72 | mkdir -p "${repo_dir}" |
Klaus Aehlig | 852c11f | 2019-03-19 06:42:17 -0700 | [diff] [blame] | 73 | tar -C "${repo_dir}" -xf "${REPO_TAR}" |
Klaus Aehlig | 6f52fca | 2019-03-18 03:43:40 -0700 | [diff] [blame] | 74 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 75 | # Copy the command line reference. |
| 76 | cp "$CLR_HTML" "$VERSION_DIR" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 79 | # Helper function for copying a Skylark rule doc. |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 80 | function copy_skylark_rule_doc { |
| 81 | local rule_family=$1 |
| 82 | local rule_family_name=$2 |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 83 | local be_dir="$VERSION_DIR/be" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 84 | |
| 85 | ( cat <<EOF |
| 86 | --- |
| 87 | layout: documentation |
| 88 | title: ${rule_family_name} Rules |
| 89 | --- |
| 90 | EOF |
| 91 | cat "$TMP/skylark/$rule_family/README.md"; ) > "$be_dir/${rule_family}.md" |
| 92 | } |
| 93 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 94 | # Copies the READMEs for Skylark rules bundled with Bazel. |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 95 | function unpack_skylark_rule_docs { |
| 96 | local tmp_dir=$TMP/skylark |
| 97 | mkdir -p $tmp_dir |
| 98 | cd "$tmp_dir" |
| 99 | tar -xf "${SKYLARK_RULE_DOCS}" |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 100 | copy_skylark_rule_doc pkg "Packaging" |
| 101 | } |
| 102 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 103 | # Processes a documentation page, such as replacing Blaze with Bazel. |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 104 | function process_doc { |
| 105 | local f=$1 |
| 106 | local tempf=$(mktemp -t bazel-doc-XXXXXX) |
| 107 | |
| 108 | chmod +w $f |
| 109 | cat "$f" | sed 's,\.md,.html,g;s,Blaze,Bazel,g;s,blaze,bazel,g' > "$tempf" |
| 110 | cat "$tempf" > "$f" |
| 111 | } |
| 112 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 113 | # Performs fixup on each doc, such as replacing instances of 'blaze' with |
| 114 | # 'bazel'. |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 115 | function process_docs { |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 116 | for f in $(find "$VERSION_DIR" -name "*.html"); do |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 117 | process_doc $f |
| 118 | done |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 119 | for f in $(find "$VERSION_DIR" -name "*.md"); do |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 120 | process_doc $f |
| 121 | done |
| 122 | } |
| 123 | |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 124 | # Generates a redirect for a documentation page under /versions/master. |
| 125 | function gen_redirect { |
| 126 | local output_dir=$OUT_DIR/$(dirname $f) |
| 127 | if [[ ! -d "$output_dir" ]]; then |
| 128 | mkdir -p "$output_dir" |
| 129 | fi |
| 130 | |
| 131 | local src_basename=$(basename $f) |
| 132 | local md_basename="${src_basename%.*}.md" |
| 133 | local html_file="${f%.*}.html" |
| 134 | local redirect_file="$output_dir/$md_basename" |
| 135 | if [[ -e "$redirect_file" ]]; then |
| 136 | echo "Cannot create redirect file $redirect_file. File exists." |
| 137 | exit 1 |
| 138 | fi |
| 139 | cat > "$redirect_file" <<EOF |
| 140 | --- |
| 141 | layout: redirect |
| 142 | redirect: /versions/$VERSION/$html_file |
| 143 | --- |
| 144 | EOF |
| 145 | } |
| 146 | |
| 147 | # During setup, all documentation under docs are moved to the /versions/master |
| 148 | # directory as the documentation from HEAD. |
| 149 | # |
| 150 | # This function henerates a redirect from the root of the site for the given |
| 151 | # doc page under /versions/master so that https://docs.bazel.build/foo.html |
| 152 | # will be redirected to https://docs.bazel.build/versions/master/foo.html |
| 153 | function gen_redirects { |
| 154 | pushd "$VERSION_DIR" > /dev/null |
| 155 | for f in $(find . -name "*.html" -type f); do |
| 156 | gen_redirect $f |
| 157 | done |
| 158 | for f in $(find . -name "*.md" -type f); do |
| 159 | gen_redirect $f |
| 160 | done |
| 161 | popd > /dev/null |
| 162 | } |
| 163 | |
| 164 | # Creates a tar archive containing the final Jekyll tree. |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 165 | function package_output { |
| 166 | cd "$OUT_DIR" |
| 167 | tar -hcf $OUTPUT $(find . -type f | sort) |
| 168 | } |
| 169 | |
| 170 | function main { |
| 171 | setup |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 172 | unpack_skylark_rule_docs |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 173 | process_docs |
dzc | 1d8cd59 | 2017-06-23 08:26:15 +0200 | [diff] [blame] | 174 | gen_redirects |
David Chen | 3f69751 | 2016-05-09 08:46:21 +0000 | [diff] [blame] | 175 | package_output |
| 176 | } |
| 177 | main |