dartHooks.dartConfigHook: Simplify packageRun function

authored by hacker1024 and committed by FlafyDev 65d2cc04 d41348a6

+32 -7
+1 -1
doc/languages-frameworks/dart.section.md
··· 56 e.g., for `build_runner`: 57 58 ```bash 59 - packageRun build_runner -- build 60 ``` 61 62 Do _not_ use `dart run <package_name>`, as this will attempt to download dependencies with Pub.
··· 56 e.g., for `build_runner`: 57 58 ```bash 59 + packageRun build_runner build 60 ``` 61 62 Do _not_ use `dart run <package_name>`, as this will attempt to download dependencies with Pub.
+30 -6
pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh
··· 10 mkdir -p .dart_tool 11 cp "$packageConfig" .dart_tool/package_config.json 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 ··· 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 43 echo "Generating the dependency list"
··· 10 mkdir -p .dart_tool 11 cp "$packageConfig" .dart_tool/package_config.json 12 13 + packagePath() { 14 + jq --raw-output --arg name "$1" '.packages.[] | select(.name == $name) .rootUri | sub("file://"; "")' .dart_tool/package_config.json 15 + } 16 + 17 + # Runs a Dart executable from a package with a custom path. 18 # 19 # Usage: 20 + # packageRunCustom <package> [executable] [bin_dir] 21 # 22 # By default, [bin_dir] is "bin", and [executable] is <package>. 23 + # i.e. `packageRunCustom build_runner` is equivalent to `packageRunCustom build_runner build_runner bin`, which runs `bin/build_runner.dart` from the build_runner package. 24 + packageRunCustom() { 25 local args=() 26 local passthrough=() 27 ··· 40 local path="${args[1]:-$name}" 41 local prefix="${args[2]:-bin}" 42 43 + dart --packages=.dart_tool/package_config.json "$(packagePath "$name")/$prefix/$path.dart" "${passthrough[@]}" 44 + } 45 + 46 + # Runs a Dart executable from a package. 47 + # 48 + # Usage: 49 + # packageRun <package> [-e executable] [...] 50 + # 51 + # To run an executable from an unconventional location, use packageRunCustom. 52 + packageRun() { 53 + local name="$1" 54 + shift 55 + 56 + local executableName="$name" 57 + if [ "$1" = "-e" ]; then 58 + shift 59 + executableName="$1" 60 + shift 61 + fi 62 + 63 + fileName="$(@yq@ --raw-output --arg name "$executableName" '.executables.[$name] // $name' "$(packagePath "$name")/pubspec.yaml")" 64 + packageRunCustom "$name" "$fileName" -- "$@" 65 } 66 67 echo "Generating the dependency list"
+1
pkgs/build-support/dart/build-dart-application/hooks/default.nix
··· 3 { 4 dartConfigHook = makeSetupHook { 5 name = "dart-config-hook"; 6 substitutions.jq = "${jq}/bin/jq"; 7 } ./dart-config-hook.sh; 8 dartBuildHook = makeSetupHook {
··· 3 { 4 dartConfigHook = makeSetupHook { 5 name = "dart-config-hook"; 6 + substitutions.yq = "${yq}/bin/yq"; 7 substitutions.jq = "${jq}/bin/jq"; 8 } ./dart-config-hook.sh; 9 dartBuildHook = makeSetupHook {