1#! /usr/bin/env nix-shell
2#! nix-shell -p python3 -i python3
3import os
4import urllib.request as ureq
5import json
6import html
7
8if not all(
9 f"UPDATE_NIX_{v}" in os.environ
10 for v in ["NAME", "PNAME", "OLD_VERSION", "ATTR_PATH"]
11) or not os.environ['UPDATE_NIX_ATTR_PATH'].startswith("nerd-fonts."):
12 raise Exception(
13 "Please don't run this script manually, only with:\n"
14 "nix-shell maintainers/scripts/update.nix --argstr path nerd-fonts "
15 "--argstr commit true"
16 )
17
18RELEASE_INFO_URL = "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest"
19FONTS_INFO_URL_TEMPLATE = "https://raw.githubusercontent.com/ryanoasis/nerd-fonts/refs/tags/{}/bin/scripts/lib/fonts.json"
20SHA256_URL_TEMPLATE = "https://github.com/ryanoasis/nerd-fonts/releases/download/{}/SHA-256.txt"
21
22RELEASE_INFO_FILENAME = "release.json"
23FONTS_INFO_FILENAME = "fonts.json"
24CHECKSUMS_FILENAME = "checksums.json"
25
26def fetchjson(url):
27 with ureq.urlopen(url) as r:
28 return json.loads(r.read())
29
30def storejson(path, obj):
31 with open(path, "w", encoding="utf-8") as f:
32 json.dump(obj, f, indent=2, ensure_ascii=False)
33 # Needed to satisfy EditorConfig's rules
34 f.write('\n')
35
36def slicedict(d, ks):
37 return {k: html.unescape(d[k]) for k in ks}
38
39os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), "manifests"))
40
41release_info = slicedict(
42 fetchjson(RELEASE_INFO_URL),
43 ["tag_name"]
44)
45
46tag_name = release_info["tag_name"]
47with open(RELEASE_INFO_FILENAME, "r", encoding="utf-8") as f:
48 former_tag_name = json.load(f)["tag_name"]
49if tag_name == former_tag_name:
50 raise Exception("no newer version available")
51# See: https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#supported-features
52print(json.dumps(
53 [
54 {
55 "attrPath": "nerd-fonts",
56 "oldVersion": former_tag_name.removeprefix("v"),
57 "newVersion": tag_name.removeprefix("v"),
58 },
59 ],
60 indent=2
61))
62
63storejson(RELEASE_INFO_FILENAME, release_info)
64
65storejson(
66 FONTS_INFO_FILENAME,
67 [
68 slicedict(
69 item,
70 [
71 "caskName",
72 "description",
73 "folderName",
74 "licenseId",
75 "patchedName",
76 "version",
77 ]
78 )
79 for item in fetchjson(FONTS_INFO_URL_TEMPLATE.format(tag_name))["fonts"]
80 ],
81)
82
83storejson(
84 CHECKSUMS_FILENAME,
85 {
86 filename: sha256
87 for row in ureq.urlopen(SHA256_URL_TEMPLATE.format(tag_name))
88 for sha256, filename in [row.decode('utf-8').split()]
89 if filename.endswith(".tar.xz")
90 },
91)