Skip to content

Commit 476dfa2

Browse files
committed
Review comments
1 parent 3186ccb commit 476dfa2

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

README.md

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

71-
The documentation version switcher (`switcher.json`) is automatically generated from git tags during the build process. Only tagged versions are included by default to ensure all links work correctly.
71+
The documentation version switcher (`switcher.json`) is automatically generated from git tags matching the `vX.Y.Z` format during the build process. Only tagged versions are included by default to ensure all links work correctly.
7272

7373
Options:
7474
- `--skip-build` (`-s`): Skip building before generating docs
@@ -118,11 +118,11 @@ python -m http.server 8000
118118
Then open http://localhost:8000 in your browser. The root automatically redirects to the latest version documentation.
119119

120120
**Versioned Documentation:**
121-
- Each git tag creates a separate documentation version (e.g., `/v26.0.5/`)
121+
- Each git tag matching `vX.Y.Z` creates a separate documentation version (e.g., `/v26.0.5/`)
122122
- A `/latest/` directory points to the newest version
123123
- Root (`/`) automatically redirects to `/latest/`
124124
- Run `git fetch --tags` before building to ensure all version tags are available
125-
- If no version tags (tags starting with `v`) are found in the repository, the build automatically falls back to a single-version Sphinx build instead of attempting the multi-version build
125+
- If no version tags matching `vX.Y.Z` are found in the repository, the build automatically falls back to a single-version Sphinx build instead of attempting the multi-version build
126126

127127
### Running the Formatter
128128

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
]
3535

3636
templates_path = ['_templates']
37-
smv_tag_whitelist = r'^v?\d+\.\d+\.\d+$'
37+
smv_tag_whitelist = r'^v\d+\.\d+\.\d+$'
3838
smv_branch_whitelist = r'^$'
3939
smv_remote_whitelist = r'^origin$'
4040
smv_latest_version = 'latest'

docs/source/readme.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,9 @@ The project includes a ``run.py`` script with several useful commands:
12541254
- ``python run.py test`` - Run tests
12551255
- ``python run.py lint`` - Run code linting
12561256
- ``python run.py format`` - Format code with black
1257-
- ``python run.py build-docs`` - Build versioned documentation (HTML uses git tags for the
1258-
navigation dropdown; run ``git fetch --tags`` locally before building)
1257+
- ``python run.py build-docs`` - Build versioned documentation (HTML uses git tags matching
1258+
``vX.Y.Z`` for the navigation dropdown; run ``git fetch --tags`` locally before building).
1259+
Falls back to a single-version build when no matching version tags are found.
12591260

12601261
- ``--skip-build`` (``-s``): Skip building the package before generating docs
12611262
- ``--local`` (``-l``): Build documentation locally for a single version (skips multi-version build)
@@ -1265,9 +1266,9 @@ The project includes a ``run.py`` script with several useful commands:
12651266

12661267
.. note::
12671268
The documentation version switcher (``switcher.json``) is automatically generated from
1268-
git tags during the build process. Only tagged versions are included to ensure all
1269-
documentation links work correctly. If ``version.json`` is newer than the latest tag,
1270-
create a git tag to include it in the version switcher.
1269+
git tags matching the ``vX.Y.Z`` format during the build process. Only tagged versions
1270+
are included to ensure all documentation links work correctly. If ``version.json`` is
1271+
newer than the latest tag, create a git tag to include it in the version switcher.
12711272

12721273
**Incremental Builds for Development:**
12731274

@@ -1296,11 +1297,11 @@ Then open http://localhost:8000 in your browser.
12961297

12971298
**Versioned Documentation Features:**
12981299

1299-
- Each git tag creates a separate documentation version (e.g., ``/v26.0.5/``)
1300+
- Each git tag matching ``vX.Y.Z`` creates a separate documentation version (e.g., ``/v26.0.5/``)
13001301
- A ``/latest/`` directory points to the newest version (symlink on Unix, copy on Windows)
13011302
- Root (``/``) automatically redirects to ``/latest/`` for convenience
13021303
- Version switcher dropdown in the navigation bar allows switching between versions
1303-
- If no version tags (tags starting with ``v``) are found in the repository, the build
1304+
- If no version tags matching ``vX.Y.Z`` are found in the repository, the build
13041305
automatically falls back to a single-version Sphinx build instead of attempting the
13051306
multi-version build
13061307

