about summary refs log tree commit diff
path: root/pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py
diff options
context:
space:
mode:
authorTomaSajt <62384384+TomaSajt@users.noreply.github.com>2024-03-30 12:22:26 +0100
committerTomaSajt <62384384+TomaSajt@users.noreply.github.com>2024-04-03 12:32:08 +0200
commit92c8f64c5e0f98c5a721fb9188e71aa9c650514a (patch)
tree7aae40428a735810a7b3dbd90e15f851fb43c31a /pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py
parente72670091620c725176d46ec44b6a6eb4bf86e37 (diff)
buildDubPackage, dub-to-nix: init
Diffstat (limited to 'pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py')
-rw-r--r--pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py b/pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py
new file mode 100644
index 0000000000000..48a9f241348a4
--- /dev/null
+++ b/pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import sys
+import json
+import os
+import subprocess
+
+def eprint(text: str):
+    print(text, file=sys.stderr)
+
+if not os.path.exists("dub.selections.json"):
+    eprint("The file `dub.selections.json` does not exist in the current working directory")
+    eprint("run `dub upgrade --annotate` to generate it")
+    sys.exit(1)
+
+with open("dub.selections.json") as f:
+    selectionsJson = json.load(f)
+
+versionDict: dict[str, str] = selectionsJson["versions"]
+
+for pname in versionDict:
+    version = versionDict[pname]
+    if version.startswith("~"):
+        eprint(f'Package "{pname}" has a branch-type version "{version}", which doesn\'t point to a fixed version')
+        eprint("You can resolve it by manually changing the required version to a fixed one inside `dub.selections.json`")
+        eprint("When packaging, you might need to create a patch for `dub.sdl` or `dub.json` to accept the changed version")
+        sys.exit(1)
+
+lockedDependenciesDict: dict[str, dict[str, str]] = {}
+
+for pname in versionDict:
+    version = versionDict[pname]
+    eprint(f"Fetching {pname}@{version}")
+    url = f"https://code.dlang.org/packages/{pname}/{version}.zip"
+    command = ["nix-prefetch-url", "--type", "sha256", url]
+    sha256 = subprocess.run(command, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.rstrip()
+    lockedDependenciesDict[pname] = {"version": version, "sha256": sha256}
+
+print(json.dumps({"dependencies": lockedDependenciesDict}, indent=2))