|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +# |
| 8 | +# See https://swift.org/LICENSE.txt for license information |
| 9 | +# See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 10 | +# |
| 11 | +# Updates the GitHub Pages documentation site thats published from the 'docs' |
| 12 | +# subdirectory in the 'gh-pages' branch of this repository. |
| 13 | +# |
| 14 | +# This script should be run by someone with commit access to the 'gh-pages' branch |
| 15 | +# at a regular frequency so that the documentation content on the GitHub Pages site |
| 16 | +# is up-to-date with the content in this repo. |
| 17 | +# |
| 18 | + |
| 19 | +set -eu |
| 20 | + |
| 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)))" |
| 27 | + |
| 28 | +# Set current directory to the repository root |
| 29 | +cd "$SWIFT_BOOK_ROOT" |
| 30 | + |
| 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 |
| 34 | + |
| 35 | +# Pretty print DocC JSON output so that it can be consistently diffed between commits |
| 36 | +export DOCC_JSON_PRETTYPRINT="YES" |
| 37 | + |
| 38 | +# Generate documentation for TSPL and output it |
| 39 | +# to the /docs subdirectory in the gh-pages worktree directory. |
| 40 | +export SWIFTPM_ENABLE_COMMAND_PLUGINS=1 |
| 41 | +swift package \ |
| 42 | + --allow-writing-to-directory "$SWIFT_BOOK_ROOT/gh-pages/docs" \ |
| 43 | + generate-documentation \ |
| 44 | + --target TSPL \ |
| 45 | + --disable-indexing \ |
| 46 | + --transform-for-static-hosting \ |
| 47 | + --experimental-enable-custom-templates \ |
| 48 | + --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` |
| 53 | + |
| 54 | +# Commit and push our changes to the gh-pages branch |
| 55 | +cd gh-pages |
| 56 | +git add docs |
| 57 | + |
| 58 | +if [ -n "$(git status --porcelain)" ]; then |
| 59 | + echo "Documentation changes found. Commiting the changes to the 'gh-pages' branch and pushing to origin." |
| 60 | + git commit -m "Update GitHub Pages documentation site to '$CURRENT_COMMIT_HASH'." |
| 61 | + git push origin HEAD:gh-pages |
| 62 | +else |
| 63 | + # No changes found, nothing to commit. |
| 64 | + echo "No documentation changes found." |
| 65 | +fi |
| 66 | + |
| 67 | +# Delete the git worktree we created |
| 68 | +cd .. |
| 69 | +exit |
| 70 | +git worktree remove gh-pages |
0 commit comments