54 lines
4.8 KiB
PowerShell
54 lines
4.8 KiB
PowerShell
[CmdletBinding(SupportsShouldProcess=$true)]
|
|
param([string]$PackagePath,[switch]$Rollback,[switch]$StopAgent)
|
|
$ErrorActionPreference='Stop'
|
|
if($env:OS -ne 'Windows_NT'){throw 'This per-user developer registration command requires Windows.'}
|
|
. (Join-Path $PSScriptRoot 'DeveloperPackage.ps1')
|
|
if(Get-Process WINWORD -ErrorAction SilentlyContinue){throw 'Close Microsoft Word normally, including any save prompts, then rerun. Word was not terminated.'}
|
|
$root=Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'PlotDirector\WordCompanionDeveloper'
|
|
$registration=Get-WordCompanionRegistration
|
|
$keyName=if($registration){$registration.Name}else{'PlotDirector.WordAddIn'}
|
|
$registry=Join-Path 'HKCU:\Software\Microsoft\Office\Word\Addins' $keyName
|
|
$backup=Join-Path $root 'previous-registration.json'
|
|
function Stop-DeveloperAgent {
|
|
foreach($agent in @(Get-Process 'PlotDirector.Companion.Agent' -ErrorAction SilentlyContinue)){
|
|
if(!$StopAgent){throw 'The companion agent is running. Rerun with -StopAgent to explicitly stop this user-session agent. Word must already be closed.'}
|
|
if($agent.SessionId -ne (Get-Process -Id $PID).SessionId){throw 'Agent belongs to a different session; it was not stopped.'}
|
|
if(!$agent.Path -or !$agent.Path.StartsWith([Environment]::GetFolderPath('UserProfile'),[StringComparison]::OrdinalIgnoreCase)){throw 'Agent path is outside this user profile; stop it explicitly after inspection.'}
|
|
if($PSCmdlet.ShouldProcess("Agent PID $($agent.Id)",'Stop for explicit developer installation')){Stop-Process -Id $agent.Id -ErrorAction Stop;$agent.WaitForExit(5000)|Out-Null}
|
|
}
|
|
}
|
|
if($Rollback){
|
|
if(!(Test-Path -LiteralPath $backup)){throw 'No previous per-user registration backup exists.'}
|
|
$saved=Get-Content -Raw $backup|ConvertFrom-Json
|
|
if($saved.PSObject.Properties.Name -contains 'registryKey'){if($saved.registryKey -notmatch '^[a-zA-Z0-9._ -]+$'){throw 'Invalid saved registration key.'};$registry=Join-Path 'HKCU:\Software\Microsoft\Office\Word\Addins' $saved.registryKey}
|
|
if($PSCmdlet.ShouldProcess($registry,'Restore previous per-user Word registration')){
|
|
Stop-DeveloperAgent
|
|
if($saved.existed){New-Item -Path $registry -Force|Out-Null;foreach($property in $saved.values.PSObject.Properties){$type=if($property.Name -eq "LoadBehavior"){"DWord"}else{"String"};New-ItemProperty -Path $registry -Name $property.Name -Value $property.Value -PropertyType $type -Force|Out-Null}}
|
|
elseif(Test-Path $registry){Remove-Item -LiteralPath $registry -Recurse}
|
|
Write-Output 'Previous registration restored. Signed packages were retained. Open Word to verify.'
|
|
}
|
|
return
|
|
}
|
|
if(!$PackagePath){throw 'Provide -PackagePath from Update-WordCompanion.ps1, or use -Rollback.'}
|
|
$package=Test-DeveloperPackage $PackagePath
|
|
$destination=Join-Path $root "versions\$($package.version)-$($package.commit.Substring(0,8))"
|
|
if($PSCmdlet.ShouldProcess($destination,'Stage signed package and register VSTO for this user only')){
|
|
Stop-DeveloperAgent
|
|
New-Item -ItemType Directory -Path $root -Force|Out-Null
|
|
if(!(Test-Path -LiteralPath $destination)){New-Item -ItemType Directory -Path $destination -Force|Out-Null;Copy-Item -Path (Join-Path $PackagePath '*') -Destination $destination -Recurse}
|
|
$null=Test-DeveloperPackage $destination
|
|
$manifest=([uri](Join-Path $destination 'PlotDirector.WordAddIn.vsto')).AbsoluteUri+'|vstolocal'
|
|
$current=Get-ItemProperty -Path $registry -ErrorAction SilentlyContinue
|
|
if($current -and $current.Manifest -eq $manifest){Write-Output "Version $($package.version) is already registered. No backup replaced.";return}
|
|
$values=@{};if($current){foreach($name in @('Manifest','LoadBehavior','FriendlyName','Description')){if($current.PSObject.Properties.Name -contains $name){$values[$name]=$current.$name}}}
|
|
@{registryKey=$keyName;existed=[bool]$current;values=$values} | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $backup -Encoding UTF8
|
|
try{
|
|
New-Item -Path $registry -Force|Out-Null
|
|
New-ItemProperty -Path $registry -Name Manifest -PropertyType String -Value $manifest -Force|Out-Null
|
|
New-ItemProperty -Path $registry -Name FriendlyName -PropertyType String -Value 'PlotDirector Word Companion' -Force|Out-Null
|
|
New-ItemProperty -Path $registry -Name Description -PropertyType String -Value 'PlotDirector by Nick Beckley' -Force|Out-Null
|
|
New-ItemProperty -Path $registry -Name LoadBehavior -PropertyType DWord -Value 3 -Force|Out-Null
|
|
}catch{throw "Registration failed; backup retained at $backup. Run this script with -Rollback. $($_.Exception.Message)"}
|
|
Write-Output "Registered version $($package.version), SHA $($package.commit), for this user. Previous installation files were preserved. Office still validates manifest signatures/trust; no trust store was changed. Open Word and qualify this build."
|
|
}
|