dartHooks.dartConfigHook: Simplify packageRun function

This commit is contained in:
hacker1024 2023-10-26 19:59:39 +11:00 committed by FlafyDev
parent d41348a68a
commit 65d2cc04a3
3 changed files with 32 additions and 7 deletions

View file

@ -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 <package_name>`, as this will attempt to download dependencies with Pub.

View file

@ -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 <package> [executable] [bin_dir]
# packageRunCustom <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() {
# 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 <package> [-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"

View file

@ -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 {