From 1ee986a228089868ef42924132df9862c486f9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melissa=20Weber=20Mendon=C3=A7a?= Date: Tue, 22 Oct 2024 14:07:56 -0300 Subject: [PATCH] Add script to update version switcher --- .github/workflows/build_and_deploy.yml | 12 +++++++++ docs/_scripts/update_switcher.py | 34 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 docs/_scripts/update_switcher.py diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index f48a1d5f2..c4d6a55d2 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -76,6 +76,18 @@ jobs: cd docs make fallback-videos + - name: Update version switcher (on new release) + if: ((github.event_name == 'release' && !contains(github.event.release.tag_name, 'rc')) || (github.event_name == 'workflow_dispatch' && github.event.inputs.target_directory != 'dev')) + run: | + python docs/_scripts/update_switcher.py ${{ github.event.inputs.target_directory || github.event.release.tag_name }} + + - name: Commit new version switcher + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add docs/_static/version_switcher.json + git commit -m "Update version switcher for release" --allow-empty + - name: Build Docs uses: aganders3/headless-gui@v2 env: diff --git a/docs/_scripts/update_switcher.py b/docs/_scripts/update_switcher.py new file mode 100644 index 000000000..b7d934ba6 --- /dev/null +++ b/docs/_scripts/update_switcher.py @@ -0,0 +1,34 @@ +import sys +import json + + +def update_version_switcher(new_version): + """Update version_switcher.json after a new release.""" + with open("docs/_static/version_switcher.json", "r") as f: + switcher = json.load(f) + oldstable = switcher[1] + + newstable = oldstable.copy() + newstable["version"] = new_version + newstable["name"] = f"stable ({new_version})" + + oldstable["name"] = f"{oldstable['version']}" + del oldstable["preferred"] + oldstable["url"] = oldstable["url"].replace("stable", oldstable["version"]) + + switcher[1] = oldstable + switcher.insert(1, newstable) + with open("docs/_static/version_switcher.json", "w") as f: + json.dump(switcher, f, indent=4) + + print(f"Version switcher updated to {new_version}") + print(f"Old stable version: {switcher[2]}") + print(f"New stable version: {switcher[1]}") + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python update_switcher.py ") + sys.exit() + new_version = sys.argv[1] + update_version_switcher(new_version)