373 lines
15 KiB
PowerShell
373 lines
15 KiB
PowerShell
# 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 (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
|
|
# (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 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\<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 mod reads what the earlier version kept (storage.js).
|
|
|
|
[CmdletBinding()]
|
|
param([switch]$Uninstall)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$Name = 'herdr-composer'
|
|
$OldName = 'ovis-composer'
|
|
|
|
$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'
|
|
|
|
# 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"
|
|
|
|
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" }
|
|
}
|
|
|
|
# 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 = 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)
|
|
$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) }
|
|
|
|
# Runs herdr-app and waits for it.
|
|
#
|
|
# herdr-app.exe is a program of the GUI subsystem: PowerShell does not wait for
|
|
# it and does not set $LASTEXITCODE (whatever the last console program left
|
|
# there stays - 1 after robocopy means "copied"). So it is started with
|
|
# Start-Process, waited for, and its exit code and output are read from the
|
|
# process and from two temporary files.
|
|
#
|
|
# Returns ExitCode, Output (stdout) and Error (stderr).
|
|
function Invoke-HerdrApp([string[]]$Arguments) {
|
|
$exe = (Get-Command 'herdr-app' -ErrorAction Stop).Source
|
|
$outFile = [IO.Path]::GetTempFileName()
|
|
$errFile = [IO.Path]::GetTempFileName()
|
|
try {
|
|
$quoted = @($Arguments | ForEach-Object {
|
|
if ($_ -match '[\s"]') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ }
|
|
})
|
|
$process = Start-Process -FilePath $exe -ArgumentList $quoted -NoNewWindow -PassThru `
|
|
-RedirectStandardOutput $outFile -RedirectStandardError $errFile
|
|
# Holding the handle keeps ExitCode readable after the process ended.
|
|
$null = $process.Handle
|
|
$process.WaitForExit()
|
|
return [pscustomobject]@{
|
|
ExitCode = $process.ExitCode
|
|
Output = [IO.File]::ReadAllText($outFile)
|
|
Error = [IO.File]::ReadAllText($errFile)
|
|
}
|
|
} finally {
|
|
Remove-Item -LiteralPath $outFile, $errFile -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
# Prints what herdr-app said (stdout, then stderr).
|
|
function Write-HerdrOutput($Result) {
|
|
foreach ($text in @($Result.Output, $Result.Error)) {
|
|
if ($text -and $text.Trim() -ne '') { Write-Host ($text.TrimEnd()) }
|
|
}
|
|
}
|
|
|
|
# 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 {
|
|
$result = Invoke-HerdrApp @('status')
|
|
if ($result.ExitCode -ne 0) { return $null }
|
|
return [string]($result.Output | ConvertFrom-Json).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)."
|
|
return
|
|
}
|
|
# `--name <x>`, not a JSON argument: quotes inside an argument do not survive.
|
|
$result = Invoke-HerdrApp @('do', 'layout.switch', '--name', $LayoutName)
|
|
Write-HerdrOutput $result
|
|
if ($result.ExitCode -ne 0) {
|
|
Write-Warning "layout.switch to '$LayoutName' failed (exit $($result.ExitCode)) - 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)))
|
|
}
|
|
|
|
# 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.
|
|
# 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) {
|
|
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]) {
|
|
Save-KeysCopy
|
|
$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
|
|
}
|
|
Save-KeysCopy
|
|
$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) {
|
|
Save-KeysCopy
|
|
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
|
|
|
|
# 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 {
|
|
$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) {
|
|
$saved = Move-ToBackup $OldMod
|
|
Write-Host "Moved the earlier version to $saved"
|
|
}
|
|
$OldLayout = Join-Path $LayoutsDir "$OldName.json"
|
|
if (Test-Path -LiteralPath $OldLayout) {
|
|
$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,
|
|
# 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
|
|
$robocopyExit = $LASTEXITCODE
|
|
# 0-7 are all "fine" for robocopy (1 = files were copied); nothing below may
|
|
# read this code as if it were the result of its own command.
|
|
$global:LASTEXITCODE = 0
|
|
if ($robocopyExit -ge 8) { throw "robocopy failed (exit $robocopyExit)" }
|
|
Write-Host "Mod copied: $ModDst"
|
|
|
|
# 4. The layout: an existing one that differs is saved first, an identical one stays.
|
|
New-Item -ItemType Directory -Force -Path $LayoutsDir | Out-Null
|
|
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; switch the layout only where the person has not chosen another one.
|
|
if (Test-HerdrApp) {
|
|
$check = Invoke-HerdrApp @('check', $ModDst)
|
|
Write-HerdrOutput $check
|
|
if ($check.ExitCode -ne 0) { Write-Warning "herdr-app check reported a problem (exit $($check.ExitCode))." }
|
|
} else {
|
|
Write-Warning 'herdr-app is not on the PATH - the mod was not checked.'
|
|
}
|
|
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')"
|
|
}
|