Skip to content

Commit acc3cfc

Browse files
committed
Iterate on the GitHub Pages script.
- Simplify the logic to move to the repo's top level. - Save the current commit before building, instead of after. - Don't assume the remote is named "origin". - Use pushd/popd to change directories and then change back. - Various rewording and revision to comments. - Remove an (accidental?) early-exit, so the temporary worktree actually gets deleted at the end.
1 parent a0633e8 commit acc3cfc

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

bin/update-book-preview

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
#!/bin/bash
2-
#
1+
#! /bin/bash
2+
33
# This source file is part of the Swift.org open source project
44
#
55
# Copyright (c) 2022 Apple Inc. and the Swift project authors
66
# Licensed under Apache License v2.0 with Runtime Library Exception
77
#
88
# See https://swift.org/LICENSE.txt for license information
99
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
10-
#
11-
# Updates the GitHub Pages documentation site thats published from the 'docs'
10+
11+
# Updates the GitHub Pages documentation site that's published from the 'docs'
1212
# subdirectory in the 'gh-pages' branch of this repository.
1313
#
1414
# This script should be run by someone with commit access to the 'gh-pages' branch
1515
# at a regular frequency so that the documentation content on the GitHub Pages site
1616
# is up-to-date with the content in this repo.
17+
18+
# To use top-of-tree DocC, set environment variables like the following:
1719
#
20+
# DOCC_EXEC=~/git/DocC/.build/arm64-apple-macosx/debug/docc
21+
# DOCC_HTML_DIR=~/git/DocC-Renderer/dist/
1822

1923
set -eu
2024

21-
# A `realpath` alternative using the default C implementation.
22-
filepath() {
23-
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
24-
}
25-
26-
SWIFT_BOOK_ROOT="$(dirname $(dirname $(filepath $0)))"
25+
REMOTE=${1:-origin}
26+
CURRENT_COMMIT_HASH="$(git rev-parse --short HEAD)"
2727

28-
# Set current directory to the repository root
29-
cd "$SWIFT_BOOK_ROOT"
28+
# Start at the top level directory of the repository.
29+
cd "$(git rev-parse --show-toplevel)"
3030

31-
# Use git worktree to checkout the gh-pages branch of this repository in a gh-pages sub-directory
32-
git fetch
33-
git worktree add --checkout gh-pages origin/gh-pages
31+
# Check out the 'gh-pages' branch in a subdirectory.
32+
git worktree add --checkout gh-pages "$REMOTE"/gh-pages
3433

35-
# Pretty print DocC JSON output so that it can be consistently diffed between commits
34+
# Pretty print DocC JSON output, so that it has a stable ordering and
35+
# can be diffed. This lets us determine whether rebuilding resulted in
36+
# any changes to the content.
3637
export DOCC_JSON_PRETTYPRINT="YES"
3738

38-
# Generate documentation for TSPL and output it
39-
# to the /docs subdirectory in the gh-pages worktree directory.
39+
# Build documentation, writing output in the gh-pages/docs subdirectory.
4040
export SWIFTPM_ENABLE_COMMAND_PLUGINS=1
4141
swift package \
42-
--allow-writing-to-directory "$SWIFT_BOOK_ROOT/gh-pages/docs" \
42+
--allow-writing-to-directory "./gh-pages/docs" \
4343
generate-documentation \
4444
--target TSPL \
4545
--disable-indexing \
4646
--transform-for-static-hosting \
4747
--experimental-enable-custom-templates \
4848
--hosting-base-path swift-book \
49-
--output-path "$SWIFT_BOOK_ROOT/gh-pages/docs"
50-
51-
# Save the current commit we've just built documentation from in a variable
52-
CURRENT_COMMIT_HASH=`git rev-parse --short HEAD`
49+
--output-path "./gh-pages/docs"
5350

5451
# Commit and push our changes to the gh-pages branch
55-
cd gh-pages
52+
pushd gh-pages
5653
git add docs
5754

58-
if [ -n "$(git status --porcelain)" ]; then
59-
echo "Documentation changes found. Commiting the changes to the 'gh-pages' branch and pushing to origin."
55+
if [[ -n "$(git status --porcelain)" ]]
56+
then
57+
echo "Found documentation changes."
58+
echo "Committing the changes to the 'gh-pages' branch and pushing to $REMOTE."
6059
git commit -m "Update GitHub Pages documentation site to '$CURRENT_COMMIT_HASH'."
61-
git push origin HEAD:gh-pages
60+
echo
61+
git push "$REMOTE" HEAD:gh-pages
6262
else
6363
# No changes found, nothing to commit.
6464
echo "No documentation changes found."
6565
fi
6666

67+
popd
68+
6769
# Delete the git worktree we created
68-
cd ..
69-
exit
7070
git worktree remove gh-pages

0 commit comments

Comments
 (0)