| 1 | param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost) |
| 2 | Set-StrictMode -Version Latest |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | & "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost |
| 5 | $fixture = Get-Content (Join-Path $Root 'notebook\fixture.json') -Raw -Encoding UTF8 | ConvertFrom-Json |
| 6 | $inputs = Join-Path $Root 'inputs' |
| 7 | New-Item -ItemType Directory -Path $inputs | Out-Null |
| 8 | $app = New-Object -ComObject OneNote.Application |
| 9 | $notebookId = '' |
| 10 | try { |
| 11 | $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0) |
| 12 | $sections = @{} |
| 13 | foreach ($path in $fixture.sections) { |
| 14 | $parts = $path.Split('/') |
| 15 | if (@($parts | Where-Object { $_ -in @('', '.', '..') -or $_.Contains('\') -or $_.Contains(':') }).Count) { throw 'Invalid fixture section path.' } |
| 16 | $parent = $notebookId |
| 17 | for ($index = 0; $index -lt $parts.Count; $index++) { |
| 18 | $id = '' |
| 19 | $app.OpenHierarchy($parts[$index], $parent, [ref]$id, $(if ($index -eq $parts.Count - 1) {3} else {2})) |
| 20 | $parent = $id |
| 21 | } |
| 22 | $sections[$path] = $id |
| 23 | } |
| 24 | $index = 0 |
| 25 | foreach ($item in $fixture.pages) { |
| 26 | $pageId = '' |
| 27 | $app.CreateNewPage($sections[$item.section], [ref]$pageId, 0) |
| 28 | [xml]$xml = $item.xml.Replace('__ASSETS__', [Security.SecurityElement]::Escape((Join-Path $Root 'notebook\fixture-data'))) |
| 29 | $xml.DocumentElement.SetAttribute('ID', $pageId) |
| 30 | [IO.File]::WriteAllText((Join-Path $inputs ('page-{0:d3}.xml' -f $index)), $xml.OuterXml, [Text.Encoding]::UTF8) |
| 31 | $app.UpdatePageContent($xml.OuterXml, [DateTime]::MinValue, 1, $true) |
| 32 | $index++ |
| 33 | } |
| 34 | $app.SyncHierarchy($notebookId) |
| 35 | $app.CloseNotebook($notebookId, $false) |
| 36 | $notebookId = '' |
| 37 | } finally { |
| 38 | if ($notebookId) { $app.CloseNotebook($notebookId, $false) } |
| 39 | [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app) |
| 40 | [GC]::Collect() |
| 41 | [GC]::WaitForPendingFinalizers() |
| 42 | } |