pack-exe.ps1 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #Requires -Version 5.1
  2. # Pack existing backup scripts into a double-click exe. Does not change backup logic.
  3. $ErrorActionPreference = "Stop"
  4. $Root = Split-Path -Parent $MyInvocation.MyCommand.Path
  5. Set-Location $Root
  6. $cscCandidates = @(
  7. "$env:WINDIR\Microsoft.NET\Framework64\v4.0.30319\csc.exe",
  8. "$env:WINDIR\Microsoft.NET\Framework\v4.0.30319\csc.exe"
  9. )
  10. $csc = $cscCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
  11. if (-not $csc) {
  12. throw "csc.exe not found (.NET Framework 4.x)."
  13. }
  14. $src = Join-Path $Root "MesDbBackup.cs"
  15. if (-not (Test-Path $src)) { throw "MesDbBackup.cs not found" }
  16. $readme = Get-ChildItem -Path $Root -Filter "*.txt" | Select-Object -First 1
  17. if (-not $readme) { throw "readme txt not found" }
  18. $required = @(
  19. "backup-ui-server.ps1",
  20. "backup-db.ps1",
  21. "db-config.ps1",
  22. "backup-ui.html",
  23. "restore-db.ps1",
  24. "install-backup-task.bat",
  25. $readme.Name
  26. )
  27. foreach ($f in $required) {
  28. if (-not (Test-Path (Join-Path $Root $f))) {
  29. throw "missing pack file: $f"
  30. }
  31. }
  32. # PS 5.1 parses no-BOM files as ANSI; add UTF-8 BOM to .ps1 so Chinese stays valid
  33. $utf8Bom = New-Object System.Text.UTF8Encoding $true
  34. $utf8NoBom = New-Object System.Text.UTF8Encoding $false
  35. Get-ChildItem -Path $Root -Filter "*.ps1" | ForEach-Object {
  36. $raw = [System.IO.File]::ReadAllBytes($_.FullName)
  37. if ($raw.Length -ge 3 -and $raw[0] -eq 0xEF -and $raw[1] -eq 0xBB -and $raw[2] -eq 0xBF) { return }
  38. $text = [System.IO.File]::ReadAllText($_.FullName, $utf8NoBom)
  39. [System.IO.File]::WriteAllText($_.FullName, $text, $utf8Bom)
  40. }
  41. # csc misreads UTF-8 without BOM; write a BOM copy of the C# source
  42. $tmpSrc = Join-Path $env:TEMP "MesDbBackup_build.cs"
  43. $utf8Bom = New-Object System.Text.UTF8Encoding $true
  44. [System.IO.File]::WriteAllText($tmpSrc, [System.IO.File]::ReadAllText($src, [System.Text.Encoding]::UTF8), $utf8Bom)
  45. $outExe = Join-Path $Root "MesDbBackup.exe"
  46. $fwDir = Split-Path $csc
  47. $winforms = Join-Path $fwDir "System.Windows.Forms.dll"
  48. $drawing = Join-Path $fwDir "System.Drawing.dll"
  49. $resArgs = @()
  50. foreach ($f in $required) {
  51. $resArgs += ('/resource:"{0}",{1}' -f (Join-Path $Root $f), $f)
  52. }
  53. $argList = @(
  54. "/nologo",
  55. "/target:winexe",
  56. "/optimize+",
  57. "/platform:anycpu",
  58. ('/out:"{0}"' -f $outExe),
  59. ('/reference:"{0}"' -f $winforms),
  60. ('/reference:"{0}"' -f $drawing)
  61. ) + $resArgs + @(('"{0}"' -f $tmpSrc))
  62. Write-Host ("Compiling: {0}" -f $outExe)
  63. $p = Start-Process -FilePath $csc -ArgumentList $argList -Wait -PassThru -NoNewWindow
  64. if ($p.ExitCode -ne 0) {
  65. throw ("compile failed, exit {0}" -f $p.ExitCode)
  66. }
  67. Remove-Item $tmpSrc -Force -ErrorAction SilentlyContinue
  68. if (-not (Test-Path $outExe)) {
  69. throw "exe not created"
  70. }
  71. $sizeKb = [math]::Round((Get-Item $outExe).Length / 1KB, 1)
  72. Write-Host ("OK: {0} ({1} KB)" -f $outExe, $sizeKb)
  73. Write-Host "Copy the exe to the server and double-click. Config is saved as db-config.json beside the exe."