dartHooks.dartConfigHook: Add packageRun utility

authored by hacker1024 and committed by FlafyDev d41348a6 92809a1c

+34 -3
+4 -3
doc/languages-frameworks/dart.section.md
··· 45 45 This can be done in `preBuild`, in one of two ways: 46 46 47 47 1. Packaging the tool with `buildDartApplication`, adding it to Nixpkgs, and running it like any other application 48 - 2. Running the tool from the Pub cache 48 + 2. Running the tool from the package cache 49 49 50 50 Of these methods, the first is recommended when using a tool that does not need 51 51 to be of a specific version. 52 52 53 - 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`. 53 + For the second method, the `packageRun` function from the `dartConfigHook` can be used. 54 + This is an alternative to `dart run` that does not rely on Pub. 54 55 55 56 e.g., for `build_runner`: 56 57 57 58 ```bash 58 - dart --packages=.dart_tool/package_config.json ${self.pubspecLock.dependencySources.build_runner.packagePath}/bin/build_runner.dart build 59 + packageRun build_runner -- build 59 60 ``` 60 61 61 62 Do _not_ use `dart run <package_name>`, as this will attempt to download dependencies with Pub.
+30
pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh
··· 10 10 mkdir -p .dart_tool 11 11 cp "$packageConfig" .dart_tool/package_config.json 12 12 13 + # Runs a Dart executable from a package. 14 + # 15 + # Usage: 16 + # packageRun <package> [executable] [bin_dir] 17 + # 18 + # By default, [bin_dir] is "bin", and [executable] is <package>. 19 + # 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. 20 + packageRun() { 21 + local args=() 22 + local passthrough=() 23 + 24 + while [ $# -gt 0 ]; do 25 + if [ "$1" != "--" ]; then 26 + args+=("$1") 27 + shift 28 + else 29 + shift 30 + passthrough=("$@") 31 + break 32 + fi 33 + done 34 + 35 + local name="${args[0]}" 36 + local path="${args[1]:-$name}" 37 + local prefix="${args[2]:-bin}" 38 + 39 + local packagePath="$(jq --raw-output --arg name "$name" '.packages.[] | select(.name == $name) .rootUri | sub("file://"; "")' .dart_tool/package_config.json)" 40 + dart --packages=.dart_tool/package_config.json "$packagePath/$prefix/$path.dart" "${passthrough[@]}" 41 + } 42 + 13 43 echo "Generating the dependency list" 14 44 dart pub deps --json | @jq@ .packages > deps.json 15 45