-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextualizer.ps1
More file actions
executable file
·174 lines (142 loc) · 6.02 KB
/
contextualizer.ps1
File metadata and controls
executable file
·174 lines (142 loc) · 6.02 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
# ==============================================================================
# AI Context Generator Script (PowerShell)
#
# Description:
# This script creates a single text file (`ai_context.txt`) containing the
# directory tree and the contents of all relevant source code files in the
# current project directory. This is ideal for providing context to an AI model.
#
# Instructions:
# 1. Place this script in the root directory of your project.
# 2. Run it: .\create_ai_context.ps1
# 3. The `ai_context.txt` file will be created in the same directory.
#
# Note: Ensure the file is saved as UTF-8. This version uses only ASCII characters
# to avoid encoding issues.
# ==============================================================================
# --- Configuration ---
# The name of the output file.
$OUTPUT_FILE = "ai_context.txt"
# Directories to ignore completely in both the tree and file content.
$IGNORE_DIRS = @("node_modules", "public", ".next", ".open-next", ".wrangler", ".git", "dist", "build", "vendor", "__pycache__", ".venv", "venv", ".idea", ".vscode")
# Specific files to ignore.
$IGNORE_FILES = @("package-lock.json")
# File extensions to include.
$INCLUDE_EXTENSIONS = @(
"js", "jsx", "ts", "tsx", "html", "css", "scss", "sass", "less",
"py", "rb", "php", "go", "java", "c", "cpp", "h", "hpp", "cs",
"json", "yml", "yaml", "xml", "md", "sql", "puml", "txt",
"Dockerfile", "docker-compose.yml", "package.json", "tsconfig.json", ".env.example"
)
# --- Functions ---
function Write-Tree {
param(
[string]$Path = ".",
[int]$Depth = 0,
[string[]]$IgnoreDirs,
[string[]]$IgnoreFiles,
[int]$MaxDepth = 10
)
if ($Depth -gt $MaxDepth) { return }
$indent = " " * $Depth
$item = Get-Item $Path
$displayName = if ($item.PSIsContainer) { $item.Name + "/" } else { $item.Name }
# Skip if in ignore list
if ($IgnoreDirs -contains $item.Name -or $IgnoreFiles -contains $item.Name) {
return
}
Write-Output "$indent$displayName"
if ($item.PSIsContainer) {
$children = Get-ChildItem -Path $Path | Where-Object {
$childName = $_.Name
$isDir = $_.PSIsContainer
$isIgnored = ($IgnoreDirs -contains $childName) -or
($isDir -and ($IgnoreDirs | Where-Object { $_.FullName -like "*\$childName*" })) -or
(-not $isDir -and $IgnoreFiles -contains $childName)
-not $isIgnored
}
foreach ($child in $children) {
Write-Tree -Path $child.FullName -Depth ($Depth + 1) -IgnoreDirs $IgnoreDirs -IgnoreFiles $IgnoreFiles
}
}
}
# --- Script Logic ---
Write-Host "Starting AI context generation..."
# Clear the output file if it already exists.
if (Test-Path $OUTPUT_FILE) {
Clear-Content -Path $OUTPUT_FILE
}
New-Item -Path $OUTPUT_FILE -ItemType File -Force | Out-Null
# 1. Add a header to the output file.
$header = @"
=================================================
PROJECT CONTEXT FOR AI ANALYSIS
=================================================
This document contains the project structure and source code for review.
Project root: $(Get-Location)
Generated on: $(Get-Date)
-------------------------------------------------
PROJECT FILE AND FOLDER STRUCTURE
-------------------------------------------------
"@
Add-Content -Path $OUTPUT_FILE -Value $header -Encoding UTF8
# 2. Generate and append the directory tree.
Write-Host "Generating file tree..."
$ignore_pattern = ($IGNORE_DIRS + $OUTPUT_FILE + $IGNORE_FILES) -join "|"
$tree_lines = Write-Tree -IgnoreDirs $IGNORE_DIRS -IgnoreFiles $IGNORE_FILES
$tree_lines | Add-Content -Path $OUTPUT_FILE -Encoding UTF8
# 3. Add a separator before the code files.
$separator = @"
-------------------------------------------------
PROJECT SOURCE CODE FILES
-------------------------------------------------
"@
Add-Content -Path $OUTPUT_FILE -Value $separator -Encoding UTF8
# 4. Find and process all relevant files.
Write-Host "Finding and processing source files..."
$files = Get-ChildItem -Recurse -File | Where-Object {
$path = $_.FullName
$fileName = $_.Name
$extension = if ($_.Extension) { $_.Extension.TrimStart('.').ToLower() } else { $fileName.ToLower() }
# Skip ignored directories
$isIgnoredDir = $IGNORE_DIRS | Where-Object { $path -like "*\$_\*" }
if ($isIgnoredDir) { return $false }
# Skip the output file and ignored files
if ($fileName -eq $OUTPUT_FILE -or $IGNORE_FILES -contains $fileName) { return $false }
# Include files with matching extensions or exact filenames
$isIncluded = $INCLUDE_EXTENSIONS -contains $extension -or $INCLUDE_EXTENSIONS -contains $fileName
return $isIncluded
}
foreach ($file in $files) {
$relativePath = $file.FullName.Substring((Get-Location).Path.Length + 1).Replace('\', '/')
Write-Host " -> Processing: $relativePath"
# Get the file extension or filename for language hint
$extension = if ($file.Extension) { $file.Extension.TrimStart('.').ToLower() } else { $file.Name.ToLower() }
$lang_hint = $extension
# Refine language hint for common cases
switch -Regex ($lang_hint) {
'^(js|jsx)$' { $lang_hint = "javascript" }
'^(ts|tsx)$' { $lang_hint = "typescript" }
'^(puml|plantuml)$' { $lang_hint = "plantuml" }
'^py$' { $lang_hint = "python" }
'^rb$' { $lang_hint = "ruby" }
'^md$' { $lang_hint = "markdown" }
'^yml$' { $lang_hint = "yaml" }
'^sh$' { $lang_hint = "bash" }
}
# Read file content
$content = Get-Content -Path $file.FullName -Raw -Encoding UTF8
# Append file header and content to the output file
# Use ```` to output literal ```
$file_block = @"
=========================================
FILE: $relativePath
=========================================
````$lang_hint
$content
````
"@
Add-Content -Path $OUTPUT_FILE -Value $file_block -Encoding UTF8
}
Write-Host "Success! Context saved to '$OUTPUT_FILE'."
Write-Host "You can now copy the contents of this file into the AI prompt."