run.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
build-docs Build the documentation.
2929
format Format all Python files in the repository using black.
3030
generate-expected-data Generate expected data for integration tests.
31-
generate-switcher Generate switcher.json from git tags.
31+
generate-switcher Generate switcher.json from git tags (vX.Y.Z format).
3232
install Install the moldflow-api package.
3333
install-package-requirements Install package dependencies.
3434
lint Lint all Python files in the repository.
@@ -67,6 +67,7 @@
6767
"""
6868

6969
import os
70+
import re
7071
import sys
7172
import json
7273
import logging
@@ -118,6 +119,14 @@
118119
PYTHON_FILES = [MOLDFLOW_DIR, DOCS_SOURCE_DIR, TEST_DIR, "run.py"]
119120
SWITCHER_JSON = os.path.join(DOCS_STATIC_DIR, 'switcher.json')
120121

122+
# Must match smv_tag_whitelist in docs/source/conf.py
123+
VERSION_TAG_RE = re.compile(r'^v\d+\.\d+\.\d+$')
124+
125+
126+
def _is_version_tag(name):
127+
"""Return True if *name* matches the vX.Y.Z version tag format."""
128+
return VERSION_TAG_RE.match(name) is not None
129+
121130

122131
def run_command(args, cwd=os.getcwd(), extra_env=None):
123132
"""Runs native executable command, args is an array of strings"""
@@ -367,7 +376,7 @@ def create_root_redirect(build_output: str) -> None:
367376

368377
def create_latest_alias(build_output: str) -> None:
369378
"""Create a 'latest' alias pointing to the newest version using symlinks when possible."""
370-
version_dirs = [d for d in os.listdir(build_output) if d.startswith('v')]
379+
version_dirs = [d for d in os.listdir(build_output) if _is_version_tag(d)]
371380
if not version_dirs:
372381
return
373382

@@ -429,9 +438,9 @@ def _build_html_docs_full(build_output, skip_switcher, include_current):
429438
"Failed to build documentation with "
430439
"sphinx_multiversion.\n"
431440
"This can happen if no Git tags or branches match "
432-
"your version pattern.\n"
441+
"the required vX.Y.Z version pattern.\n"
433442
"Try running 'git fetch --tags' and ensure version "
434-
"tags exist in the repo.\n"
443+
"tags (e.g. v1.2.3) exist in the repo.\n"
435444
"Underlying error: %s",
436445
str(err),
437446
)
@@ -445,11 +454,11 @@ def _get_missing_version_tags(build_output):
445454
if os.path.exists(build_output):
446455
for item in os.listdir(build_output):
447456
item_path = os.path.join(build_output, item)
448-
if os.path.isdir(item_path) and item.startswith('v'):
457+
if os.path.isdir(item_path) and _is_version_tag(item):
449458
if item != 'latest':
450459
existing_versions.add(item)
451460

452-
all_tags = {tag.name for tag in GIT_REPO.tags if tag.name.startswith('v')}
461+
all_tags = {tag.name for tag in GIT_REPO.tags if _is_version_tag(tag.name)}
453462

454463
missing = list(all_tags - existing_versions)
455464

@@ -570,11 +579,14 @@ def _run_sphinx_build(target):
570579
)
571580

572581

573-
def _remove_stale_switcher():
574-
"""Remove any leftover switcher.json so a single-version build doesn't ship stale data."""
582+
def _clean_stale_docs():
583+
"""Remove stale switcher.json from the source tree and wipe the build directory."""
575584
if os.path.exists(SWITCHER_JSON):
576585
os.remove(SWITCHER_JSON)
577586
logging.info('Removed stale %s', SWITCHER_JSON)
587+
if os.path.exists(DOCS_BUILD_DIR):
588+
logging.info('Removing stale build directory %s', DOCS_BUILD_DIR)
589+
shutil.rmtree(DOCS_BUILD_DIR)
578590

579591

580592
# pylint: disable=R0912
@@ -590,13 +602,12 @@ def build_docs(
590602

591603
if not incremental:
592604
logging.info('Removing existing Sphinx documentation...')
593-
if os.path.exists(DOCS_BUILD_DIR):
594-
shutil.rmtree(DOCS_BUILD_DIR)
605+
_clean_stale_docs()
595606
else:
596607
logging.info('Incremental build mode: preserving existing documentation...')
597608

598609
try:
599-
has_version_tags = any(tag.name.startswith('v') for tag in GIT_REPO.tags)
610+
has_version_tags = any(_is_version_tag(tag.name) for tag in GIT_REPO.tags)
600611

601612
if target == 'html' and not local and has_version_tags:
602613
build_output = os.path.join(DOCS_BUILD_DIR, 'html')
@@ -610,21 +621,21 @@ def build_docs(
610621
create_root_redirect(build_output)
611622
elif target == 'html' and not local and not has_version_tags:
612623
logging.warning(
613-
'No version tags starting with \'v\' found in the repository. '
624+
'No version tags matching vX.Y.Z found in the repository. '
614625
'Falling back to a single-version documentation build.'
615626
)
616-
_remove_stale_switcher()
627+
_clean_stale_docs()
617628
_run_sphinx_build(target)
618629
else:
619630
if target == 'html' and not skip_switcher:
620631
if has_version_tags:
621632
generate_switcher(include_current=include_current)
622633
else:
623634
logging.warning(
624-
'No version tags starting with \'v\' found in the repository. '
635+
'No version tags matching vX.Y.Z found in the repository. '
625636
'Skipping switcher.json generation.'
626637
)
627-
_remove_stale_switcher()
638+
_clean_stale_docs()
628639
_run_sphinx_build(target)
629640
logging.info('Sphinx documentation built successfully.')
630641
except Exception as err:
@@ -876,7 +887,7 @@ def _get_current_version_if_newer():
876887
)
877888

878889
# Get latest git tag
879-
tags = [tag.name for tag in GIT_REPO.tags if tag.name.startswith('v')]
890+
tags = [tag.name for tag in GIT_REPO.tags if _is_version_tag(tag.name)]
880891

881892
if not tags:
882893
return None
@@ -898,8 +909,8 @@ def _get_current_version_if_newer():
898909

899910

900911
def generate_switcher(include_current=False):
901-
"""Generate switcher.json from git tags"""
902-
logging.info('Generating switcher.json from git tags')
912+
"""Generate switcher.json from git tags (vX.Y.Z format)."""
913+
logging.info('Generating switcher.json from git tags (vX.Y.Z)')
903914
switcher_script = os.path.join(ROOT_DIR, 'scripts', 'generate_switcher.py')
904915
cmd = [sys.executable, switcher_script]
905916
if include_current:

scripts/generate_switcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
# Must match smv_tag_whitelist in docs/source/conf.py
27-
SMV_TAG_PATTERN = re.compile(r'^v?\d+\.\d+\.\d+$')
27+
SMV_TAG_PATTERN = re.compile(r'^v\d+\.\d+\.\d+$')
2828

2929

3030
# Paths

0 commit comments

Comments
 (0)