|
| 1 | +#!/usr/bin/env bash |
| 2 | +# set -x |
| 3 | + |
| 4 | +# This script manages the deployment of Substrate rustdocs to https://paritytech.github.io/substrate/. |
| 5 | +# - With `deploy` sub-command, it will checkout the passed-in branch/tag ref, build the rustdocs |
| 6 | +# locally (this takes some time), update the `index.html` index page, and push it to remote |
| 7 | +# `gh-pages` branch. So users running this command need to have write access to the remote |
| 8 | +# `gh-pages` branch. This sub-command depends on [@jimmychu0807/index-tpl-crud](https://www.npmjs.com/package/@jimmychu0807/index-tpl-crud) |
| 9 | +# to update the DOM of index.html page. |
| 10 | +# - With `remove` sub-command, it will remove the deployed rustdocs from `gh-pages`, and update the |
| 11 | +# index.html page as necessary. It may remove the `latest` symbolic link. |
| 12 | +# |
| 13 | +# Examples: |
| 14 | +# # Showing help text |
| 15 | +# rustdocs-release.sh -h |
| 16 | +# |
| 17 | +# # Deploy rustdocs of `monthly-2021-10` tag |
| 18 | +# rustdocs-release.sh deploy monthly-2021-10 |
| 19 | +# |
| 20 | +# # In addition to the above, the `latest` symlink will point to this version of rustdocs |
| 21 | +# rustdocs-release.sh deploy -l monthly-2021-10 |
| 22 | +# |
| 23 | +# # Remove the rustdocs of `monthly-2021-10` from `gh-pages`. |
| 24 | +# rustdocs-release.sh remove monthly-2021-10 |
| 25 | +# |
| 26 | +# Dependencies: |
| 27 | +# - @jimmychu0807/index-tpl-crud - https://www.npmjs.com/package/@jimmychu0807/index-tpl-crud |
| 28 | +# |
| 29 | + |
| 30 | +# Script setting |
| 31 | +# The git repo http URL |
| 32 | +REMOTE_REPO="https://github.com/paritytech/substrate.git" |
| 33 | +TMP_PREFIX="/tmp" # tmp location that the built doc is copied to. |
| 34 | +DOC_INDEX_PAGE="sc_service/index.html" |
| 35 | + |
| 36 | +# Set to `true` if using cargo `nightly` toolchain to build the doc. |
| 37 | +# Set to `false` to use the default cargo toolchain. This is preferred if you want to build |
| 38 | +# the rustdocs with a pinned nightly version set to your default toolchain. |
| 39 | +CARGO_NIGHTLY=false |
| 40 | + |
| 41 | +# Set the git remote name. Most of the time the default is `origin`. |
| 42 | +GIT_REMOTE="origin" |
| 43 | +LATEST=false |
| 44 | + |
| 45 | +# Setting the help text |
| 46 | +declare -A HELP_TXT |
| 47 | +HELP_TXT["deploy"]=$(cat <<-EOH |
| 48 | +Build and deploy the rustdocs of the specified branch/tag to \`gh-pages\` branch. |
| 49 | +
|
| 50 | + usage: $0 deploy [-l] <git_branch_ref> |
| 51 | + example: $0 deploy -l monthly-2021-10 |
| 52 | +
|
| 53 | + options: |
| 54 | + -l The \`latest\` path will be sym'linked to this rustdocs version |
| 55 | +EOH |
| 56 | +) |
| 57 | + |
| 58 | +HELP_TXT["remove"]=$(cat <<-EOH |
| 59 | +Remove the rustdocs of the specified version from \`gh-pages\` branch. |
| 60 | +
|
| 61 | + usage: $0 remove <git_branch_ref> |
| 62 | + example: $0 remove monthly-2021-10 |
| 63 | +EOH |
| 64 | +) |
| 65 | + |
| 66 | +set_and_check_rustdoc_ref() { |
| 67 | + [[ -z "$1" ]] && { |
| 68 | + echo -e "git branch_ref is not specified.\n" |
| 69 | + echo "${HELP_TXT[$2]}" |
| 70 | + exit 1 |
| 71 | + } |
| 72 | + BUILD_RUSTDOC_REF=$1 |
| 73 | +} |
| 74 | + |
| 75 | +check_local_change() { |
| 76 | + # Check there is no local changes before proceeding |
| 77 | + [[ -n $(git status --porcelain) ]] \ |
| 78 | + && echo "Local changes exist, please either discard or commit them as this command will change the current checkout branch." \ |
| 79 | + && exit 1 |
| 80 | +} |
| 81 | + |
| 82 | +build_rustdocs() { |
| 83 | + # Build the docs |
| 84 | + time cargo $($CARGO_NIGHTLY && echo "+nightly") doc --workspace --all-features --verbose \ |
| 85 | + || { echo "Generate $1 rustdocs failed" && exit 1; } |
| 86 | + rm -f target/doc/.lock |
| 87 | + |
| 88 | + # Moving the built doc to the tmp location |
| 89 | + mv target/doc "${2}" |
| 90 | + [[ -n "${DOC_INDEX_PAGE}" ]] \ |
| 91 | + && echo "<meta http-equiv=refresh content=0;url=${DOC_INDEX_PAGE}>" > "${2}/index.html" |
| 92 | +} |
| 93 | + |
| 94 | +upsert_index_page() { |
| 95 | + # Check if `index-tpl-crud` exists |
| 96 | + which index-tpl-crud &> /dev/null || yarn global add @jimmychu0807/index-tpl-crud |
| 97 | + index-tpl-crud upsert $($1 && echo "-l") ./index.html "$2" |
| 98 | +} |
| 99 | + |
| 100 | +rm_index_page() { |
| 101 | + which index-tpl-crud &> /dev/null || yarn global add @jimmychu0807/index-tpl-crud |
| 102 | + index-tpl-crud rm ./index.html "$1" |
| 103 | +} |
| 104 | + |
| 105 | +git_add_commit_push() { |
| 106 | + git add --all |
| 107 | + git commit -m "$1" || echo "Nothing to commit" |
| 108 | + git push "${GIT_REMOTE}" gh-pages --force |
| 109 | +} |
| 110 | + |
| 111 | +import_gh_key() { |
| 112 | + [[ -n $GITHUB_SSH_PRIV_KEY ]] && { |
| 113 | + eval $(ssh-agent) |
| 114 | + ssh-add - <<< $GITHUB_SSH_PRIV_KEY |
| 115 | + } |
| 116 | + |
| 117 | + # Adding github.com as known_hosts |
| 118 | + ssh-keygen -F github.com &>/dev/null || { |
| 119 | + [[ -e ~/.ssh ]] || mkdir ~/.ssh |
| 120 | + [[ -e ~/.ssh/known_hosts ]] || touch ~/.ssh/known_hosts |
| 121 | + ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +deploy_main() { |
| 126 | + check_local_change |
| 127 | + import_gh_key |
| 128 | + |
| 129 | + CURRENT_GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 130 | + TMP_PROJECT_PATH="${TMP_PREFIX}/${PROJECT_NAME}" |
| 131 | + DOC_PATH="${TMP_PROJECT_PATH}/${BUILD_RUSTDOC_REF}" |
| 132 | + |
| 133 | + # Build the tmp project path |
| 134 | + rm -rf "${TMP_PROJECT_PATH}" && mkdir "${TMP_PROJECT_PATH}" |
| 135 | + |
| 136 | + # Copy .gitignore file to tmp |
| 137 | + [[ -e "${PROJECT_PATH}/.gitignore" ]] && cp "${PROJECT_PATH}/.gitignore" "${TMP_PROJECT_PATH}" |
| 138 | + |
| 139 | + git fetch --all |
| 140 | + git checkout -f ${BUILD_RUSTDOC_REF} || { echo "Checkout \`${BUILD_RUSTDOC_REF}\` error." && exit 1; } |
| 141 | + build_rustdocs "${BUILD_RUSTDOC_REF}" "${DOC_PATH}" |
| 142 | + |
| 143 | + # git checkout `gh-pages` branch |
| 144 | + git fetch "${GIT_REMOTE}" gh-pages |
| 145 | + git checkout gh-pages |
| 146 | + # Move the built back |
| 147 | + [[ -e "${TMP_PROJECT_PATH}/.gitignore" ]] && cp -f "${TMP_PROJECT_PATH}/.gitignore" . |
| 148 | + # Ensure the destination dir doesn't exist under current path. |
| 149 | + rm -rf "${BUILD_RUSTDOC_REF}" |
| 150 | + mv -f "${DOC_PATH}" "${BUILD_RUSTDOC_REF}" |
| 151 | + |
| 152 | + upsert_index_page $LATEST "${BUILD_RUSTDOC_REF}" |
| 153 | + # Add the latest symlink |
| 154 | + $LATEST && rm -rf latest && ln -sf "${BUILD_RUSTDOC_REF}" latest |
| 155 | + |
| 156 | + git_add_commit_push "___Deployed rustdocs of ${BUILD_RUSTDOC_REF}___" |
| 157 | + # Clean up |
| 158 | + # Remove the tmp asset created |
| 159 | + rm -rf "${TMP_PROJECT_PATH}" |
| 160 | + # Resume back previous checkout branch. |
| 161 | + git checkout -f "$CURRENT_GIT_BRANCH" |
| 162 | +} |
| 163 | + |
| 164 | +remove_main() { |
| 165 | + check_local_change |
| 166 | + import_gh_key |
| 167 | + |
| 168 | + CURRENT_GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 169 | + |
| 170 | + # git checkout `gh-pages` branch |
| 171 | + git fetch "${GIT_REMOTE}" gh-pages |
| 172 | + git checkout gh-pages |
| 173 | + |
| 174 | + rm -rf "${BUILD_RUSTDOC_REF}" |
| 175 | + rm_index_page "${BUILD_RUSTDOC_REF}" |
| 176 | + # check if the destination of `latest` exists and rmove if not. |
| 177 | + [[ -e "latest" ]] || rm latest |
| 178 | + |
| 179 | + git_add_commit_push "___Removed rustdocs of ${BUILD_RUSTDOC_REF}___" |
| 180 | + |
| 181 | + # Resume back previous checkout branch. |
| 182 | + git checkout -f "$CURRENT_GIT_BRANCH" |
| 183 | +} |
| 184 | + |
| 185 | +# ---- The script execution entry point starts here ---- |
| 186 | + |
| 187 | +# Arguments handling |
| 188 | +SUBCMD=$1 |
| 189 | +[[ $SUBCMD == "deploy" || $SUBCMD == "remove" ]] \ |
| 190 | + || { echo "Please specify a subcommand of \`deploy\` or \`remove\`" && exit 1 ;} |
| 191 | +shift |
| 192 | + |
| 193 | +# After removing the subcommand, there could only be 1 or 2 parameters afterward |
| 194 | +[[ $# -lt 1 || $# -gt 2 ]] && { |
| 195 | + echo "${HELP_TXT[${SUBCMD}]}" |
| 196 | + exit 1 |
| 197 | +} |
| 198 | + |
| 199 | +# Parsing options and argument for `deploy` subcommand |
| 200 | +[[ $SUBCMD == "deploy" ]] && { |
| 201 | + while getopts :lh opt; do |
| 202 | + case $opt in |
| 203 | + l) |
| 204 | + LATEST=true |
| 205 | + ;; |
| 206 | + h) |
| 207 | + echo "${HELP_TXT[$SUBCMD]}" |
| 208 | + exit 0 |
| 209 | + ;; |
| 210 | + \?) |
| 211 | + echo "Invalid option: -$OPTARG" >&2 |
| 212 | + exit 1 |
| 213 | + ;; |
| 214 | + esac |
| 215 | + done |
| 216 | +} |
| 217 | +# Parsing options and argument for `remove` subcommand |
| 218 | +[[ $SUBCMD == "remove" ]] && { |
| 219 | + while getopts :h opt; do |
| 220 | + case $opt in |
| 221 | + h) |
| 222 | + echo "${HELP_TXT[$SUBCMD]}" |
| 223 | + exit 0 |
| 224 | + ;; |
| 225 | + \?) |
| 226 | + echo "Invalid option: -$OPTARG" >&2 |
| 227 | + exit 1 |
| 228 | + ;; |
| 229 | + esac |
| 230 | + done |
| 231 | +} |
| 232 | + |
| 233 | +shift $(($OPTIND - 1)) |
| 234 | +set_and_check_rustdoc_ref ${1:-''} $SUBCMD |
| 235 | + |
| 236 | +SCRIPT=$(realpath $0) |
| 237 | +SCRIPT_PATH=$(dirname $SCRIPT) |
| 238 | +PROJECT_PATH=$(dirname ${SCRIPT_PATH}) |
| 239 | +PROJECT_NAME=$(basename "$PROJECT_PATH") |
| 240 | + |
| 241 | +pushd "${PROJECT_PATH}" &>/dev/null |
| 242 | +[[ $SUBCMD == "deploy" ]] && deploy_main |
| 243 | +[[ $SUBCMD == "remove" ]] && remove_main |
| 244 | +popd &>/dev/null |
0 commit comments