-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
160 lines (137 loc) · 4.32 KB
/
install.sh
File metadata and controls
160 lines (137 loc) · 4.32 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash
set -euo pipefail
REPO="${TRI_AGENT_ROUTER_REPO:-IceMasterT/Opencode-Tri-Agent-Router}"
RAW_BASE="${TRI_AGENT_ROUTER_RAW_BASE:-https://raw.githubusercontent.com/${REPO}/main}"
INSTALL_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/opencode/plugin"
GLOBAL_DIR="$HOME/.opencode/plugin"
MANIFEST_NAME="tri-agent-router.manifest.json"
usage() {
printf '%s\n' "Usage: install.sh [install|update|check|uninstall] [--dir PATH] [--global]"
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
printf 'Missing required command: %s\n' "$1" >&2
exit 1
}
}
ACTION="install"
INSTALL_GLOBAL=false
while [ "$#" -gt 0 ]; do
case "$1" in
install|update|check|uninstall)
ACTION="$1"
;;
--dir)
shift
INSTALL_DIR="$1"
;;
--global)
INSTALL_GLOBAL=true
;;
-h|--help)
usage
exit 0
;;
*)
printf 'Unknown argument: %s\n' "$1" >&2
usage >&2
exit 1
;;
esac
shift
done
need_cmd curl
need_cmd mktemp
need_cmd python3
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
# Uninstall function
do_uninstall() {
local dir="$1"
if [ -d "$dir" ] || [ -f "$dir/${MANIFEST_NAME}" ]; then
rm -f "$dir/tri-agent-router.ts" 2>/dev/null || true
rm -f "$dir/tri-agent-router.manifest.json" 2>/dev/null || true
rm -rf "$dir/agent-trondo" 2>/dev/null || true
printf 'Tri-Agent Router uninstalled from %s\n' "$dir"
fi
}
if [ "$ACTION" = "uninstall" ]; then
do_uninstall "$INSTALL_DIR"
if [ "$INSTALL_GLOBAL" = true ] || [ "$INSTALL_GLOBAL" = "true" ]; then
do_uninstall "$GLOBAL_DIR"
fi
exit 0
fi
REMOTE_MANIFEST="$TMP_DIR/manifest.json"
curl -fsSL "${RAW_BASE}/manifest.json" -o "$REMOTE_MANIFEST"
remote_version="$(python3 - <<'PY' "$REMOTE_MANIFEST"
import json, sys
with open(sys.argv[1], 'r', encoding='utf-8') as f:
data = json.load(f)
print(data['version'])
PY
)"
local_manifest="${INSTALL_DIR}/${MANIFEST_NAME}"
local_version=""
if [ -f "$local_manifest" ]; then
local_version="$(python3 - <<'PY' "$local_manifest"
import json, sys
with open(sys.argv[1], 'r', encoding='utf-8') as f:
data = json.load(f)
print(data.get('version', ''))
PY
)"
fi
if [ "$ACTION" = "check" ]; then
if [ "$local_version" = "$remote_version" ] && [ -n "$local_version" ]; then
printf 'Tri-Agent Router is up to date (%s).\n' "$local_version"
elif [ -z "$local_version" ]; then
printf 'Tri-Agent Router is not installed. Latest available version: %s\n' "$remote_version"
else
printf 'Update available: installed %s, latest %s\n' "$local_version" "$remote_version"
fi
exit 0
fi
if [ "$ACTION" = "update" ] && [ "$local_version" = "$remote_version" ] && [ -n "$local_version" ]; then
printf 'Tri-Agent Router is already up to date (%s).\n' "$local_version"
exit 0
fi
mkdir -p "$INSTALL_DIR"
if [ "$INSTALL_GLOBAL" = true ] || [ "$INSTALL_GLOBAL" = "true" ]; then
mkdir -p "$GLOBAL_DIR"
fi
python3 - <<'PY' "$REMOTE_MANIFEST" "$RAW_BASE" "$INSTALL_DIR" "$GLOBAL_DIR" "$INSTALL_GLOBAL"
import json
import pathlib
import sys
import urllib.request
manifest_path = pathlib.Path(sys.argv[1])
raw_base = sys.argv[2]
install_dir = pathlib.Path(sys.argv[3])
global_dir = pathlib.Path(sys.argv[4])
install_global = sys.argv[4].lower() == 'true'
manifest = json.loads(manifest_path.read_text(encoding='utf-8'))
# Install to local directory
for relative in manifest['files']:
target = install_dir / pathlib.Path(relative).name
with urllib.request.urlopen(f"{raw_base}/{relative}") as response:
target.write_bytes(response.read())
(install_dir / 'tri-agent-router.manifest.json').write_text(
json.dumps(manifest, indent=2) + '\n',
encoding='utf-8',
)
# Install to global directory if --global flag is set
if install_global:
for relative in manifest['files']:
target = global_dir / pathlib.Path(relative).name
with urllib.request.urlopen(f"{raw_base}/{relative}") as response:
target.write_bytes(response.read())
(global_dir / 'tri-agent-router.manifest.json').write_text(
json.dumps(manifest, indent=2) + '\n',
encoding='utf-8',
)
PY
printf 'Tri-Agent Router %s installed to %s\n' "$remote_version" "$INSTALL_DIR"
if [ "$INSTALL_GLOBAL" = true ] || [ "$INSTALL_GLOBAL" = "true" ]; then
printf 'Tri-Agent Router %s also installed globally to %s\n' "$remote_version" "$GLOBAL_DIR"
fi