#!/usr/bin/env bash
# TVAnarchy task runner. Thin, discoverable wrapper over the repo's build/test
# scripts so the common actions have one obvious name. Add a target by giving it
# a `task::<name>` function below — it shows up in `./run` (no args) automatically.
set -euo pipefail
cd "$(dirname "$0")"

# plum is this MacBook — build a Release app and install it to ~/Applications.
task::update:plum() { ./build-install.sh "$@"; }

# Regenerate the Xcode project from project.yml.
task::generate() { xcodegen generate; }

# Run the unit-test bundle (regenerates the project first so new files are picked
# up). Pass extra args straight through, e.g. `./run test -only-testing:...`.
task::test() {
  xcodegen generate >/dev/null
  xcodebuild test -project TVAnarchy.xcodeproj -scheme TVAnarchy \
    -destination 'platform=macOS' "$@"
}

usage() {
  echo "usage: ./run <target> [args]"
  echo
  echo "targets:"
  # List every task::<name> function, stripping the prefix.
  declare -F | sed -n 's/^declare -f task::/  /p'
}

main() {
  local target="${1:-}"
  [ -n "$target" ] || { usage; exit 1; }
  shift
  if ! declare -F "task::$target" >/dev/null; then
    echo "✗ unknown target: $target" >&2
    echo >&2
    usage >&2
    exit 1
  fi
  "task::$target" "$@"
}

main "$@"
