From 48779cb930b30d3d841a7e66dc18949242c86f46 Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 12:59:44 +0100 Subject: [PATCH] fix: installer saves every copy under backups/, switches layout only on first install, README notes Co-Authored-By: Ovis Fast (Claude) --- README.md | 23 +++++--- scripts/install.ps1 | 127 +++++++++++++++++++++++++++++++------------- 2 files changed, 104 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 8ec2330..e2f98b6 100644 --- a/README.md +++ b/README.md @@ -36,14 +36,21 @@ The installer: - pulls the repo (`git pull --ff-only`, only when it is a clean clone; a failure is a warning); - **copies** the mod to `~\.config\herdr-app\mods\herdr-composer` (a mirror) and the - layout to `layouts\herdr-composer.json`, so Herdr loads the new files by itself; -- merges `keys.json` of the package into yours (a backup is kept once as - `keys.json.bak-herdr-composer`; a key you bind to something else is left alone, with a warning); + layout to `layouts\herdr-composer.json`, so Herdr loads the new files by itself (a layout that + is already there and differs is saved first; an identical one is left alone); +- merges `keys.json` of the package into yours. **The installer rewrites `keys.json`**: the + formatting changes (indentation, line breaks), the meaning does not. A copy is saved first, and a + key you bind to something else is left alone, with a warning; - clears away the earlier version, `ovis-composer` (a junction is unlinked, a directory or - layout is moved to a `.bak-` copy, its key is removed) - your history, favourites, - drafts and queue are kept: the mod reads what the earlier version stored; -- runs `herdr-app check` on the mod and switches to the `herdr-composer` layout - (without `herdr-app` on the PATH these two are warnings, not errors). + layout is moved out, its key is removed) - your history, favourites, drafts and queue are + kept: the mod reads what the earlier version stored; +- runs `herdr-app check` on the mod and switches to the `herdr-composer` layout - on the first + install, or when the earlier `ovis-composer` layout was the shown one; otherwise it only says + how to switch (`ctrl-shift-l`). Without `herdr-app` on the PATH these are warnings, not errors. + +Every copy the installer makes (the earlier mod, layouts, `keys.json`) goes to +`~\.config\herdr-app\backups\herdr-composer\\`, never next to the originals: +Herdr loads whatever it finds in `mods\` and `layouts\`, so a backup left there would be loaded. Files of this repo are never deleted. @@ -59,7 +66,7 @@ install.cmd -Uninstall ``` Removes `mods\herdr-composer`, `layouts\herdr-composer.json` and its key from `keys.json`, and -switches to the `main` layout. Only `.bak` copies stay. +switches to the `main` layout. Only the copies in `backups\herdr-composer\` stay. ## Develop diff --git a/scripts/install.ps1 b/scripts/install.ps1 index ffe1667..a2f7dea 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -9,15 +9,24 @@ # 2. COPIES herdr\herdr-composer\mods\herdr-composer to # ~\.config\herdr-app\mods\herdr-composer (a mirror, so Herdr sees the new # files and reloads the mod by itself) and the layout to -# ~\.config\herdr-app\layouts\herdr-composer.json; +# ~\.config\herdr-app\layouts\herdr-composer.json (an existing layout that +# differs is saved first; an identical one is left alone); # 3. merges the keys of the package (keys.json) into the person's keys.json -# (a backup is kept once as keys.json.bak-herdr-composer; a key the person -# already binds to something else is left alone, with a warning); +# (the file is rewritten - the formatting changes, the meaning does not - +# and a copy is saved first; a key the person already binds to something +# else is left alone, with a warning); # 4. clears away the earlier version (ovis-composer): a junction is only -# unlinked, a directory or a layout file is moved to a .bak- copy, -# its key is taken out of keys.json - files of this repo are never deleted; -# 5. herdr-app check on the mod, then switches to the herdr-composer layout -# (no herdr-app on the PATH is a warning, not an error). +# unlinked, a directory or a layout file is moved out, its key is taken +# out of keys.json - files of this repo are never deleted; +# 5. herdr-app check on the mod; the layout is switched to herdr-composer on +# the first install, or when the earlier ovis-composer was the shown one - +# otherwise the person is told how to switch (ctrl-shift-l). No herdr-app +# on the PATH is a warning, not an error. +# +# EVERY copy this script makes goes to +# ~\.config\herdr-app\backups\herdr-composer\\ +# and never next to the originals: Herdr loads whatever it finds in mods\ and +# layouts\, so a backup left there would be loaded as a mod or a layout. # # The person's data (history, favourites, drafts, the queue) is not touched: # the mod reads what the earlier version kept (storage.js). @@ -29,7 +38,6 @@ $ErrorActionPreference = 'Stop' $Name = 'herdr-composer' $OldName = 'ovis-composer' -$Stamp = Get-Date -Format 'yyyyMMdd' $Repo = Split-Path -Parent $PSScriptRoot $Package = Join-Path $Repo 'herdr\herdr-composer' @@ -37,13 +45,16 @@ $ModSrc = Join-Path $Package "mods\$Name" $LayoutSrc = Join-Path $Package "layouts\$Name.json" $KeysSrc = Join-Path $Package 'keys.json' -$HerdrDir = if ($env:HERDR_APP_HOME) { $env:HERDR_APP_HOME } else { Join-Path $env:USERPROFILE '.config\herdr-app' } -$ModsDir = Join-Path $HerdrDir 'mods' +$HerdrDir = if ($env:HERDR_APP_HOME) { $env:HERDR_APP_HOME } else { Join-Path $env:USERPROFILE '.config\herdr-app' } +$ModsDir = Join-Path $HerdrDir 'mods' $LayoutsDir = Join-Path $HerdrDir 'layouts' -$ModDst = Join-Path $ModsDir $Name -$LayoutDst = Join-Path $LayoutsDir "$Name.json" -$KeysFile = Join-Path $HerdrDir 'keys.json' -$KeysBak = "$KeysFile.bak-$Name" +$ModDst = Join-Path $ModsDir $Name +$LayoutDst = Join-Path $LayoutsDir "$Name.json" +$KeysFile = Join-Path $HerdrDir 'keys.json' + +# One folder per run; made when the first copy is saved. +$BackupDir = Join-Path $HerdrDir "backups\$Name\$(Get-Date -Format 'yyyyMMdd-HHmmss')" +$script:KeysSaved = $false $OurAction = "$Name.focus" $OldAction = "$OldName.focus" @@ -60,17 +71,25 @@ function Remove-Junction([string]$Path) { if ($LASTEXITCODE -ne 0) { throw "rmdir failed for $Path" } } -# A path for a backup that does not exist yet. -function Get-FreeBackupPath([string]$Path) { - $candidate = "$Path.bak-$Stamp" +# The path in the backup folder for an item with this name (never an existing one). +function Get-BackupPath([string]$ItemName) { + New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null + $candidate = Join-Path $BackupDir $ItemName $n = 1 while (Test-Path -LiteralPath $candidate) { - $candidate = "$Path.bak-$Stamp-$n" + $candidate = Join-Path $BackupDir "$ItemName-$n" $n++ } return $candidate } +# Moves a file or directory out of mods\ / layouts\ into the backup folder. +function Move-ToBackup([string]$Path) { + $target = Get-BackupPath (Split-Path -Leaf $Path) + Move-Item -LiteralPath $Path -Destination $target + return $target +} + # Nothing this script deletes or overwrites may be inside the repo. function Assert-OutsideRepo([string]$Path) { $full = [IO.Path]::GetFullPath($Path).TrimEnd([char]92) @@ -82,6 +101,17 @@ function Assert-OutsideRepo([string]$Path) { function Test-HerdrApp { return [bool](Get-Command 'herdr-app' -ErrorAction SilentlyContinue) } +# The name of the layout the window shows now, or nothing when it cannot be told. +function Get-ActiveLayout { + if (-not (Test-HerdrApp)) { return $null } + try { + $status = (& herdr-app status | Out-String) | ConvertFrom-Json + return [string]$status.layout + } catch { + return $null + } +} + function Switch-Layout([string]$LayoutName) { if (-not (Test-HerdrApp)) { Write-Warning "herdr-app is not on the PATH - switch the layout to '$LayoutName' by hand (ctrl-shift-l)." @@ -108,11 +138,13 @@ function Write-Json($Object, [string]$Path) { [IO.File]::WriteAllText($Path, $json, (New-Object Text.UTF8Encoding($false))) } -function Backup-KeysOnce { - if ((Test-Path -LiteralPath $KeysFile) -and -not (Test-Path -LiteralPath $KeysBak)) { - Copy-Item -LiteralPath $KeysFile -Destination $KeysBak - Write-Host "Backed up keys.json to $KeysBak" - } +# A copy of keys.json, once per run, before it is rewritten. +function Save-KeysCopy { + if ($script:KeysSaved -or -not (Test-Path -LiteralPath $KeysFile)) { return } + $target = Get-BackupPath 'keys.json' + Copy-Item -LiteralPath $KeysFile -Destination $target + $script:KeysSaved = $true + Write-Host "Saved a copy of keys.json: $target" } # Takes out every key (in every section) that is bound to one of $Actions. @@ -148,14 +180,14 @@ function Update-Keys { # The key of the earlier version points at an action that no longer exists. if ((Remove-KeyActions $mine @($OldAction)) -gt 0) { - Backup-KeysOnce + Save-KeysCopy $changed = $true Write-Host "keys.json: removed the key of $OldName" } foreach ($section in $package.PSObject.Properties) { if (-not $mine.PSObject.Properties[$section.Name]) { - Backup-KeysOnce + Save-KeysCopy $mine | Add-Member -NotePropertyName $section.Name -NotePropertyValue ([pscustomobject]@{}) $changed = $true } @@ -168,7 +200,7 @@ function Update-Keys { } continue } - Backup-KeysOnce + Save-KeysCopy $target | Add-Member -NotePropertyName $key.Name -NotePropertyValue $key.Value $changed = $true Write-Host "keys.json: $($key.Name) -> $($key.Value)" @@ -182,7 +214,7 @@ function Remove-OurKeys { if (-not (Test-Path -LiteralPath $KeysFile)) { return } $mine = Read-Json $KeysFile if ((Remove-KeyActions $mine @($OurAction, $OldAction)) -gt 0) { - Backup-KeysOnce + Save-KeysCopy Write-Json $mine $KeysFile Write-Host "keys.json: removed the keys of $Name" } @@ -215,6 +247,10 @@ if (-not (Test-Path -LiteralPath $HerdrDir)) { throw "Herdr config not found: $H Assert-OutsideRepo $ModDst Assert-OutsideRepo $LayoutDst +# What decides whether the layout is switched at the end - read before anything changes. +$firstInstall = -not (Test-Path -LiteralPath $LayoutDst) +$activeLayout = Get-ActiveLayout + # 1. Update the repo when it is a clean clone. if ((Test-Path -LiteralPath (Join-Path $Repo '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) { try { @@ -236,15 +272,13 @@ if (Test-Junction $OldMod) { Remove-Junction $OldMod Write-Host "Removed the junction of the earlier version: $OldMod" } elseif (Test-Path -LiteralPath $OldMod) { - $backup = Get-FreeBackupPath $OldMod - Move-Item -LiteralPath $OldMod -Destination $backup - Write-Host "Moved the earlier version to $backup" + $saved = Move-ToBackup $OldMod + Write-Host "Moved the earlier version to $saved" } $OldLayout = Join-Path $LayoutsDir "$OldName.json" if (Test-Path -LiteralPath $OldLayout) { - $backup = Get-FreeBackupPath $OldLayout - Move-Item -LiteralPath $OldLayout -Destination $backup - Write-Host "Moved the earlier layout to $backup" + $saved = Move-ToBackup $OldLayout + Write-Host "Moved the earlier layout to $saved" } # 3. The mod: a mirror copy. gpui-kit.d.ts and jsconfig.json are the editor's, @@ -256,19 +290,36 @@ robocopy $ModSrc $ModDst /MIR /XF gpui-kit.d.ts jsconfig.json /NFL /NDL /NJH /NJ if ($LASTEXITCODE -ge 8) { throw "robocopy failed (exit $LASTEXITCODE)" } Write-Host "Mod copied: $ModDst" -# 4. The layout. +# 4. The layout: an existing one that differs is saved first, an identical one stays. New-Item -ItemType Directory -Force -Path $LayoutsDir | Out-Null -Copy-Item -LiteralPath $LayoutSrc -Destination $LayoutDst -Force -Write-Host "Layout copied: $LayoutDst" +if (Test-Path -LiteralPath $LayoutDst) { + $same = (Get-FileHash -LiteralPath $LayoutSrc -Algorithm SHA256).Hash -eq (Get-FileHash -LiteralPath $LayoutDst -Algorithm SHA256).Hash + if ($same) { + Write-Host "Layout is up to date: $LayoutDst" + } else { + $saved = Move-ToBackup $LayoutDst + Write-Host "Saved the layout that was there: $saved" + Copy-Item -LiteralPath $LayoutSrc -Destination $LayoutDst -Force + Write-Host "Layout copied: $LayoutDst" + } +} else { + Copy-Item -LiteralPath $LayoutSrc -Destination $LayoutDst -Force + Write-Host "Layout copied: $LayoutDst" +} # 5. Keys. Update-Keys -# 6. Check the mod, show the layout. +# 6. Check the mod; switch the layout only where the person has not chosen another one. if (Test-HerdrApp) { & herdr-app check $ModDst if ($LASTEXITCODE -ne 0) { Write-Warning "herdr-app check reported a problem (exit $LASTEXITCODE)." } } else { Write-Warning 'herdr-app is not on the PATH - the mod was not checked.' } -Switch-Layout $Name +if ($firstInstall -or $activeLayout -eq $OldName) { + Switch-Layout $Name +} elseif ($activeLayout -ne $Name) { + # ASCII only in this file (Windows PowerShell 5 reads it in the ANSI code page): the Polish letters are built. + Write-Host "Prze$([char]0x0142)$([char]0x0105)cz layout: ctrl-shift-l (layout '$Name')" +}