blob: 7c332ed3759e74ba3179e15f2bd4620c0e0b7838 [file] [log] [blame]
David Chen3f697512016-05-09 08:46:21 +00001#!/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
ahumesky15e63342021-10-28 19:17:31 -070016# 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#
dzc1d8cd592017-06-23 08:26:15 +020027# This script constructs the final Jekyll tree by combining the static Jekyll
28# site files with generated documentation, such as the Build Encyclopedia and
gregceca48e9a2020-04-14 08:54:38 -070029# Starlark Library. It then constructs the site directory structure for
dzc1d8cd592017-06-23 08:26:15 +020030# Bazel documentation at HEAD by moving all documentation into the
wyv565f7742020-05-29 00:41:43 -070031# /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.
dzc1d8cd592017-06-23 08:26:15 +020035
David Chen3f697512016-05-09 08:46:21 +000036set -eu
37
38readonly OUTPUT=${PWD}/$1
39shift
wyv565f7742020-05-29 00:41:43 -070040readonly LATEST_RELEASE_VERSION=$1
41shift
David Chen3f697512016-05-09 08:46:21 +000042readonly JEKYLL_BASE=${PWD}/$1
43shift
gregceacab2c22020-05-28 18:11:32 -070044readonly STARLARK_RULE_DOCS=${PWD}/$1
David Chen3f697512016-05-09 08:46:21 +000045shift
46readonly BE_ZIP=${PWD}/$1
47shift
48readonly SL_ZIP=${PWD}/$1
Ulf Adams9e24ebd2016-06-23 09:24:57 +000049shift
50readonly CLR_HTML=${PWD}/$1
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070051shift
Klaus Aehlig852c11f2019-03-19 06:42:17 -070052readonly REPO_TAR="${PWD}/$1"
David Chen3f697512016-05-09 08:46:21 +000053
54# Create temporary directory that is removed when this script exits.
Damien Martin-Guillerez7ac7a152016-05-25 13:30:38 +000055readonly TMP=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXX")
David Chen3f697512016-05-09 08:46:21 +000056readonly OUT_DIR="$TMP/out"
57trap "rm -rf ${TMP}" EXIT
58
Tony Aiutob2dd6fb2021-06-09 10:29:29 -070059readonly VERSION="${DOC_VERSION:-main}"
dzc1d8cd592017-06-23 08:26:15 +020060readonly VERSION_DIR="$OUT_DIR/versions/$VERSION"
61
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070062# Unpacks the base Jekyll tree, Build Encyclopedia, etc.
David Chen3f697512016-05-09 08:46:21 +000063function setup {
64 mkdir -p "$OUT_DIR"
65 cd "$OUT_DIR"
jingwen4f69ae82021-02-18 18:33:28 -080066 tar -xf "${JEKYLL_BASE}"
David Chen3f697512016-05-09 08:46:21 +000067
dzc1d8cd592017-06-23 08:26:15 +020068 mkdir -p "$VERSION_DIR"
69 mv "$OUT_DIR"/docs/* "$VERSION_DIR"
70 rm -r "$OUT_DIR"/docs
71
wyv565f7742020-05-29 00:41:43 -070072 # Unpack the Build Encyclopedia into versions/$VERSION/be
dzc1d8cd592017-06-23 08:26:15 +020073 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000074 mkdir -p "$be_dir"
75 unzip -qq "$BE_ZIP" -d "$be_dir"
76 mv "$be_dir/be-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000077
wyv565f7742020-05-29 00:41:43 -070078 # Unpack the Starlark Library into versions/$VERSION/skylark/lib
dzc1d8cd592017-06-23 08:26:15 +020079 local sl_dir="$VERSION_DIR/skylark/lib"
David Chen3f697512016-05-09 08:46:21 +000080 mkdir -p "$sl_dir"
81 unzip -qq "$SL_ZIP" -d "$sl_dir"
82 mv "$sl_dir/skylark-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000083
wyv565f7742020-05-29 00:41:43 -070084 # Unpack the documentation for the repository rules to versions/$VERSION/repo
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070085 local repo_dir="${VERSION_DIR}/repo"
86 mkdir -p "${repo_dir}"
Klaus Aehlig852c11f2019-03-19 06:42:17 -070087 tar -C "${repo_dir}" -xf "${REPO_TAR}"
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070088
dzc1d8cd592017-06-23 08:26:15 +020089 # Copy the command line reference.
90 cp "$CLR_HTML" "$VERSION_DIR"
David Chen3f697512016-05-09 08:46:21 +000091}
92
gregceca48e9a2020-04-14 08:54:38 -070093# Helper function for copying a Starlark rule doc.
David Chen3f697512016-05-09 08:46:21 +000094function copy_skylark_rule_doc {
95 local rule_family=$1
96 local rule_family_name=$2
dzc1d8cd592017-06-23 08:26:15 +020097 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000098
99 ( cat <<EOF
100---
101layout: documentation
102title: ${rule_family_name} Rules
103---
104EOF
105 cat "$TMP/skylark/$rule_family/README.md"; ) > "$be_dir/${rule_family}.md"
106}
107
gregceca48e9a2020-04-14 08:54:38 -0700108# Copies the READMEs for Starlark rules bundled with Bazel.
David Chen3f697512016-05-09 08:46:21 +0000109function unpack_skylark_rule_docs {
110 local tmp_dir=$TMP/skylark
111 mkdir -p $tmp_dir
112 cd "$tmp_dir"
jingwen4f69ae82021-02-18 18:33:28 -0800113 tar -xf "${STARLARK_RULE_DOCS}"
David Chen3f697512016-05-09 08:46:21 +0000114 copy_skylark_rule_doc pkg "Packaging"
115}
116
dzc1d8cd592017-06-23 08:26:15 +0200117# Processes a documentation page, such as replacing Blaze with Bazel.
David Chen3f697512016-05-09 08:46:21 +0000118function process_doc {
119 local f=$1
120 local tempf=$(mktemp -t bazel-doc-XXXXXX)
121
122 chmod +w $f
arostovtsev9a1c5d32020-06-16 10:16:20 -0700123 # 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 Chen3f697512016-05-09 08:46:21 +0000135 cat "$tempf" > "$f"
136}
137
dzc1d8cd592017-06-23 08:26:15 +0200138# Performs fixup on each doc, such as replacing instances of 'blaze' with
139# 'bazel'.
David Chen3f697512016-05-09 08:46:21 +0000140function process_docs {
dzc1d8cd592017-06-23 08:26:15 +0200141 for f in $(find "$VERSION_DIR" -name "*.html"); do
David Chen3f697512016-05-09 08:46:21 +0000142 process_doc $f
143 done
dzc1d8cd592017-06-23 08:26:15 +0200144 for f in $(find "$VERSION_DIR" -name "*.md"); do
David Chen3f697512016-05-09 08:46:21 +0000145 process_doc $f
146 done
147}
148
wyv565f7742020-05-29 00:41:43 -0700149# Generates a redirect for a documentation page under
150# /versions/$LATEST_RELEASE_VERSION.
dzc1d8cd592017-06-23 08:26:15 +0200151function 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---
167layout: redirect
wyv565f7742020-05-29 00:41:43 -0700168redirect: /versions/$LATEST_RELEASE_VERSION/$html_file
dzc1d8cd592017-06-23 08:26:15 +0200169---
170EOF
171}
172
wyv565f7742020-05-29 00:41:43 -0700173# During setup, all documentation under docs are moved to the /versions/$VERSION
174# directory. This leaves nothing in the root directory.
dzc1d8cd592017-06-23 08:26:15 +0200175#
Tony Aiutob2dd6fb2021-06-09 10:29:29 -0700176# As a fix, when generating the head tarball, this function generates a
wyv9bea69a2020-06-01 05:37:09 -0700177# redirect from the root of the site for the given doc page under
178# /versions/$LATEST_RELEASE_VERSION so that
wyv565f7742020-05-29 00:41:43 -0700179# https://docs.bazel.build/foo.html will be redirected to
180# https://docs.bazel.build/versions/$LATEST_RELEASE_VERSION/foo.html
dzc1d8cd592017-06-23 08:26:15 +0200181function gen_redirects {
Tony Aiutob2dd6fb2021-06-09 10:29:29 -0700182 if [[ "$VERSION" == "main" ]]; then
wyv9bea69a2020-06-01 05:37:09 -0700183 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
dzc1d8cd592017-06-23 08:26:15 +0200192}
193
Tony Aiutob2dd6fb2021-06-09 10:29:29 -0700194# 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.
200function 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---
213layout: redirect
214redirect: /versions/main/$html_file
215---
216EOF
217}
218
219function 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
dzc1d8cd592017-06-23 08:26:15 +0200229# Creates a tar archive containing the final Jekyll tree.
David Chen3f697512016-05-09 08:46:21 +0000230function package_output {
231 cd "$OUT_DIR"
232 tar -hcf $OUTPUT $(find . -type f | sort)
233}
234
235function main {
236 setup
David Chen3f697512016-05-09 08:46:21 +0000237 unpack_skylark_rule_docs
David Chen3f697512016-05-09 08:46:21 +0000238 process_docs
dzc1d8cd592017-06-23 08:26:15 +0200239 gen_redirects
Tony Aiutob2dd6fb2021-06-09 10:29:29 -0700240 gen_master_redirects
David Chen3f697512016-05-09 08:46:21 +0000241 package_output
242}
243main