-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.sh
More file actions
executable file
·392 lines (335 loc) · 12.4 KB
/
installer.sh
File metadata and controls
executable file
·392 lines (335 loc) · 12.4 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
384
385
386
387
388
389
390
391
392
#!/usr/bin/env bash
set -euo pipefail
# Ring → OpenCode installer
#
# Installs Ring's unified plugin architecture into ~/.config/opencode
# WITHOUT deleting any existing user content.
#
# Architecture:
# - Unified plugin system (RingUnifiedPlugin) with hook-based architecture
# - Config injection for agents/skills/commands via ring-config.json
# - Background task management with schema validation
#
# Behavior:
# - Copies (overwrites) only the Ring-managed files that share exact paths
# - NEVER deletes unknown files in the target directory
# - Backs up any overwritten files into ~/.config/opencode/.ring-backups/<timestamp>/
# - Merges required dependencies into ~/.config/opencode/package.json (preserving existing fields)
# - Copies JSON schema files for IDE autocomplete support
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_ASSETS="$SCRIPT_DIR/assets"
SOURCE_PLUGIN="$SCRIPT_DIR/plugin"
TARGET_ROOT="${OPENCODE_CONFIG_DIR:-"$HOME/.config/opencode"}"
# Validate TARGET_ROOT is an absolute path (security check)
if [[ -z "$TARGET_ROOT" || "$TARGET_ROOT" == ".config/opencode" || "$TARGET_ROOT" != /* ]]; then
echo "ERROR: Cannot determine config directory. HOME is not set or TARGET_ROOT is not absolute." >&2
echo "Set OPENCODE_CONFIG_DIR or HOME environment variable." >&2
exit 1
fi
# Node version check - require 18-24, warn if 25+
check_node_version() {
if ! command -v node >/dev/null 2>&1; then
echo "WARN: Node.js not found. Will attempt to use bun for installation." >&2
return 0
fi
local node_version
node_version=$(node -v | sed 's/^v//' | cut -d. -f1)
if [[ "$node_version" -lt 18 ]]; then
echo "ERROR: Node.js version $node_version is too old. Requires Node 18-24 (LTS)." >&2
echo "Please install Node 22 LTS: https://nodejs.org/" >&2
exit 1
fi
if [[ "$node_version" -ge 25 ]]; then
echo "WARN: Node.js $node_version detected. This installer is tested with Node 18-24." >&2
echo "WARN: better-sqlite3 may fail to build on Node 25+." >&2
echo "WARN: Consider using Node 22 LTS for best compatibility." >&2
echo ""
fi
}
check_node_version
if [[ ! -d "$SOURCE_ASSETS" ]]; then
echo "ERROR: Source directory not found: $SOURCE_ASSETS" >&2
echo "Expected this script to live at: ring-for-opencode/installer.sh" >&2
echo "And source at: ring-for-opencode/assets/" >&2
exit 1
fi
if [[ ! -d "$SOURCE_PLUGIN" ]]; then
echo "ERROR: Plugin directory not found: $SOURCE_PLUGIN" >&2
echo "Expected plugin at: ring-for-opencode/plugin/" >&2
exit 1
fi
mkdir -p "$TARGET_ROOT"
STAMP="$(date -u +"%Y%m%dT%H%M%SZ")"
BACKUP_DIR="$TARGET_ROOT/.ring-backups/$STAMP"
mkdir -p "$BACKUP_DIR"
backup_if_exists() {
local rel="$1"
local source_base="${2:-$SOURCE_ASSETS}"
local src="$source_base/$rel"
local dst="$TARGET_ROOT/$rel"
# Only consider files we manage (exist in source)
[[ -e "$src" ]] || return 0
if [[ -e "$dst" ]]; then
mkdir -p "$(dirname "$BACKUP_DIR/$rel")"
cp -a "$dst" "$BACKUP_DIR/$rel"
fi
}
copy_tree_no_delete() {
local rel_dir="$1"
local source_base="${2:-$SOURCE_ASSETS}"
# Ensure destination exists
mkdir -p "$TARGET_ROOT/$rel_dir"
# rsync WITHOUT --delete: preserves user content
# -a: archive (permissions, times)
# --checksum: safer overwrites when timestamps differ
rsync -a --checksum "$source_base/$rel_dir/" "$TARGET_ROOT/$rel_dir/"
}
# Expand {OPENCODE_CONFIG} placeholder in file content
expand_placeholders() {
local file="$1"
local config_dir
# Match TypeScript logic: OPENCODE_CONFIG_DIR -> XDG_CONFIG_HOME -> default
# This ensures consistency between install-time and runtime expansion
if [[ -n "${OPENCODE_CONFIG_DIR:-}" ]]; then
config_dir="$OPENCODE_CONFIG_DIR"
elif [[ -n "${XDG_CONFIG_HOME:-}" ]]; then
config_dir="$XDG_CONFIG_HOME/opencode"
else
config_dir="$HOME/.config/opencode"
fi
# Escape sed special characters in replacement: & \ |
# This prevents sed injection when config_dir contains special characters
local escaped_config_dir
escaped_config_dir=$(printf '%s\n' "$config_dir" | sed 's/[&/\|]/\\&/g')
# Use sed to replace {OPENCODE_CONFIG} with the actual config directory
# -i '' for macOS, -i for Linux (detect based on sed behavior)
if sed --version >/dev/null 2>&1; then
# GNU sed (Linux)
sed -i "s|{OPENCODE_CONFIG}|$escaped_config_dir|g" "$file"
else
# BSD sed (macOS)
sed -i '' "s|{OPENCODE_CONFIG}|$escaped_config_dir|g" "$file"
fi
}
# Copy tree and expand placeholders in markdown files
copy_tree_with_expansion() {
local rel_dir="$1"
local source_base="${2:-$SOURCE_ASSETS}"
# First, use regular copy to get all files
copy_tree_no_delete "$rel_dir" "$source_base"
# Then, expand placeholders in all markdown files
echo "Expanding placeholders in $rel_dir markdown files..."
find "$TARGET_ROOT/$rel_dir" -type f -name "*.md" | while read -r md_file; do
if grep -q "{OPENCODE_CONFIG}" "$md_file" 2>/dev/null; then
expand_placeholders "$md_file"
echo " Expanded: ${md_file#"$TARGET_ROOT"/}"
fi
done
}
copy_file() {
local rel="$1"
local source_base="${2:-$SOURCE_ASSETS}"
local src="$source_base/$rel"
local dst="$TARGET_ROOT/$rel"
if [[ -e "$src" ]]; then
mkdir -p "$(dirname "$dst")"
cp -a "$src" "$dst"
echo "Copied: $rel"
fi
}
backup_root_config() {
local filename="$1"
local dest_name="${2:-$1}"
local src="$SCRIPT_DIR/$filename"
local dst="$TARGET_ROOT/ring/$dest_name"
[[ -e "$src" ]] || return 0
if [[ -e "$dst" ]]; then
mkdir -p "$BACKUP_DIR/ring"
cp -a "$dst" "$BACKUP_DIR/ring/$dest_name"
fi
}
copy_root_config() {
local filename="$1"
local dest_name="${2:-$1}"
local src="$SCRIPT_DIR/$filename"
local dst="$TARGET_ROOT/ring/$dest_name"
if [[ -e "$src" ]]; then
mkdir -p "$(dirname "$dst")"
cp -a "$src" "$dst"
echo "Copied: ring/$dest_name"
fi
}
# Backup any files we might overwrite
# Plugin files (from root plugin/)
backup_if_exists "plugin/index.ts" "$SOURCE_PLUGIN/.."
backup_if_exists "plugin/ring-plugin.ts" "$SOURCE_PLUGIN/.."
backup_if_exists "plugin/ring-unified.ts" "$SOURCE_PLUGIN/.."
backup_if_exists "package.json"
backup_root_config "ring.jsonc" "config.jsonc"
# Back up all Ring plugin .ts files (best-effort)
if [[ -d "$SOURCE_PLUGIN" ]]; then
while IFS= read -r -d '' f; do
rel="${f#"$SCRIPT_DIR/"}"
backup_if_exists "$rel" "$SCRIPT_DIR"
done < <(find "$SOURCE_PLUGIN" -type f -name "*.ts" -print0)
fi
# Copy plugin directory from root level
echo "Copying plugin directory..."
copy_tree_no_delete "plugin" "$SCRIPT_DIR"
# Copy scripts directory from root level
echo "Copying scripts directory..."
if [[ -d "$SCRIPT_DIR/scripts" ]]; then
copy_tree_no_delete "scripts" "$SCRIPT_DIR"
fi
# Copy skill/command/agent/standards/templates from assets with placeholder expansion
echo "Copying skill/command/agent/standards/templates directories..."
for d in skill command agent standards templates; do
if [[ -d "$SOURCE_ASSETS/$d" ]]; then
copy_tree_with_expansion "$d" "$SOURCE_ASSETS"
fi
done
# Copy schema files for IDE autocomplete
echo "Copying schema files..."
copy_file "ring-config.schema.json" "$SOURCE_ASSETS"
copy_file "background-tasks.schema.json" "$SOURCE_ASSETS"
# Copy config templates
copy_root_config "ring.jsonc" "config.jsonc"
# Ensure global state dir exists in user config (no overwrite)
# Note: Project-level state is in <project>/.opencode/state/ and created dynamically
mkdir -p "$TARGET_ROOT/state"
# Merge package.json deps (preserves existing user package.json fields)
REQUIRED_DEPS_JSON='{
"dependencies": {
"@opencode-ai/plugin": "1.1.3",
"better-sqlite3": "12.6.0",
"zod": "^4.1.8",
"jsonc-parser": "^3.3.1",
"@clack/prompts": "^0.11.0",
"picocolors": "^1.1.1",
"commander": "^14.0.2"
},
"devDependencies": {
"@types/better-sqlite3": "7.6.13",
"@types/node": "22.19.5",
"typescript": "5.9.3",
"@biomejs/biome": "^1.9.4"
}
}'
REQUIRED_DEPS_JSON="$REQUIRED_DEPS_JSON" node - <<'NODE'
const fs = require('fs');
const os = require('os');
const path = require('path');
// Handle HOME undefined gracefully - matches TypeScript behavior
const home = process.env.HOME || os.homedir() || '';
if (!home && !process.env.OPENCODE_CONFIG_DIR) {
console.error('ERROR: Cannot determine home directory. Set HOME or OPENCODE_CONFIG_DIR environment variable.');
process.exit(1);
}
const targetRoot = process.env.OPENCODE_CONFIG_DIR || path.join(home, '.config/opencode');
const pkgPath = path.join(targetRoot, 'package.json');
const required = JSON.parse(process.env.REQUIRED_DEPS_JSON);
function mergeSection(target, sectionName) {
const src = required[sectionName] || {};
const dst = target[sectionName] || {};
target[sectionName] = { ...dst, ...src };
}
let pkg = {};
if (fs.existsSync(pkgPath)) {
try {
pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
} catch (e) {
console.error(`ERROR: Failed to parse existing ${pkgPath}: ${e}`);
process.exit(1);
}
}
mergeSection(pkg, 'dependencies');
mergeSection(pkg, 'devDependencies');
// Ensure package.json is valid even if it didn't exist
pkg.name ??= 'opencode-config';
pkg.private ??= true;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n', { encoding: 'utf8', mode: 0o600 });
console.log(`Updated ${pkgPath}`);
NODE
# Install deps
if command -v bun >/dev/null 2>&1; then
echo "Installing dependencies with bun..."
if ! (cd "$TARGET_ROOT" && CXXFLAGS='-std=c++20' bun install); then
echo "" >&2
echo "ERROR: bun install failed." >&2
echo "" >&2
echo "Common causes:" >&2
echo " - Node.js version incompatibility (better-sqlite3 requires Node 18-24)" >&2
echo " - Missing C++ build tools" >&2
echo "" >&2
echo "Recommended fix:" >&2
echo " 1. Install Node 22 LTS: https://nodejs.org/" >&2
echo " 2. Ensure you have build tools installed:" >&2
echo " - macOS: xcode-select --install" >&2
echo " - Linux: apt-get install build-essential python3" >&2
echo " 3. Re-run this installer" >&2
exit 1
fi
else
echo "WARN: bun not found; skipping dependency install." >&2
fi
# Detect platform for pre-built binaries
detect_platform() {
local os arch
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
if [[ "$arch" == "x86_64" ]]; then
arch="amd64"
elif [[ "$arch" == "aarch64" || "$arch" == "arm64" ]]; then
arch="arm64"
fi
echo "${os}_${arch}"
}
PLATFORM=$(detect_platform)
# Path where pre-built binaries would be if copied from source/release
RELEASE_BIN_DIR="$TARGET_ROOT/scripts/codereview/bin/releases/$PLATFORM"
# Target path where tools expect binaries to be
TARGET_BIN_DIR="$TARGET_ROOT/scripts/codereview/bin"
if [[ -d "$RELEASE_BIN_DIR" ]]; then
echo "Found pre-built binaries for $PLATFORM..."
mkdir -p "$TARGET_BIN_DIR"
# Copy binaries, ignoring CHECKSUMS files
find "$RELEASE_BIN_DIR" -maxdepth 1 -type f -not -name "CHECKSUMS*" -exec cp {} "$TARGET_BIN_DIR/" \;
chmod +x "$TARGET_BIN_DIR"/*
echo "Installed pre-built binaries to $TARGET_BIN_DIR"
fi
if command -v go >/dev/null 2>&1; then
if [[ -d "$TARGET_ROOT/scripts/codereview" ]]; then
echo "Building codereview tools from source..."
if (cd "$TARGET_ROOT/scripts/codereview" && GOFLAGS="-buildvcs=false" make build); then
echo "Codereview tools built successfully."
chmod +x "$TARGET_BIN_DIR"/*
else
echo "WARN: Failed to build codereview tools. 'go' command failed." >&2
fi
else
echo "WARN: Codereview source directory not found. Skipping build." >&2
fi
elif [[ ! -d "$RELEASE_BIN_DIR" ]]; then
echo "WARN: Pre-built binaries not found and 'go' is missing." >&2
echo "Codereview tools source installed but not built." >&2
echo "To enable deep code review analysis, install Go and run: cd ~/.config/opencode/scripts/codereview && make build" >&2
fi
echo ""
echo "=========================================="
echo " Ring for OpenCode - Install Complete"
echo "=========================================="
echo ""
echo "Installed components:"
echo " - RingUnifiedPlugin (unified plugin with hook-based architecture)"
echo " - Skills, commands, agents, standards, and templates from assets/"
echo " - JSON schemas for IDE autocomplete"
echo ""
echo "Backup location (if any): $BACKUP_DIR"
echo ""
echo "To verify installation:"
echo " 1. Start OpenCode in your project directory"
echo " 2. Check that Ring skills appear in the command palette"
echo " 3. Create a ring-config.json for custom configuration"
echo ""
echo "For more info, see: https://github.com/LerianStudio/ring-for-opencode"
echo ""