diff --git a/doc/languages-frameworks/dart.section.md b/doc/languages-frameworks/dart.section.md index e5acd9902634..512f55a68b6b 100644 --- a/doc/languages-frameworks/dart.section.md +++ b/doc/languages-frameworks/dart.section.md @@ -56,7 +56,7 @@ This is an alternative to `dart run` that does not rely on Pub. e.g., for `build_runner`: ```bash -packageRun build_runner -- build +packageRun build_runner build ``` Do _not_ use `dart run `, 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 a4d2809c44b8..dae74c3f77e6 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,14 +10,18 @@ dartConfigHook() { mkdir -p .dart_tool cp "$packageConfig" .dart_tool/package_config.json - # Runs a Dart executable from a package. + packagePath() { + jq --raw-output --arg name "$1" '.packages.[] | select(.name == $name) .rootUri | sub("file://"; "")' .dart_tool/package_config.json + } + + # Runs a Dart executable from a package with a custom path. # # Usage: - # packageRun [executable] [bin_dir] + # packageRunCustom [executable] [bin_dir] # # By default, [bin_dir] is "bin", and [executable] is . - # 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() { + # 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. + packageRunCustom() { local args=() local passthrough=() @@ -36,8 +40,28 @@ dartConfigHook() { 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[@]}" + dart --packages=.dart_tool/package_config.json "$(packagePath "$name")/$prefix/$path.dart" "${passthrough[@]}" + } + + # Runs a Dart executable from a package. + # + # Usage: + # packageRun [-e executable] [...] + # + # To run an executable from an unconventional location, use packageRunCustom. + packageRun() { + local name="$1" + shift + + local executableName="$name" + if [ "$1" = "-e" ]; then + shift + executableName="$1" + shift + fi + + fileName="$(@yq@ --raw-output --arg name "$executableName" '.executables.[$name] // $name' "$(packagePath "$name")/pubspec.yaml")" + packageRunCustom "$name" "$fileName" -- "$@" } echo "Generating the dependency list" diff --git a/pkgs/build-support/dart/build-dart-application/hooks/default.nix b/pkgs/build-support/dart/build-dart-application/hooks/default.nix index 49599128ff63..253d3132ad02 100644 --- a/pkgs/build-support/dart/build-dart-application/hooks/default.nix +++ b/pkgs/build-support/dart/build-dart-application/hooks/default.nix @@ -3,6 +3,7 @@ { dartConfigHook = makeSetupHook { name = "dart-config-hook"; + substitutions.yq = "${yq}/bin/yq"; substitutions.jq = "${jq}/bin/jq"; } ./dart-config-hook.sh; dartBuildHook = makeSetupHook {