# install.ps1 - install the herdr-composer package into the Herdr config. # # install.cmd install or update (the same command both times) # install.cmd -Uninstall remove it again # # What install does (Windows PowerShell 5): # 1. git pull --ff-only when this folder is a git clone without local changes # (a failure is a warning - the install goes on with what is here); # 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; # 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); # 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). # # The person's data (history, favourites, drafts, the queue) is not touched: # the mod reads what the earlier version kept (storage.js). [CmdletBinding()] param([switch]$Uninstall) $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' $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' $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" $OurAction = "$Name.focus" $OldAction = "$OldName.focus" 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) } # rmdir on a junction removes only the link, never the target's contents. function Remove-Junction([string]$Path) { cmd /c rmdir "`"$Path`"" | Out-Null 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" $n = 1 while (Test-Path -LiteralPath $candidate) { $candidate = "$Path.bak-$Stamp-$n" $n++ } return $candidate } # Nothing this script deletes or overwrites may be inside the repo. function Assert-OutsideRepo([string]$Path) { $full = [IO.Path]::GetFullPath($Path).TrimEnd([char]92) $repo = [IO.Path]::GetFullPath($Repo).TrimEnd([char]92) if ($full -ieq $repo -or $full.StartsWith($repo + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "$Path is inside the repo - refusing to touch it." } } function Test-HerdrApp { return [bool](Get-Command 'herdr-app' -ErrorAction SilentlyContinue) } 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)." return } & herdr-app do layout.switch --name $LayoutName if ($LASTEXITCODE -ne 0) { Write-Warning "layout.switch to '$LayoutName' failed (exit $LASTEXITCODE) - switch it by hand (ctrl-shift-l)." } } # --- keys.json ---------------------------------------------------------------- function Read-Json([string]$Path) { $raw = [IO.File]::ReadAllText($Path) if ($raw.Trim() -eq '') { return [pscustomobject]@{} } $obj = $raw | ConvertFrom-Json if ($obj -isnot [pscustomobject]) { throw "$Path is not a JSON object - resolve by hand." } return $obj } function Write-Json($Object, [string]$Path) { $json = $Object | ConvertTo-Json -Depth 32 [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" } } # Takes out every key (in every section) that is bound to one of $Actions. # Returns how many it removed. function Remove-KeyActions($Keys, [string[]]$Actions) { $removed = 0 foreach ($section in @($Keys.PSObject.Properties)) { if ($section.Value -isnot [pscustomobject]) { continue } foreach ($key in @($section.Value.PSObject.Properties)) { if ($Actions -contains [string]$key.Value) { $section.Value.PSObject.Properties.Remove($key.Name) $removed++ } } } return $removed } # Merges the keys of the package into the person's: adds what is missing, keeps # what is there, warns about a key the person binds to something else. function Update-Keys { if (-not (Test-Path -LiteralPath $KeysSrc)) { return } $package = Read-Json $KeysSrc if (-not (Test-Path -LiteralPath $KeysFile)) { Write-Json $package $KeysFile Write-Host "keys.json created from the package" return } $mine = Read-Json $KeysFile $changed = $false # The key of the earlier version points at an action that no longer exists. if ((Remove-KeyActions $mine @($OldAction)) -gt 0) { Backup-KeysOnce $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 $mine | Add-Member -NotePropertyName $section.Name -NotePropertyValue ([pscustomobject]@{}) $changed = $true } $target = $mine.($section.Name) foreach ($key in $section.Value.PSObject.Properties) { $existing = $target.PSObject.Properties[$key.Name] if ($existing) { if ([string]$existing.Value -ne [string]$key.Value) { Write-Warning "keys.json: '$($key.Name)' is already bound to '$($existing.Value)' - left as it is (the package wants '$($key.Value)')." } continue } Backup-KeysOnce $target | Add-Member -NotePropertyName $key.Name -NotePropertyValue $key.Value $changed = $true Write-Host "keys.json: $($key.Name) -> $($key.Value)" } } if ($changed) { Write-Json $mine $KeysFile } else { Write-Host "keys.json already has the keys" } } function Remove-OurKeys { if (-not (Test-Path -LiteralPath $KeysFile)) { return } $mine = Read-Json $KeysFile if ((Remove-KeyActions $mine @($OurAction, $OldAction)) -gt 0) { Backup-KeysOnce Write-Json $mine $KeysFile Write-Host "keys.json: removed the keys of $Name" } } # --- uninstall ---------------------------------------------------------------- if ($Uninstall) { Assert-OutsideRepo $ModDst if (Test-Junction $ModDst) { Remove-Junction $ModDst Write-Host "Removed junction $ModDst" } elseif (Test-Path -LiteralPath $ModDst) { Remove-Item -LiteralPath $ModDst -Recurse -Force Write-Host "Removed $ModDst" } if (Test-Path -LiteralPath $LayoutDst) { Remove-Item -LiteralPath $LayoutDst -Force Write-Host "Removed $LayoutDst" } Remove-OurKeys Switch-Layout 'main' exit 0 } # --- install ------------------------------------------------------------------ if (-not (Test-Path -LiteralPath (Join-Path $ModSrc 'main.js'))) { throw "Package not found: $ModSrc" } if (-not (Test-Path -LiteralPath $HerdrDir)) { throw "Herdr config not found: $HerdrDir (is Herdr installed and started once?)" } Assert-OutsideRepo $ModDst Assert-OutsideRepo $LayoutDst # 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 { $dirty = & git -C $Repo status --porcelain if ($dirty) { Write-Warning 'Local changes in the repo - not updating it (git pull skipped).' } else { & git -C $Repo pull --ff-only if ($LASTEXITCODE -ne 0) { Write-Warning "git pull --ff-only failed (exit $LASTEXITCODE) - installing what is here." } } } catch { Write-Warning "git pull failed: $($_.Exception.Message) - installing what is here." } } # 2. The earlier version (ovis-composer) goes out of the way. $OldMod = Join-Path $ModsDir $OldName 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" } $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" } # 3. The mod: a mirror copy. gpui-kit.d.ts and jsconfig.json are the editor's, # written by Herdr next to the mod - they are not part of the copy and are not # purged from the destination. New-Item -ItemType Directory -Force -Path $ModsDir | Out-Null if (Test-Junction $ModDst) { Remove-Junction $ModDst } robocopy $ModSrc $ModDst /MIR /XF gpui-kit.d.ts jsconfig.json /NFL /NDL /NJH /NJS /NP | Out-Null if ($LASTEXITCODE -ge 8) { throw "robocopy failed (exit $LASTEXITCODE)" } Write-Host "Mod copied: $ModDst" # 4. The layout. New-Item -ItemType Directory -Force -Path $LayoutsDir | Out-Null Copy-Item -LiteralPath $LayoutSrc -Destination $LayoutDst -Force Write-Host "Layout copied: $LayoutDst" # 5. Keys. Update-Keys # 6. Check the mod, show the layout. 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