blob: 0f9d383045a3ecf93677b3d081650b4d5b867f7d [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
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 Chen3f697512016-05-09 08:46:21 +000024set -eu
25
26readonly OUTPUT=${PWD}/$1
27shift
28readonly JEKYLL_BASE=${PWD}/$1
29shift
30readonly SKYLARK_RULE_DOCS=${PWD}/$1
31shift
32readonly BE_ZIP=${PWD}/$1
33shift
34readonly SL_ZIP=${PWD}/$1
Ulf Adams9e24ebd2016-06-23 09:24:57 +000035shift
36readonly CLR_HTML=${PWD}/$1
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070037shift
Klaus Aehlig852c11f2019-03-19 06:42:17 -070038readonly REPO_TAR="${PWD}/$1"
David Chen3f697512016-05-09 08:46:21 +000039
40# Create temporary directory that is removed when this script exits.
Damien Martin-Guillerez7ac7a152016-05-25 13:30:38 +000041readonly TMP=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXX")
David Chen3f697512016-05-09 08:46:21 +000042readonly OUT_DIR="$TMP/out"
43trap "rm -rf ${TMP}" EXIT
44
Jingwen Chen186bdcd2018-12-14 10:27:23 -080045readonly VERSION="${DOC_VERSION:-master}"
dzc1d8cd592017-06-23 08:26:15 +020046readonly VERSION_DIR="$OUT_DIR/versions/$VERSION"
47
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070048# Unpacks the base Jekyll tree, Build Encyclopedia, etc.
David Chen3f697512016-05-09 08:46:21 +000049function setup {
50 mkdir -p "$OUT_DIR"
51 cd "$OUT_DIR"
52 tar -xf "${JEKYLL_BASE}"
David Chen3f697512016-05-09 08:46:21 +000053
dzc1d8cd592017-06-23 08:26:15 +020054 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 Chen3f697512016-05-09 08:46:21 +000060 mkdir -p "$be_dir"
61 unzip -qq "$BE_ZIP" -d "$be_dir"
62 mv "$be_dir/be-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000063
dzc1d8cd592017-06-23 08:26:15 +020064 # Unpack the Skylark Library into versions/master/skylark/lib
65 local sl_dir="$VERSION_DIR/skylark/lib"
David Chen3f697512016-05-09 08:46:21 +000066 mkdir -p "$sl_dir"
67 unzip -qq "$SL_ZIP" -d "$sl_dir"
68 mv "$sl_dir/skylark-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000069
Benjamin Petersondac096c2019-03-22 10:29:40 -070070 # Unpack the documentation for the repository rules to repo subdirectory
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070071 local repo_dir="${VERSION_DIR}/repo"
72 mkdir -p "${repo_dir}"
Klaus Aehlig852c11f2019-03-19 06:42:17 -070073 tar -C "${repo_dir}" -xf "${REPO_TAR}"
Klaus Aehlig6f52fca2019-03-18 03:43:40 -070074
dzc1d8cd592017-06-23 08:26:15 +020075 # Copy the command line reference.
76 cp "$CLR_HTML" "$VERSION_DIR"
David Chen3f697512016-05-09 08:46:21 +000077}
78
dzc1d8cd592017-06-23 08:26:15 +020079# Helper function for copying a Skylark rule doc.
David Chen3f697512016-05-09 08:46:21 +000080function copy_skylark_rule_doc {
81 local rule_family=$1
82 local rule_family_name=$2
dzc1d8cd592017-06-23 08:26:15 +020083 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000084
85 ( cat <<EOF
86---
87layout: documentation
88title: ${rule_family_name} Rules
89---
90EOF
91 cat "$TMP/skylark/$rule_family/README.md"; ) > "$be_dir/${rule_family}.md"
92}
93
dzc1d8cd592017-06-23 08:26:15 +020094# Copies the READMEs for Skylark rules bundled with Bazel.
David Chen3f697512016-05-09 08:46:21 +000095function 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 Chen3f697512016-05-09 08:46:21 +0000100 copy_skylark_rule_doc pkg "Packaging"
101}
102
dzc1d8cd592017-06-23 08:26:15 +0200103# Processes a documentation page, such as replacing Blaze with Bazel.
David Chen3f697512016-05-09 08:46:21 +0000104function 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
dzc1d8cd592017-06-23 08:26:15 +0200113# Performs fixup on each doc, such as replacing instances of 'blaze' with
114# 'bazel'.
David Chen3f697512016-05-09 08:46:21 +0000115function process_docs {
dzc1d8cd592017-06-23 08:26:15 +0200116 for f in $(find "$VERSION_DIR" -name "*.html"); do
David Chen3f697512016-05-09 08:46:21 +0000117 process_doc $f
118 done
dzc1d8cd592017-06-23 08:26:15 +0200119 for f in $(find "$VERSION_DIR" -name "*.md"); do
David Chen3f697512016-05-09 08:46:21 +0000120 process_doc $f
121 done
122}
123
dzc1d8cd592017-06-23 08:26:15 +0200124# Generates a redirect for a documentation page under /versions/master.
125function 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---
141layout: redirect
142redirect: /versions/$VERSION/$html_file
143---
144EOF
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
153function 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 Chen3f697512016-05-09 08:46:21 +0000165function package_output {
166 cd "$OUT_DIR"
167 tar -hcf $OUTPUT $(find . -type f | sort)
168}
169
170function main {
171 setup
David Chen3f697512016-05-09 08:46:21 +0000172 unpack_skylark_rule_docs
David Chen3f697512016-05-09 08:46:21 +0000173 process_docs
dzc1d8cd592017-06-23 08:26:15 +0200174 gen_redirects
David Chen3f697512016-05-09 08:46:21 +0000175 package_output
176}
177main