-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from reubenmiller/feat-version-rotation
feat: delete older versions of the same major version when creating a snapshot
- Loading branch information
Showing
2 changed files
with
162 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
if [ -n "${CI:-}" ]; then | ||
# Enable debugging in CI builds | ||
set -x | ||
fi | ||
|
||
help() { | ||
cat << EOT | ||
Create snapshot of the documentation | ||
$0 <VERSION> | ||
$0 <VERSION> <BRANCH> | ||
POSITIONAL ARGUMENTS | ||
VERSION Documentation version to snapshot | ||
BRANCH Branch to use as the source of the doc snapshot (e.g. useful when backporting doc fixes from the main branch) | ||
FLAGS | ||
--project|-p <PROJECT> Github project to use as source | ||
--remove/--skip-remove Remove older versions with the same major version | ||
EXAMPLES | ||
$0 1.1.1 | ||
# Create a doc snapshot for the tagged 1.1.1 version | ||
$0 1.1.1 main | ||
# Create a doc snapshot for the 1.1.1 version but use the 'main' branch as the source (not the 1.1.1 tag) | ||
$0 1.2.0 --skip-remove | ||
# Create a doc snapshot but don't remove older versions | ||
EOT | ||
} | ||
|
||
log () { | ||
echo "$@" >&2 | ||
} | ||
|
||
fail_with_usage () { | ||
echo "ERROR: $*" >&2 | ||
echo >&2 | ||
help | ||
exit 1 | ||
} | ||
|
||
PROJECT=${PROJECT:-} | ||
REMOVE_OLD_VERSIONS=${REMOVE_OLD_VERSIONS:-1} | ||
|
||
POSITIONAL_ARGS=() | ||
|
||
while [ $# -gt 0 ]; do | ||
case "$1" in | ||
--project) | ||
PROJECT="$2" | ||
shift | ||
;; | ||
|
||
# Remove old versions | ||
--remove) | ||
REMOVE_OLD_VERSIONS=1 | ||
;; | ||
--skip-remove) | ||
REMOVE_OLD_VERSIONS=0 | ||
;; | ||
|
||
--help|-h) | ||
help | ||
exit 0 | ||
;; | ||
--*|-*) | ||
fail_with_usage "Unknown flag. $1" | ||
;; | ||
*) | ||
POSITIONAL_ARGS+=("$1") | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
# Restore positional arguments | ||
set -- "${POSITIONAL_ARGS[@]}" | ||
|
||
if [ -z "$PROJECT" ]; then | ||
fail_with_usage "Project is empty or not defined" | ||
fi | ||
|
||
if [ $# -lt 1 ]; then | ||
fail_with_usage "Missing required positional argument, VERSION" | ||
fi | ||
|
||
VERSION="$1" | ||
BRANCH=${BRANCH:-} | ||
|
||
if [ $# -ge 2 ]; then | ||
BRANCH="$2" | ||
fi | ||
|
||
log "Cloning latest official release" | ||
rm -rf ../thin-edge.io_versioned | ||
git clone "$PROJECT" ../thin-edge.io_versioned | ||
|
||
TAG="$VERSION" | ||
if [ -z "$TAG" ]; then | ||
TAG="$(git -C ../thin-edge.io_versioned describe --tags "$(git -C ../thin-edge.io_versioned rev-list --tags --max-count=1)")" | ||
fi | ||
|
||
SOURCE_REPO_INFO="" | ||
if [ -n "$BRANCH" ]; then | ||
log "Checking out a branch" | ||
git -C ../thin-edge.io_versioned checkout "$BRANCH" | ||
SOURCE_REPO_INFO="$(git -C ../thin-edge.io_versioned describe --always) $BRANCH" | ||
else | ||
log "Checking out a tag" | ||
git -C ../thin-edge.io_versioned checkout "$TAG" -b "latest" | ||
SOURCE_REPO_INFO="$TAG" | ||
fi | ||
|
||
log "Copying docs from $TAG" | ||
mkdir -p versioned_docs | ||
rm -rf "./versioned_docs/version-$TAG" | ||
# Symlinks are not supported here, so we have to copy the files | ||
cp -R ../thin-edge.io_versioned/docs/src "./versioned_docs/version-$TAG" | ||
|
||
# Store marker info about the source repo so it is easier to trace | ||
printf '%s' "$SOURCE_REPO_INFO" > "./versioned_docs/version-$TAG/.version" | ||
|
||
# Remove old versions | ||
if [ "$REMOVE_OLD_VERSIONS" = 1 ]; then | ||
log "Removing older versions (only keeping the latest of each major version)" | ||
# Note: Use `sort -Vr` to sort versions naturally (e.g. 1.2.0 < 1.10.0) | ||
# and in reverse order (as the most recent should be kept) | ||
prev_major_version= | ||
find versioned_docs -type d -depth 1 | sort -Vr | | ||
while read -r cur_path; do | ||
cur_version=$(echo "$cur_path" | cut -d- -f2) | ||
major_version=$(echo "$cur_version" | cut -d. -f1) | ||
if [ "$major_version" = "$prev_major_version" ]; then | ||
log "Removing previous major version. $cur_path" | ||
rm -rf "$cur_path" | ||
rm -f "versioned_sidebars/version-${cur_version}-sidebars.json" | ||
fi | ||
prev_major_version="$major_version" | ||
done | ||
fi | ||
|
||
# Create versions | ||
log "Creating list of versions to be included" | ||
find versioned_docs -type d -depth 1 | sort -Vr | cut -d- -f2 | jq -s -R 'split("\n") | .[:-1]' -c > versions.json | ||
|
||
log "Creating versioned sidebars" | ||
mkdir -p versioned_sidebars | ||
|
||
# Rebuild all versioned sidebars | ||
for folder in versioned_docs/*; do | ||
version_dir=$(basename "$folder") | ||
printf '{"tutorialSidebar": [{"type": "autogenerated", "dirName": "."}]}' > "versioned_sidebars/${version_dir}-sidebars.json" | ||
done |