blob: 5655ed462f8b2cbc35d3c7f3314241ea0663e0d7 [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
David Chen3f697512016-05-09 08:46:21 +000037
38# Create temporary directory that is removed when this script exits.
Damien Martin-Guillerez7ac7a152016-05-25 13:30:38 +000039readonly TMP=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXX")
David Chen3f697512016-05-09 08:46:21 +000040readonly OUT_DIR="$TMP/out"
41trap "rm -rf ${TMP}" EXIT
42
dzc1d8cd592017-06-23 08:26:15 +020043# TODO: Create a variant of this script for cutting versions of documentation
44# for Bazel releases. For that case, consider extracting the Git branch or tag
45# name to be used as the versioned directory name.
46readonly VERSION="master"
47readonly VERSION_DIR="$OUT_DIR/versions/$VERSION"
48
49# Unpacks the base Jekyll tree, Build Encyclopedia, Skylark Library, and
50# command line reference.
David Chen3f697512016-05-09 08:46:21 +000051function setup {
52 mkdir -p "$OUT_DIR"
53 cd "$OUT_DIR"
54 tar -xf "${JEKYLL_BASE}"
David Chen3f697512016-05-09 08:46:21 +000055
dzc1d8cd592017-06-23 08:26:15 +020056 mkdir -p "$VERSION_DIR"
57 mv "$OUT_DIR"/docs/* "$VERSION_DIR"
58 rm -r "$OUT_DIR"/docs
59
60 # Unpack the Build Encyclopedia into versions/master/be
61 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000062 mkdir -p "$be_dir"
63 unzip -qq "$BE_ZIP" -d "$be_dir"
64 mv "$be_dir/be-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000065
dzc1d8cd592017-06-23 08:26:15 +020066 # Unpack the Skylark Library into versions/master/skylark/lib
67 local sl_dir="$VERSION_DIR/skylark/lib"
David Chen3f697512016-05-09 08:46:21 +000068 mkdir -p "$sl_dir"
69 unzip -qq "$SL_ZIP" -d "$sl_dir"
70 mv "$sl_dir/skylark-nav.html" "$OUT_DIR/_includes"
David Chen15c09dd2016-08-29 08:56:37 +000071
dzc1d8cd592017-06-23 08:26:15 +020072 # Copy the command line reference.
73 cp "$CLR_HTML" "$VERSION_DIR"
David Chen3f697512016-05-09 08:46:21 +000074}
75
dzc1d8cd592017-06-23 08:26:15 +020076# Helper function for copying a Skylark rule doc.
David Chen3f697512016-05-09 08:46:21 +000077function copy_skylark_rule_doc {
78 local rule_family=$1
79 local rule_family_name=$2
dzc1d8cd592017-06-23 08:26:15 +020080 local be_dir="$VERSION_DIR/be"
David Chen3f697512016-05-09 08:46:21 +000081
82 ( cat <<EOF
83---
84layout: documentation
85title: ${rule_family_name} Rules
86---
87EOF
88 cat "$TMP/skylark/$rule_family/README.md"; ) > "$be_dir/${rule_family}.md"
89}
90
dzc1d8cd592017-06-23 08:26:15 +020091# Copies the READMEs for Skylark rules bundled with Bazel.
David Chen3f697512016-05-09 08:46:21 +000092function unpack_skylark_rule_docs {
93 local tmp_dir=$TMP/skylark
94 mkdir -p $tmp_dir
95 cd "$tmp_dir"
96 tar -xf "${SKYLARK_RULE_DOCS}"
97 copy_skylark_rule_doc docker "Docker"
98 copy_skylark_rule_doc pkg "Packaging"
99}
100
dzc1d8cd592017-06-23 08:26:15 +0200101# Processes a documentation page, such as replacing Blaze with Bazel.
David Chen3f697512016-05-09 08:46:21 +0000102function process_doc {
103 local f=$1
104 local tempf=$(mktemp -t bazel-doc-XXXXXX)
105
106 chmod +w $f
107 cat "$f" | sed 's,\.md,.html,g;s,Blaze,Bazel,g;s,blaze,bazel,g' > "$tempf"
108 cat "$tempf" > "$f"
109}
110
dzc1d8cd592017-06-23 08:26:15 +0200111# Performs fixup on each doc, such as replacing instances of 'blaze' with
112# 'bazel'.
David Chen3f697512016-05-09 08:46:21 +0000113function process_docs {
dzc1d8cd592017-06-23 08:26:15 +0200114 for f in $(find "$VERSION_DIR" -name "*.html"); do
David Chen3f697512016-05-09 08:46:21 +0000115 process_doc $f
116 done
dzc1d8cd592017-06-23 08:26:15 +0200117 for f in $(find "$VERSION_DIR" -name "*.md"); do
David Chen3f697512016-05-09 08:46:21 +0000118 process_doc $f
119 done
120}
121
dzc1d8cd592017-06-23 08:26:15 +0200122# Generates a redirect for a documentation page under /versions/master.
123function gen_redirect {
124 local output_dir=$OUT_DIR/$(dirname $f)
125 if [[ ! -d "$output_dir" ]]; then
126 mkdir -p "$output_dir"
127 fi
128
129 local src_basename=$(basename $f)
130 local md_basename="${src_basename%.*}.md"
131 local html_file="${f%.*}.html"
132 local redirect_file="$output_dir/$md_basename"
133 if [[ -e "$redirect_file" ]]; then
134 echo "Cannot create redirect file $redirect_file. File exists."
135 exit 1
136 fi
137 cat > "$redirect_file" <<EOF
138---
139layout: redirect
140redirect: /versions/$VERSION/$html_file
141---
142EOF
143}
144
145# During setup, all documentation under docs are moved to the /versions/master
146# directory as the documentation from HEAD.
147#
148# This function henerates a redirect from the root of the site for the given
149# doc page under /versions/master so that https://docs.bazel.build/foo.html
150# will be redirected to https://docs.bazel.build/versions/master/foo.html
151function gen_redirects {
152 pushd "$VERSION_DIR" > /dev/null
153 for f in $(find . -name "*.html" -type f); do
154 gen_redirect $f
155 done
156 for f in $(find . -name "*.md" -type f); do
157 gen_redirect $f
158 done
159 popd > /dev/null
160}
161
162# Creates a tar archive containing the final Jekyll tree.
David Chen3f697512016-05-09 08:46:21 +0000163function package_output {
164 cd "$OUT_DIR"
165 tar -hcf $OUTPUT $(find . -type f | sort)
166}
167
168function main {
169 setup
David Chen3f697512016-05-09 08:46:21 +0000170 unpack_skylark_rule_docs
David Chen3f697512016-05-09 08:46:21 +0000171 process_docs
dzc1d8cd592017-06-23 08:26:15 +0200172 gen_redirects
David Chen3f697512016-05-09 08:46:21 +0000173 package_output
174}
175main