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