1#! /usr/bin/env nix-shell
2#! nix-shell -i python3 -p python3 nix-prefetch-git
3
4import json
5import subprocess
6import sys
7from pathlib import Path
8
9THIS_FOLDER = Path(__file__).parent.resolve()
10PUBSPEC_LOCK = THIS_FOLDER / "pubspec.lock.json"
11GIT_HASHES = THIS_FOLDER / "gitHashes.json"
12
13
14def fetch_git_hash(url: str, rev: str) -> str:
15 result = subprocess.run(
16 ["nix-prefetch-git", "--url", url, "--rev", rev],
17 capture_output=True,
18 text=True,
19 check=True,
20 )
21 return json.loads(result.stdout)["hash"]
22
23
24def main() -> None:
25 if not PUBSPEC_LOCK.exists():
26 sys.exit(1)
27 try:
28 data = json.loads(PUBSPEC_LOCK.read_text())
29 except json.JSONDecodeError:
30 sys.exit(1)
31 output: dict[str, str] = {}
32 for name, info in data.get("packages", {}).items():
33 if info.get("source") != "git":
34 continue
35 desc = info.get("description")
36 if not isinstance(desc, dict):
37 continue
38 url = desc.get("url")
39 rev = desc.get("resolved-ref")
40 if not (isinstance(url, str) and isinstance(rev, str)):
41 continue
42 try:
43 package_hash = fetch_git_hash(url, rev)
44 except subprocess.CalledProcessError:
45 continue
46 output[name] = package_hash
47 GIT_HASHES.write_text(json.dumps(output, indent=2) + "\n")
48
49
50if __name__ == "__main__":
51 main()