param( [string]$Root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path ) $ErrorActionPreference = 'Stop' $catalogPath = Join-Path $Root 'src\YMhut.Box.Core\Api\PearApiCatalog.cs' $outputPath = Join-Path $Root 'src\box-winUI\Views\Tools\GeneratedPearApiToolPages.cs' $pattern = 'D\("(?[^"]+)",\s*\d+,' $ids = [Collections.Generic.List[string]]::new() foreach ($line in (Get-Content -LiteralPath $catalogPath -Encoding UTF8)) { $match = [regex]::Match($line, $pattern) if ($match.Success) { $ids.Add($match.Groups['id'].Value) } } if ($ids.Count -ne 74) { throw "Expected 74 PearAPI definitions, found $($ids.Count)." } if (($ids | Select-Object -Unique).Count -ne $ids.Count) { throw 'PearAPI tool IDs must be unique.' } $legacyAliases = @('baidu_hot', 'bili_hot', 'zhihu_hot') function Get-ClassStem([string]$id) { $parts = $id -split '[^A-Za-z0-9]+' return (($parts | Where-Object { $_ } | ForEach-Object { if ($_.Length -eq 1) { $_.ToUpperInvariant() } else { $_.Substring(0, 1).ToUpperInvariant() + $_.Substring(1) } }) -join '') } $lines = [Collections.Generic.List[string]]::new() $lines.Add('// ') $lines.Add('#nullable enable') $lines.Add('using YMhut.Box.Core.Api;') $lines.Add('using YMhut.Box.Core.Tools;') $lines.Add('using YMhut.Box.WinUI.ViewModels.Tools;') $lines.Add('') $lines.Add('namespace YMhut.Box.WinUI.Views.Tools;') $lines.Add('') $lines.Add('public sealed partial class ToolPageRegistry') $lines.Add('{') $lines.Add(' private static partial void RegisterPearApi(ToolPageRegistry registry)') $lines.Add(' {') foreach ($id in $ids) { $stem = Get-ClassStem $id $lines.Add(" registry.Register<${stem}PearApiToolPage, ${stem}PearApiToolViewModel>(`"$id`", (module, goBack) => new ${stem}PearApiToolPage(module, goBack));") } foreach ($id in $legacyAliases) { $stem = (($id -split '_') | ForEach-Object { if ($_.Length -gt 0) { $_.Substring(0, 1).ToUpperInvariant() + $_.Substring(1) } }) -join '' $lines.Add(" registry.Register<${stem}PearApiToolPage, ${stem}PearApiToolViewModel>(`"$id`", (module, goBack) => new ${stem}PearApiToolPage(module, goBack));") } $lines.Add(' }') $lines.Add('}') $lines.Add('') foreach ($id in $ids) { $stem = Get-ClassStem $id $lines.Add("public sealed class ${stem}PearApiToolViewModel(IToolModule module) : PearApiToolViewModel(module, PearApiCatalog.GetRequired(`"$id`"));") $lines.Add("public sealed class ${stem}PearApiToolPage(IToolModule module, Action? goBack = null) : PearApiToolPageBase(module, new ${stem}PearApiToolViewModel(module), goBack);") $lines.Add('') } foreach ($id in $legacyAliases) { $stem = (($id -split '_') | ForEach-Object { if ($_.Length -gt 0) { $_.Substring(0, 1).ToUpperInvariant() + $_.Substring(1) } }) -join '' $lines.Add("public sealed class ${stem}PearApiToolViewModel(IToolModule module) : PearApiToolViewModel(module, PearApiCatalog.GetRequired(`"$id`"));") $lines.Add("public sealed class ${stem}PearApiToolPage(IToolModule module, Action? goBack = null) : PearApiToolPageBase(module, new ${stem}PearApiToolViewModel(module), goBack);") $lines.Add('') } Set-Content -LiteralPath $outputPath -Value $lines -Encoding UTF8 Write-Host "Generated $($ids.Count) PearAPI pages and $($legacyAliases.Count) legacy aliases in $outputPath"