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