Skip to content

Commit

Permalink
Merge pull request #84 from reubenmiller/feat-version-rotation
Browse files Browse the repository at this point in the history
feat: delete older versions of the same major version when creating a snapshot
  • Loading branch information
reubenmiller authored Jul 18, 2024
2 parents d5a9f49 + e80d593 commit 88cbd47
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 40 deletions.
42 changes: 2 additions & 40 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,8 @@ init:
# they are only copied during build time. This is to reduce the 'noise'
# in the commit history.
# See the docs for more info: https://docusaurus.io/docs/versioning
checkout-version version="" branch="":
#!/usr/bin/env bash
set -euxo pipefail
echo "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
echo "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
echo "Checking out a tag"
git -C ../thin-edge.io_versioned checkout "$TAG" -b "latest"
SOURCE_REPO_INFO="$TAG"
fi

echo "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"

# Create versions
echo "Creating list of versions to be included (only 1 is supported atm)"
ls -1r versioned_docs | cut -d- -f2- | jq -s -R 'split("\n") | .[:-1]' -c > versions.json

echo "Creating versioned sidebars"
mkdir -p versioned_sidebars
printf '{"tutorialSidebar": [{"type": "autogenerated", "dirName": "."}]}' > "versioned_sidebars/version-$TAG-sidebars.json"
checkout-version version="" branch="" *ARGS="":
./scripts/checkout-version.sh {{version}} {{branch}} --project {{project}} {{ARGS}}

# Format nodejs code
format:
Expand Down
160 changes: 160 additions & 0 deletions scripts/checkout-version.sh
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

0 comments on commit 88cbd47

Please sign in to comment.