blob: 97cd4535d40cecc0cf664850578565392dbae3d2 [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
dzc1d8cd592017-06-23 08:26:15 +020016# This script constructs the final Jekyll tree by combining the static Jekyll
17# site files with generated documentation, such as the Build Encyclopedia and
gregceca48e9a2020-04-14 08:54:38 -070018# Starlark Library. It then constructs the site directory structure for
dzc1d8cd592017-06-23 08:26:15 +020019# Bazel documentation at HEAD by moving all documentation into the
wyv565f7742020-05-29 00:41:43 -070020# /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.
dzc1d8cd592017-06-23 08:26:15 +020024
David Chen3f697512016-05-09 08:46:21 +000025set -eu
26
27readonly OUTPUT=${PWD}/$1
28shift
wyv565f7742020-05-29 00:41:43 -070029readonly LATEST_RELEASE_VERSION=$1
30shift
David Chen3f697512016-05-09 08:46:21 +000031readonly JEKYLL_BASE=${PWD}/$1
32shift
gregceacab2c22020-05-28 18:11:32 -070033readonly STARLARK_RULE_DOCS=${PWD}/$1
David Chen3f697512016-05-09 08:46:21 +000034shift
35readonly BE_ZIP=${PWD}/$1
36shift
37readonly SL_ZIP=${PWD}/$1
Ulf Adams9e24ebd2016-06-23 09:24:57 +000038shift
39readonly CLR_HTML=${PWD}/$1
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070040shift
Klaus Aehlig852c11f2019-03-19 06:42:17 -070041readonly REPO_TAR="${PWD}/$1"
David Chen3f697512016-05-09 08:46:21 +000042
43# Create temporary directory that is removed when this script exits.
Damien Martin-Guillerez7ac7a152016-05-25 13:30:38 +000044readonly TMP=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXX")
David Chen3f697512016-05-09 08:46:21 +000045readonly OUT_DIR="$TMP/out"
46trap "rm -rf ${TMP}" EXIT
47
Jingwen Chen186bdcd2018-12-14 10:27:23 -080048readonly VERSION="${DOC_VERSION:-master}"
dzc1d8cd592017-06-23 08:26:15 +020049readonly VERSION_DIR="$OUT_DIR/versions/$VERSION"
50
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070051# Unpacks the base Jekyll tree, Build Encyclopedia, etc.
David Chen3f697512016-05-09 08:46:21 +000052function setup {
53 mkdir -p "$OUT_DIR"
54 cd "$OUT_DIR"
55 tar -xf "${JEKYLL_BASE}"
David Chen3f697512016-05-09 08:46:21 +000056
dzc1d8cd592017-06-23 08:26:15 +020057 mkdir -p "$VERSION_DIR"
58 mv "$OUT_DIR"/docs/* "$VERSION_DIR"
59 rm -r "$OUT_DIR"/docs
60
wyv565f7742020-05-29 00:41:43 -070061 # Unpack the Build Encyclopedia into versions/$VERSION/be
dzc1d8cd592017-06-23 08:26:15 +020062 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000063 mkdir -p "$be_dir"
64 unzip -qq "$BE_ZIP" -d "$be_dir"
65 mv "$be_dir/be-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000066
wyv565f7742020-05-29 00:41:43 -070067 # Unpack the Starlark Library into versions/$VERSION/skylark/lib
dzc1d8cd592017-06-23 08:26:15 +020068 local sl_dir="$VERSION_DIR/skylark/lib"
David Chen3f697512016-05-09 08:46:21 +000069 mkdir -p "$sl_dir"
70 unzip -qq "$SL_ZIP" -d "$sl_dir"
71 mv "$sl_dir/skylark-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000072
wyv565f7742020-05-29 00:41:43 -070073 # Unpack the documentation for the repository rules to versions/$VERSION/repo
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070074 local repo_dir="${VERSION_DIR}/repo"
75 mkdir -p "${repo_dir}"
Klaus Aehlig852c11f2019-03-19 06:42:17 -070076 tar -C "${repo_dir}" -xf "${REPO_TAR}"
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070077
dzc1d8cd592017-06-23 08:26:15 +020078 # Copy the command line reference.
79 cp "$CLR_HTML" "$VERSION_DIR"
David Chen3f697512016-05-09 08:46:21 +000080}
81
gregceca48e9a2020-04-14 08:54:38 -070082# Helper function for copying a Starlark rule doc.
David Chen3f697512016-05-09 08:46:21 +000083function copy_skylark_rule_doc {
84 local rule_family=$1
85 local rule_family_name=$2
dzc1d8cd592017-06-23 08:26:15 +020086 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000087
88 ( cat <<EOF
89---
90layout: documentation
91title: ${rule_family_name} Rules
92---
93EOF
94 cat "$TMP/skylark/$rule_family/README.md"; ) > "$be_dir/${rule_family}.md"
95}
96
gregceca48e9a2020-04-14 08:54:38 -070097# Copies the READMEs for Starlark rules bundled with Bazel.
David Chen3f697512016-05-09 08:46:21 +000098function unpack_skylark_rule_docs {
99 local tmp_dir=$TMP/skylark
100 mkdir -p $tmp_dir
101 cd "$tmp_dir"
gregceacab2c22020-05-28 18:11:32 -0700102 tar -xf "${STARLARK_RULE_DOCS}"
David Chen3f697512016-05-09 08:46:21 +0000103 copy_skylark_rule_doc pkg "Packaging"
104}
105
dzc1d8cd592017-06-23 08:26:15 +0200106# Processes a documentation page, such as replacing Blaze with Bazel.
David Chen3f697512016-05-09 08:46:21 +0000107function process_doc {
108 local f=$1
109 local tempf=$(mktemp -t bazel-doc-XXXXXX)
110
111 chmod +w $f
arostovtsev9a1c5d32020-06-16 10:16:20 -0700112 # 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 Chen3f697512016-05-09 08:46:21 +0000124 cat "$tempf" > "$f"
125}
126
dzc1d8cd592017-06-23 08:26:15 +0200127# Performs fixup on each doc, such as replacing instances of 'blaze' with
128# 'bazel'.
David Chen3f697512016-05-09 08:46:21 +0000129function process_docs {
dzc1d8cd592017-06-23 08:26:15 +0200130 for f in $(find "$VERSION_DIR" -name "*.html"); do
David Chen3f697512016-05-09 08:46:21 +0000131 process_doc $f
132 done
dzc1d8cd592017-06-23 08:26:15 +0200133 for f in $(find "$VERSION_DIR" -name "*.md"); do
David Chen3f697512016-05-09 08:46:21 +0000134 process_doc $f
135 done
136}
137
wyv565f7742020-05-29 00:41:43 -0700138# Generates a redirect for a documentation page under
139# /versions/$LATEST_RELEASE_VERSION.
dzc1d8cd592017-06-23 08:26:15 +0200140function 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---
156layout: redirect
wyv565f7742020-05-29 00:41:43 -0700157redirect: /versions/$LATEST_RELEASE_VERSION/$html_file
dzc1d8cd592017-06-23 08:26:15 +0200158---
159EOF
160}
161
wyv565f7742020-05-29 00:41:43 -0700162# During setup, all documentation under docs are moved to the /versions/$VERSION
163# directory. This leaves nothing in the root directory.
dzc1d8cd592017-06-23 08:26:15 +0200164#
wyv9bea69a2020-06-01 05:37:09 -0700165# 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
wyv565f7742020-05-29 00:41:43 -0700168# https://docs.bazel.build/foo.html will be redirected to
169# https://docs.bazel.build/versions/$LATEST_RELEASE_VERSION/foo.html
dzc1d8cd592017-06-23 08:26:15 +0200170function gen_redirects {
wyv9bea69a2020-06-01 05:37:09 -0700171 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
dzc1d8cd592017-06-23 08:26:15 +0200181}
182
183# Creates a tar archive containing the final Jekyll tree.
David Chen3f697512016-05-09 08:46:21 +0000184function package_output {
185 cd "$OUT_DIR"
186 tar -hcf $OUTPUT $(find . -type f | sort)
187}
188
189function main {
190 setup
David Chen3f697512016-05-09 08:46:21 +0000191 unpack_skylark_rule_docs
David Chen3f697512016-05-09 08:46:21 +0000192 process_docs
dzc1d8cd592017-06-23 08:26:15 +0200193 gen_redirects
David Chen3f697512016-05-09 08:46:21 +0000194 package_output
195}
196main