1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#!/bin/bash
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
#
# Updates the GitHub Pages documentation site thats published from the 'docs'
# subdirectory in the 'gh-pages' branch of this repository.
#
# This script should be run by someone with commit access to the 'gh-pages' branch
# at a regular frequency so that the documentation content on the GitHub Pages site
# is up-to-date with the content in this repo.
#
set -eu
# A `realpath` alternative using the default C implementation.
filepath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
SWIFT_DOCC_ROOT="$(dirname $(dirname $(filepath $0)))"
DOCC_BUILD_DIR="$SWIFT_DOCC_ROOT"/.build/docc-gh-pages-build
DOCC_OUTPUT_DIR="$DOCC_BUILD_DIR"/SwiftDocC.doccarchive
DOCC_UTILITIES_OUTPUT_DIR="$DOCC_BUILD_DIR"/SwiftDocCUtilities.doccarchive
mkdir -p "$DOCC_UTILITIES_OUTPUT_DIR"
# Set current directory to the repository root
cd "$SWIFT_DOCC_ROOT"
# Use git worktree to checkout the gh-pages branch of this repository in a gh-pages sub-directory
git fetch
git worktree add --checkout gh-pages origin/gh-pages
# Pretty print DocC JSON output so that it can be consistently diffed between commits
export DOCC_JSON_PRETTYPRINT="YES"
# Generate documentation for the 'SwiftDocC' target and output it
# to the /docs subdirectory in the gh-pages worktree directory.
echo -e "\033[34;1m Building SwiftDocC docs at $DOCC_OUTPUT_DIR \033[0m"
swift package \
--allow-writing-to-directory "$SWIFT_DOCC_ROOT" \
generate-documentation \
--target SwiftDocC \
--disable-indexing \
--source-service github \
--source-service-base-url https://github.com/apple/swift-docc/blob/main \
--checkout-path "$SWIFT_DOCC_ROOT" \
--transform-for-static-hosting \
--hosting-base-path swift-docc \
--output-path "$DOCC_OUTPUT_DIR"
echo -e "\033[34;1m Building SwiftDocC Utilities docs at $DOCC_UTILITIES_OUTPUT_DIR \033[0m"
# Generate documentation for the 'SwiftDocCUtilities' target and output it
# to a temporary output directory in the .build directory.
swift package \
--allow-writing-to-directory "$DOCC_BUILD_DIR" \
generate-documentation \
--target SwiftDocCUtilities \
--disable-indexing \
--source-service github \
--source-service-base-url https://github.com/apple/swift-docc/blob/main \
--checkout-path "$SWIFT_DOCC_ROOT" \
--transform-for-static-hosting \
--hosting-base-path swift-docc \
--output-path "$DOCC_UTILITIES_OUTPUT_DIR"
echo -e "\033[34;1m Merging docs \033q[0m"
# Remove the output directory so that the merge command can output there
rm -rf "$SWIFT_DOCC_ROOT/gh-pages/docs"
# Merge the SwiftDocCUtilities docs into the primary SwiftDocC docs
swift run docc merge \
"$DOCC_OUTPUT_DIR" \
"$DOCC_UTILITIES_OUTPUT_DIR" \
--output-path "$SWIFT_DOCC_ROOT/gh-pages/docs"
# Save the current commit we've just built documentation from in a variable
CURRENT_COMMIT_HASH=`git rev-parse --short HEAD`
# Commit and push our changes to the gh-pages branch
cd gh-pages
git add docs
if [ -n "$(git status --porcelain)" ]; then
echo "Documentation changes found. Committing the changes to the 'gh-pages' branch and pushing to origin."
git commit -m "Update GitHub Pages documentation site to $CURRENT_COMMIT_HASH"
git push origin HEAD:gh-pages
else
# No changes found, nothing to commit.
echo "No documentation changes found."
fi
# Delete the git worktree we created
cd ..
git worktree remove gh-pages
|