-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclean.ps1
More file actions
198 lines (165 loc) · 5.31 KB
/
Copy pathclean.ps1
File metadata and controls
198 lines (165 loc) · 5.31 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
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[switch]$Deep,
[switch]$Reset, # Full blank-slate: everything Deep does + wipes settings.json, PLAN.md, TASK.md
[switch]$PruneDist # Keep only the current Cargo.toml version under dist/
)
$ErrorActionPreference = "Stop"
$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $repoRoot
$removed = New-Object System.Collections.Generic.List[string]
function Get-CurrentVersion {
$cargoTomlPath = Join-Path $repoRoot "Cargo.toml"
if (-not (Test-Path -LiteralPath $cargoTomlPath)) {
throw "Cargo.toml not found at repo root."
}
$cargoToml = Get-Content -LiteralPath $cargoTomlPath -Raw
$match = [regex]::Match($cargoToml, '(?m)^version\s*=\s*"([^"]+)"')
if (-not $match.Success) {
throw "Could not determine package version from Cargo.toml."
}
$match.Groups[1].Value
}
function Remove-TreeContents {
param(
[Parameter(Mandatory = $true)]
[string]$LiteralPath
)
if (-not (Test-Path -LiteralPath $LiteralPath)) {
return
}
Get-ChildItem -LiteralPath $LiteralPath -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($PSCmdlet.ShouldProcess($_.FullName, "Remove")) {
Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
$removed.Add($_.FullName) | Out-Null
}
}
}
function Remove-IfExists {
param(
[Parameter(Mandatory = $true)]
[string]$LiteralPath
)
if (-not (Test-Path -LiteralPath $LiteralPath)) {
return
}
$resolved = Resolve-Path -LiteralPath $LiteralPath -ErrorAction SilentlyContinue
$display = if ($resolved) { $resolved.Path } else { (Join-Path $repoRoot $LiteralPath) }
if ($PSCmdlet.ShouldProcess($display, "Remove")) {
Remove-Item -LiteralPath $LiteralPath -Recurse -Force -ErrorAction SilentlyContinue
$removed.Add($display) | Out-Null
}
}
function Remove-Matches {
param(
[Parameter(Mandatory = $true)]
[string]$Pattern
)
Get-ChildItem -Path $Pattern -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($PSCmdlet.ShouldProcess($_.FullName, "Remove")) {
Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
$removed.Add($_.FullName) | Out-Null
}
}
}
function Remove-StaleDistArtifacts {
$distRoot = Join-Path $repoRoot "dist"
if (-not (Test-Path -LiteralPath $distRoot)) {
return
}
$currentVersion = Get-CurrentVersion
$keepPrefix = "Hematite-$currentVersion"
$candidateParents = @($distRoot)
$candidateParents += Get-ChildItem -LiteralPath $distRoot -Directory -Force -ErrorAction SilentlyContinue | ForEach-Object {
$_.FullName
}
foreach ($parent in $candidateParents | Select-Object -Unique) {
Get-ChildItem -LiteralPath $parent -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.Name -like "Hematite-*" -and $_.Name -notlike "$keepPrefix*") {
if ($PSCmdlet.ShouldProcess($_.FullName, "Remove stale dist artifact")) {
Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
$removed.Add($_.FullName) | Out-Null
}
}
}
}
}
$contentDirs = @(
".hematite\ghost",
".hematite\scratch",
".hematite\memories",
".hematite\sandbox",
".hematite_logs",
".hematite_scratch",
"tmp"
)
foreach ($dir in $contentDirs) {
Remove-TreeContents -LiteralPath $dir
}
$runtimeFiles = @(
".hematite\session.json",
".hematite\last_request.json",
".hematite\vein.db-shm",
".hematite\vein.db-wal",
"hematite_memory.db-shm",
"hematite_memory.db-wal",
"error.log",
"error_log.txt",
"our_errors.txt",
"error_lines.txt",
"build_output.txt",
"build_errors.txt",
"build_errors_utf8.txt",
"build_errors.txt.txt",
"build_errors.txt.json",
"errors.txt",
"errors.txt.json",
"errors.json",
"errors.json.txt"
)
foreach ($file in $runtimeFiles) {
Remove-IfExists -LiteralPath $file
}
$runtimeGlobs = @(
"cargo_errors*.txt"
)
foreach ($pattern in $runtimeGlobs) {
Remove-Matches -Pattern $pattern
}
Remove-TreeContents -LiteralPath ".hematite\reports"
if ($Deep -or $Reset) {
$deepTargets = @(
"target",
"onnx_lib",
".hematite\vein.db"
)
foreach ($target in $deepTargets) {
Remove-IfExists -LiteralPath $target
}
}
if ($Reset) {
# Full blank-slate — simulates a new user with no prior state.
# settings.json and mcp_servers.json are intentionally NOT wiped here;
# delete them manually if you want to test the absolute default config path.
$resetTargets = @(
".hematite\PLAN.md",
".hematite\TASK.md"
)
foreach ($target in $resetTargets) {
Remove-IfExists -LiteralPath $target
}
}
if ($PruneDist) {
Remove-StaleDistArtifacts
}
$summary = if ($WhatIfPreference) {
"Hematite cleanup dry run complete."
} elseif ($removed.Count -eq 0) {
"Hematite cleanup: nothing to remove."
} else {
"Hematite cleanup removed $($removed.Count) item(s)."
}
Write-Host $summary
if ($removed.Count -gt 0) {
$removed | Sort-Object | ForEach-Object { Write-Host " - $_" }
}