generate-test-report.ps1 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # generate-test-report.ps1
  2. # 使用模拟数据测试 html-report-generator skill 的输出效果
  3. # Usage: .\workflows\generate-test-report.ps1 [-MockDataPath <path>]
  4. param(
  5. [string]$MockDataPath = (Join-Path $PSScriptRoot "output\mock-report-data.json")
  6. )
  7. $templatePath = Join-Path $PSScriptRoot "templates\report_template.html"
  8. $outputPath = Join-Path $PSScriptRoot "output\test-report-home-fragrance.html"
  9. # ============================================================
  10. # 验证文件存在
  11. # ============================================================
  12. if (-not (Test-Path $templatePath)) {
  13. Write-Error "Template not found: $templatePath"
  14. exit 1
  15. }
  16. if (-not (Test-Path $MockDataPath)) {
  17. Write-Error "Mock data not found: $MockDataPath"
  18. exit 1
  19. }
  20. $outputDir = Split-Path $outputPath
  21. if (-not (Test-Path $outputDir)) {
  22. New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
  23. }
  24. # ============================================================
  25. # STEP 1: 读取 Mock 数据 JSON(UTF-8)
  26. # ============================================================
  27. $mockJson = [System.IO.File]::ReadAllText($MockDataPath, [System.Text.Encoding]::UTF8)
  28. $mockObj = $mockJson | ConvertFrom-Json
  29. $templateVars = $mockObj.templateVars
  30. $reportDataObj = $mockObj.reportData
  31. $reportDataStr = ($reportDataObj | ConvertTo-Json -Depth 20 -Compress:$false)
  32. # ============================================================
  33. # STEP 2: 读取 HTML 模板
  34. # ============================================================
  35. $html = [System.IO.File]::ReadAllText($templatePath, [System.Text.Encoding]::UTF8)
  36. # ============================================================
  37. # STEP 3: 替换 {{placeholder}} 变量
  38. # ============================================================
  39. $templateVars.PSObject.Properties | ForEach-Object {
  40. $html = $html.Replace("{{$($_.Name)}}", $_.Value)
  41. }
  42. # ============================================================
  43. # STEP 4: 注入 REPORT_DATA JSON
  44. # ============================================================
  45. $html = $html.Replace('/*{{REPORT_DATA_JSON}}*/', $reportDataStr)
  46. # ============================================================
  47. # STEP 5: 输出 HTML 文件(UTF-8 无 BOM)
  48. # ============================================================
  49. $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
  50. [System.IO.File]::WriteAllText($outputPath, $html, $utf8NoBom)
  51. Write-Host "========================================"
  52. Write-Host " HTML Report Test Generated"
  53. Write-Host "========================================"
  54. Write-Host ""
  55. Write-Host "Output: $outputPath" -ForegroundColor Green
  56. Write-Host "Open in browser to preview the 9-slide report."
  57. Write-Host ""
  58. Write-Host "Template vars filled:" -ForegroundColor Cyan
  59. $templateVars.PSObject.Properties | ForEach-Object {
  60. Write-Host " {{$($_.Name)}} = $($_.Value)"
  61. }
  62. Write-Host ""
  63. Write-Host "REPORT_DATA fields injected:" -ForegroundColor Cyan
  64. $reportDataObj.PSObject.Properties.Name | ForEach-Object {
  65. Write-Host " $_"
  66. }