-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-codebase.ps1
More file actions
136 lines (115 loc) · 5.94 KB
/
import-codebase.ps1
File metadata and controls
136 lines (115 loc) · 5.94 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
# import-codebase.ps1
$InputFile = "imported.txt"
$ErrorActionPreference = "Stop"
# Ensure Ctrl+C breaks the script
[Console]::TreatControlCAsInput = $false
while ($true) {
Clear-Host
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host " Continuous Code Importer (Strip Path) " -ForegroundColor White
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host "Reading from: $InputFile" -ForegroundColor DarkGray
Write-Host "Format expected inside imported.txt:" -ForegroundColor DarkGray
# Use single quotes here to prevent backticks from escaping the string
Write-Host ' ```python' -ForegroundColor DarkGray
Write-Host " # src/path/to/file.py" -ForegroundColor Green
Write-Host " <file content>" -ForegroundColor DarkGray
# Use single quotes here to prevent backticks from escaping the string
Write-Host ' ```' -ForegroundColor DarkGray
Write-Host "Note: The '# path' line will be removed from the saved file." -ForegroundColor Yellow
Write-Host ""
Write-Host "Press Ctrl+C to exit." -ForegroundColor DarkGray
Write-Host ""
# 1. Create imported.txt if it doesn't exist
if (-not (Test-Path $InputFile)) {
New-Item -Path $InputFile -ItemType File -Force | Out-Null
Write-Host "Created empty '$InputFile'. Paste your AI response there." -ForegroundColor Yellow
}
# 2. Read content
$lines = Get-Content -Path $InputFile -Encoding UTF8 -ErrorAction SilentlyContinue
if ($null -eq $lines -or $lines.Count -eq 0) {
Write-Host "File '$InputFile' is empty." -ForegroundColor Yellow
}
else {
$inBlock = $false
$currentRelativePath = $null
$currentContent = @()
$filesExtracted = 0
# Regex to find start of block (ignores language like python/json)
# Matches: Start of line, optional space, 3 backticks, optional anything else
$startBlockRegex = '^\s*```.*$'
# Regex to find end of block
$endBlockRegex = '^\s*```\s*$'
# Regex to find the path inside the block (Must start with #)
$pathLineRegex = '^\s*#\s+(?<Path>.+)\s*$'
foreach ($line in $lines) {
if (-not $inBlock) {
# Look for Start Block
if ($line -match $startBlockRegex) {
$inBlock = $true
$currentRelativePath = $null
$currentContent = @()
}
}
else {
# Look for End Block
if ($line -match $endBlockRegex) {
# Only save if we actually found a path inside this block
if (-not [string]::IsNullOrWhiteSpace($currentRelativePath)) {
try {
# Normalize path for Windows
$normPath = $currentRelativePath.Replace('/', [System.IO.Path]::DirectorySeparatorChar).Replace('\', [System.IO.Path]::DirectorySeparatorChar)
$fullPath = Join-Path $PSScriptRoot $normPath
$parentDir = Split-Path $fullPath -Parent
# Create directory if it doesn't exist
if (-not [string]::IsNullOrWhiteSpace($parentDir)) {
if (-not (Test-Path $parentDir)) {
New-Item -ItemType Directory -Path $parentDir -Force | Out-Null
}
}
# Write content
$finalContent = $currentContent -join [Environment]::NewLine
# Ensure file ends with a newline
if (-not $finalContent.EndsWith([Environment]::NewLine)) {
$finalContent += [Environment]::NewLine
}
Set-Content -Path $fullPath -Value $finalContent -Force -Encoding UTF8
Write-Host " -> Saved to: $normPath" -ForegroundColor Green
$filesExtracted++
}
catch {
Write-Host " -> Error saving $currentRelativePath : $_" -ForegroundColor Red
}
}
# Reset state
$inBlock = $false
$currentRelativePath = $null
$currentContent = @()
}
else {
# We are inside a block.
# Check if we are still looking for the path
if ($null -eq $currentRelativePath) {
if ($line -match $pathLineRegex) {
$rawPath = $Matches['Path'].Trim()
# Basic validation: ensure it has a separator or extension to look like a file
if ($rawPath -match '\.|/|\\') {
$currentRelativePath = $rawPath
Write-Host "Found file: $currentRelativePath" -ForegroundColor Cyan
# IMPORTANT: Continue loop here so this line is NOT added to $currentContent
continue
}
}
}
# If we reach here, it's normal content (or a comment that wasn't a path)
$currentContent += $line
}
}
}
Write-Host ""
Write-Host "Extraction cycle complete. Extracted: $filesExtracted files." -ForegroundColor White
}
Write-Host ""
Write-Host "Press [Enter] to re-scan '$InputFile'..." -ForegroundColor Green
$null = Read-Host
}