fix: installer saves every copy under backups/, switches layout only on first install, README notes

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 12:59:44 +01:00
1 parent df4605bac1
commit 48779cb930
2 files changed
+104 -46

No files matched your search

+15 -8
View File
@@ -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); - 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 - **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; layout to `layouts\herdr-composer.json`, so Herdr loads the new files by itself (a layout that
- merges `keys.json` of the package into yours (a backup is kept once as is already there and differs is saved first; an identical one is left alone);
`keys.json.bak-herdr-composer`; a key you bind to something else is left alone, with a warning); - 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 - clears away the earlier version, `ovis-composer` (a junction is unlinked, a directory or
layout is moved to a `.bak-<date>` copy, its key is removed) - your history, favourites, layout is moved out, its key is removed) - your history, favourites, drafts and queue are
drafts and queue are kept: the mod reads what the earlier version stored; kept: the mod reads what the earlier version stored;
- runs `herdr-app check` on the mod and switches to the `herdr-composer` layout - runs `herdr-app check` on the mod and switches to the `herdr-composer` layout - on the first
(without `herdr-app` on the PATH these two are warnings, not errors). 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\<yyyyMMdd-HHmmss>\`, 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. 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 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 ## Develop
+89 -38
View File
@@ -9,15 +9,24 @@
# 2. COPIES herdr\herdr-composer\mods\herdr-composer to # 2. COPIES herdr\herdr-composer\mods\herdr-composer to
# ~\.config\herdr-app\mods\herdr-composer (a mirror, so Herdr sees the new # ~\.config\herdr-app\mods\herdr-composer (a mirror, so Herdr sees the new
# files and reloads the mod by itself) and the layout to # 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 # 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 # (the file is rewritten - the formatting changes, the meaning does not -
# already binds to something else is left alone, with a warning); # 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 # 4. clears away the earlier version (ovis-composer): a junction is only
# unlinked, a directory or a layout file is moved to a .bak-<date> copy, # unlinked, a directory or a layout file is moved out, its key is taken
# its key is taken out of keys.json - files of this repo are never deleted; # 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 # 5. herdr-app check on the mod; the layout is switched to herdr-composer on
# (no herdr-app on the PATH is a warning, not an error). # 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\<yyyyMMdd-HHmmss>\
# 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 person's data (history, favourites, drafts, the queue) is not touched:
# the mod reads what the earlier version kept (storage.js). # the mod reads what the earlier version kept (storage.js).
@@ -29,7 +38,6 @@ $ErrorActionPreference = 'Stop'
$Name = 'herdr-composer' $Name = 'herdr-composer'
$OldName = 'ovis-composer' $OldName = 'ovis-composer'
$Stamp = Get-Date -Format 'yyyyMMdd'
$Repo = Split-Path -Parent $PSScriptRoot $Repo = Split-Path -Parent $PSScriptRoot
$Package = Join-Path $Repo 'herdr\herdr-composer' $Package = Join-Path $Repo 'herdr\herdr-composer'
@@ -37,13 +45,16 @@ $ModSrc = Join-Path $Package "mods\$Name"
$LayoutSrc = Join-Path $Package "layouts\$Name.json" $LayoutSrc = Join-Path $Package "layouts\$Name.json"
$KeysSrc = Join-Path $Package 'keys.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' } $HerdrDir = if ($env:HERDR_APP_HOME) { $env:HERDR_APP_HOME } else { Join-Path $env:USERPROFILE '.config\herdr-app' }
$ModsDir = Join-Path $HerdrDir 'mods' $ModsDir = Join-Path $HerdrDir 'mods'
$LayoutsDir = Join-Path $HerdrDir 'layouts' $LayoutsDir = Join-Path $HerdrDir 'layouts'
$ModDst = Join-Path $ModsDir $Name $ModDst = Join-Path $ModsDir $Name
$LayoutDst = Join-Path $LayoutsDir "$Name.json" $LayoutDst = Join-Path $LayoutsDir "$Name.json"
$KeysFile = Join-Path $HerdrDir 'keys.json' $KeysFile = Join-Path $HerdrDir 'keys.json'
$KeysBak = "$KeysFile.bak-$Name"
# 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" $OurAction = "$Name.focus"
$OldAction = "$OldName.focus" $OldAction = "$OldName.focus"
@@ -60,17 +71,25 @@ function Remove-Junction([string]$Path) {
if ($LASTEXITCODE -ne 0) { throw "rmdir failed for $Path" } if ($LASTEXITCODE -ne 0) { throw "rmdir failed for $Path" }
} }
# A path for a backup that does not exist yet. # The path in the backup folder for an item with this name (never an existing one).
function Get-FreeBackupPath([string]$Path) { function Get-BackupPath([string]$ItemName) {
$candidate = "$Path.bak-$Stamp" New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null
$candidate = Join-Path $BackupDir $ItemName
$n = 1 $n = 1
while (Test-Path -LiteralPath $candidate) { while (Test-Path -LiteralPath $candidate) {
$candidate = "$Path.bak-$Stamp-$n" $candidate = Join-Path $BackupDir "$ItemName-$n"
$n++ $n++
} }
return $candidate 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. # Nothing this script deletes or overwrites may be inside the repo.
function Assert-OutsideRepo([string]$Path) { function Assert-OutsideRepo([string]$Path) {
$full = [IO.Path]::GetFullPath($Path).TrimEnd([char]92) $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) } 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) { function Switch-Layout([string]$LayoutName) {
if (-not (Test-HerdrApp)) { if (-not (Test-HerdrApp)) {
Write-Warning "herdr-app is not on the PATH - switch the layout to '$LayoutName' by hand (ctrl-shift-l)." 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))) [IO.File]::WriteAllText($Path, $json, (New-Object Text.UTF8Encoding($false)))
} }
function Backup-KeysOnce { # A copy of keys.json, once per run, before it is rewritten.
if ((Test-Path -LiteralPath $KeysFile) -and -not (Test-Path -LiteralPath $KeysBak)) { function Save-KeysCopy {
Copy-Item -LiteralPath $KeysFile -Destination $KeysBak if ($script:KeysSaved -or -not (Test-Path -LiteralPath $KeysFile)) { return }
Write-Host "Backed up keys.json to $KeysBak" $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. # 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. # The key of the earlier version points at an action that no longer exists.
if ((Remove-KeyActions $mine @($OldAction)) -gt 0) { if ((Remove-KeyActions $mine @($OldAction)) -gt 0) {
Backup-KeysOnce Save-KeysCopy
$changed = $true $changed = $true
Write-Host "keys.json: removed the key of $OldName" Write-Host "keys.json: removed the key of $OldName"
} }
foreach ($section in $package.PSObject.Properties) { foreach ($section in $package.PSObject.Properties) {
if (-not $mine.PSObject.Properties[$section.Name]) { if (-not $mine.PSObject.Properties[$section.Name]) {
Backup-KeysOnce Save-KeysCopy
$mine | Add-Member -NotePropertyName $section.Name -NotePropertyValue ([pscustomobject]@{}) $mine | Add-Member -NotePropertyName $section.Name -NotePropertyValue ([pscustomobject]@{})
$changed = $true $changed = $true
} }
@@ -168,7 +200,7 @@ function Update-Keys {
} }
continue continue
} }
Backup-KeysOnce Save-KeysCopy
$target | Add-Member -NotePropertyName $key.Name -NotePropertyValue $key.Value $target | Add-Member -NotePropertyName $key.Name -NotePropertyValue $key.Value
$changed = $true $changed = $true
Write-Host "keys.json: $($key.Name) -> $($key.Value)" Write-Host "keys.json: $($key.Name) -> $($key.Value)"
@@ -182,7 +214,7 @@ function Remove-OurKeys {
if (-not (Test-Path -LiteralPath $KeysFile)) { return } if (-not (Test-Path -LiteralPath $KeysFile)) { return }
$mine = Read-Json $KeysFile $mine = Read-Json $KeysFile
if ((Remove-KeyActions $mine @($OurAction, $OldAction)) -gt 0) { if ((Remove-KeyActions $mine @($OurAction, $OldAction)) -gt 0) {
Backup-KeysOnce Save-KeysCopy
Write-Json $mine $KeysFile Write-Json $mine $KeysFile
Write-Host "keys.json: removed the keys of $Name" 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 $ModDst
Assert-OutsideRepo $LayoutDst 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. # 1. Update the repo when it is a clean clone.
if ((Test-Path -LiteralPath (Join-Path $Repo '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) { if ((Test-Path -LiteralPath (Join-Path $Repo '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) {
try { try {
@@ -236,15 +272,13 @@ if (Test-Junction $OldMod) {
Remove-Junction $OldMod Remove-Junction $OldMod
Write-Host "Removed the junction of the earlier version: $OldMod" Write-Host "Removed the junction of the earlier version: $OldMod"
} elseif (Test-Path -LiteralPath $OldMod) { } elseif (Test-Path -LiteralPath $OldMod) {
$backup = Get-FreeBackupPath $OldMod $saved = Move-ToBackup $OldMod
Move-Item -LiteralPath $OldMod -Destination $backup Write-Host "Moved the earlier version to $saved"
Write-Host "Moved the earlier version to $backup"
} }
$OldLayout = Join-Path $LayoutsDir "$OldName.json" $OldLayout = Join-Path $LayoutsDir "$OldName.json"
if (Test-Path -LiteralPath $OldLayout) { if (Test-Path -LiteralPath $OldLayout) {
$backup = Get-FreeBackupPath $OldLayout $saved = Move-ToBackup $OldLayout
Move-Item -LiteralPath $OldLayout -Destination $backup Write-Host "Moved the earlier layout to $saved"
Write-Host "Moved the earlier layout to $backup"
} }
# 3. The mod: a mirror copy. gpui-kit.d.ts and jsconfig.json are the editor's, # 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)" } if ($LASTEXITCODE -ge 8) { throw "robocopy failed (exit $LASTEXITCODE)" }
Write-Host "Mod copied: $ModDst" 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 New-Item -ItemType Directory -Force -Path $LayoutsDir | Out-Null
Copy-Item -LiteralPath $LayoutSrc -Destination $LayoutDst -Force if (Test-Path -LiteralPath $LayoutDst) {
Write-Host "Layout copied: $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. # 5. Keys.
Update-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) { if (Test-HerdrApp) {
& herdr-app check $ModDst & herdr-app check $ModDst
if ($LASTEXITCODE -ne 0) { Write-Warning "herdr-app check reported a problem (exit $LASTEXITCODE)." } if ($LASTEXITCODE -ne 0) { Write-Warning "herdr-app check reported a problem (exit $LASTEXITCODE)." }
} else { } else {
Write-Warning 'herdr-app is not on the PATH - the mod was not checked.' 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')"
}