-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·235 lines (208 loc) · 6.73 KB
/
setup
File metadata and controls
executable file
·235 lines (208 loc) · 6.73 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
#!/bin/bash
# forge setup -- Initialize context repo, link commands, wire hooks
#
# Usage:
# ./forge/setup # Auto-detect project name from directory
# ./forge/setup --name myproject # Explicit project name
set -euo pipefail
FORGE_DIR="$(cd "$(dirname "$0")" && pwd)"
WORKSPACE_DIR="$(dirname "$FORGE_DIR")"
# Parse args
PROJECT_NAME=""
while [[ $# -gt 0 ]]; do
case $1 in
--name) PROJECT_NAME="$2"; shift 2 ;;
*) echo "Unknown arg: $1"; exit 1 ;;
esac
done
# Auto-detect project name from workspace directory
if [ -z "$PROJECT_NAME" ]; then
PROJECT_NAME=$(basename "$WORKSPACE_DIR" | sed 's/-workspace$//')
echo "Detected project: $PROJECT_NAME"
echo " (override with: ./forge/setup --name yourproject)"
fi
CONTEXT_DIR="$WORKSPACE_DIR/${PROJECT_NAME}-context"
COMMANDS_DIR="$WORKSPACE_DIR/.claude/commands"
SETTINGS_FILE="$WORKSPACE_DIR/.claude/settings.json"
echo ""
echo "forge setup"
echo "==========================================="
echo " Workspace: $WORKSPACE_DIR"
echo " Project: $PROJECT_NAME"
echo " Context: $CONTEXT_DIR"
echo " Commands: $COMMANDS_DIR"
echo "==========================================="
echo ""
# Step 1: Create context repo if it doesn't exist
if [ -d "$CONTEXT_DIR" ]; then
echo "Context repo exists: $CONTEXT_DIR"
# Ensure RULES.md exists (may be missing from older setups)
if [ ! -f "$CONTEXT_DIR/RULES.md" ]; then
sed "s/{{PROJECT_NAME}}/$PROJECT_NAME/g" "$FORGE_DIR/templates/RULES.md.tmpl" > "$CONTEXT_DIR/RULES.md"
echo " Created: RULES.md"
fi
else
echo "Creating context repo: $CONTEXT_DIR"
mkdir -p "$CONTEXT_DIR"/{wiki/{architecture,api,bugs,decisions,patterns},raw,SESSIONS}
# Copy templates
for tmpl in "$FORGE_DIR"/templates/*.tmpl; do
target="$CONTEXT_DIR/$(basename "$tmpl" .tmpl)"
if [ ! -f "$target" ]; then
sed "s/{{PROJECT_NAME}}/$PROJECT_NAME/g" "$tmpl" > "$target"
echo " Created: $(basename "$target")"
fi
done
# Create empty data files
touch "$CONTEXT_DIR/LEARNINGS.jsonl"
touch "$CONTEXT_DIR/timeline.jsonl"
touch "$CONTEXT_DIR/SESSIONS/.gitkeep"
touch "$CONTEXT_DIR/raw/.gitkeep"
# Create .gitignore
cat > "$CONTEXT_DIR/.gitignore" << 'EOF'
node_modules/
.DS_Store
*.tmp
EOF
# Create README
cat > "$CONTEXT_DIR/README.md" << EOF
# ${PROJECT_NAME}-context
Persistent context and knowledge base for the ${PROJECT_NAME} workspace.
Created by [forge](https://github.com/vakra-dev/forge).
Every state change is a git commit. Full history and diffs.
## Structure
- \`INDEX.md\` -- Navigate the wiki (read this first)
- \`STATE.md\` -- Current health of all repos/services
- \`BACKLOG.md\` -- Known issues + what was tried
- \`RULES.md\` -- Project rules (loaded automatically via hooks)
- \`LEARNINGS.jsonl\` -- Cross-session institutional knowledge
- \`timeline.jsonl\` -- Skill event history
- \`SESSIONS/\` -- Session checkpoints
- \`wiki/\` -- LLM-compiled knowledge base articles
- \`raw/\` -- Raw source material for wiki compilation
EOF
# Git init
cd "$CONTEXT_DIR"
git init
git add -A
git commit -m "forge: initialize ${PROJECT_NAME}-context"
cd "$WORKSPACE_DIR"
echo " Context repo created and committed."
fi
# Step 2: Link commands into .claude/commands/
echo ""
echo "Linking commands..."
mkdir -p "$COMMANDS_DIR"
for cmd in "$FORGE_DIR"/commands/*.md; do
name=$(basename "$cmd")
target="$COMMANDS_DIR/$name"
# Remove old symlink or file
if [ -L "$target" ] || [ -f "$target" ]; then
rm "$target"
fi
# Create symlink
ln -s "$cmd" "$target"
echo " Linked: /$(basename "$name" .md)"
done
# Step 3: Wire hooks into .claude/settings.json
echo ""
echo "Wiring hooks..."
mkdir -p "$WORKSPACE_DIR/.claude"
# Build the hooks settings
HOOKS_JSON=$(cat << 'HOOKEOF'
{
"hooks": {
"SessionStart": [
{
"matcher": "startup|compact",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/forge/hooks/session-init.sh",
"timeout": 15,
"statusMessage": "Loading project context..."
}
]
}
],
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"if": "Bash(git commit *)",
"command": "\"$CLAUDE_PROJECT_DIR\"/forge/hooks/pre-commit-check.sh",
"timeout": 5,
"statusMessage": "Checking project rules..."
}
]
}
],
"SessionEnd": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/forge/hooks/session-end.sh",
"timeout": 5
}
]
}
]
}
}
HOOKEOF
)
if [ -f "$SETTINGS_FILE" ]; then
# Check if hooks are already configured
if grep -q '"hooks"' "$SETTINGS_FILE" 2>/dev/null; then
echo " Hooks already configured in $SETTINGS_FILE"
echo " To update, delete the 'hooks' key and re-run setup."
else
# Merge hooks into existing settings using a simple approach
# Remove the closing brace, add hooks, re-close
TMP_FILE=$(mktemp)
# Remove trailing } and whitespace, add comma + hooks
sed '$ s/}$//' "$SETTINGS_FILE" | sed -e :a -e '/^[[:space:]]*$/d;/^\n*$/{ $d; N; ba' -e '}' > "$TMP_FILE"
echo ',' >> "$TMP_FILE"
echo "$HOOKS_JSON" | sed '1d;$d' >> "$TMP_FILE" # Strip outer braces
echo '}' >> "$TMP_FILE"
mv "$TMP_FILE" "$SETTINGS_FILE"
echo " Hooks merged into $SETTINGS_FILE"
fi
else
echo "$HOOKS_JSON" > "$SETTINGS_FILE"
echo " Created $SETTINGS_FILE with hooks"
fi
# Step 4: Check for CLAUDE.md
echo ""
if [ -f "$WORKSPACE_DIR/CLAUDE.md" ]; then
echo "CLAUDE.md exists. Checking for forge routing..."
if grep -q "forge" "$WORKSPACE_DIR/CLAUDE.md" 2>/dev/null; then
echo " Routing rules found."
else
echo " No forge routing rules found."
echo " Add skill routing to CLAUDE.md manually, or run /compile-wiki to auto-add."
fi
else
echo "No CLAUDE.md found."
echo " Copy forge/templates/CLAUDE.md.tmpl to CLAUDE.md and fill in your project details."
echo " Or run /compile-wiki to auto-generate one."
fi
echo ""
echo "==========================================="
echo " forge is ready."
echo ""
echo " What's set up:"
echo " Context repo: $CONTEXT_DIR"
echo " Commands: /status, /recall, /checkpoint, /compile-wiki,"
echo " /investigate, /test-fix, /review, /rule"
echo " Hooks: SessionStart (auto-context), PreToolUse (rule check),"
echo " SessionEnd (timeline)"
echo ""
echo " Next steps:"
echo " /compile-wiki Build knowledge base from codebase"
echo " /rule Add project-specific rules"
echo " /status Check your stack health"
echo "==========================================="