Files
herdrMario/scripts/install.ps1
T

155 lines
6.5 KiB
PowerShell

# install.ps1 - link the repo's ovis-composer mod into the Herdr config.
#
# .\scripts\install.ps1 install (idempotent)
# .\scripts\install.ps1 -Uninstall remove the junction, restore backups
# .\scripts\install.ps1 -Reload recreate the junction so Herdr loads the mod again
#
# Herdr does NOT see changes behind the junction on its own: after editing
# mod\ovis-composer run -Reload (the junction is removed and made again, and
# Herdr reloads the mod).
#
# Works on Windows PowerShell 5.
[CmdletBinding()]
param([switch]$Uninstall, [switch]$Reload)
$ErrorActionPreference = 'Stop'
$Stamp = '20261119'
$Repo = Split-Path -Parent $PSScriptRoot
$HerdrDir = Join-Path $env:USERPROFILE '.config\herdr-app'
$ModsDir = Join-Path $HerdrDir 'mods'
$Junction = Join-Path $ModsDir 'ovis-composer'
$ModBackup = "$Junction.bak-$Stamp"
$ModSource = Join-Path $Repo 'mod\ovis-composer'
$LayoutSrc = Join-Path $Repo 'layouts\ovis-composer.json'
$LayoutDst = Join-Path $HerdrDir 'layouts\ovis-composer.json'
$LayoutBak = "$LayoutDst.bak-$Stamp"
$KeysFile = Join-Path $HerdrDir 'keys.json'
$KeysBak = "$KeysFile.bak-$Stamp"
function Test-Junction([string]$Path) {
if (-not (Test-Path -LiteralPath $Path)) { return $false }
$item = Get-Item -LiteralPath $Path -Force
return [bool]($item.Attributes -band [IO.FileAttributes]::ReparsePoint)
}
function Remove-Junction([string]$Path) {
# rmdir on a junction removes only the link, never the target's contents.
cmd /c rmdir "`"$Path`"" | Out-Null
if ($LASTEXITCODE -ne 0) { throw "rmdir failed for $Path" }
}
# Merge "ctrl-shift-space": "ovis-composer.focus" into keys.json "global",
# leaving every other entry alone. Backs the file up once; idempotent.
function Install-KeyEntry {
$chord = 'ctrl-shift-space'
$action = 'ovis-composer.focus'
if (-not (Test-Path -LiteralPath $KeysFile)) {
$obj = [pscustomobject]@{ global = [pscustomobject]@{ $chord = $action } }
} else {
$raw = [IO.File]::ReadAllText($KeysFile)
$obj = $raw | ConvertFrom-Json
if ($obj -isnot [pscustomobject]) { throw "keys.json is not a JSON object - resolve by hand." }
if (-not $obj.PSObject.Properties['global']) {
$obj | Add-Member -NotePropertyName 'global' -NotePropertyValue ([pscustomobject]@{})
}
$global = $obj.global
if ($global.PSObject.Properties[$chord] -and $global.$chord -eq $action) {
Write-Host "keys.json already has $chord"
return
}
if (-not (Test-Path -LiteralPath $KeysBak)) {
Copy-Item -LiteralPath $KeysFile -Destination $KeysBak
Write-Host "Backed up keys.json to $KeysBak"
}
$global | Add-Member -NotePropertyName $chord -NotePropertyValue $action -Force
}
$json = $obj | ConvertTo-Json -Depth 32
[IO.File]::WriteAllText($KeysFile, $json, (New-Object Text.UTF8Encoding($false)))
Write-Host "keys.json: $chord -> $action"
}
if ($Reload) {
if (-not (Test-Junction $Junction)) { throw "$Junction is not a junction - run install.ps1 first." }
Remove-Junction $Junction
if (-not (Test-Path -LiteralPath (Join-Path $ModSource 'main.js'))) {
throw 'Repo files vanished after removing the junction - aborting.'
}
cmd /c mklink /J "`"$Junction`"" "`"$ModSource`"" | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'mklink /J failed' }
Write-Host "Junction recreated: $Junction -> $ModSource (Herdr loads the mod again)"
exit 0
}
if ($Uninstall) {
if (Test-Junction $Junction) {
Remove-Junction $Junction
if (-not (Test-Path -LiteralPath (Join-Path $ModSource 'main.js'))) {
throw 'Repo files vanished after removing the junction - aborting.'
}
Write-Host "Removed junction $Junction"
}
if ((Test-Path -LiteralPath $ModBackup) -and -not (Test-Path -LiteralPath $Junction)) {
Move-Item -LiteralPath $ModBackup -Destination $Junction
Write-Host "Restored $Junction"
}
if (Test-Path -LiteralPath $LayoutBak) {
Copy-Item -LiteralPath $LayoutBak -Destination $LayoutDst -Force
Write-Host "Restored layout"
}
if (Test-Path -LiteralPath $KeysBak) {
Copy-Item -LiteralPath $KeysBak -Destination $KeysFile -Force
Write-Host "Restored keys.json"
}
herdr-app do layout.switch '{"name":"main"}'
if ($LASTEXITCODE -ne 0) { Write-Warning "layout.switch to main failed (exit $LASTEXITCODE) - switch the layout by hand." }
exit 0
}
# 1. Mod directory -> junction to the repo.
$relink = $false
if (Test-Junction $Junction) {
$target = (Get-Item -LiteralPath $Junction -Force).Target
if ($target -is [array]) { $target = $target[0] }
if ($target -and ([IO.Path]::GetFullPath($target).TrimEnd([char]92) -ieq [IO.Path]::GetFullPath($ModSource).TrimEnd([char]92))) {
Write-Host "Junction already in place: $Junction"
} else {
Write-Host "Junction points at '$target' - recreating it for $ModSource"
Remove-Junction $Junction
$relink = $true
}
} else { $relink = $true }
if ($relink) {
if (Test-Path -LiteralPath $Junction) {
if (Test-Path -LiteralPath $ModBackup) { throw "$ModBackup already exists - resolve by hand." }
Move-Item -LiteralPath $Junction -Destination $ModBackup
Write-Host "Backed up to $ModBackup"
}
cmd /c mklink /J "`"$Junction`"" "`"$ModSource`"" | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'mklink /J failed' }
Write-Host "Junction: $Junction -> $ModSource"
}
# gpui-kit.d.ts is git-ignored; keep a copy next to the sources for the editor.
$kitDst = Join-Path $ModSource 'gpui-kit.d.ts'
$kitSrc = if (Test-Path -LiteralPath (Join-Path $ModBackup 'gpui-kit.d.ts')) { Join-Path $ModBackup 'gpui-kit.d.ts' } else { Join-Path $HerdrDir 'gpui-kit.d.ts' }
if (-not (Test-Path -LiteralPath $kitDst) -and (Test-Path -LiteralPath $kitSrc)) {
Copy-Item -LiteralPath $kitSrc -Destination $kitDst
}
# 2. Layout (back up the existing file once).
if ((Test-Path -LiteralPath $LayoutDst) -and -not (Test-Path -LiteralPath $LayoutBak)) {
Copy-Item -LiteralPath $LayoutDst -Destination $LayoutBak
Write-Host "Backed up layout to $LayoutBak"
}
Copy-Item -LiteralPath $LayoutSrc -Destination $LayoutDst -Force
Write-Host "Layout copied"
# 3. Key binding (Task 2).
Install-KeyEntry
# 4. Show the layout.
herdr-app do layout.switch '{"name":"ovis-composer"}'
if ($LASTEXITCODE -ne 0) { Write-Warning "layout.switch to ovis-composer failed (exit $LASTEXITCODE) - switch the layout by hand." }