Skip to content

Commit 7510729

Browse files
committed
Automate generation of switcher.json for Sphinx version dropdown
1 parent a02362f commit 7510729

7 files changed

Lines changed: 352 additions & 49 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ jobs:
5151

5252
steps:
5353
- uses: actions/checkout@v5
54+
with:
55+
fetch-depth: 0 # Fetch all history and tags
5456

5557
- name: Set up Python
5658
uses: actions/setup-python@v5

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ instance/
7171

7272
# Sphinx documentation
7373
docs/_build/
74+
docs/source/_static/switcher.json
7475

7576
# PyBuilder
7677
.pybuilder/

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ python run.py build
6868
python run.py build-docs
6969
```
7070

71-
> ***Note: When releasing a new version, update ``switcher.json`` in ``docs/source/_static/`` to include the new tag in the version dropdown for documentation.***
71+
The documentation version switcher (`switcher.json`) is automatically generated from git tags and `version.json` during the build process.
7272

7373
Options:
7474
- `--skip-build` (`-s`): Skip building before generating docs
7575
- `--local` (`-l`): Build documentation locally for a single version (skips multi-version build)
76+
- `--skip-switcher`: Skip generating switcher.json (useful for offline builds or custom switcher configurations)
77+
78+
**Debug command:** To manually generate `switcher.json` without building docs:
79+
```sh
80+
python run.py generate-switcher
81+
```
7682

7783
The documentation can be accessed locally by serving the docs/build/html/ folder:
7884
```sh

docs/source/_static/switcher.json

Lines changed: 0 additions & 44 deletions
This file was deleted.

docs/source/readme.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,10 +1259,11 @@ The project includes a ``run.py`` script with several useful commands:
12591259

12601260
- ``--skip-build`` (``-s``): Skip building the package before generating docs
12611261
- ``--local`` (``-l``): Build documentation locally for a single version (skips multi-version build)
1262+
- ``--skip-switcher``: Skip generating switcher.json (useful for offline builds or custom switcher configurations)
12621263

12631264
.. note::
1264-
When releasing a new version, update ``switcher.json`` in ``docs/source/_static/``
1265-
to include the new tag in the version dropdown.
1265+
The documentation version switcher (``switcher.json``) is automatically generated from
1266+
git tags and ``version.json`` during the build process.
12661267

12671268
**Viewing Documentation Locally**
12681269

run.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
run.py clean-up
99
run.py build [-P | --publish] [-i | --install]
1010
run.py build-docs [-t <target> | --target=<target>] [-s | --skip-build] [-l | --local]
11+
[--skip-switcher]
1112
run.py format [--check]
1213
run.py generate-expected-data [<markers>...]
14+
run.py generate-switcher
1315
run.py install [-s | --skip-build]
1416
run.py install-package-requirements
1517
run.py lint [-s | --skip-build]
@@ -25,6 +27,7 @@
2527
build-docs Build the documentation.
2628
format Format all Python files in the repository using black.
2729
generate-expected-data Generate expected data for integration tests.
30+
generate-switcher Generate switcher.json from git tags.
2831
install Install the moldflow-api package.
2932
install-package-requirements Install package dependencies.
3033
lint Lint all Python files in the repository.
@@ -54,6 +57,7 @@
5457
--repo-url=<url> Custom PyPI repository URL.
5558
--github-api-url=<url> Custom GitHub API URL.
5659
-l, --local Build documentation locally (single version).
60+
--skip-switcher Skip generating switcher.json for documentation.
5761
<markers> Markers to filter data generation by: mesh_summary, etc.
5862
"""
5963

@@ -402,7 +406,7 @@ def version_key(v):
402406
shutil.copytree(latest_src, latest_dest)
403407

404408

405-
def build_docs(target, skip_build, local=False):
409+
def build_docs(target, skip_build, local=False, skip_switcher=False):
406410
"""Build Documentation"""
407411

408412
if not skip_build:
@@ -416,6 +420,9 @@ def build_docs(target, skip_build, local=False):
416420

417421
try:
418422
if target == 'html' and not local:
423+
# Generate switcher.json for docs versioning dropdown from git tags and version.json
424+
if not skip_switcher:
425+
generate_switcher()
419426
build_output = os.path.join(DOCS_BUILD_DIR, 'html')
420427
try:
421428
# fmt: off
@@ -688,6 +695,13 @@ def generate_expected_data(markers: list[str]):
688695
run_command([sys.executable, '-m', generate_data_module] + markers, ROOT_DIR)
689696

690697

698+
def generate_switcher():
699+
"""Generate switcher.json from git tags"""
700+
logging.info('Generating switcher.json from git tags')
701+
switcher_script = os.path.join(ROOT_DIR, 'scripts', 'generate_switcher.py')
702+
run_command([sys.executable, switcher_script], ROOT_DIR)
703+
704+
691705
def set_version():
692706
"""Set current version and write version file to package directory"""
693707

@@ -734,6 +748,9 @@ def main():
734748
markers = args.get('<markers>') or []
735749
generate_expected_data(markers=markers)
736750

751+
elif args.get('generate-switcher'):
752+
generate_switcher()
753+
737754
elif args.get('test'):
738755
tests = args.get('<tests>') or []
739756
marker = args.get('--marker') or args.get('-m')
@@ -779,8 +796,11 @@ def main():
779796
target = args.get('--target') or args.get('-t') or 'html'
780797
skip_build = args.get('--skip-build') or args.get('-s')
781798
local = args.get('--local') or args.get('-l')
799+
skip_switcher = args.get('--skip-switcher')
782800

783-
build_docs(target=target, skip_build=skip_build, local=local)
801+
build_docs(
802+
target=target, skip_build=skip_build, local=local, skip_switcher=skip_switcher
803+
)
784804

785805
elif args.get('install-package-requirements'):
786806
install_package(target_path=os.path.join(ROOT_DIR, SITE_PACKAGES))

0 commit comments

Comments
 (0)