-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ProjectContext.ps1
More file actions
365 lines (308 loc) · 11.3 KB
/
Copy pathGet-ProjectContext.ps1
File metadata and controls
365 lines (308 loc) · 11.3 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
<#
.SYNOPSIS
Generate a comprehensive project summary for AI tools.
.DESCRIPTION
Creates a context summary of the current project including:
- Project type detection
- Key files and structure
- Dependencies
- Git status
- Available scripts/commands
.PARAMETER Path
Path to project directory (defaults to current directory).
.PARAMETER Brief
Output a short summary only.
.PARAMETER AsJson
Output as JSON for MCP tools.
.PARAMETER Copy
Copy output to clipboard.
.EXAMPLE
context
context -Brief
context -AsJson
context -Copy
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[string]$Path = '.',
[switch]$Brief,
[switch]$AsJson,
[switch]$Copy
)
$Path = Resolve-Path $Path -ErrorAction SilentlyContinue
if (-not $Path) {
Write-Host "Path not found" -ForegroundColor Red
exit 1
}
# Initialize context object
$context = [ordered]@{
path = $Path.ToString()
name = Split-Path $Path -Leaf
type = $null
framework = $null
dependencies = @()
scripts = @()
git = $null
structure = @()
environment = [ordered]@{
os = 'Windows'
shell = 'PowerShell'
wsl = (Get-Command wsl -ErrorAction SilentlyContinue) -ne $null
}
}
# Detect project type
function Get-ProjectType {
param([string]$ProjectPath)
$types = @()
$framework = $null
# Check Laravel first
if ((Test-Path "$ProjectPath\artisan") -and (Test-Path "$ProjectPath\composer.json")) {
$composer = Get-Content "$ProjectPath\composer.json" -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($composer.require.'laravel/framework') {
return @{ type = 'PHP'; framework = 'Laravel' }
}
}
# Check Symfony
if (Test-Path "$ProjectPath\symfony.lock") {
return @{ type = 'PHP'; framework = 'Symfony' }
}
# Check Node/React/Vue
if (Test-Path "$ProjectPath\package.json") {
$pkg = Get-Content "$ProjectPath\package.json" -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($pkg.dependencies.react -or $pkg.devDependencies.react) {
$fw = 'React'
if ($pkg.dependencies.next -or $pkg.devDependencies.next) { $fw = 'Next.js' }
return @{ type = 'JavaScript/Node'; framework = $fw }
}
if ($pkg.dependencies.vue -or $pkg.devDependencies.vue) {
$fw = 'Vue'
if ($pkg.dependencies.nuxt -or $pkg.devDependencies.nuxt) { $fw = 'Nuxt' }
return @{ type = 'JavaScript/Node'; framework = $fw }
}
if ($pkg.dependencies.express) {
return @{ type = 'JavaScript/Node'; framework = 'Express' }
}
return @{ type = 'JavaScript/Node'; framework = $null }
}
# Check PHP
if ((Test-Path "$ProjectPath\composer.json") -or (Get-ChildItem "$ProjectPath\*.php" -ErrorAction SilentlyContinue | Select-Object -First 1)) {
return @{ type = 'PHP'; framework = $null }
}
# Check Perl
if ((Test-Path "$ProjectPath\Makefile.PL") -or (Test-Path "$ProjectPath\cpanfile") -or
(Get-ChildItem "$ProjectPath\*.pl" -ErrorAction SilentlyContinue | Select-Object -First 1) -or
(Get-ChildItem "$ProjectPath\*.pm" -ErrorAction SilentlyContinue | Select-Object -First 1)) {
return @{ type = 'Perl'; framework = $null }
}
# Check Python
if ((Test-Path "$ProjectPath\requirements.txt") -or (Test-Path "$ProjectPath\pyproject.toml") -or (Test-Path "$ProjectPath\setup.py")) {
$fw = $null
if (Test-Path "$ProjectPath\manage.py") { $fw = 'Django' }
if ((Test-Path "$ProjectPath\app.py") -or (Test-Path "$ProjectPath\wsgi.py")) { $fw = 'Flask' }
return @{ type = 'Python'; framework = $fw }
}
return @{ type = 'Unknown'; framework = $null }
}
# Get dependencies
function Get-Dependencies {
param([string]$ProjectPath)
$deps = @()
# Node dependencies
if (Test-Path "$ProjectPath\package.json") {
$pkg = Get-Content "$ProjectPath\package.json" -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($pkg.dependencies) {
$pkg.dependencies.PSObject.Properties | ForEach-Object {
$deps += @{ name = $_.Name; version = $_.Value; type = 'npm' }
}
}
}
# PHP dependencies
if (Test-Path "$ProjectPath\composer.json") {
$composer = Get-Content "$ProjectPath\composer.json" -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($composer.require) {
$composer.require.PSObject.Properties | Where-Object { $_.Name -ne 'php' } | ForEach-Object {
$deps += @{ name = $_.Name; version = $_.Value; type = 'composer' }
}
}
}
# Perl dependencies
if (Test-Path "$ProjectPath\cpanfile") {
Get-Content "$ProjectPath\cpanfile" | ForEach-Object {
if ($_ -match "requires\s+'([^']+)'") {
$deps += @{ name = $Matches[1]; version = '*'; type = 'cpan' }
}
}
}
# Python dependencies
if (Test-Path "$ProjectPath\requirements.txt") {
Get-Content "$ProjectPath\requirements.txt" | ForEach-Object {
if ($_ -match '^([a-zA-Z0-9_-]+)') {
$deps += @{ name = $Matches[1]; version = '*'; type = 'pip' }
}
}
}
return $deps
}
# Get available scripts
function Get-AvailableScripts {
param([string]$ProjectPath)
$scripts = @()
# NPM scripts
if (Test-Path "$ProjectPath\package.json") {
$pkg = Get-Content "$ProjectPath\package.json" -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($pkg.scripts) {
$pkg.scripts.PSObject.Properties | ForEach-Object {
$scripts += @{ name = "npm run $($_.Name)"; command = $_.Value }
}
}
}
# Composer scripts
if (Test-Path "$ProjectPath\composer.json") {
$composer = Get-Content "$ProjectPath\composer.json" -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($composer.scripts) {
$composer.scripts.PSObject.Properties | ForEach-Object {
$scripts += @{ name = "composer $($_.Name)"; command = $_.Value }
}
}
}
# Laravel artisan
if (Test-Path "$ProjectPath\artisan") {
$scripts += @{ name = 'php artisan (art)'; command = 'Laravel CLI' }
}
return $scripts
}
# Get git info
function Get-GitInfo {
param([string]$ProjectPath)
if (-not (Test-Path "$ProjectPath\.git")) { return $null }
Push-Location $ProjectPath
try {
$branch = git branch --show-current 2>$null
$status = git status --porcelain 2>$null
$remotes = git remote -v 2>$null | Select-Object -First 1
$ahead = 0
$behind = 0
$tracking = git rev-parse --abbrev-ref '@{upstream}' 2>$null
if ($tracking) {
$counts = git rev-list --left-right --count "HEAD...$tracking" 2>$null
if ($counts -match '(\d+)\s+(\d+)') {
$ahead = [int]$Matches[1]
$behind = [int]$Matches[2]
}
}
$modified = ($status | Where-Object { $_ -match '^\s*M' }).Count
$added = ($status | Where-Object { $_ -match '^\?\?' }).Count
$deleted = ($status | Where-Object { $_ -match '^\s*D' }).Count
return [ordered]@{
branch = $branch
remote = if ($remotes -match '\s+(\S+)\s+') { $Matches[1] } else { $null }
ahead = $ahead
behind = $behind
modified = $modified
untracked = $added
deleted = $deleted
clean = ($status.Count -eq 0)
}
} finally {
Pop-Location
}
}
# Get directory structure (top level)
function Get-ProjectStructure {
param([string]$ProjectPath)
$items = Get-ChildItem $ProjectPath -Force | Where-Object {
$_.Name -notin @('node_modules', 'vendor', '.git', '__pycache__', '.idea', '.vscode', 'venv', '.venv')
} | Sort-Object { -not $_.PSIsContainer }, Name | Select-Object -First 20
$structure = @()
foreach ($item in $items) {
$type = if ($item.PSIsContainer) { 'dir' } else { 'file' }
$structure += @{ name = $item.Name; type = $type }
}
return $structure
}
# Build context
$projectType = Get-ProjectType -ProjectPath $Path
$context.type = $projectType.type
$context.framework = $projectType.framework
$context.dependencies = Get-Dependencies -ProjectPath $Path
$context.scripts = Get-AvailableScripts -ProjectPath $Path
$context.git = Get-GitInfo -ProjectPath $Path
$context.structure = Get-ProjectStructure -ProjectPath $Path
# Output
if ($AsJson) {
$output = $context | ConvertTo-Json -Depth 10
if ($Copy) {
$output | Set-Clipboard
Write-Host "JSON context copied to clipboard" -ForegroundColor Green
}
$output
exit 0
}
# Text output
$output = ""
if ($Brief) {
$output = @"
Project: $($context.name)
Type: $($context.type)$(if ($context.framework) { " ($($context.framework))" })
Path: $($context.path)
$(if ($context.git) { "Branch: $($context.git.branch)$(if (-not $context.git.clean) { ' (modified)' })" })
Dependencies: $($context.dependencies.Count)
"@
} else {
$output = @"
================================================================================
PROJECT CONTEXT: $($context.name)
================================================================================
TYPE: $($context.type)$(if ($context.framework) { " / $($context.framework)" })
PATH: $($context.path)
"@
if ($context.git) {
$gitStatus = if ($context.git.clean) { "clean" } else { "$($context.git.modified)M $($context.git.untracked)? $($context.git.deleted)D" }
$output += @"
GIT:
Branch: $($context.git.branch)
Remote: $($context.git.remote)
Status: $gitStatus
$(if ($context.git.ahead -gt 0) { "Ahead: $($context.git.ahead) commits" })
$(if ($context.git.behind -gt 0) { "Behind: $($context.git.behind) commits" })
"@
}
$output += @"
STRUCTURE:
"@
foreach ($item in $context.structure) {
$icon = if ($item.type -eq 'dir') { '[D]' } else { '[F]' }
$output += "`n $icon $($item.name)"
}
if ($context.dependencies.Count -gt 0) {
$output += "`n`nDEPENDENCIES ($($context.dependencies.Count)):"
$grouped = $context.dependencies | Group-Object { $_.type }
foreach ($group in $grouped) {
$output += "`n [$($group.Name)]"
$group.Group | Select-Object -First 10 | ForEach-Object {
$output += "`n - $($_.name)"
}
if ($group.Group.Count -gt 10) {
$output += "`n ... and $($group.Group.Count - 10) more"
}
}
}
if ($context.scripts.Count -gt 0) {
$output += "`n`nAVAILABLE SCRIPTS:"
foreach ($script in $context.scripts | Select-Object -First 10) {
$output += "`n - $($script.name)"
}
}
$output += @"
================================================================================
ENVIRONMENT: Windows / PowerShell$(if ($context.environment.wsl) { ' / WSL available' })
================================================================================
"@
}
if ($Copy) {
$output | Set-Clipboard
Write-Host "Context copied to clipboard" -ForegroundColor Green
Write-Host ""
}
Write-Output $output