From f0680ae0e2671724d0efe8d80312a9ce77fdbb84 Mon Sep 17 00:00:00 2001 From: qzylinra Date: Tue, 19 Aug 2025 18:32:17 +0800 Subject: [PATCH] pub2nix.generatePackageGraph: init --- .../hooks/dart-config-hook.sh | 1 + .../build-dart-application/hooks/default.nix | 3 ++ .../dart/pub2nix/package-graph.py | 54 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 pkgs/build-support/dart/pub2nix/package-graph.py diff --git a/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh b/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh index 50754a7b56d4..56f20a4f63a3 100644 --- a/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh +++ b/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh @@ -9,6 +9,7 @@ dartConfigHook() { echo "Installing dependencies" mkdir -p .dart_tool cp "$packageConfig" .dart_tool/package_config.json + @python3@ @packageGraphScript@ > .dart_tool/package_graph.json packagePath() { jq --raw-output --arg name "$1" '.packages.[] | select(.name == $name) .rootUri | sub("file://"; "")' .dart_tool/package_config.json diff --git a/pkgs/build-support/dart/build-dart-application/hooks/default.nix b/pkgs/build-support/dart/build-dart-application/hooks/default.nix index bf4b88da5d2e..64f1019f882f 100644 --- a/pkgs/build-support/dart/build-dart-application/hooks/default.nix +++ b/pkgs/build-support/dart/build-dart-application/hooks/default.nix @@ -4,6 +4,7 @@ dart, yq, jq, + python3, }: { @@ -11,6 +12,8 @@ name = "dart-config-hook"; substitutions.yq = "${yq}/bin/yq"; substitutions.jq = "${jq}/bin/jq"; + substitutions.python3 = lib.getExe (python3.withPackages (ps: with ps; [ pyyaml ])); + substitutions.packageGraphScript = ../../pub2nix/package-graph.py; } ./dart-config-hook.sh; dartBuildHook = makeSetupHook { name = "dart-build-hook"; diff --git a/pkgs/build-support/dart/pub2nix/package-graph.py b/pkgs/build-support/dart/pub2nix/package-graph.py new file mode 100644 index 000000000000..a63b5b042045 --- /dev/null +++ b/pkgs/build-support/dart/pub2nix/package-graph.py @@ -0,0 +1,54 @@ +""" +https://github.com/dart-lang/pub/issues/4522 +This script generates a package_graph.json file. +""" + +import json +import os +from pathlib import Path +from urllib.parse import unquote, urlparse + +import yaml + + +def get_package(pubspec_path: Path, dev_dependencies: bool = False): + with pubspec_path.open("r", encoding="utf-8") as f: + pubspec = yaml.load(f, Loader=yaml.CSafeLoader) + package = { + "name": pubspec["name"], + "version": pubspec.get("version") or "0.0.0", + "dependencies": list(pubspec.get("dependencies") or {}), + } + if dev_dependencies: + package["devDependencies"] = list(pubspec.get("dev_dependencies") or {}) + return package + + +def main() -> None: + package_config_file_path = Path(os.environ["packageConfig"]) # noqa: SIM112 + with package_config_file_path.open("r", encoding="utf-8") as f: + package_config = json.load(f) + package_graph = [] + root_package = get_package(Path("pubspec.yaml"), dev_dependencies=True) + for data in package_config.get("packages", []): + if data["name"] == root_package["name"] or data["rootUri"] == "flutter_gen": + continue + package_graph.append( + get_package(Path(unquote(urlparse(data["rootUri"]).path)) / "pubspec.yaml") + ) + package_graph.append(root_package) + print( + json.dumps( + { + "roots": [root_package["name"]], + "packages": package_graph, + "configVersion": 1, + }, + indent=2, + ensure_ascii=False, + ) + ) + + +if __name__ == "__main__": + main()