summaryrefslogtreecommitdiff
path: root/scripts/discover-plugins.sh
blob: 651b963bb14bb1ba94a933c15a47c65d4c0060f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
# discover-plugins.sh — list device backends (Lite: configured directly in max25d.ini, no plugins/ tree).
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MANIFEST="${ROOT}/plugins/manifest.yaml"
JSON=0

usage() {
  cat <<'EOF'
Usage: discover-plugins.sh [--json] [--type TYPE] [--id ID]

  --json       Machine-readable output (one JSON object per line)
  --type TYPE  Filter: hardware | device
  --id ID      Show single plugin by id
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --json) JSON=1; shift ;;
    --type) TYPE_FILTER="${2:-}"; shift 2 ;;
    --id) ID_FILTER="${2:-}"; shift 2 ;;
    -h|--help) usage; exit 0 ;;
    *) echo "Unknown option: $1" >&2; usage; exit 1 ;;
  esac
done

if [[ ! -f "$MANIFEST" ]]; then
  echo "discover-plugins: no plugin manifest in MAX25-Stack-Lite — devices are set in max25d.ini directly (see README.md / max25-lite-setup)" >&2
  exit 1
fi

emit_plugin() {
  local type="$1" id="$2" path="$3" extra="${4:-}"
  local full="${ROOT}/plugins/${path}"
  local yaml="${full}/plugin.yaml"
  local label="" status="" hybbx=""

  if [[ -f "$yaml" ]]; then
    label=$(grep -E '^label:' "$yaml" 2>/dev/null | head -1 | sed 's/^label:[[:space:]]*//' || true)
    status=$(grep -E '^status:' "$yaml" 2>/dev/null | head -1 | sed 's/^status:[[:space:]]*//' || true)
    hybbx=$(grep -E '^(hybbx_plugin|plugin):' "$yaml" 2>/dev/null | head -1 | sed 's/^[^:]*:[[:space:]]*//' || true)
  fi

  if [[ -n "${TYPE_FILTER:-}" && "$type" != "$TYPE_FILTER" ]]; then return; fi
  if [[ -n "${ID_FILTER:-}" && "$id" != "$ID_FILTER" ]]; then return; fi

  if [[ "$JSON" -eq 1 ]]; then
    printf '{"type":"%s","id":"%s","path":"plugins/%s","label":"%s","status":"%s","hybbx":"%s"%s}\n' \
      "$type" "$id" "$path" "$label" "$status" "$hybbx" "$extra"
  else
    printf '%-14s %-18s %-30s %s\n' "$type" "$id" "plugins/$path" "${label:-—}"
  fi
}

if [[ "$JSON" -eq 0 && -z "${ID_FILTER:-}" ]]; then
  printf '%-14s %-18s %-30s %s\n' "TYPE" "ID" "PATH" "LABEL"
  printf '%s\n' "--------------------------------------------------------------------------------"
fi

# Parse manifest sections (simple line-based — no PyYAML dependency)
section=""
while IFS= read -r line || [[ -n "$line" ]]; do
  line="${line%%#*}"
  line="${line%"${line##*[![:space:]]}"}"
  [[ -z "$line" ]] && continue

  case "$line" in
    betriebsform:) section=""; continue ;;
    hardware:) section=hardware; continue ;;
    devices:) section=device; continue ;;
    schema_version:*|stack:*|description:*) continue ;;
  esac

  [[ -z "$section" ]] && continue

  if [[ "$line" =~ ^[[:space:]]*-[[:space:]]+id:[[:space:]]*(.+)$ ]]; then
    cur_id="${BASH_REMATCH[1]}"
    cur_path=""
    continue
  fi
  if [[ -n "${cur_id:-}" && "$line" =~ ^[[:space:]]+path:[[:space:]]*(.+)$ ]]; then
    cur_path="${BASH_REMATCH[1]}"
    emit_plugin "$section" "$cur_id" "$cur_path"
    cur_id=""
  fi
done < "$MANIFEST"

# Also scan for plugin.yaml not in manifest (future auto-gen)
while IFS= read -r yaml; do
  dir="$(dirname "$yaml")"
  rel="${dir#${ROOT}/plugins/}"
  id="$(grep -E '^id:' "$yaml" | head -1 | sed 's/^id:[[:space:]]*//' || basename "$dir")"
  type="$(grep -E '^type:' "$yaml" | head -1 | sed 's/^type:[[:space:]]*//' || echo unknown)"
  # Skip if already emitted via manifest for same id+type would duplicate — acceptable for scaffold
done < <(find "${ROOT}/plugins" -name plugin.yaml -not -path '*/betriebsform/*' 2>/dev/null | sort)
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com