|
| 1 | +import os |
| 2 | +import json |
| 3 | +import subprocess |
| 4 | +from typing import List, Dict |
| 5 | + |
| 6 | +import re |
| 7 | +import os |
| 8 | +import json |
| 9 | +import subprocess |
| 10 | + |
| 11 | + |
| 12 | +def get_sorted_versions(repo_root): |
| 13 | + # Get the git repository root |
| 14 | + tags_output = subprocess.check_output(['git', '-C', repo_root, 'tag'], text=True).strip().split('\n') |
| 15 | + # Change to the repository root |
| 16 | + original_cwd = os.getcwd() |
| 17 | + os.chdir(repo_root) |
| 18 | + # Fetch tags and branches |
| 19 | + tags_output = subprocess.check_output(['git', 'tag'], text=True).strip().split('\n') |
| 20 | + tag_regex = re.compile(r"^v\d+\.\d+\.\d+$") # Matches tags like v0.0.1 |
| 21 | + tags = [tag for tag in tags_output if tag_regex.match(tag)] |
| 22 | + |
| 23 | + # Sort tags |
| 24 | + def version_key(tag): |
| 25 | + version = tag.lstrip('v') |
| 26 | + return [int(x) for x in version.split('.')] |
| 27 | + |
| 28 | + sorted_tags = sorted(tags, key=version_key, reverse=True) |
| 29 | + |
| 30 | + # Prepare versions list (similar to previous implementation) |
| 31 | + versions = [] |
| 32 | + |
| 33 | + if sorted_tags: |
| 34 | + versions.append({ |
| 35 | + "name": f"{sorted_tags[0]} (stable)", |
| 36 | + "version": sorted_tags[0].lstrip('v'), |
| 37 | + "url": f"/{sorted_tags[0].lstrip('v')}/" |
| 38 | + }) |
| 39 | + |
| 40 | + for tag in sorted_tags[1:]: |
| 41 | + versions.append({ |
| 42 | + "version": tag.lstrip('v'), |
| 43 | + "url": f"/{tag.lstrip('v')}/" |
| 44 | + }) |
| 45 | + |
| 46 | + # Get branches |
| 47 | + branches_output = subprocess.check_output(['git', 'branch', '-r'], text=True).strip().split('\n') |
| 48 | + dev_branches = [ |
| 49 | + branch.strip().split('/')[-1] |
| 50 | + for branch in branches_output |
| 51 | + if 'origin/dev' in branch and "HEAD" not in branch |
| 52 | + ] |
| 53 | + print(dev_branches, branches_output) |
| 54 | + |
| 55 | + for branch in dev_branches: |
| 56 | + versions.append({ |
| 57 | + "version": f"{branch} (dev)", |
| 58 | + "url": f"/{branch}/" |
| 59 | + }) |
| 60 | + |
| 61 | + return versions |
| 62 | + |
| 63 | + |
| 64 | +def generate_versions_json(app, repo_root): |
| 65 | + """ |
| 66 | + Sphinx extension to generate versions.json during documentation build. |
| 67 | + """ |
| 68 | + # Only generate if no exception occurred |
| 69 | + # Ensure _static directory exists |
| 70 | + print(app.srcdir) |
| 71 | + static_dir = os.path.join(repo_root, "docs", '_static') |
| 72 | + os.makedirs(static_dir, exist_ok=True) |
| 73 | + |
| 74 | + # Generate and write versions JSON |
| 75 | + versions = get_sorted_versions(repo_root) |
| 76 | + versions_path = os.path.join(static_dir, 'versions.json') |
| 77 | + |
| 78 | + # Always generate a JSON, even if empty |
| 79 | + with open(versions_path, 'w') as f: |
| 80 | + json.dump(versions, f, indent=4) |
| 81 | + |
| 82 | + print(f"Generated versions.json with {len(versions)} versions") |
0 commit comments