|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ./script/task.sh: Run a VSCode task from tasks.json including its dependencies. |
| 3 | +# |
| 4 | +# Usage: ./scripts/task.sh TASK_LABEL |
| 5 | +# |
| 6 | +# This script looks for .vscode/tasks.json in your workspace folder (or current directory) |
| 7 | +# and executes the command associated with the given task label. |
| 8 | +# |
| 9 | +# It also handles the "dependsOn" field recursively. |
| 10 | +# |
| 11 | +# Requirements: jq sed |
| 12 | + |
| 13 | +# https://jackson.dev/post/a-portable-nix-shell-shebang/ |
| 14 | +if [ -z "$INSIDE_NIX_RANDOMSTRING" ] && command -v nix &> /dev/null; then |
| 15 | + # If the user has nix, relaunch in nix shell with dependencies added |
| 16 | + INSIDE_NIX_RANDOMSTRING=1 nix shell \ |
| 17 | + nixpkgs#jq \ |
| 18 | + nixpkgs#gnused \ |
| 19 | + --command "$0" "$@" |
| 20 | + exit $? |
| 21 | +fi |
| 22 | + |
| 23 | +required_dependencies=(jq sed) |
| 24 | + |
| 25 | +# Check if the dependencies are installed |
| 26 | +for cmd in "${required_dependencies[@]}"; do |
| 27 | + if ! command -v "$cmd" &> /dev/null; then |
| 28 | + echo "Error: '$cmd' is not installed. Please install it and try again." >&2 |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | +done |
| 32 | + |
| 33 | + |
| 34 | +set -euo pipefail |
| 35 | + |
| 36 | +# Use WORKSPACE_FOLDER if set; otherwise, assume current directory. |
| 37 | +WORKSPACE_FOLDER="${WORKSPACE_FOLDER:-$(pwd)}" |
| 38 | +TASKS_FILE="$WORKSPACE_FOLDER/.vscode/tasks.json" |
| 39 | + |
| 40 | +if [ ! -f "$TASKS_FILE" ]; then |
| 41 | + echo "Error: tasks.json not found at $TASKS_FILE" >&2 |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +if [ $# -lt 1 ]; then |
| 46 | + echo "Usage: $0 TASK_LABEL" >&2 |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +# Remove comment lines (lines starting with //) from the tasks file to be compliant with the JSON format. |
| 51 | +TASKS_JSON=$(sed '/^[[:space:]]*\/\//d' "$TASKS_FILE") |
| 52 | + |
| 53 | +TASK_LABEL="$1" |
| 54 | +shift |
| 55 | + |
| 56 | +# run_task recursively looks up the task by label, |
| 57 | +# runs any dependencies first, then executes its command. |
| 58 | +run_task() { |
| 59 | + local label="$1" |
| 60 | + echo "Looking up task: $label" |
| 61 | + |
| 62 | + # Get the task object from the cleaned JSON. |
| 63 | + local task |
| 64 | + task=$(echo "$TASKS_JSON" | jq --arg label "$label" -r '.tasks[] | select(.label == $label)') |
| 65 | + if [ -z "$task" ]; then |
| 66 | + echo "Error: Task with label '$label' not found in $TASKS_FILE" >&2 |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + # If the task has dependencies, run them first. |
| 71 | + local depends |
| 72 | + depends=$(echo "$task" | jq -r '.dependsOn? // empty') |
| 73 | + if [ -n "$depends" ] && [ "$depends" != "null" ]; then |
| 74 | + # "dependsOn" can be an array or a string. |
| 75 | + if echo "$depends" | jq -e 'if type=="array" then . else empty end' >/dev/null; then |
| 76 | + for dep in $(echo "$depends" | jq -r '.[]'); do |
| 77 | + run_task "$dep" |
| 78 | + done |
| 79 | + else |
| 80 | + run_task "$depends" |
| 81 | + fi |
| 82 | + fi |
| 83 | + |
| 84 | + # Check if the task has either a command or script. |
| 85 | + local has_command has_script |
| 86 | + has_command=$(echo "$task" | jq -r 'has("command")') |
| 87 | + has_script=$(echo "$task" | jq -r 'has("script")') |
| 88 | + if [[ "$has_command" != "true" && "$has_script" != "true" ]]; then |
| 89 | + echo "Task '$label' has no command or script; skipping execution." |
| 90 | + return |
| 91 | + fi |
| 92 | + |
| 93 | + # Determine the command to run: |
| 94 | + local cmd="" |
| 95 | + if echo "$task" | jq 'has("command")' | grep -q "true"; then |
| 96 | + cmd=$(echo "$task" | jq -r '.command') |
| 97 | + elif echo "$task" | jq 'has("script")' | grep -q "true"; then |
| 98 | + local script |
| 99 | + script=$(echo "$task" | jq -r '.script') |
| 100 | + local path |
| 101 | + path=$(echo "$task" | jq -r '.path // empty') |
| 102 | + if [ -n "$path" ]; then |
| 103 | + cmd="cd $path && npm run $script" |
| 104 | + else |
| 105 | + cmd="npm run $script" |
| 106 | + fi |
| 107 | + else |
| 108 | + echo "Error: Task '$label' does not have a command or script." >&2 |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | + |
| 112 | + # Substitute ${workspaceFolder} with the actual folder path. |
| 113 | + cmd="${cmd//\$\{workspaceFolder\}/$WORKSPACE_FOLDER}" |
| 114 | + |
| 115 | + echo "Running task '$label': $cmd" |
| 116 | + # Run the task in a subshell so that directory changes don't persist. |
| 117 | + ( eval "$cmd" ) |
| 118 | +} |
| 119 | + |
| 120 | +run_task "$TASK_LABEL" |
0 commit comments