buildDartApplication: add workspace-package-config.py

This commit is contained in:
qzylinra 2025-10-22 08:41:27 +08:00
parent 1b1e9e78b5
commit a8e88a1be4
3 changed files with 46 additions and 0 deletions

View file

@ -9,6 +9,9 @@ dartConfigHook() {
echo "Installing dependencies"
mkdir -p .dart_tool
cp "$packageConfig" .dart_tool/package_config.json
chmod u+w .dart_tool/package_config.json
@python3@ @workspacePackageConfigScript@
chmod u-w .dart_tool/package_config.json
@python3@ @packageGraphScript@ > .dart_tool/package_graph.json
packagePath() {

View file

@ -14,6 +14,7 @@
substitutions.jq = "${jq}/bin/jq";
substitutions.python3 = lib.getExe (python3.withPackages (ps: with ps; [ pyyaml ]));
substitutions.packageGraphScript = ../../pub2nix/package-graph.py;
substitutions.workspacePackageConfigScript = ../workspace-package-config.py;
} ./dart-config-hook.sh;
dartBuildHook = makeSetupHook {
name = "dart-build-hook";

View file

@ -0,0 +1,42 @@
import json
import re
from pathlib import Path
import yaml
def main() -> None:
with Path("pubspec.yaml").open("r", encoding="utf-8") as f:
pubspec = yaml.load(f, Loader=yaml.CSafeLoader)
if not pubspec.get("workspace"):
return
with Path(".dart_tool/package_config.json").open("r", encoding="utf-8") as f:
package_config = json.load(f)
for package_path in pubspec.get("workspace", []):
with (Path(package_path) / "pubspec.yaml").open("r", encoding="utf-8") as f:
package_pubspec = yaml.load(f, Loader=yaml.CSafeLoader)
m = re.match(
r"^[ \t]*(\^|>=|>)?[ \t]*([0-9]+\.[0-9]+)\.[0-9]+.*$",
package_pubspec.get("environment", {}).get("sdk", ""),
)
if m:
languageVersion = m.group(2)
elif package_pubspec.get("environment", {}).get("sdk") == "any":
languageVersion = "null"
else:
languageVersion = "2.7"
if not any(
pkg["name"] == package_pubspec["name"] for pkg in package_config["packages"]
):
package_config["packages"].append({
"name": package_pubspec["name"],
"rootUri": Path(package_path).resolve().as_uri(),
"packageUri": "lib/",
"languageVersion": languageVersion,
})
with Path(".dart_tool/package_config.json").open("w", encoding="utf-8") as f:
json.dump(package_config, f, sort_keys=True, indent=4)
if __name__ == "__main__":
main()