about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhacker1024 <hacker1024@users.sourceforge.net>2023-10-26 19:30:23 +1100
committerFlafyDev <flafyarazi@gmail.com>2023-12-26 17:08:51 +0200
commitd41348a68a130b5358740e394f50d7fb5fc69838 (patch)
treefd2bc308c013870eff5f0af0c907271f03b861ef
parent92809a1cc5fb50d706a01ff232128ca9421f4584 (diff)
downloadnixlib-d41348a68a130b5358740e394f50d7fb5fc69838.tar
nixlib-d41348a68a130b5358740e394f50d7fb5fc69838.tar.gz
nixlib-d41348a68a130b5358740e394f50d7fb5fc69838.tar.bz2
nixlib-d41348a68a130b5358740e394f50d7fb5fc69838.tar.lz
nixlib-d41348a68a130b5358740e394f50d7fb5fc69838.tar.xz
nixlib-d41348a68a130b5358740e394f50d7fb5fc69838.tar.zst
nixlib-d41348a68a130b5358740e394f50d7fb5fc69838.zip
dartHooks.dartConfigHook: Add packageRun utility
-rw-r--r--doc/languages-frameworks/dart.section.md7
-rw-r--r--pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh30
2 files changed, 34 insertions, 3 deletions
diff --git a/doc/languages-frameworks/dart.section.md b/doc/languages-frameworks/dart.section.md
index 933e7210133a..e5acd9902634 100644
--- a/doc/languages-frameworks/dart.section.md
+++ b/doc/languages-frameworks/dart.section.md
@@ -45,17 +45,18 @@ Many Dart applications require executables from the `dev_dependencies` section i
 This can be done in `preBuild`, in one of two ways:
 
 1. Packaging the tool with `buildDartApplication`, adding it to Nixpkgs, and running it like any other application
-2. Running the tool from the Pub cache
+2. Running the tool from the package cache
 
 Of these methods, the first is recommended when using a tool that does not need
 to be of a specific version.
 
-To use the second method, first make the derivation accessible within itself (e.g. `let self = ...; in self`), and then run it from the Pub cache in `preBuild`.
+For the second method, the `packageRun` function from the `dartConfigHook` can be used.
+This is an alternative to `dart run` that does not rely on Pub.
 
 e.g., for `build_runner`:
 
 ```bash
-dart --packages=.dart_tool/package_config.json ${self.pubspecLock.dependencySources.build_runner.packagePath}/bin/build_runner.dart build
+packageRun build_runner -- build
 ```
 
 Do _not_ use `dart run <package_name>`, as this will attempt to download dependencies with Pub.
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 81e87c651755..a4d2809c44b8 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
@@ -10,6 +10,36 @@ dartConfigHook() {
     mkdir -p .dart_tool
     cp "$packageConfig" .dart_tool/package_config.json
 
+    # Runs a Dart executable from a package.
+    #
+    # Usage:
+    # packageRun <package> [executable] [bin_dir]
+    #
+    # By default, [bin_dir] is "bin", and [executable] is <package>.
+    # i.e. `packageRun build_runner` is equivalent to `packageRun build_runner build_runner bin`, which runs `bin/build_runner.dart` from the build_runner package.
+    packageRun() {
+        local args=()
+        local passthrough=()
+
+        while [ $# -gt 0 ]; do
+            if [ "$1" != "--" ]; then
+                args+=("$1")
+                shift
+            else
+                shift
+                passthrough=("$@")
+                break
+            fi
+        done
+
+        local name="${args[0]}"
+        local path="${args[1]:-$name}"
+        local prefix="${args[2]:-bin}"
+
+        local packagePath="$(jq --raw-output --arg name "$name" '.packages.[] | select(.name == $name) .rootUri | sub("file://"; "")' .dart_tool/package_config.json)"
+        dart --packages=.dart_tool/package_config.json "$packagePath/$prefix/$path.dart" "${passthrough[@]}"
+    }
+
     echo "Generating the dependency list"
     dart pub deps --json | @jq@ .packages > deps.json