|
| 1 | +"""Download and configure a pre-built clangd index from GitHub releases.""" |
| 2 | + |
| 3 | +import gzip |
| 4 | +import subprocess |
| 5 | +import urllib.request |
| 6 | +import urllib.error |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def exec_shell(command): |
| 11 | + ret = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 12 | + return ret.stdout |
| 13 | + |
| 14 | + |
| 15 | +def fetch_clangd_index(root: Path): |
| 16 | + """Fetch the clangd index for the nearest dx-* tag and configure .clangd.""" |
| 17 | + tag = exec_shell(["git", "describe", "--tags", "--abbrev=0", "--match", "dx-*"]).strip() |
| 18 | + if not tag: |
| 19 | + return |
| 20 | + |
| 21 | + dx_dir = root / ".dx" |
| 22 | + dx_dir.mkdir(exist_ok=True) |
| 23 | + idx_path = dx_dir / "papermario-dx.idx" |
| 24 | + tag_hash = exec_shell(["git", "rev-parse", f"{tag}^{{}}"]).strip() |
| 25 | + tag_file = dx_dir / "configure-tag" |
| 26 | + |
| 27 | + # Check if we already have the index for this tag |
| 28 | + need_download = True |
| 29 | + if idx_path.exists() and tag_file.exists(): |
| 30 | + current_tag = tag_file.read_text().strip() |
| 31 | + if current_tag == tag_hash: |
| 32 | + need_download = False |
| 33 | + |
| 34 | + if need_download: |
| 35 | + repo = "bates64/papermario-dx" |
| 36 | + url = f"https://github.com/{repo}/releases/download/{tag}/papermario-dx.idx" |
| 37 | + print(f"configure: downloading clangd index for {tag}...") |
| 38 | + try: |
| 39 | + gz_path = idx_path.with_suffix(".idx.gz") |
| 40 | + url_gz = url.replace(".idx", ".idx.gz") |
| 41 | + urllib.request.urlretrieve(url_gz, str(gz_path)) |
| 42 | + # Decompress and replace $$ROOT$$ with local project root |
| 43 | + abs_root = str(root.resolve()) |
| 44 | + with gzip.open(gz_path, "rt") as f: |
| 45 | + content = f.read() |
| 46 | + content = content.replace("$$ROOT$$", abs_root) |
| 47 | + idx_path.write_text(content) |
| 48 | + gz_path.unlink() |
| 49 | + tag_file.write_text(tag_hash + "\n") |
| 50 | + print("configure: clangd index downloaded") |
| 51 | + except urllib.error.HTTPError as e: |
| 52 | + print(f"configure: clangd index not available for {tag} ({e.code}), skipping") |
| 53 | + except Exception as e: |
| 54 | + print(f"configure: failed to download clangd index: {e}") |
| 55 | + |
| 56 | + # Update .clangd config with index path |
| 57 | + if idx_path.exists(): |
| 58 | + _update_clangd_config(root, idx_path) |
| 59 | + |
| 60 | + |
| 61 | +def _update_clangd_config(root: Path, idx_path: Path): |
| 62 | + """Add or update the Index.External section in .clangd.""" |
| 63 | + clangd_path = root / ".clangd" |
| 64 | + abs_idx = str(idx_path.resolve()) |
| 65 | + abs_root = str(root.resolve()) + "/" |
| 66 | + |
| 67 | + # Read existing config, preserving other sections |
| 68 | + existing_lines = [] |
| 69 | + if clangd_path.exists(): |
| 70 | + with open(clangd_path) as f: |
| 71 | + existing_lines = f.readlines() |
| 72 | + |
| 73 | + # Remove any existing Index section |
| 74 | + filtered = [] |
| 75 | + in_index = False |
| 76 | + for line in existing_lines: |
| 77 | + if line.rstrip() == "Index:": |
| 78 | + in_index = True |
| 79 | + continue |
| 80 | + if in_index and (line.startswith(" ") or line.startswith("\t")): |
| 81 | + continue |
| 82 | + in_index = False |
| 83 | + filtered.append(line) |
| 84 | + |
| 85 | + # Append Index.External section |
| 86 | + if filtered and not filtered[-1].endswith("\n"): |
| 87 | + filtered.append("\n") |
| 88 | + filtered.append("Index:\n") |
| 89 | + filtered.append(" External:\n") |
| 90 | + filtered.append(f" File: {abs_idx}\n") |
| 91 | + filtered.append(f" MountPoint: {abs_root}\n") |
| 92 | + |
| 93 | + with open(clangd_path, "w") as f: |
| 94 | + f.writelines(filtered) |
0 commit comments