Run compiled ribbon checks without changing Windows PowerShell policy

This commit is contained in:
nick 2026-11-23 19:29:17 +00:00
parent 2b8e7cf7cf
commit cfcd40b5b9

View File

@ -1,39 +1,49 @@
[CmdletBinding()]
param([Parameter(Mandatory)][string]$PackagePath)
$ErrorActionPreference='Stop'
# Run under Windows PowerShell/.NET Framework. Loads signed build bytes in this
# short-lived process; never starts Office, registers VSTO or changes installation.
if($env:OS -ne 'Windows_NT' -or $PSVersionTable.PSEdition -ne 'Desktop'){throw 'Use Windows PowerShell for the .NET Framework VSTO ribbon check.'}
# Existing .NET Framework compiler; no Office, registration, policy or installation changes.
if($env:OS -ne 'Windows_NT'){throw 'Run the compiled VSTO ribbon check on Windows.'}
. (Join-Path $PSScriptRoot 'DeveloperPackage.ps1')
$package=Test-DeveloperPackage $PackagePath
$script:passed=0
function Check([bool]$condition,[string]$message){if(!$condition){throw $message};$script:passed++;Write-Output "PASS $message"}
$assembly=[Reflection.Assembly]::LoadFrom((Join-Path $PackagePath 'PlotDirector.WordAddIn.dll'))
Check ($assembly.GetName().Version.ToString() -eq $package.version) 'Loaded ribbon assembly matches the qualified package version'
$adapterType=$assembly.GetType('PlotDirector.WordAddIn.WordAdapter',$true)
$ribbonType=$assembly.GetType('PlotDirector.WordAddIn.NativeRibbon',$true)
$factoryType=[Func``1].MakeGenericType($adapterType)
$factoryMethod=[Reflection.Emit.DynamicMethod]::new('NoWordAdapter',$adapterType,[Type[]]@())
$il=$factoryMethod.GetILGenerator();$il.Emit([Reflection.Emit.OpCodes]::Ldnull);$il.Emit([Reflection.Emit.OpCodes]::Ret)
$factory=$factoryMethod.CreateDelegate($factoryType)
$ribbon=[Activator]::CreateInstance($ribbonType,[object[]]@($factory))
function Callback([string]$name){$ribbonType.GetMethod($name,[Reflection.BindingFlags]'Public,Instance').Invoke($ribbon,[object[]]@($null))}
$xml=[xml](Callback 'GetCustomUI')
$ns=New-Object Xml.XmlNamespaceManager($xml.NameTable);$ns.AddNamespace('r','http://schemas.microsoft.com/office/2009/07/customui')
$hold=$xml.SelectSingleNode('//r:toggleButton[@id="PlotDirectorHold"]',$ns)
Check ($hold -and $hold.size -eq 'large' -and $hold.ParentNode.id -eq 'PlotDirectorSharedFocus') 'Large focus toggle remains in the Manuscript focus group'
Check ($hold.getLabel -eq 'GetFocusAction' -and $hold.getImage -eq 'GetFocusIcon' -and $hold.getPressed -eq 'GetFocusHeld' -and $hold.getEnabled -eq 'CanHoldFocus' -and $hold.onAction -eq 'ToggleFocusHold') 'Dynamic label, image, enabled, pressed and existing action bindings are present'
Check (!$hold.HasAttribute('imageMso')) 'Static Lock icon cannot override the dynamic Pause/Play image'
Check (!$xml.SelectSingleNode('//*[@id="PlotDirectorFocusState"]') -and !$xml.SelectSingleNode('//*[@id="PlotDirectorIntent"]')) 'Redundant State and retired Intent controls are absent'
foreach($control in @('PlotDirectorBook','PlotDirectorChapter','PlotDirectorScene','PlotDirectorContext','PlotDirectorWeb','PlotDirectorOpen','PlotDirectorAudioReview','PlotDirectorSynchronise','PlotDirectorTools')){
Check ($null -ne $xml.SelectSingleNode("//*[@id='$control']")) "Preserved ribbon control $control"
$work=Join-Path ([IO.Path]::GetTempPath()) ('pd-ribbon-check-'+[guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $work | Out-Null
$source=Join-Path $work 'RibbonChecks.cs';$runner=Join-Path $work 'RibbonChecks.exe'
try {
@'
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Xml;
class RibbonChecks {
static int count;static object ribbon;static Type type;
static void Check(bool ok,string name){if(!ok)throw new Exception(name);Console.WriteLine("PASS "+name);count++;}
static object Call(string name){return type.GetMethod(name,BindingFlags.Public|BindingFlags.Instance).Invoke(ribbon,new object[]{null});}
static int Main(string[] args){try{
var assembly=Assembly.LoadFrom(System.IO.Path.Combine(args[0],"PlotDirector.WordAddIn.dll"));
Check(assembly.GetName().Version.ToString()==args[1],"Loaded ribbon assembly matches the qualified package version");
var adapter=assembly.GetType("PlotDirector.WordAddIn.WordAdapter",true);type=assembly.GetType("PlotDirector.WordAddIn.NativeRibbon",true);
var factoryType=typeof(Func<>).MakeGenericType(adapter);var method=new DynamicMethod("NoWordAdapter",adapter,Type.EmptyTypes);var il=method.GetILGenerator();il.Emit(OpCodes.Ldnull);il.Emit(OpCodes.Ret);
ribbon=Activator.CreateInstance(type,new object[]{method.CreateDelegate(factoryType)});
var xml=new XmlDocument();xml.LoadXml((string)Call("GetCustomUI"));var ns=new XmlNamespaceManager(xml.NameTable);ns.AddNamespace("r","http://schemas.microsoft.com/office/2009/07/customui");
var hold=(XmlElement)xml.SelectSingleNode("//r:toggleButton[@id='PlotDirectorHold']",ns);
Check(hold!=null&&hold.GetAttribute("size")=="large"&&((XmlElement)hold.ParentNode).GetAttribute("id")=="PlotDirectorSharedFocus","Large focus toggle remains in the Manuscript focus group");
Check(hold.GetAttribute("getLabel")=="GetFocusAction"&&hold.GetAttribute("getImage")=="GetFocusIcon"&&hold.GetAttribute("getPressed")=="GetFocusHeld"&&hold.GetAttribute("getEnabled")=="CanHoldFocus"&&hold.GetAttribute("onAction")=="ToggleFocusHold","Dynamic label, image, enabled, pressed and existing action bindings");
Check(!hold.HasAttribute("imageMso"),"Static Lock icon cannot override dynamic Pause/Play");
Check(xml.SelectSingleNode("//*[@id='PlotDirectorFocusState']")==null&&xml.SelectSingleNode("//*[@id='PlotDirectorIntent']")==null,"Redundant State and retired Intent controls absent");
foreach(var id in new[]{"PlotDirectorBook","PlotDirectorChapter","PlotDirectorScene","PlotDirectorContext","PlotDirectorWeb","PlotDirectorOpen","PlotDirectorAudioReview","PlotDirectorAudioReviewOpen","PlotDirectorAudioBack","PlotDirectorAudioPlay","PlotDirectorAudioPause","PlotDirectorAudioForward","PlotDirectorSynchronise","PlotDirectorTools"})Check(xml.SelectSingleNode("//*[@id='"+id+"']")!=null,"Preserved ribbon control "+id);
Check((string)Call("GetFocusAction")=="Hold"&&!(bool)Call("CanHoldFocus")&&!(bool)Call("GetFocusHeld"),"Unavailable callbacks: Hold, disabled and unpressed");
Check(((string)Call("GetFocusTip")).Contains("Connect Companion"),"Unavailable tooltip explains recovery");
var pause=Call("GetFocusIcon");Check(pause!=null&&Object.ReferenceEquals(pause,Call("GetFocusIcon")),"Unavailable Pause icon renders and is cached");
var image=type.GetMethod("GetPlaybackIcon",BindingFlags.NonPublic|BindingFlags.Instance,null,new[]{typeof(string)},null);
var play=image.Invoke(ribbon,new object[]{"PlotDirectorAudioPlay"});var cachedPause=image.Invoke(ribbon,new object[]{"PlotDirectorAudioPause"});
Check(play!=null&&!Object.ReferenceEquals(play,pause)&&Object.ReferenceEquals(cachedPause,pause),"Play and Pause reuse distinct Audio Review image cache");
Console.WriteLine("PASS Windows compiled ribbon qualification: "+count+" assertions; Word and installer not executed.");return 0;
}catch(Exception e){Console.Error.WriteLine(e);return 1;}}
}
'@ | Set-Content -LiteralPath $source -Encoding UTF8
Invoke-Checked (Join-Path $env:WINDIR 'Microsoft.NET\Framework64\v4.0.30319\csc.exe') @('/nologo','/target:exe',('/out:'+$runner),$source)
Invoke-Checked $runner @((Resolve-Path -LiteralPath $PackagePath).Path,$package.version)
} finally {
Remove-Item -LiteralPath $source,$runner -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $work -ErrorAction SilentlyContinue
}
Check ((Callback 'GetFocusAction') -eq 'Hold' -and !(Callback 'CanHoldFocus') -and !(Callback 'GetFocusHeld')) 'Unavailable callback state is Hold, disabled and unpressed'
Check ((Callback 'GetFocusTip') -match 'Connect Companion') 'Unavailable tooltip explains how to recover'
$pause=Callback 'GetFocusIcon';$again=Callback 'GetFocusIcon'
Check ($null -ne $pause -and [object]::ReferenceEquals($pause,$again)) 'Unavailable Pause icon is rendered and cached without ribbon churn'
$imageMethod=$ribbonType.GetMethod('GetPlaybackIcon',[Reflection.BindingFlags]'NonPublic,Instance',$null,[Type[]]@([string]),$null)
$play=$imageMethod.Invoke($ribbon,[object[]]@('PlotDirectorAudioPlay'))
$cachedPause=$imageMethod.Invoke($ribbon,[object[]]@('PlotDirectorAudioPause'))
Check ($null -ne $play -and ![object]::ReferenceEquals($play,$pause) -and [object]::ReferenceEquals($cachedPause,$pause)) 'Play and Pause reuse the established distinct Audio Review image cache'
Write-Output "PASS Windows compiled ribbon qualification: $script:passed assertions; Word and the installer were not executed."