build_studio_exe.ps1 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Build Motor-CAD Scan Studio as a single-file Windows executable.
  2. # Requires: git commit with clean working tree, Python 3.10+, PyQt5,
  3. # ansys-motorcad-core, pyinstaller.
  4. #
  5. # Usage:
  6. # powershell -ExecutionPolicy Bypass -File .\build_studio_exe.ps1
  7. # powershell -ExecutionPolicy Bypass -File .\build_studio_exe.ps1 -AppName MotorCADScanStudio
  8. param(
  9. [string]$AppName = 'MotorCADScanStudio',
  10. [string]$PythonExe = 'C:\Users\admin\AppData\Local\Programs\Python\Python312\python.exe'
  11. )
  12. $ErrorActionPreference = 'Stop'
  13. Set-Location -LiteralPath $PSScriptRoot
  14. # --- Git preflight: must have a commit and clean tracked files ---
  15. $safeDir = $PSScriptRoot.Replace('\', '/')
  16. $commit = git -c "safe.directory=$safeDir" rev-parse --short HEAD
  17. if ($LASTEXITCODE -ne 0) {
  18. throw 'No Git commit exists. Commit before building.'
  19. }
  20. $dirty = git -c "safe.directory=$safeDir" status --porcelain --untracked-files=no
  21. if ($dirty) {
  22. throw 'Tracked files have uncommitted changes. Commit before building.'
  23. }
  24. Write-Host "Git commit: $commit" -ForegroundColor Cyan
  25. # --- Embed build info ---
  26. $buildInfo = "BUILD_COMMIT = '$commit'`nAPP_VERSION = '1.0.0'`n"
  27. [System.IO.File]::WriteAllText(
  28. (Join-Path $PSScriptRoot 'build_info.py'),
  29. $buildInfo,
  30. [System.Text.Encoding]::ASCII
  31. )
  32. # --- PyInstaller: one-file, windowed ---
  33. if (-not (Test-Path -LiteralPath $PythonExe)) {
  34. throw "Python executable not found: $PythonExe"
  35. }
  36. $pyVersion = & $PythonExe --version 2>&1
  37. Write-Host "Using Python: $PythonExe ($pyVersion)" -ForegroundColor Cyan
  38. Write-Host 'Building with PyInstaller...' -ForegroundColor Cyan
  39. & $PythonExe -m PyInstaller `
  40. --noconfirm `
  41. --clean `
  42. --onefile `
  43. --windowed `
  44. --name $AppName `
  45. --hidden-import solver `
  46. --collect-submodules ansys `
  47. --add-data "scan_parameters.json;." `
  48. main.py
  49. if ($LASTEXITCODE -ne 0) {
  50. throw 'PyInstaller build failed.'
  51. }
  52. # --- Copy config next to exe ---
  53. $distDir = Join-Path $PSScriptRoot "dist\$AppName"
  54. if (-not (Test-Path -LiteralPath $distDir)) {
  55. # onefile mode puts exe directly in dist/
  56. $distDir = Join-Path $PSScriptRoot 'dist'
  57. }
  58. Copy-Item -LiteralPath '.\scan_parameters.json' -Destination (Join-Path $distDir 'scan_parameters.json') -Force
  59. $exePath = Join-Path $distDir "$AppName.exe"
  60. if (Test-Path -LiteralPath $exePath) {
  61. $sizeMB = [math]::Round((Get-Item -LiteralPath $exePath).Length / 1MB, 1)
  62. Write-Host ""
  63. Write-Host "Build complete: $exePath" -ForegroundColor Green
  64. Write-Host "Size: $sizeMB MB" -ForegroundColor Green
  65. } else {
  66. Write-Host "Build finished but exe not found at $exePath" -ForegroundColor Yellow
  67. }