claude-meta/scripts/link-project-memory.ps1

72 lines
2.5 KiB
PowerShell

# link-project-memory.ps1 — Add a new project to the memory backup map and create junction
param(
[Parameter(Mandatory)][string]$EncodedName,
[Parameter(Mandatory)][string]$FriendlyName
)
$ErrorActionPreference = "Stop"
$repoRoot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$projectMapFile = Join-Path $repoRoot "memory\projects\project-map.json"
$memorySrc = Join-Path $repoRoot "memory\projects\$FriendlyName"
$claudeDir = Join-Path $env:USERPROFILE ".claude"
$projectDir = Join-Path $claudeDir "projects\$EncodedName"
$memoryDst = Join-Path $projectDir "memory"
# Validate project dir exists
if (-not (Test-Path $projectDir)) {
Write-Host "ERROR: Project dir not found: $projectDir" -ForegroundColor Red
exit 1
}
# Create repo memory dir
if (-not (Test-Path $memorySrc)) {
New-Item -ItemType Directory -Path $memorySrc -Force | Out-Null
New-Item -ItemType File -Path (Join-Path $memorySrc ".gitkeep") -Force | Out-Null
Write-Host "Created: $memorySrc" -ForegroundColor Cyan
}
# Update project-map.json
$map = @{}
if (Test-Path $projectMapFile) {
$json = Get-Content $projectMapFile -Raw | ConvertFrom-Json
foreach ($prop in $json.PSObject.Properties) {
$map[$prop.Name] = $prop.Value
}
}
$map[$EncodedName] = $FriendlyName
$sorted = [ordered]@{}
$map.GetEnumerator() | Sort-Object Key | ForEach-Object { $sorted[$_.Key] = $_.Value }
$sorted | ConvertTo-Json | Set-Content $projectMapFile -Encoding UTF8
Write-Host "Updated: project-map.json" -ForegroundColor Cyan
# Move existing memory files
if (Test-Path $memoryDst) {
$item = Get-Item $memoryDst -Force
if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
Write-Host "Already a junction: $memoryDst" -ForegroundColor Green
exit 0
}
$existing = Get-ChildItem $memoryDst -File -ErrorAction SilentlyContinue
foreach ($f in $existing) {
$destFile = Join-Path $memorySrc $f.Name
if (-not (Test-Path $destFile)) {
Move-Item $f.FullName $destFile
Write-Host "Moved: $($f.Name)" -ForegroundColor Cyan
}
}
Remove-Item $memoryDst -Recurse -Force
}
# Create junction
cmd /c "mklink /J `"$memoryDst`" `"$memorySrc`"" | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host "JUNCTION: $memoryDst -> $memorySrc" -ForegroundColor Green
} else {
Write-Host "ERROR: Junction creation failed" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "Done! Memory for '$FriendlyName' is now backed up in the repo." -ForegroundColor Green