Repository navigation
Expand file tree
/
Copy pathProjectShell.Migration.ps1
More file actions
506 lines (493 loc) · 27.6 KB
/
Copy pathProjectShell.Migration.ps1
File metadata and controls
506 lines (493 loc) · 27.6 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# ProjectShell application-name migration v1.
# User-controlled metadata transition. Never renames source trees, repositories,
# EXEs, installers, workspace folders, or command/script contents.
$script:NameMigrationRoot = Join-Path $script:AppRoot 'name-migrations'
function Assert-ProjectNameTransitionName([string]$Name) {
if ([string]::IsNullOrWhiteSpace($Name) -or $Name.Trim() -ne $Name -or
$Name.Length -gt 80 -or $Name.Length -lt 3 -or
$Name -match '[\\/:*?"<>|\x00-\x1f]' -or $Name.EndsWith('.')) {
throw 'Enter a name of 3-80 characters without path separators, invalid filename characters or surrounding spaces.'
}
}
function Convert-ProjectNameTransitionValue([string]$Value,[string]$OldName,[string]$NewName,[string]$OldAlias='', [string]$NewAlias='') {
if ([string]::Equals($Value,$OldName,[StringComparison]::OrdinalIgnoreCase)) { return $NewName }
if ($Value.Length -gt ($OldName.Length+3) -and
$Value.Substring(0,$OldName.Length).Equals($OldName,[StringComparison]::OrdinalIgnoreCase) -and
$Value.Substring($OldName.Length).StartsWith(' - ',[StringComparison]::Ordinal)) {
return ($NewName + $Value.Substring($OldName.Length))
}
if ($OldAlias) {
if ([string]::Equals($Value,$OldAlias,[StringComparison]::OrdinalIgnoreCase)) { return $NewAlias }
if ($Value.Length -gt ($OldAlias.Length+3) -and
$Value.Substring(0,$OldAlias.Length).Equals($OldAlias,[StringComparison]::OrdinalIgnoreCase) -and
$Value.Substring($OldAlias.Length).StartsWith(' - ',[StringComparison]::Ordinal)) {
return ($NewAlias + $Value.Substring($OldAlias.Length))
}
}
return $Value
}
function Get-ProjectNameTransitionHash([string]$Path) {
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return '' }
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToUpperInvariant()
}
function New-ProjectNameTransitionPlan([string]$OldName,[string]$NewName,[string]$OldAlias='', [string]$NewAlias='') {
Assert-ProjectNameTransitionName $OldName
Assert-ProjectNameTransitionName $NewName
if ($OldName.Equals($NewName,[StringComparison]::OrdinalIgnoreCase)) {
throw 'The previous and proposed names must be different. Case-only renames are deferred.'
}
if ($OldAlias -or $NewAlias) {
Assert-ProjectNameTransitionName $OldAlias
Assert-ProjectNameTransitionName $NewAlias
if ($OldAlias.Equals($NewAlias,[StringComparison]::OrdinalIgnoreCase) -or
$OldAlias.Equals($OldName,[StringComparison]::OrdinalIgnoreCase)) {
throw 'The optional legacy alias must be distinct from the main name and its replacement.'
}
}
$profiles = if (Test-Path -LiteralPath $script:StorePath -PathType Leaf) { @(Read-Store) } else { @() }
$workspace = Read-WorkspaceStore
$allWorkspaces = @($workspace.Workspaces)
$profileChanges = [System.Collections.Generic.List[object]]::new()
$workspaceChanges = [System.Collections.Generic.List[object]]::new()
$plannedFiles = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
foreach ($profile in $profiles) {
if ([string]$profile.ShortcutFile -notmatch '^[^\/:*?"<>|\x00-\x1f]+\.lnk$') {
throw ('Unsafe saved shortcut filename for profile: ' + $profile.Name)
}
$newTitle = Convert-ProjectNameTransitionValue ([string]$profile.Name) $OldName $NewName $OldAlias $NewAlias
$newGroup = Convert-ProjectNameTransitionValue ([string]$profile.Group) $OldName $NewName $OldAlias $NewAlias
if ($newTitle -cne [string]$profile.Name -or $newGroup -cne [string]$profile.Group) {
if (@($profiles | Where-Object { $_.Id -ne $profile.Id -and $_.Name -ieq $newTitle }).Count -gt 0) {
throw ('A project shortcut already uses the new name: ' + $newTitle)
}
$newLink = [string]$profile.ShortcutFile
if ($newTitle -cne [string]$profile.Name) {
$safeBase = 'ProjectShell - ' + (Get-SafeName $newTitle)
$index = 1
do {
$suffix = if ($index -eq 1) { '' } else { ' (' + $index + ')' }
$candidate = $safeBase + $suffix + '.lnk'
$target = Join-Path $script:Desktop $candidate
$taken = ((Test-Path -LiteralPath $target) -and
$candidate -ine [string]$profile.ShortcutFile) -or
$plannedFiles.Contains($candidate) -or
(@($profiles | Where-Object { $_.Id -ne $profile.Id -and $_.ShortcutFile -ieq $candidate }).Count -gt 0)
$index++
} while ($taken -and $index -le 1000)
if ($taken) { throw 'No safe desktop shortcut name available.' }
$newLink = $candidate
}
[void]$plannedFiles.Add($newLink)
$profileChanges.Add([pscustomobject]@{
Id=[string]$profile.Id
OldName=[string]$profile.Name
NewName=$newTitle
OldGroup=[string]$profile.Group
NewGroup=$newGroup
OldShortcut=[string]$profile.ShortcutFile
NewShortcut=$newLink
StartingFolder=[string]$profile.StartDirectory
Launchable=[string]$profile.MainLaunchable
StartupValue=[string]$profile.StartupValue
})
}
}
foreach ($item in $allWorkspaces) {
$newLabel = Convert-ProjectNameTransitionValue ([string]$item.Name) $OldName $NewName $OldAlias $NewAlias
if ($newLabel -cne [string]$item.Name) {
if (@($allWorkspaces | Where-Object { $_.Id -ne $item.Id -and $_.Name -ieq $newLabel }).Count -gt 0) {
throw ('A saved workspace already uses the new name: ' + $newLabel)
}
$workspaceChanges.Add([pscustomobject]@{
Id=[string]$item.Id
OldName=[string]$item.Name
NewName=$newLabel
Folder=[string]$item.Path
Launchable=[string]$item.Launchable
Installer=[string]$item.Installer
})
}
}
return [pscustomobject]@{
SchemaVersion=1
PreviousName=$OldName
ProposedName=$NewName
PreviousAlias=$OldAlias
ProposedAlias=$NewAlias
ProfileHash=(Get-ProjectNameTransitionHash $script:StorePath)
WorkspaceHash=(Get-ProjectNameTransitionHash $script:WorkspaceStorePath)
ProfileChanges=@($profileChanges.ToArray())
WorkspaceChanges=@($workspaceChanges.ToArray())
}
}
function Show-ProjectNameTransitionPlan($Plan) {
Write-Host ''
Write-Host 'PROJECTSHELL | APPLICATION NAME TRANSITION — PREVIEW' -ForegroundColor Cyan
Write-Host ('Previous name: ' + $Plan.PreviousName)
Write-Host ('Proposed name: ' + $Plan.ProposedName)
if ($Plan.PreviousAlias) { Write-Host ('Explicit alias: ' + $Plan.PreviousAlias + ' -> ' + $Plan.ProposedAlias) }
Write-Host ('Saved shortcuts affected: ' + @($Plan.ProfileChanges).Count)
foreach ($item in $Plan.ProfileChanges) {
Write-Host (' ' + $item.OldName + ' -> ' + $item.NewName)
Write-Host (' Desktop: ' + $item.OldShortcut + ' -> ' + $item.NewShortcut)
Write-Host (' Start folder stays: ' + $item.StartingFolder)
}
Write-Host ('Saved workspaces affected: ' + @($Plan.WorkspaceChanges).Count)
foreach ($item in $Plan.WorkspaceChanges) {
Write-Host (' ' + $item.OldName + ' -> ' + $item.NewName)
Write-Host (' Folder stays: ' + $item.Folder)
}
Write-Host 'External identity checks needed for full transition:' -ForegroundColor Yellow
Write-Host ' Source code identifiers, app title, package IDs and internal configuration'
Write-Host ' EXE/ZIP/MSIX names, manifest/publisher data, installer/uninstaller/update paths'
Write-Host ' README, help, tests, GitHub repository, CI, published release and backlinks'
Write-Host ' Existing installs, backwards compatibility, rollback and Windows launch tests'
Write-Host 'No application folders, source files, GitHub projects or installed programs are renamed.'
}
function Test-ProjectNameTransitionOwnedLink([string]$Path,[string]$Id) {
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return $false }
$shell = $null; $link = $null
try {
$shell = New-Object -ComObject WScript.Shell
$link = $shell.CreateShortcut($Path)
return ([string]$link.Arguments -match '(?i)(?:^|\s)-LaunchId(?:\s|$)' -and
[string]$link.Arguments -match [regex]::Escape($Id) -and
[string]$link.Arguments -match [regex]::Escape('ProjectShell.ps1'))
}
catch { return $false }
finally {
if ($null -ne $link) { [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($link) }
if ($null -ne $shell) { [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($shell) }
}
}
function Invoke-ProjectNameTransition($Plan,[string]$Confirmation) {
if ($Confirmation -cne ('RENAME ' + $Plan.PreviousName)) {
throw ('To proceed, enter exactly: RENAME ' + $Plan.PreviousName)
}
if (@($Plan.ProfileChanges).Count + @($Plan.WorkspaceChanges).Count -eq 0) {
throw 'No exact/prefix ProjectShell names match. Nothing to rename.'
}
if ($Plan.ProfileHash -cne (Get-ProjectNameTransitionHash $script:StorePath) -or
$Plan.WorkspaceHash -cne (Get-ProjectNameTransitionHash $script:WorkspaceStorePath)) {
throw 'Saved projects changed after the preview. Create a new plan before applying.'
}
foreach ($change in $Plan.ProfileChanges) {
$target = Join-Path $script:Desktop $change.NewShortcut
if ($change.NewShortcut -ine $change.OldShortcut -and (Test-Path -LiteralPath $target)) {
throw ('Desktop shortcut already exists; preview again: ' + $target)
}
}
Ensure-Store
$id = (Get-Date).ToUniversalTime().ToString('yyyyMMddTHHmmssZ') + '-' + [guid]::NewGuid().ToString('N')
$folder = Join-Path $script:NameMigrationRoot $id
New-Item -Path $folder -ItemType Directory -Force | Out-Null
$profilesBefore = Join-Path $folder 'profiles.before.json'
$workspacesBefore = Join-Path $folder 'workspaces.before.json'
$oldLinks = [System.Collections.Generic.List[object]]::new()
$newLinks = [System.Collections.Generic.List[string]]::new()
$profileTouched = (@($Plan.ProfileChanges).Count -gt 0)
$workspaceTouched = (@($Plan.WorkspaceChanges).Count -gt 0)
$profilesAfterHash = ''; $workspacesAfterHash = ''
$committed = $false
try {
if ($profileTouched) { Copy-Item -LiteralPath $script:StorePath -Destination $profilesBefore -ErrorAction Stop }
if ($workspaceTouched) { Copy-Item -LiteralPath $script:WorkspaceStorePath -Destination $workspacesBefore -ErrorAction Stop }
foreach ($change in $Plan.ProfileChanges) {
if ($change.OldShortcut -ine $change.NewShortcut) {
$oldPath = Join-Path $script:Desktop $change.OldShortcut
$backupFile = Join-Path $folder ('shortcut-' + [guid]::NewGuid().ToString('N') + '.lnk')
$owned = Test-ProjectNameTransitionOwnedLink $oldPath $change.Id
if ($owned) { Copy-Item -LiteralPath $oldPath -Destination $backupFile -ErrorAction Stop }
$oldLinks.Add([pscustomobject]@{
Path=$oldPath
Backup=$(if ($owned) { $backupFile } else { '' })
Owned=$owned
})
}
}
if ($profileTouched) {
$profiles = @(Read-Store)
foreach ($profile in $profiles) {
foreach ($change in $Plan.ProfileChanges) {
if ([string]$profile.Id -eq $change.Id) {
$profile.Name = $change.NewName
$profile.Group = $change.NewGroup
$profile.ShortcutFile = $change.NewShortcut
}
}
}
Save-Store $profiles
foreach ($change in $Plan.ProfileChanges) {
if ($change.NewShortcut -ine $change.OldShortcut) {
$entry = @($profiles | Where-Object { $_.Id -eq $change.Id })[0]
$newPath = Join-Path $script:Desktop $change.NewShortcut
[void]$newLinks.Add($newPath)
Write-ProfileLink $entry
if (-not (Test-Path -LiteralPath $newPath -PathType Leaf)) {
throw ('New shortcut was not created: ' + $newPath)
}
}
}
$profilesAfterHash = Get-ProjectNameTransitionHash $script:StorePath
}
if ($workspaceTouched) {
$data = Read-WorkspaceStore
foreach ($item in $data.Workspaces) {
foreach ($change in $Plan.WorkspaceChanges) {
if ([string]$item.Id -eq $change.Id) { $item.Name = $change.NewName }
}
}
Save-WorkspaceStore $data
$workspacesAfterHash = Get-ProjectNameTransitionHash $script:WorkspaceStorePath
}
$receipt = [pscustomobject]@{
SchemaVersion=1; Id=$id; PreviousName=$Plan.PreviousName; ProposedName=$Plan.ProposedName
PreviousAlias=$Plan.PreviousAlias; ProposedAlias=$Plan.ProposedAlias
ProfileTouched=$profileTouched; WorkspaceTouched=$workspaceTouched
ProfilesBeforeHash=$Plan.ProfileHash; WorkspacesBeforeHash=$Plan.WorkspaceHash
ProfilesAfterHash=$profilesAfterHash; WorkspacesAfterHash=$workspacesAfterHash
ProfileChanges=@($Plan.ProfileChanges); WorkspaceChanges=@($Plan.WorkspaceChanges)
OldLinks=@($oldLinks.ToArray()); NewLinks=@($newLinks.ToArray())
ExternalNamingVerified=$false
}
$manifestPath = Join-Path $folder 'migration.json'
$receipt | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $manifestPath -Encoding UTF8 -ErrorAction Stop
# Only remove a previous ProjectShell-owned link after the new link and
# receipt exist. Leave ambiguous/unowned links untouched.
foreach ($link in $oldLinks) {
if ($link.Owned -and (Test-Path -LiteralPath $link.Path -PathType Leaf)) {
Remove-Item -LiteralPath $link.Path -Force -ErrorAction Stop
}
}
$committed = $true
Write-Host ('Migration receipt and backups: ' + $folder) -ForegroundColor Green
return $receipt
}
finally {
if (-not $committed) {
'Transaction did not complete; metadata restore attempted.' | Set-Content -LiteralPath (Join-Path $folder 'aborted.txt')
if ($profileTouched -and (Test-Path -LiteralPath $profilesBefore -PathType Leaf)) {
Copy-Item -LiteralPath $profilesBefore -Destination $script:StorePath -Force
}
if ($workspaceTouched -and (Test-Path -LiteralPath $workspacesBefore -PathType Leaf)) {
Copy-Item -LiteralPath $workspacesBefore -Destination $script:WorkspaceStorePath -Force
}
foreach ($newLink in $newLinks) {
if (Test-Path -LiteralPath $newLink -PathType Leaf) { Remove-Item -LiteralPath $newLink -Force }
}
foreach ($old in $oldLinks) {
if ($old.Backup -and -not (Test-Path -LiteralPath $old.Path) -and
(Test-Path -LiteralPath $old.Backup -PathType Leaf)) {
Copy-Item -LiteralPath $old.Backup -Destination $old.Path
}
}
}
}
}
function Get-ProjectNameTransitionLatest {
if (-not (Test-Path -LiteralPath $script:NameMigrationRoot -PathType Container)) { return $null }
$folders = @(Get-ChildItem -LiteralPath $script:NameMigrationRoot -Directory |
Sort-Object -Property Name -Descending)
foreach ($dir in $folders) {
$manifest = Join-Path $dir.FullName 'migration.json'
$rollback = Join-Path $dir.FullName 'rolled-back.txt'
$aborted = Join-Path $dir.FullName 'aborted.txt'
if ((Test-Path -LiteralPath $manifest -PathType Leaf) -and
-not (Test-Path -LiteralPath $rollback -PathType Leaf) -and
-not (Test-Path -LiteralPath $aborted -PathType Leaf)) {
return [pscustomobject]@{ Folder=$dir.FullName; Receipt=(Get-Content -LiteralPath $manifest -Raw -Encoding UTF8 | ConvertFrom-Json) }
}
}
return $null
}
function Test-ProjectNameTransition($Record) {
if (-not $Record) { throw 'No completed migration receipt is available.' }
$r = $Record.Receipt
$profileHashOK = (-not $r.ProfileTouched) -or ($r.ProfilesAfterHash -ceq (Get-ProjectNameTransitionHash $script:StorePath))
$workspaceHashOK = (-not $r.WorkspaceTouched) -or ($r.WorkspacesAfterHash -ceq (Get-ProjectNameTransitionHash $script:WorkspaceStorePath))
$newLinksOK = $true
foreach ($link in $r.NewLinks) {
if (-not (Test-Path -LiteralPath ([string]$link) -PathType Leaf)) { $newLinksOK = $false }
}
$remainingOld = @($r.OldLinks | Where-Object { Test-Path -LiteralPath ([string]$_.Path) -PathType Leaf })
return [pscustomobject]@{
ProjectShellMetadataVerified=($profileHashOK -and $workspaceHashOK -and $newLinksOK)
OldShortcutLinksRemaining=$remainingOld.Count
ExternalApplicationRenameVerified=$false
ReceiptFolder=$Record.Folder
}
}
function Undo-ProjectNameTransition($Record,[string]$Confirmation) {
if (-not $Record) { throw 'No completed migration to roll back.' }
$r = $Record.Receipt
if ($Confirmation -cne ('ROLLBACK ' + $r.Id)) { throw ('Type exactly: ROLLBACK ' + $r.Id) }
$check = Test-ProjectNameTransition $Record
if (-not $check.ProjectShellMetadataVerified) {
throw 'Projects or shortcuts changed since migration. Rollback refuses to overwrite them.'
}
if ($r.ProfileTouched) {
$before = Join-Path $Record.Folder 'profiles.before.json'
if ((Get-ProjectNameTransitionHash $before) -cne $r.ProfilesBeforeHash) { throw 'Profile backup checksum mismatch.' }
}
if ($r.WorkspaceTouched) {
$before = Join-Path $Record.Folder 'workspaces.before.json'
if ((Get-ProjectNameTransitionHash $before) -cne $r.WorkspacesBeforeHash) { throw 'Workspace backup checksum mismatch.' }
}
foreach ($old in $r.OldLinks) {
if ($old.Backup -and (Test-Path -LiteralPath ([string]$old.Path))) {
throw ('Old desktop shortcut path has been reused; rollback stopped: ' + $old.Path)
}
}
foreach ($link in $r.NewLinks) {
if (-not (Test-ProjectNameTransitionOwnedLink ([string]$link) ([string](@($r.ProfileChanges | Where-Object {
(Join-Path $script:Desktop $_.NewShortcut) -eq [string]$link })[0].Id)))) {
throw ('New shortcut ownership cannot be verified; rollback stopped: ' + $link)
}
}
if ($r.ProfileTouched) {
Copy-Item -LiteralPath (Join-Path $Record.Folder 'profiles.before.json') -Destination $script:StorePath -Force
}
if ($r.WorkspaceTouched) {
Copy-Item -LiteralPath (Join-Path $Record.Folder 'workspaces.before.json') -Destination $script:WorkspaceStorePath -Force
}
foreach ($link in $r.NewLinks) { Remove-Item -LiteralPath ([string]$link) -Force }
foreach ($old in $r.OldLinks) {
if ($old.Backup) { Copy-Item -LiteralPath ([string]$old.Backup) -Destination ([string]$old.Path) -Force }
}
'Rolled back by explicit user request.' | Set-Content -LiteralPath (Join-Path $Record.Folder 'rolled-back.txt')
Write-Host ('ProjectShell metadata restored from: ' + $Record.Folder) -ForegroundColor Green
}
function Find-ProjectNameTransitionReferences([string]$ProjectRoot,[string]$OldName,[string]$NewName) {
Assert-ProjectNameTransitionName $OldName
Assert-ProjectNameTransitionName $NewName
if (-not (Test-Path -LiteralPath $ProjectRoot -PathType Container)) {
throw 'Select an existing project folder for the read-only inventory.'
}
$root = [IO.Path]::GetFullPath($ProjectRoot)
if ($root.TrimEnd('\') -eq [IO.Path]::GetPathRoot($root).TrimEnd('\')) {
throw 'Choose a specific project folder, not an entire drive.'
}
if ((Get-Item -LiteralPath $root -Force).Attributes -band [IO.FileAttributes]::ReparsePoint) {
throw 'Do not select a symlink or junction root.'
}
$ignoredFolders = @('.git','.venv','venv','node_modules','.tox','__pycache__','.vs','.idea','.pytest_cache','dist','build')
$allowedExtensions = @('.ps1','.psm1','.psd1','.py','.json','.md','.txt','.yml','.yaml','.toml','.ini','.cfg','.xml','.cs','.c','.h','.js','.ts','.tsx','.jsx','.cmd','.bat','.spec','.wxs','.iss')
$toVisit = [System.Collections.Generic.Queue[string]]::new()
$toVisit.Enqueue($root)
$findings = [System.Collections.Generic.List[object]]::new()
$scanned = 0; $skipped = 0; $entries = 0
while ($toVisit.Count -gt 0) {
$dir = $toVisit.Dequeue()
foreach ($item in (Get-ChildItem -LiteralPath $dir -Force -ErrorAction Stop)) {
$entries++
if ($entries -gt 3000) { throw 'Inventory limit reached (3000 entries). Narrow the source folder before scanning.' }
if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { $skipped++; continue }
if ($item.PSIsContainer) {
if ($item.Name -in $ignoredFolders) { $skipped++; continue }
$toVisit.Enqueue($item.FullName)
continue
}
$fileOld = $item.Name.IndexOf($OldName,[StringComparison]::OrdinalIgnoreCase) -ge 0
$fileNew = $item.Name.IndexOf($NewName,[StringComparison]::OrdinalIgnoreCase) -ge 0
$contentOld = 0; $contentNew = 0
$scanContent = ([IO.Path]::GetExtension($item.Name).ToLowerInvariant() -in $allowedExtensions) -and
($item.Length -le 262144) -and ($item.Name -notmatch '(?i)(password|secret|token|credential|\.env)')
if ($scanContent) {
try {
$content = Get-Content -LiteralPath $item.FullName -Raw -Encoding UTF8 -ErrorAction Stop
$contentOld = [regex]::Matches($content,[regex]::Escape($OldName),[Text.RegularExpressions.RegexOptions]::IgnoreCase).Count
$contentNew = [regex]::Matches($content,[regex]::Escape($NewName),[Text.RegularExpressions.RegexOptions]::IgnoreCase).Count
$scanned++
}
catch { $skipped++ }
} else { $skipped++ }
if ($fileOld -or $fileNew -or $contentOld -or $contentNew) {
$findings.Add([pscustomobject]@{
RelativePath=$item.FullName.Substring($root.TrimEnd('\').Length).TrimStart('\')
OldNameInFileName=$fileOld
NewNameInFileName=$fileNew
OldNameTextMatches=$contentOld
NewNameTextMatches=$contentNew
})
}
}
}
return [pscustomobject]@{
ProjectRoot=$root
OldName=$OldName
NewName=$NewName
TextFilesInspected=$scanned
ItemsSkipped=$skipped
NameReferences=@($findings.ToArray())
ExternalIdentityVerified=$false
}
}
function Show-ProjectNameTransitionMenu {
$plan = $null
while ($true) {
Write-Host ''
Write-Host 'PROJECTSHELL | APPLICATION NAME TRANSITION' -ForegroundColor Cyan
Write-Host ' 1. Plan/preview a name transition (read-only)'
Write-Host ' 2. Apply only ProjectShell-managed names (backup + exact approval)'
Write-Host ' 3. Verify latest ProjectShell-managed transition'
Write-Host ' 4. Roll back latest transition (only if no later changes)'
Write-Host ' 5. Inventory a selected app folder for old/new name references (read-only)'
Write-Host ' 0. Back'
$choice = (Read-Host 'Number').Trim()
try {
switch ($choice) {
'1' {
$old = (Read-Host 'Current application name').Trim()
$next = (Read-Host 'Proposed application name').Trim()
$oldAlias = (Read-Host 'Existing short name/alias to update too (optional)').Trim()
$newAlias = ''
if ($oldAlias) { $newAlias = (Read-Host 'New short name/alias').Trim() }
$plan = New-ProjectNameTransitionPlan $old $next $oldAlias $newAlias
Show-ProjectNameTransitionPlan $plan
}
'2' {
if (-not $plan) { Write-Host 'Use option 1 to preview first.' -ForegroundColor Yellow; continue }
if (-not (Read-YesNo 'Backups will be created. Apply only ProjectShell names?' $false)) { continue }
$typed = Read-Host ('Type RENAME ' + $plan.PreviousName)
$null = Invoke-ProjectNameTransition $plan $typed
$plan = $null
Write-Host 'Only ProjectShell-managed names changed. External identity checks are still pending.' -ForegroundColor Yellow
}
'3' {
$latest = Get-ProjectNameTransitionLatest
if (-not $latest) { Write-Host 'No completed transition.'; continue }
$result = Test-ProjectNameTransition $latest
Write-Host ('ProjectShell metadata verified: ' + $result.ProjectShellMetadataVerified)
Write-Host ('Previous desktop shortcuts remaining: ' + $result.OldShortcutLinksRemaining)
Write-Host 'Full application naming transition: NOT VERIFIED (external gates need separate review).' -ForegroundColor Yellow
Write-Host ('Receipt: ' + $result.ReceiptFolder)
}
'4' {
$latest = Get-ProjectNameTransitionLatest
if (-not $latest) { Write-Host 'No completed transition to roll back.'; continue }
Write-Host ('Selected receipt: ' + $latest.Receipt.Id)
if (-not (Read-YesNo 'Restore the saved ProjectShell metadata and links?' $false)) { continue }
$typed = Read-Host ('Type ROLLBACK ' + $latest.Receipt.Id)
Undo-ProjectNameTransition $latest $typed
}
'5' {
$old = if ($plan) { $plan.PreviousName } else { (Read-Host 'Existing application name').Trim() }
$next = if ($plan) { $plan.ProposedName } else { (Read-Host 'Proposed application name').Trim() }
$folder = (Read-Host 'Application source folder (full path; no changes)').Trim().Trim('"')
$result = Find-ProjectNameTransitionReferences $folder $old $next
Write-Host ('Text files inspected: ' + $result.TextFilesInspected)
Write-Host ('Skipped entries: ' + $result.ItemsSkipped)
Write-Host ('Files with old/new name references: ' + @($result.NameReferences).Count)
foreach ($item in $result.NameReferences) {
Write-Host (' ' + $item.RelativePath + ' | old: ' + $item.OldNameTextMatches + ' | new: ' + $item.NewNameTextMatches)
}
Write-Host 'Inventory is bounded, read-only and incomplete for binaries, installers, GitHub or ignored folders.' -ForegroundColor Yellow
Write-Host 'A full renaming transition still requires manual acceptance.'
}
'0' { return }
default { Write-Host 'Choose a listed number.' -ForegroundColor Yellow }
}
}
catch { Write-Host ('MIGRATION ERROR: ' + $_.Exception.Message) -ForegroundColor Red }
}
}