-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.ps1
More file actions
291 lines (247 loc) · 11 KB
/
Copy pathinstall.ps1
File metadata and controls
291 lines (247 loc) · 11 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
<# .SYNOPSIS
One-command installer for cgraph on Windows.
Handles Node.js installation if missing, downloads cgraph, builds, and links.
.DESCRIPTION
For developers who don't have Node.js or git installed.
Downloads a portable Node.js, fetches cgraph source, builds it,
and makes the `cgraph` command available system-wide.
.EXAMPLE
# Run from PowerShell (no prerequisites needed):
powershell -ExecutionPolicy Bypass -File install.ps1
# Or with a specific install directory:
powershell -ExecutionPolicy Bypass -File install.ps1 -InstallDir "D:\tools\cgraph"
#>
param(
[string]$InstallDir = "$env:USERPROFILE\.cgraph-install",
[string]$NodeVersion = "22.16.0",
[switch]$SkipNodeInstall
)
$ErrorActionPreference = 'Stop'
function Write-Step { param([string]$msg) Write-Host "`n=> $msg" -ForegroundColor Cyan }
function Write-Ok { param([string]$msg) Write-Host " $msg" -ForegroundColor Green }
function Write-Warn { param([string]$msg) Write-Host " $msg" -ForegroundColor Yellow }
Write-Host ""
Write-Host "============================" -ForegroundColor Cyan
Write-Host " cgraph Installer (Windows)" -ForegroundColor Cyan
Write-Host "============================" -ForegroundColor Cyan
Write-Host ""
# --- 1. Check/Install Node.js ---
Write-Step "Checking Node.js..."
$nodeCmd = Get-Command node -ErrorAction SilentlyContinue
if ($nodeCmd -and -not $SkipNodeInstall) {
$nodeVer = & node --version 2>$null
$major = [int]($nodeVer -replace '^v(\d+)\..*','$1')
if ($major -ge 18) {
Write-Ok "Found Node.js $nodeVer (OK)"
$nodePath = $nodeCmd.Source
} else {
Write-Warn "Found Node.js $nodeVer but need >= 18. Will install portable Node."
$nodeCmd = $null
}
}
if (-not $nodeCmd) {
Write-Step "Installing portable Node.js $NodeVersion..."
$arch = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" }
$nodeDir = Join-Path $InstallDir "node"
$nodeZip = Join-Path $InstallDir "node.zip"
$nodeUrl = "https://nodejs.org/dist/v$NodeVersion/node-v$NodeVersion-win-$arch.zip"
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
Write-Host " Downloading Node.js from $nodeUrl ..."
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $nodeUrl -OutFile $nodeZip -UseBasicParsing
Write-Host " Extracting..."
Expand-Archive -Path $nodeZip -DestinationPath $InstallDir -Force
$extractedDir = Get-ChildItem $InstallDir -Directory | Where-Object { $_.Name -match "^node-v" } | Select-Object -First 1
if (Test-Path $nodeDir) { Remove-Item $nodeDir -Recurse -Force }
Rename-Item $extractedDir.FullName $nodeDir
Remove-Item $nodeZip -Force
$nodePath = Join-Path $nodeDir "node.exe"
$npmPath = Join-Path $nodeDir "npm.cmd"
# Add to current session PATH
$env:PATH = "$nodeDir;$env:PATH"
Write-Ok "Portable Node.js installed at $nodeDir"
} else {
$nodePath = (Get-Command node).Source
}
# Verify
$ver = & node --version 2>$null
if (-not $ver) {
Write-Host "ERROR: Node.js not working. Please install manually from https://nodejs.org" -ForegroundColor Red
exit 1
}
Write-Ok "Using Node.js $ver"
# --- 2. Get cgraph source ---
Write-Step "Setting up cgraph..."
$cgraphDir = Join-Path $InstallDir "cgraph"
# Check if git is available
$hasGit = $null -ne (Get-Command git -ErrorAction SilentlyContinue)
if (Test-Path (Join-Path $cgraphDir "package.json")) {
Write-Ok "cgraph source already exists at $cgraphDir"
if ($hasGit -and (Test-Path (Join-Path $cgraphDir ".git"))) {
Write-Host " Pulling latest..."
Push-Location $cgraphDir
git pull --ff-only 2>$null
Pop-Location
}
} elseif ($hasGit) {
# Clone via git
Write-Host " Cloning cgraph repository..."
git clone "https://github.com/samarth-w/agent_graph.git" $cgraphDir
} else {
# Download as zip (no git required)
Write-Host " Downloading cgraph (no git needed)..."
$zipUrl = "https://github.com/samarth-w/agent_graph/archive/refs/heads/master.zip"
$zipFile = Join-Path $InstallDir "cgraph.zip"
# Download zip from GitHub
Write-Warn "For now, assuming cgraph source is at: $cgraphDir"
# If running locally, copy from current directory
$scriptDir = $PSScriptRoot
if ($scriptDir -and (Test-Path (Join-Path $scriptDir "package.json"))) {
Write-Host " Copying from local source..."
New-Item -ItemType Directory -Path $cgraphDir -Force | Out-Null
Copy-Item -Path "$scriptDir\*" -Destination $cgraphDir -Recurse -Exclude @("node_modules", "dist", ".cgraph", ".git")
} else {
Write-Host "ERROR: Could not locate cgraph source. Please provide the repo URL." -ForegroundColor Red
Write-Host " Or copy the cgraph folder to: $cgraphDir" -ForegroundColor Red
exit 1
}
}
# --- 3. Install dependencies + build ---
Write-Step "Installing dependencies..."
Push-Location $cgraphDir
try {
& npm install 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) { throw "npm install failed" }
Write-Ok "Dependencies installed"
Write-Step "Building..."
& npm run build 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) { throw "Build failed" }
Write-Ok "Build complete"
# --- 4. Create a launcher batch file ---
Write-Step "Creating launcher..."
$binDir = Join-Path $InstallDir "bin"
New-Item -ItemType Directory -Path $binDir -Force | Out-Null
$launcherContent = @"
@echo off
"$nodePath" "$cgraphDir\bin\cgraph.js" %*
"@
$launcherPath = Join-Path $binDir "cgraph.cmd"
$launcherContent | Set-Content $launcherPath -Encoding ASCII
Write-Ok "Launcher created at $launcherPath"
# --- 5. Add to PATH ---
Write-Step "Configuring PATH..."
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($userPath -notlike "*$binDir*") {
[Environment]::SetEnvironmentVariable("PATH", "$binDir;$userPath", "User")
$env:PATH = "$binDir;$env:PATH"
Write-Ok "Added $binDir to user PATH"
Write-Warn "Restart your terminal for PATH changes to take effect"
} else {
Write-Ok "$binDir already in PATH"
}
# --- 6. Configure VS Code user-level MCP (works in ALL workspaces) ---
Write-Step "Configuring VS Code MCP (user-level, all workspaces)..."
$vscodeSettingsDir = Join-Path $env:APPDATA "Code\User"
$vscodeSettingsFile = Join-Path $vscodeSettingsDir "settings.json"
if (Test-Path $vscodeSettingsFile) {
$settingsRaw = Get-Content $vscodeSettingsFile -Raw -Encoding UTF8
# Remove trailing whitespace/newlines for clean parsing
$settingsRaw = $settingsRaw.Trim()
if (-not $settingsRaw) { $settingsRaw = '{}' }
} else {
New-Item -ItemType Directory -Path $vscodeSettingsDir -Force | Out-Null
$settingsRaw = '{}'
}
$nodePathEscaped = $nodePath -replace '\\', '\\\\'
$cgraphJsEscaped = (Join-Path $cgraphDir "bin\cgraph.js") -replace '\\', '\\\\'
if ($settingsRaw -match '"cgraph"') {
Write-Ok "cgraph MCP already configured in VS Code settings"
} else {
# Build the MCP server entry
$mcpBlock = @"
"cgraph": {
"command": "$nodePathEscaped",
"args": ["$cgraphJsEscaped", "serve", "--mcp"],
"cwd": "`${workspaceFolder}"
}
"@
if ($settingsRaw -match '"mcp"\s*:\s*\{\s*"servers"\s*:\s*\{') {
# mcp.servers exists — inject cgraph into it
$settingsRaw = $settingsRaw -replace '("mcp"\s*:\s*\{\s*"servers"\s*:\s*\{)', "`$1`n$mcpBlock,"
} elseif ($settingsRaw -match '"mcp"\s*:\s*\{') {
# mcp exists but no servers — add servers block
$settingsRaw = $settingsRaw -replace '("mcp"\s*:\s*\{)', "`$1`n `"servers`": {`n$mcpBlock`n },"
} else {
# No mcp key at all — add it before the closing brace
$mcpSection = @"
"mcp": {
"servers": {
$mcpBlock
}
}
"@
if ($settingsRaw -eq '{}') {
$settingsRaw = "{$mcpSection`n}"
} else {
$settingsRaw = $settingsRaw -replace '\}\s*$', ",$mcpSection`n}"
}
}
$settingsRaw | Set-Content $vscodeSettingsFile -Encoding UTF8
Write-Ok "Added cgraph MCP server to VS Code user settings"
Write-Ok "cgraph is now available in ALL workspaces — no per-project setup needed"
}
# --- 7. Configure Copilot to prefer cgraph tools ---
Write-Step "Configuring Copilot agent instructions..."
# Re-read settings (may have been updated by MCP step)
if (Test-Path $vscodeSettingsFile) {
$settingsRaw = (Get-Content $vscodeSettingsFile -Raw -Encoding UTF8).Trim()
}
if (-not $settingsRaw) { $settingsRaw = '{}' }
$instructionText = "Use cgraph MCP tools (cgraph_node, cgraph_callers, cgraph_callees, cgraph_search, cgraph_explore, cgraph_context, cgraph_impact, cgraph_trace, cgraph_affected, cgraph_export, cgraph_changed) for code exploration instead of read_file or grep_search. Only use read_file for raw config files or non-code assets."
if ($settingsRaw -match 'codeGeneration\.instructions') {
Write-Ok "Copilot codeGeneration.instructions already configured"
} else {
$instructionBlock = @"
"github.copilot.chat.codeGeneration.instructions": [
{
"text": "$instructionText"
}
]
"@
if ($settingsRaw -eq '{}') {
$settingsRaw = "{`n$instructionBlock`n}"
} else {
$settingsRaw = $settingsRaw -replace '\}\s*$', ",`n$instructionBlock`n}"
}
$settingsRaw | Set-Content $vscodeSettingsFile -Encoding UTF8
Write-Ok "Added Copilot instruction: prefer cgraph tools for code exploration"
}
# --- 8. Verify ---
Write-Step "Verifying installation..."
$testOutput = & $launcherPath --version 2>&1 | Out-String
Write-Ok "cgraph $($testOutput.Trim()) installed successfully!"
} finally {
Pop-Location
}
# --- Done ---
Write-Host ""
Write-Host "============================" -ForegroundColor Green
Write-Host " Installation Complete!" -ForegroundColor Green
Write-Host "============================" -ForegroundColor Green
Write-Host ""
Write-Host " Install dir: $InstallDir" -ForegroundColor White
Write-Host " Command: cgraph" -ForegroundColor White
Write-Host " Node: $nodePath" -ForegroundColor White
Write-Host ""
Write-Host " VS Code MCP: Configured globally (user settings)" -ForegroundColor Green
Write-Host " Works in ALL workspaces automatically" -ForegroundColor Green
Write-Host " Auto-indexes on first Copilot query" -ForegroundColor Green
Write-Host ""
Write-Host " That's it — open any project in VS Code and ask Copilot!" -ForegroundColor Yellow
Write-Host ""
Write-Host " CLI also available:" -ForegroundColor Yellow
Write-Host " cgraph index # manual index" -ForegroundColor White
Write-Host " cgraph status # check what was indexed" -ForegroundColor White
Write-Host " cgraph search myFunction # find symbols" -ForegroundColor White
Write-Host " cgraph callers myFunction # who calls it?" -ForegroundColor White
Write-Host ""