-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathadapters.sh
More file actions
383 lines (341 loc) · 13.2 KB
/
adapters.sh
File metadata and controls
383 lines (341 loc) · 13.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env bash
# Adapter loading infrastructure — registries, builders, generic fallbacks, and loaders
# shellcheck disable=SC2329 # Functions defined inside adapter builders are invoked indirectly
# ── Editor Registry ────────────────────────────────────────────────────
# Declarative definitions for standard editor adapters.
# Custom adapters (nano) remain as override files in adapters/editor/.
#
# Format: name|cmd|type|err_msg|flags
# name — adapter name (used in --editor flag, config, completions)
# cmd — executable to check/invoke (must be in PATH)
# type — "standard" (GUI, opens directory) or "terminal" (runs in current tty)
# err_msg — user-facing message when cmd is not found
# flags — comma-separated modifiers (optional):
# "workspace" — pass .code-workspace file instead of directory
# "background" — launch with & (for terminal editors that fork)
# "dot" — cd to directory and pass "." instead of full path
#
# Loading: file override (adapters/editor/<name>.sh) → registry → generic PATH fallback
_EDITOR_REGISTRY="
antigravity|agy|standard|Antigravity 'agy' command not found. Install from https://antigravity.google|workspace,dot
atom|atom|standard|Atom not found. Install from https://atom.io|
cursor|cursor|standard|Cursor not found. Install from https://cursor.com or enable the shell command.|workspace
emacs|emacs|terminal|Emacs not found. Install from https://www.gnu.org/software/emacs/|background
idea|idea|standard|IntelliJ IDEA 'idea' command not found. Enable shell launcher in Tools > Create Command-line Launcher|
nvim|nvim|terminal|Neovim not found. Install from https://neovim.io|
pycharm|pycharm|standard|PyCharm 'pycharm' command not found. Enable shell launcher in Tools > Create Command-line Launcher|
sublime|subl|standard|Sublime Text 'subl' command not found. Install from https://www.sublimetext.com|
vim|vim|terminal|Vim not found. Install via your package manager.|
vscode|code|standard|VS Code 'code' command not found. Install from https://code.visualstudio.com|workspace
webstorm|webstorm|standard|WebStorm 'webstorm' command not found. Enable shell launcher in Tools > Create Command-line Launcher|
zed|zed|standard|Zed not found. Install from https://zed.dev|
"
# ── AI Tool Registry ──────────────────────────────────────────────────
# Declarative definitions for standard AI coding tool adapters.
# Custom adapters (claude, cursor) remain as override files in adapters/ai/.
#
# Format: name|cmd|err_msg|info_lines
# name — adapter name (used in --ai flag, config, completions)
# cmd — executable to check/invoke (must be in PATH)
# err_msg — user-facing message when cmd is not found
# info_lines — semicolon-separated additional help lines shown on error
#
# Loading: file override (adapters/ai/<name>.sh) → registry → generic PATH fallback
_AI_REGISTRY="
aider|aider|Aider not found. Install with: pip install aider-chat|See https://aider.chat for more information
auggie|auggie|Auggie CLI not found. Install with: npm install -g @augmentcode/auggie|See https://www.augmentcode.com/product/CLI for more information
codex|codex|Codex CLI not found. Install with: npm install -g @openai/codex|Or: brew install codex;See https://github.com/openai/codex for more info
continue|cn|Continue CLI not found. Install from https://continue.dev|See https://docs.continue.dev/cli/install for installation
copilot|copilot|GitHub Copilot CLI not found.|Install with: npm install -g @github/copilot;Or: brew install copilot-cli;See https://github.com/github/copilot-cli for more information
gemini|gemini|Gemini CLI not found. Install with: npm install -g @google/gemini-cli|Or: brew install gemini-cli;See https://github.com/google-gemini/gemini-cli for more info
opencode|opencode|OpenCode not found. Install from https://opencode.ai|Make sure the 'opencode' CLI is available in your PATH
"
# Registry lookup — find an adapter entry by name
# Usage: _registry_lookup <registry_content> <name>
# Prints matching line on success, returns 1 on miss
_registry_lookup() {
local registry="$1" name="$2"
local line
while IFS= read -r line; do
[ -z "$line" ] && continue
if [ "${line%%|*}" = "$name" ]; then
printf "%s" "$line"
return 0
fi
done <<EOF
$registry
EOF
return 1
}
# Load an editor adapter from a registry entry
# Parses fields and calls the appropriate builder
_load_from_editor_registry() {
local entry="$1"
# Parse: name|cmd|type|err_msg|flags
local remainder="$entry"
local name="${remainder%%|*}"; remainder="${remainder#*|}"
local cmd="${remainder%%|*}"; remainder="${remainder#*|}"
local type="${remainder%%|*}"; remainder="${remainder#*|}"
local err_msg="${remainder%%|*}"; remainder="${remainder#*|}"
local flags="$remainder"
_EDITOR_CMD="$cmd"
_EDITOR_ERR_MSG="$err_msg"
_EDITOR_WORKSPACE=0
_EDITOR_BACKGROUND=0
_EDITOR_DOT=0
case ",$flags," in
*,workspace,*) _EDITOR_WORKSPACE=1 ;;
esac
case ",$flags," in
*,background,*) _EDITOR_BACKGROUND=1 ;;
esac
case ",$flags," in
*,dot,*) _EDITOR_DOT=1 ;;
esac
case "$type" in
terminal) _editor_define_terminal ;;
*) _editor_define_standard ;;
esac
}
# Load an AI adapter from a registry entry
# Parses fields and calls the standard builder
_load_from_ai_registry() {
local entry="$1"
# Parse: name|cmd|err_msg|info_lines
local remainder="$entry"
local name="${remainder%%|*}"; remainder="${remainder#*|}"
local cmd="${remainder%%|*}"; remainder="${remainder#*|}"
local err_msg="${remainder%%|*}"; remainder="${remainder#*|}"
local info_lines_raw="$remainder"
_AI_CMD="$cmd"
_AI_ERR_MSG="$err_msg"
_AI_INFO_LINES=()
if [ -n "$info_lines_raw" ]; then
local old_ifs="$IFS"
IFS=';'
set -f # Disable globbing during split
# shellcheck disable=SC2086
set -- $info_lines_raw
set +f
_AI_INFO_LINES=("$@")
IFS="$old_ifs"
fi
_ai_define_standard
}
# List all adapter names from a registry
# Usage: _list_registry_names <registry_content>
_list_registry_names() {
local registry="$1"
local names="" line
while IFS= read -r line; do
[ -z "$line" ] && continue
names="${names:+$names, }${line%%|*}"
done <<EOF
$registry
EOF
printf "%s" "$names"
}
# Generic adapter functions (used when no explicit adapter file exists)
# These will be overridden if an adapter file is sourced
# Globals set by load_editor_adapter: GTR_EDITOR_CMD, GTR_EDITOR_CMD_NAME
editor_can_open() {
command -v "$GTR_EDITOR_CMD_NAME" >/dev/null 2>&1
}
editor_open() {
local path="$1"
local workspace="${2:-}"
local target="$path"
# Use workspace file if provided and exists
if [ -n "$workspace" ] && [ -f "$workspace" ]; then
# shellcheck disable=SC2034
target="$workspace"
fi
_run_configured_command "$GTR_EDITOR_CMD" "$target"
}
# Globals set by load_ai_adapter: GTR_AI_CMD, GTR_AI_CMD_NAME
ai_can_start() {
command -v "$GTR_AI_CMD_NAME" >/dev/null 2>&1
}
ai_start() {
local path="$1"
shift
(cd "$path" && _run_configured_command "$GTR_AI_CMD" "$@")
}
# Parse and run a config-supplied command string while preserving quoted args.
_run_configured_command() {
local command_string="$1"
shift
local extra_args=("$@")
(
eval "set -- $command_string" || exit 1
[ "$#" -gt 0 ] || exit 1
"$@" "${extra_args[@]}"
)
}
# Standard AI adapter builder — used by adapter files that follow the common pattern
# Sets globals then call this: _AI_CMD, _AI_ERR_MSG, _AI_INFO_LINES (array)
_ai_define_standard() {
# shellcheck disable=SC2317 # Functions are called indirectly via adapter dispatch
ai_can_start() {
command -v "$_AI_CMD" >/dev/null 2>&1
}
# shellcheck disable=SC2317
ai_start() {
local path="$1"; shift
if ! ai_can_start; then
log_error "$_AI_ERR_MSG"
local _line
for _line in "${_AI_INFO_LINES[@]}"; do
log_info "$_line"
done
return 1
fi
if [ ! -d "$path" ]; then
log_error "Directory not found: $path"
return 1
fi
(cd "$path" && "$_AI_CMD" "$@")
}
}
# Standard editor adapter builder — used by adapter files that follow the common pattern
# Sets globals then call this: _EDITOR_CMD, _EDITOR_ERR_MSG, _EDITOR_WORKSPACE (optional, 0 or 1), _EDITOR_DOT (optional, 0 or 1)
_editor_define_standard() {
# shellcheck disable=SC2317 # Functions are called indirectly via adapter dispatch
editor_can_open() {
command -v "$_EDITOR_CMD" >/dev/null 2>&1
}
# shellcheck disable=SC2317
editor_open() {
local path="$1"
local workspace="${2:-}"
if ! editor_can_open; then
log_error "$_EDITOR_ERR_MSG"
return 1
fi
if [ "${_EDITOR_WORKSPACE:-0}" = "1" ] && [ -n "$workspace" ] && [ -f "$workspace" ]; then
"$_EDITOR_CMD" "$workspace"
elif [ "${_EDITOR_DOT:-0}" = "1" ]; then
(cd "$path" && "$_EDITOR_CMD" .)
else
"$_EDITOR_CMD" "$path"
fi
}
}
# Terminal editor adapter builder — for editors that run in the current terminal
# Sets globals then call this: _EDITOR_CMD, _EDITOR_ERR_MSG, _EDITOR_BACKGROUND (optional, 0 or 1)
_editor_define_terminal() {
# shellcheck disable=SC2317 # Functions are called indirectly via adapter dispatch
editor_can_open() {
command -v "$_EDITOR_CMD" >/dev/null 2>&1
}
# shellcheck disable=SC2317
editor_open() {
local path="$1"
if ! editor_can_open; then
log_error "$_EDITOR_ERR_MSG"
return 1
fi
if [ "${_EDITOR_BACKGROUND:-0}" = "1" ]; then
"$_EDITOR_CMD" "$path" &
else
(cd "$path" && "$_EDITOR_CMD" .)
fi
}
}
# Resolve workspace file for VS Code/Cursor/Antigravity editors
# Returns the workspace file path if found, empty otherwise
resolve_workspace_file() {
local worktree_path="$1"
# Check config first (gtr.editor.workspace or editor.workspace in .gtrconfig)
local configured
configured=$(cfg_default gtr.editor.workspace "" "" editor.workspace)
# Opt-out: "none" disables workspace lookup entirely
if [ "$configured" = "none" ]; then
return 0
fi
if [ -n "$configured" ]; then
local full_path="$worktree_path/$configured"
if [ -f "$full_path" ]; then
echo "$full_path"
fi
# Explicit config set - don't fall through to auto-detect
return 0
fi
# Auto-detect: find first .code-workspace in worktree root
local ws_file
ws_file=$(find "$worktree_path" -maxdepth 1 -name "*.code-workspace" -type f 2>/dev/null | head -1)
if [ -n "$ws_file" ]; then
echo "$ws_file"
fi
}
# Load an adapter by type (shared implementation for editor and AI adapters)
# Usage: _load_adapter <type> <name> <label> <builtin_list> <path_hint>
_load_adapter() {
local type="$1" name="$2" label="$3" builtin_list="$4" path_hint="$5"
local adapter_selector="${name%% *}"
local adapter_file="$GTR_DIR/adapters/${type}/${adapter_selector}.sh"
# 1. Try loading explicit adapter file (custom overrides like claude, nano)
case "$adapter_selector" in
*/* | *..* | *\\*) ;;
*)
if [ -f "$adapter_file" ]; then
# shellcheck disable=SC1090
. "$adapter_file"
return 0
fi
;;
esac
# 2. Try registry lookup (declarative adapters)
local registry entry
if [ "$type" = "editor" ]; then
registry="$_EDITOR_REGISTRY"
else
registry="$_AI_REGISTRY"
fi
if entry=$(_registry_lookup "$registry" "$name"); then
if [ "$type" = "editor" ]; then
_load_from_editor_registry "$entry"
else
_load_from_ai_registry "$entry"
fi
return 0
fi
# 3. Generic fallback: check if command exists in PATH
# Extract first word (command name) from potentially multi-word string
local cmd_name="${name%% *}"
if ! command -v "$cmd_name" >/dev/null 2>&1; then
log_error "$label '$name' not found"
log_info "Built-in adapters: $builtin_list"
log_info "Or use any $label command available in your PATH (e.g., $path_hint)"
return 1
fi
# Reject shell metacharacters in config-supplied command names to prevent injection
# Allows multi-word commands (e.g., "code --wait") but blocks shell operators
# shellcheck disable=SC2016 # Literal '$(' pattern match is intentional
case "$name" in
*\;* | *\`* | *'$('* | *\|* | *\&* | *'>'* | *'<'*)
log_error "$label '$name' contains shell metacharacters — refusing to execute"
log_info "Use a simple command name, optionally with flags (e.g., 'code --wait')"
return 1
;;
esac
# Set globals for generic adapter functions
# Note: $name may contain arguments (e.g., "code --wait", "bunx @github/copilot@latest")
if [ "$type" = "editor" ]; then
GTR_EDITOR_CMD="$name"
GTR_EDITOR_CMD_NAME="$cmd_name"
else
GTR_AI_CMD="$name"
GTR_AI_CMD_NAME="$cmd_name"
fi
}
load_editor_adapter() {
local builtin_names
builtin_names="$(_list_registry_names "$_EDITOR_REGISTRY"), nano"
_load_adapter "editor" "$1" "Editor" "$builtin_names" "code-insiders, fleet"
}
load_ai_adapter() {
local builtin_names
builtin_names="$(_list_registry_names "$_AI_REGISTRY"), claude, cursor"
_load_adapter "ai" "$1" "AI tool" "$builtin_names" "bunx, gpt"
}