build-windows.ps1 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. param(
  2. [switch]$RunBuild
  3. )
  4. # Post-build helper for Windows:
  5. # - optionally runs the tauri windows build when -RunBuild is passed
  6. # - finds the generated release EXE and copies it to a Chinese-named exe alongside the original
  7. Set-StrictMode -Version Latest
  8. $repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
  9. Set-Location $repoRoot
  10. if ($RunBuild) {
  11. Write-Output "Running npm install and tauri windows build..."
  12. npm install
  13. npm run tauri:build:windows
  14. }
  15. $possibleDirs = @(
  16. "$repoRoot\src-tauri\target\release",
  17. "$repoRoot\src-tauri\target\x86_64-pc-windows-msvc\release",
  18. "$repoRoot\src-tauri\target\x86_64-pc-windows-msvc\release\bundle\nsis"
  19. )
  20. $foundExe = $null
  21. $foundDir = $null
  22. foreach ($dir in $possibleDirs) {
  23. if (-not (Test-Path $dir)) { continue }
  24. $candidate = Get-ChildItem -Path $dir -Filter *.exe -File -Recurse -ErrorAction SilentlyContinue |
  25. Where-Object { $_.Name -notmatch 'setup|bundle|uninstaller' } |
  26. Sort-Object LastWriteTime -Descending |
  27. Select-Object -First 1
  28. if ($candidate) {
  29. $foundExe = $candidate
  30. $foundDir = $dir
  31. break
  32. }
  33. }
  34. if (-not $foundExe) {
  35. Write-Error "No executable (.exe) found in any of: `n$($possibleDirs -join "`n")"
  36. Exit 1
  37. }
  38. $destName = 'BUFF选品-STEAM扫货(游铛铛).exe'
  39. $tauriConf = Join-Path $repoRoot 'src-tauri\tauri.conf.json'
  40. $productName = $null
  41. if (Test-Path $tauriConf) {
  42. try {
  43. $json = Get-Content -Path $tauriConf -Raw | ConvertFrom-Json -ErrorAction Stop
  44. if ($json.package -and $json.package.productName) {
  45. $productName = $json.package.productName
  46. }
  47. } catch {
  48. Write-Warning "Failed to read tauri.conf.json: $($_.Exception.Message)"
  49. }
  50. }
  51. if (-not $productName) {
  52. $productName = $foundExe.BaseName
  53. }
  54. # Sanitize filename: remove characters invalid in Windows filenames
  55. $invalidChars = [System.IO.Path]::GetInvalidFileNameChars()
  56. $sanitized = $productName
  57. foreach ($c in $invalidChars) {
  58. $sanitized = $sanitized -replace [regex]::Escape($c), '-'
  59. }
  60. # Ensure .exe extension
  61. if (-not $sanitized.ToLower().EndsWith('.exe')) {
  62. $sanitized = "$sanitized.exe"
  63. }
  64. $destPath = Join-Path $foundDir $sanitized
  65. Try {
  66. Copy-Item -Path $foundExe.FullName -Destination $destPath -Force
  67. Write-Output "Copied '$($foundExe.Name)' -> '$sanitized' in $foundDir"
  68. } Catch {
  69. Write-Error "Failed to copy exe: $($_.Exception.Message)"
  70. Exit 1
  71. }
  72. Exit 0