diff --git a/justfile b/justfile index 49ce39b..3c4724d 100644 --- a/justfile +++ b/justfile @@ -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: diff --git a/scripts/checkout-version.sh b/scripts/checkout-version.sh new file mode 100755 index 0000000..860fbb8 --- /dev/null +++ b/scripts/checkout-version.sh @@ -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 +$0 + +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 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