1{
2 stdenv,
3 pkgs,
4 lib,
5 writeScript,
6 python3,
7 common-updater-scripts,
8}:
9{
10 packageName,
11 attrPath ? packageName,
12 versionPolicy ? "tagged",
13 freeze ? false,
14}:
15
16let
17 python = python3.withPackages (p: [
18 p.requests
19 p.libversion
20 ]);
21 package =
22 lib.attrByPath (lib.splitString "." attrPath) (throw "Cannot find attribute ‘${attrPath}’.")
23 pkgs;
24 packageVersion = lib.getVersion package;
25 upperBound =
26 let
27 versionComponents = lib.versions.splitVersion packageVersion;
28 minorVersion = lib.versions.minor packageVersion;
29 minorAvailable =
30 builtins.length versionComponents > 1 && builtins.match "[0-9]+" minorVersion != null;
31 nextMinor = builtins.fromJSON minorVersion + 1;
32 upperBound = "${lib.versions.major packageVersion}.${builtins.toString nextMinor}";
33 in
34 if builtins.isBool freeze then
35 lib.optionals (freeze && minorAvailable) [ upperBound ]
36 else if builtins.isString freeze then
37 [ freeze ]
38 else
39 throw "“freeze” argument needs to be either a boolean, or a version string.";
40 updateScript = writeScript "gnome-update-script" ''
41 #!${python}/bin/python
42 import json
43 import os
44 import subprocess
45 import sys
46 from libversion import Version
47
48 _, attr_path, package_name, package_version, version_policy, *remaining_args = sys.argv
49
50 flv_args = [
51 package_name,
52 version_policy,
53 os.environ.get("GNOME_UPDATE_STABILITY", "stable"),
54 ]
55
56 match remaining_args:
57 case []:
58 pass
59 case [upper_bound]:
60 flv_args.append(f"--upper-bound={upper_bound}")
61 case other:
62 print("gnome-update-script: Received too many arguments.", file=sys.stderr)
63 sys.exit(1)
64
65 latest_tag = subprocess.check_output(
66 [
67 "${python}/bin/python",
68 "${./find-latest-version.py}",
69 *flv_args,
70 ],
71 encoding="utf-8",
72 )
73
74 if Version(latest_tag) <= Version(package_version):
75 # No newer updates found.
76 print(json.dumps([]))
77 sys.exit(0)
78
79 latest_tag = latest_tag.strip()
80 subprocess.run(
81 [
82 "${common-updater-scripts}/bin/update-source-version",
83 attr_path,
84 latest_tag,
85 ],
86 check=True,
87 )
88
89 report = [
90 {
91 "attrPath": attr_path,
92 "commitBody": f"https://gitlab.gnome.org/GNOME/{package_name}/-/compare/{package_version}...{latest_tag}",
93 },
94 ]
95
96 print(json.dumps(report))
97 '';
98in
99{
100 name = "gnome-update-script";
101 command = [
102 updateScript
103 attrPath
104 packageName
105 packageVersion
106 versionPolicy
107 ]
108 ++ upperBound;
109 supportedFeatures = [
110 "commit"
111 ];
112 inherit attrPath;
113}