# Geschichten-Werkstatt installieren oder aktualisieren (Geschichtenkiste, ARCHITEKTUR.md Abschnitt 9). # # irm https://gschichtlichiste.pages.dev/werkstatt/installieren.ps1 | iex # # Laedt manifest.json und werkstatt-.zip, prueft die SHA-256, entpackt nach # %LOCALAPPDATA%\Geschichtenkiste\app--\ und uebergibt an installation\einrichten.ps1 aus dem zip # (Python, ffmpeg, venv, Einstellungen, Verknuepfung, Start). Nochmal ausfuehren repariert oder aktualisiert. # # Diese Datei bleibt reines ASCII: irm | iex kennt die Kodierung nicht, und Windows PowerShell 5.1 liest Skripte ohne # BOM als ANSI. Umlaute in Meldungen schreibt Kiste-Text aus ~a ~o ~u. Kein exit bei irm | iex (schloesse das Fenster). # # Umgebung (Tests und Fachleute): KISTE_QUELLE (URL oder Ordner mit manifest.json), KISTE_PROGRAMM (Programmordner), # KISTE_PI_ORDNER (Laufwerk des Pi, "-" = ohne Pi), KISTE_KEIN_START=1, KISTE_AKTUALISIEREN=1 (von aktualisieren.ps1). # # $ErrorActionPreference und $ProgressPreference setzt nur Kiste-Installieren: bei irm | iex blieben sie sonst im Fenster # gesetzt. einrichten.ps1 laeuft immer in Windows PowerShell 5.1 (wie im Windows-CI geprueft), auch aus PowerShell 7. function Kiste-Text([string]$s) { $s.Replace('~a', [string][char]0xE4).Replace('~o', [string][char]0xF6).Replace('~u', [string][char]0xFC). Replace('~A', [string][char]0xC4).Replace('~O', [string][char]0xD6).Replace('~U', [string][char]0xDC) } function Kiste-Quelle { $q = $env:KISTE_QUELLE if (-not $q) { $q = 'https://gschichtlichiste.pages.dev/werkstatt/' } if ($q -match '^https?://') { if (-not $q.EndsWith('/')) { $q += '/' } } $q } function Kiste-Holen([string]$quelle, [string]$name, [string]$ziel) { if ($quelle -match '^https?://') { $url = $quelle + $name if ($name -eq 'manifest.json') { $url += '?t=' + [DateTime]::UtcNow.Ticks } $versuch = 0 while ($true) { try { Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $ziel -TimeoutSec 600 return } catch { $versuch++ if ($versuch -ge 3) { throw ((Kiste-Text 'Download fehlgeschlagen: {0} ({1}). Internet pr~ufen und den Befehl nochmal ausf~uhren.') -f $url, $_.Exception.Message) } Start-Sleep -Seconds (2 * $versuch) } } } $pfad = Join-Path $quelle $name if (-not (Test-Path -LiteralPath $pfad)) { throw ('Nicht gefunden: {0}' -f $pfad) } Copy-Item -LiteralPath $pfad -Destination $ziel -Force } function Kiste-Wiederholen([scriptblock]$tun) { # Virenscanner halten frisch entpackte Dateien kurz fest for ($i = 1; ; $i++) { try { & $tun; return } catch { if ($i -ge 10) { throw }; Start-Sleep -Milliseconds 300 } } } function Kiste-PowerShell { # Windows PowerShell 5.1 (64 Bit auch aus einer 32-Bit-Sitzung); ohne Windows (Tests) die laufende PowerShell $ps = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe' if ([Environment]::Is64BitOperatingSystem -and -not [Environment]::Is64BitProcess) { $ps = Join-Path $env:SystemRoot 'Sysnative\WindowsPowerShell\v1.0\powershell.exe' } if (-not $env:SystemRoot -or -not (Test-Path -LiteralPath $ps)) { $ps = (Get-Process -Id $PID).Path } $ps } function Kiste-Sha256([string]$pfad) { # .NET statt Get-FileHash: Windows PowerShell 5.1 fand Get-FileHash nicht, wenn sie aus PowerShell 7 heraus startete # (geerbter PSModulePath, CI-Lauf 237) $s = [IO.File]::OpenRead($pfad) $h = [Security.Cryptography.SHA256]::Create() try { ([BitConverter]::ToString($h.ComputeHash($s)) -replace '-', '').ToLowerInvariant() } finally { $h.Dispose(); $s.Dispose() } } function Kiste-Installieren { $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' # sonst laedt Invoke-WebRequest in PowerShell 5.1 sehr langsam $quelle = Kiste-Quelle $programm = $env:KISTE_PROGRAMM if (-not $programm) { $programm = Join-Path $env:LOCALAPPDATA 'Geschichtenkiste' } $aktualisieren = ($env:KISTE_AKTUALISIEREN -eq '1') Write-Host '' if ($aktualisieren) { Write-Host 'Geschichten-Werkstatt aktualisieren' -ForegroundColor Cyan } else { Write-Host 'Geschichten-Werkstatt einrichten' -ForegroundColor Cyan } if ($PSVersionTable.PSVersion.Major -lt 5) { throw (Kiste-Text 'PowerShell ab 5.1 n~otig (Windows 10 oder 11).') } if (-not [Environment]::Is64BitOperatingSystem) { throw (Kiste-Text 'Die Werkstatt braucht ein 64-Bit-Windows.') } try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { } $tmp = Join-Path ([IO.Path]::GetTempPath()) ('geschichtenkiste-' + [guid]::NewGuid().ToString('N').Substring(0, 8)) New-Item -ItemType Directory -Path $tmp -Force | Out-Null try { $mpfad = Join-Path $tmp 'manifest.json' Kiste-Holen $quelle 'manifest.json' $mpfad $m = [IO.File]::ReadAllText($mpfad) | ConvertFrom-Json if ($m.format -ne 1 -or "$($m.version)" -notmatch '^\d+(\.\d+){1,3}$' -or "$($m.zip)" -notmatch '^werkstatt-[0-9.]+\.zip$' -or "$($m.sha256)" -notmatch '^[0-9a-f]{64}$') { throw ((Kiste-Text 'manifest.json von {0} ist ung~ultig.') -f $quelle) } $kennung = '{0}-{1}' -f $m.version, $m.sha256.Substring(0, 8) $app = Join-Path $programm ('app-' + $kennung) $einrichten = Join-Path $app 'installation\einrichten.ps1' if (Test-Path -LiteralPath $einrichten) { Write-Host ('Version {0} liegt schon bereit.' -f $m.version) } else { Write-Host ('Lade Version {0} ({1:N1} MB) ...' -f $m.version, ($m.groesse / 1MB)) $zip = Join-Path $tmp $m.zip Kiste-Holen $quelle $m.zip $zip $sha = Kiste-Sha256 $zip if ($sha -ne $m.sha256) { throw ((Kiste-Text 'Download besch~adigt (SHA-256 {0} statt {1}). Befehl nochmal ausf~uhren.') -f $sha.Substring(0, 12), $m.sha256.Substring(0, 12)) } New-Item -ItemType Directory -Path $programm -Force | Out-Null $neu = $app + '.neu' if (Test-Path -LiteralPath $neu) { Kiste-Wiederholen { Remove-Item -LiteralPath $neu -Recurse -Force } } Add-Type -AssemblyName System.IO.Compression.FileSystem [IO.Compression.ZipFile]::ExtractToDirectory($zip, $neu) # ein app-Ordner ohne einrichten.ps1 ist ein Rest (halb geloescht): weg damit, sonst scheitert das Umbenennen if (Test-Path -LiteralPath $app) { Kiste-Wiederholen { Remove-Item -LiteralPath $app -Recurse -Force } } Kiste-Wiederholen { Rename-Item -LiteralPath $neu -NewName (Split-Path -Leaf $app) } Write-Host ((Kiste-Text 'Gepr~uft und entpackt: {0}') -f $app) } # einrichten.ps1 hat Umlaute (UTF-8 mit BOM) und laeuft deshalb als Datei in einer eigenen PowerShell mit # derselben Konsole. Start-Process -Wait wartete auch auf die Werkstatt, die einrichten.ps1 startet. $argumente = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $einrichten, '-Programm', $programm, '-Kennung', $kennung, '-Quelle', $quelle) if ($aktualisieren) { $argumente += '-Aktualisieren' } $zeile = ($argumente | ForEach-Object { Kiste-Zitat $_ }) -join ' ' $ps = Kiste-PowerShell # Aus PowerShell 7 erbte 5.1 deren PSModulePath und faende eigene Befehle nicht (CI-Lauf 237): ohne die # Variable baut 5.1 ihre eigene. Nur fuer den Unterprozess; die Sitzung behaelt ihre. $modulpfad = $env:PSModulePath if ($PSVersionTable.PSEdition -eq 'Core') { Remove-Item -Path Env:\PSModulePath -ErrorAction SilentlyContinue } try { $p = Start-Process -FilePath $ps -ArgumentList $zeile -NoNewWindow -PassThru } finally { if ($null -ne $modulpfad) { $env:PSModulePath = $modulpfad } } $null = $p.Handle # ohne das liefert PowerShell 5.1 manchmal keinen ExitCode $p.WaitForExit() $script:KisteCode = $p.ExitCode } finally { Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue } } function Kiste-Zitat([string]$a) { # Windows-Regeln fuer eine Befehlszeile (CommandLineToArgvW) if ($a -ne '' -and $a -notmatch '[\s"]') { return $a } $s = [regex]::Replace($a, '(\\*)"', { param($t) ($t.Groups[1].Value * 2) + '\"' }) $s = [regex]::Replace($s, '(\\+)$', { param($t) $t.Groups[1].Value * 2 }) '"' + $s + '"' } $script:KisteCode = 1 try { Kiste-Installieren } catch { Write-Host '' Write-Host ('Fehler: ' + $_.Exception.Message) -ForegroundColor Red $script:KisteCode = 1 } # Als Datei (-File, aktualisieren.ps1): Exitcode. Ueber irm | iex nie exit, sonst schloesse sich das Fenster oder das # aufrufende Skript; {}.File ist nur gesetzt, wenn dieser Text aus einer Datei stammt. if ({}.File) { exit $script:KisteCode }