Unverified Commit 9deb84da authored by Tristan Ross's avatar Tristan Ross Committed by GitHub
Browse files

buildDartApplication: add workspace-package-config.py (#454169)

parents 6fdf87c0 b483a6f0
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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() {
+1 −0
Original line number Diff line number Diff line
@@ -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";
+42 −0
Original line number Diff line number Diff line
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()
+2 −2
Original line number Diff line number Diff line
diff --git a/bin/melos.dart b/bin/melos.dart
index 0db7013..218276f 100644
--- a/bin/melos.dart
+++ b/bin/melos.dart
--- a/packages/melos/bin/melos.dart
+++ b/packages/melos/bin/melos.dart
@@ -1,11 +1,37 @@
+import 'dart:io';
 import 'package:cli_launcher/cli_launcher.dart';
+3 −5
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@ buildDartApplication {
  pname = "melos";
  inherit version src;

  sourceRoot = "${src.name}/packages/melos";

  patches = [
    # This patch (created a melos 6.1.0) modify the method melos use to find path to the root of the projects.
    # It is needed because when melos is in the nixstore, it break it and fail to find the projects root with melos.yaml
@@ -28,12 +26,12 @@ buildDartApplication {

  # hard code the path to the melos templates
  preBuild = ''
    substituteInPlace lib/src/common/utils.dart \
    substituteInPlace packages/melos/lib/src/common/utils.dart \
      --replace-fail "final melosPackageFileUri = await Isolate.resolvePackageUri(melosPackageUri);" "return \"$out\";"
    substituteInPlace lib/src/common/utils.dart \
    substituteInPlace packages/melos/lib/src/common/utils.dart \
      --replace-fail "return p.normalize('\''${melosPackageFileUri!.toFilePath()}/../..');" " "
    mkdir --parents $out
    cp --recursive templates $out/
    cp --recursive packages/melos/templates $out/
  '';

  meta = {
Loading