-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup-projects.sh
More file actions
executable file
·225 lines (190 loc) · 6.56 KB
/
setup-projects.sh
File metadata and controls
executable file
·225 lines (190 loc) · 6.56 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
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2026 Marcus Krueger
# Auto-detect projects under /workspaces/ and register them in Project Manager's projects.json.
# Runs an initial scan synchronously, then starts an inotifywait background watcher
# that updates the project list instantly when directories are created or removed.
set -euo pipefail
WORKSPACE_ROOT="${WORKSPACE_ROOT:-/workspaces}"
PROJECTS_DIR="${WORKSPACE_ROOT}/.config/project-manager"
PROJECTS_FILE="${PROJECTS_DIR}/projects.json"
PID_FILE="/tmp/project-watcher.pid"
LOG_PREFIX="[setup-projects]"
# Directories to exclude from project detection (hidden/config dirs)
EXCLUDED_DIRS=".claude .gh .tmp .devcontainer .config node_modules .git"
# --- Helpers ---
is_excluded() {
local name="$1"
# Skip hidden directories (start with .)
[[ "$name" == .* ]] && return 0
# Skip explicitly excluded names
for excluded in $EXCLUDED_DIRS; do
[[ "$name" == "$excluded" ]] && return 0
done
return 1
}
has_project_markers() {
local dir="$1"
[ -d "$dir/.git" ] || [ -f "$dir/.git" ] || [ -f "$dir/package.json" ] || [ -f "$dir/pyproject.toml" ] ||
[ -f "$dir/Cargo.toml" ] || [ -f "$dir/go.mod" ] || [ -f "$dir/deno.json" ] ||
[ -f "$dir/Makefile" ] || [ -f "$dir/CLAUDE.md" ]
}
detect_tags() {
local dir="$1"
local tags=()
if [ -f "$dir/.git" ] && grep -q "gitdir:" "$dir/.git" 2>/dev/null; then
tags+=("git" "worktree")
elif [ -d "$dir/.git" ]; then
tags+=("git")
fi
[ -f "$dir/package.json" ] && tags+=("node")
[ -f "$dir/pyproject.toml" ] && tags+=("python")
[ -f "$dir/Cargo.toml" ] && tags+=("rust")
[ -f "$dir/go.mod" ] && tags+=("go")
[ -f "$dir/deno.json" ] && tags+=("deno")
[ -f "$dir/Makefile" ] && tags+=("make")
[ -f "$dir/CLAUDE.md" ] && tags+=("claude")
# Always add "auto" tag to mark as script-managed
tags+=("auto")
# If no markers found (only "auto"), add "folder" tag
if [ ${#tags[@]} -eq 1 ]; then
tags=("folder" "${tags[@]}")
fi
# Output as JSON array
printf '%s\n' "${tags[@]}" | jq -R . | jq -s .
}
register_project() {
local projects_json="$1"
local name="$2"
local dir="$3"
local tags
tags=$(detect_tags "$dir")
echo "$projects_json" | jq \
--arg name "$name" \
--arg path "$dir" \
--argjson tags "$tags" \
'. += [{"name": $name, "rootPath": ($path | rtrimstr("/")), "tags": $tags, "enabled": true}]'
}
scan_and_update() {
# Build the new auto-detected projects array (two-pass: depth 1 + depth 2)
local new_projects="[]"
for dir in "$WORKSPACE_ROOT"/*/; do
[ -d "$dir" ] || continue
local name
name=$(basename "$dir")
is_excluded "$name" && continue
if has_project_markers "$dir"; then
# Depth 1: directory has project markers — register it directly
new_projects=$(register_project "$new_projects" "$name" "$dir")
else
# Depth 2: no markers — treat as container dir, scan its children
for subdir in "${dir%/}"/*/; do
[ -d "$subdir" ] || continue
local subname
subname=$(basename "$subdir")
is_excluded "$subname" && continue
new_projects=$(register_project "$new_projects" "$subname" "$subdir")
done
# Depth 3: .worktrees/ is hidden (not matched by */) — scan explicitly
local wtcontainer="${dir%/}/.worktrees"
if [ -d "$wtcontainer" ]; then
for wtdir in "${wtcontainer%/}"/*/; do
[ -d "$wtdir" ] || continue
local wtname
wtname=$(basename "$wtdir")
if has_project_markers "$wtdir"; then
new_projects=$(register_project "$new_projects" "$wtname" "$wtdir")
fi
done
fi
fi
done
# Read existing projects.json (or empty array if missing/invalid)
local existing="[]"
if [ -f "$PROJECTS_FILE" ] && jq empty "$PROJECTS_FILE" 2>/dev/null; then
existing=$(cat "$PROJECTS_FILE")
fi
# Separate user entries (no "auto" tag) from auto entries
local user_entries
user_entries=$(echo "$existing" | jq '[.[] | select(.tags | index("auto") | not)]')
# Merge: user entries + new auto-detected entries
local merged
merged=$(jq -n --argjson user "$user_entries" --argjson auto "$new_projects" '$user + $auto')
# Only write if content changed (avoid unnecessary file writes that trigger Project Manager reloads)
local current_hash new_hash
current_hash=""
[ -f "$PROJECTS_FILE" ] && current_hash=$(md5sum "$PROJECTS_FILE" 2>/dev/null | cut -d' ' -f1)
local tmp_file
tmp_file=$(mktemp)
echo "$merged" | jq '.' >"$tmp_file"
new_hash=$(md5sum "$tmp_file" | cut -d' ' -f1)
if [ "$current_hash" != "$new_hash" ]; then
mv "$tmp_file" "$PROJECTS_FILE"
chmod 644 "$PROJECTS_FILE"
local count
count=$(echo "$merged" | jq 'length')
echo "$LOG_PREFIX Updated projects.json ($count projects)"
else
rm -f "$tmp_file"
fi
}
stop_watcher() {
if [ -f "$PID_FILE" ]; then
local old_pid
old_pid=$(cat "$PID_FILE" 2>/dev/null)
if [ -n "$old_pid" ]; then
# Kill the process group to stop both the subshell and inotifywait pipeline
kill -- -"$old_pid" 2>/dev/null || kill "$old_pid" 2>/dev/null || true
fi
rm -f "$PID_FILE"
fi
}
start_watcher() {
# Guard: don't start if already running
if [ -f "$PID_FILE" ]; then
local old_pid
old_pid=$(cat "$PID_FILE" 2>/dev/null)
if [ -n "$old_pid" ] && kill -0 "$old_pid" 2>/dev/null; then
echo "$LOG_PREFIX Watcher already running (PID $old_pid), skipping"
return 0
fi
# Stale PID file — clean up
stop_watcher
fi
# Check if inotifywait is available (installed by tmux feature at build time)
if ! command -v inotifywait >/dev/null 2>&1; then
echo "$LOG_PREFIX WARNING: inotify-tools not installed, watcher disabled"
return 1
fi
# Fork background watcher in its own process group for clean shutdown
set -m
(
# Watch for directory create/delete/move events recursively under /workspaces/
# -r watches subdirectories (catches events inside container dirs like projects/)
# --exclude filters noisy dirs that generate frequent irrelevant events
inotifywait -m -r -q -e create,delete,moved_to,moved_from \
--exclude '(node_modules|\.git|\.tmp|__pycache__|\.venv)' \
--format '%w%f %e' "$WORKSPACE_ROOT" 2>/dev/null |
while read -r _path event; do
# Small delay to let filesystem settle (e.g., move operations)
sleep 1
scan_and_update
done
# Cleanup on exit
rm -f "$PID_FILE"
) &>/dev/null &
local watcher_pid=$!
set +m
echo "$watcher_pid" >"$PID_FILE"
disown
echo "$LOG_PREFIX Background watcher started (PID $watcher_pid)"
}
# --- Main ---
echo "$LOG_PREFIX Configuring Project Manager auto-detection..."
# Ensure projects directory exists
mkdir -p "$PROJECTS_DIR"
# Initial scan (synchronous — runs in <1 second)
scan_and_update
# Start background watcher
start_watcher
echo "$LOG_PREFIX Done"