| 1 | param([Parameter(Mandatory=$true)][string]$Root, |
| 2 | [Parameter(Mandatory=$true)][string]$SharedPath, |
| 3 | [Parameter(Mandatory=$true)][string]$CloneHost, |
| 4 | [switch]$ResumeCache, |
| 5 | [int]$StartSequence = 1) |
| 6 | function Keep-Container($page, $target) { |
| 7 | $container = $target |
| 8 | while ($container.ParentNode -ne $page.DocumentElement) { $container = $container.ParentNode } |
| 9 | foreach ($sibling in @($page.DocumentElement.ChildNodes)) { |
| 10 | if ($sibling -ne $container -and $sibling.LocalName -in @('Outline', 'Title', 'Image', 'InkDrawing')) { |
| 11 | [void]$page.DocumentElement.RemoveChild($sibling) |
| 12 | } |
| 13 | } |
| 14 | } |
| 15 | Set-StrictMode -Version Latest |
| 16 | $ErrorActionPreference = 'Stop' |
| 17 | . "$PSScriptRoot\text.ps1" |
| 18 | if ($SharedPath -notmatch '^\\\\192\.168\.77\.1\\agent\\m6-[a-z0-9-]+$') { throw 'Choose a disposable Linux lab notebook.' } |
| 19 | if ($env:COMPUTERNAME -ne $CloneHost) { throw 'The collaboration controller requires its owned clone.' } |
| 20 | if (-not $ResumeCache) { & "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost } |
| 21 | $shared = $SharedPath |
| 22 | if (-not (Test-Path "$shared\synthetic.one")) { throw 'Create the shared fixture first.' } |
| 23 | New-Item -ItemType Directory "$Root\inbox", "$Root\outbox" -Force | Out-Null |
| 24 | $user = 'HKCU:\Software\Microsoft\Office\Common\UserInfo' |
| 25 | New-Item $user -Force | Out-Null |
| 26 | New-ItemProperty $user -Name UserName -Value $CloneHost -PropertyType String -Force | Out-Null |
| 27 | New-ItemProperty $user -Name UserInitials -Value $CloneHost.Substring($CloneHost.Length - 3) -PropertyType String -Force | Out-Null |
| 28 | $app = New-Object -ComObject OneNote.Application |
| 29 | $notebook = '' |
| 30 | try { |
| 31 | $app.OpenHierarchy($shared, '', [ref]$notebook, 0) |
| 32 | $section = '' |
| 33 | $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0) |
| 34 | [IO.File]::WriteAllText("$Root\ready", 'ready') |
| 35 | $deadline = [DateTime]::UtcNow.AddMinutes(30) |
| 36 | $sequence = $StartSequence |
| 37 | while ([DateTime]::UtcNow -lt $deadline) { |
| 38 | $name = '{0:d4}.json' -f $sequence |
| 39 | $input = "$Root\inbox\$name" |
| 40 | if (-not (Test-Path $input)) { Start-Sleep -Milliseconds 100; continue } |
| 41 | $command = Get-Content -Raw -Encoding UTF8 $input | ConvertFrom-Json |
| 42 | $output = "$Root\outbox\$sequence" |
| 43 | New-Item -ItemType Directory $output | Out-Null |
| 44 | try { |
| 45 | if ($command.action -in @('prepare-stress', 'stress')) { |
| 46 | . "$PSScriptRoot\stress.ps1" |
| 47 | Invoke-Stress $app $section $command $output $shared |
| 48 | } |
| 49 | elseif ($command.action -eq 'sync') { $app.SyncHierarchy($notebook) } |
| 50 | elseif ($command.action -in @('pages', 'move', 'delete-page')) { |
| 51 | $namespace = 'http://schemas.microsoft.com/office/onenote/2010/onenote' |
| 52 | $hierarchy = '' |
| 53 | $app.GetHierarchy($section, 4, [ref]$hierarchy, 1) |
| 54 | [xml]$tree = $hierarchy |
| 55 | $listed = @($tree.SelectNodes('//*[local-name()="Page"]')) |
| 56 | $named = { param($title) @($listed | Where-Object { $_.GetAttribute('name') -eq $title }) } |
| 57 | if ($command.action -eq 'pages') { |
| 58 | # New titled pages with one body paragraph each, appended in order. |
| 59 | foreach ($title in $command.titles) { |
| 60 | $page = '' |
| 61 | $app.CreateNewPage($section, [ref]$page, 2) |
| 62 | $body = [Security.SecurityElement]::Escape('Body ' + $title + '.') |
| 63 | $name = [Security.SecurityElement]::Escape($title) |
| 64 | $xml = '<one:Page xmlns:one="' + $namespace + '" ID="' + $page + '"><one:Title><one:OE><one:T>' + $name + '</one:T></one:OE></one:Title><one:Outline><one:OEChildren><one:OE><one:T>' + $body + '</one:T></one:OE></one:OEChildren></one:Outline></one:Page>' |
| 65 | $app.UpdatePageContent($xml, [DateTime]::MinValue, 1, $false) |
| 66 | } |
| 67 | } elseif ($command.action -eq 'move') { |
| 68 | # Moves the page titled `page` before the one titled `before`, or last. |
| 69 | $moved = @(& $named $command.page) |
| 70 | if ($moved.Count -ne 1) { throw 'Expected one page to move.' } |
| 71 | $order = [Collections.ArrayList]@($listed | Where-Object { $_ -ne $moved[0] }) |
| 72 | $at = $order.Count |
| 73 | if ($command.before) { |
| 74 | $anchor = @(& $named $command.before) |
| 75 | if ($anchor.Count -ne 1) { throw 'Expected one anchor page.' } |
| 76 | $at = $order.IndexOf($anchor[0]) |
| 77 | } |
| 78 | $order.Insert($at, $moved[0]) |
| 79 | $xml = '<one:Section xmlns:one="' + $namespace + '" ID="' + $section + '">' |
| 80 | foreach ($node in $order) { |
| 81 | $xml += '<one:Page ID="' + $node.GetAttribute('ID') + '" pageLevel="' + $node.GetAttribute('pageLevel') + '"/>' |
| 82 | } |
| 83 | $app.UpdateHierarchy($xml + '</one:Section>', 1) |
| 84 | } else { |
| 85 | $doomed = @(& $named $command.page) |
| 86 | if ($doomed.Count -ne 1) { throw 'Expected one page to delete.' } |
| 87 | $app.DeleteHierarchy($doomed[0].GetAttribute('ID'), [DateTime]::MinValue, $false) |
| 88 | } |
| 89 | } |
| 90 | elseif ($command.action -eq 'kill-process') { |
| 91 | Get-Process ONENOTE | Stop-Process -Force |
| 92 | [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app) |
| 93 | $app = New-Object -ComObject OneNote.Application |
| 94 | $app.OpenHierarchy($shared, '', [ref]$notebook, 0) |
| 95 | $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0) |
| 96 | } |
| 97 | elseif ($command.action -eq 'edit' -or $command.action -eq 'snapshot') { |
| 98 | $hierarchy = '' |
| 99 | $app.GetHierarchy($section, 4, [ref]$hierarchy, 1) |
| 100 | [IO.File]::WriteAllText("$output\hierarchy.xml", $hierarchy, [Text.Encoding]::UTF8) |
| 101 | [xml]$tree = $hierarchy |
| 102 | $matches = 0 |
| 103 | $editedPage = $null |
| 104 | $i = 0 |
| 105 | foreach ($node in $tree.SelectNodes('//*[local-name()="Page"]')) { |
| 106 | $content = '' |
| 107 | $app.GetPageContent($node.GetAttribute('ID'), [ref]$content, 1, 1) |
| 108 | [IO.File]::WriteAllText("$output\page-$i.xml", $content, [Text.Encoding]::UTF8) |
| 109 | [xml]$page = $content |
| 110 | if ($command.action -eq 'edit') { |
| 111 | $targets = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object { |
| 112 | (Get-ParagraphText $_.InnerText) -eq $command.expected |
| 113 | }) |
| 114 | if ($targets.Count -gt 1) { throw 'Expected one matching text object.' } |
| 115 | if ($targets.Count -eq 1) { |
| 116 | $matches++ |
| 117 | if ($matches -gt 1) { throw 'The text occurs on multiple pages.' } |
| 118 | $targets[0].InnerText = [Security.SecurityElement]::Escape($command.text) |
| 119 | Keep-Container $page $targets[0] |
| 120 | $editedPage = $page |
| 121 | } |
| 122 | } |
| 123 | $i++ |
| 124 | } |
| 125 | if ($command.action -eq 'edit' -and $matches -ne 1) { throw 'Expected one matching text object.' } |
| 126 | if ($editedPage) { $app.UpdatePageContent($editedPage.OuterXml, [DateTime]::MinValue, 1, $false) } |
| 127 | } elseif ($command.action -ne 'close') { throw 'Unknown collaboration command.' } |
| 128 | [IO.File]::WriteAllText("$output\done", 'ok') |
| 129 | } catch { |
| 130 | [IO.File]::WriteAllText("$output\error", $_.Exception.ToString()) |
| 131 | } |
| 132 | if ($command.action -eq 'close') { break } |
| 133 | $sequence++ |
| 134 | } |
| 135 | } finally { |
| 136 | try { if ($notebook) { $app.CloseNotebook($notebook, $false) } } |
| 137 | finally { |
| 138 | [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app) |
| 139 | $app = $null |
| 140 | [GC]::Collect() |
| 141 | [GC]::WaitForPendingFinalizers() |
| 142 | Get-Process ONENOTE -ErrorAction SilentlyContinue | Wait-Process -Timeout 10 |
| 143 | [IO.File]::WriteAllText("$Root\closed", 'closed') |
| 144 | } |
| 145 | } |