|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tomllib |
| 7 | +import urllib.request |
| 8 | + |
| 9 | + |
| 10 | +def get_latest_npm(package): |
| 11 | + with urllib.request.urlopen( |
| 12 | + f"https://registry.npmjs.org/{package}/latest" |
| 13 | + ) as response: |
| 14 | + return json.load(response)["version"] |
| 15 | + |
| 16 | + |
| 17 | +def get_latest_pypi(package): |
| 18 | + with urllib.request.urlopen(f"https://pypi.org/pypi/{package}/json") as response: |
| 19 | + return json.load(response)["info"]["version"] |
| 20 | + |
| 21 | + |
| 22 | +def get_latest_github_release(repo): |
| 23 | + request = urllib.request.Request( |
| 24 | + f"https://api.github.com/repos/{repo}/releases/latest" |
| 25 | + ) |
| 26 | + request.add_header("User-Agent", "lsp-client-updater") |
| 27 | + import os |
| 28 | + |
| 29 | + token = os.environ.get("GITHUB_TOKEN") |
| 30 | + if token: |
| 31 | + request.add_header("Authorization", f"Bearer {token}") |
| 32 | + with urllib.request.urlopen(request) as response: |
| 33 | + return json.load(response)["tag_name"] |
| 34 | + |
| 35 | + |
| 36 | +def get_latest_custom(command: str) -> str: |
| 37 | + result = subprocess.run( |
| 38 | + command, shell=True, capture_output=True, text=True, check=True |
| 39 | + ) |
| 40 | + return result.stdout.strip() |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + with open("registry/wiki.toml", "rb") as f: |
| 45 | + wiki = tomllib.load(f) |
| 46 | + |
| 47 | + versions = {} |
| 48 | + for server, config in wiki.items(): |
| 49 | + if not isinstance(config, dict): |
| 50 | + continue |
| 51 | + try: |
| 52 | + if config["type"] == "npm": |
| 53 | + versions[server] = get_latest_npm(config["package"]) |
| 54 | + elif config["type"] == "pypi": |
| 55 | + versions[server] = get_latest_pypi(config["package"]) |
| 56 | + elif config["type"] == "github": |
| 57 | + v = get_latest_github_release(config["repo"]) |
| 58 | + if config.get("strip_v"): |
| 59 | + v = v.lstrip("v") |
| 60 | + versions[server] = v |
| 61 | + elif config["type"] == "custom": |
| 62 | + versions[server] = get_latest_custom(config["command"]) |
| 63 | + except Exception as e: |
| 64 | + print(f"Error fetching version for {server}: {e}", file=sys.stderr) |
| 65 | + continue |
| 66 | + |
| 67 | + print(json.dumps(versions, indent=2)) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments