Build Avalonia.MacOS.ps1 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. param (
  2. [Parameter()]
  3. [string]$Platform,
  4. [Parameter()]
  5. [string]$outputPath,
  6. [Parameter()]
  7. [string]$appVersion
  8. )
  9. # Define the core project path relative to the script's location
  10. $coreProjectPath = Join-Path -Path $PSScriptRoot -ChildPath "..\src\PicView.Core\PicView.Core.csproj"
  11. # Load the .csproj file as XML
  12. [xml]$coreCsproj = Get-Content $coreProjectPath
  13. # Define the package reference to replace
  14. $packageRefX64 = "Magick.NET-Q8-x64"
  15. $packageRefArm64 = "Magick.NET-Q8-arm64"
  16. # Find the Magick.NET package reference and update it based on the platform
  17. $packageNodes = $coreCsproj.Project.ItemGroup.PackageReference | Where-Object { $_.Include -eq $packageRefX64 -or $_.Include -eq $packageRefArm64 }
  18. if ($packageNodes) {
  19. foreach ($packageNode in $packageNodes) {
  20. if ($Platform -eq "arm64") {
  21. $packageNode.Include = $packageRefArm64
  22. } else {
  23. $packageNode.Include = $packageRefX64
  24. }
  25. }
  26. }
  27. # Save the updated .csproj file
  28. $coreCsproj.Save($coreProjectPath)
  29. # Define the project path for the actual build target
  30. $avaloniaProjectPath = Join-Path -Path $PSScriptRoot -ChildPath "../src/PicView.Avalonia.MacOS/PicView.Avalonia.MacOS.csproj"
  31. # Create temporary build output directory
  32. $tempBuildPath = Join-Path -Path $outputPath -ChildPath "temp"
  33. New-Item -ItemType Directory -Force -Path $tempBuildPath
  34. # Run dotnet publish for the Avalonia project
  35. dotnet publish $avaloniaProjectPath `
  36. --runtime "osx-$Platform" `
  37. --self-contained true `
  38. --configuration Release `
  39. -p:UseAppHost=true `
  40. -p:PublishSingleFile=false `
  41. --output $tempBuildPath
  42. # Create .app bundle structure
  43. $appBundlePath = Join-Path -Path $outputPath -ChildPath "PicView.app"
  44. $contentsPath = Join-Path -Path $appBundlePath -ChildPath "Contents"
  45. $macOSPath = Join-Path -Path $contentsPath -ChildPath "MacOS"
  46. $resourcesPath = Join-Path -Path $contentsPath -ChildPath "Resources"
  47. # Create directory structure
  48. New-Item -ItemType Directory -Force -Path $macOSPath
  49. New-Item -ItemType Directory -Force -Path $resourcesPath
  50. # Use template Info.plist and patch version and architecture
  51. $infoPlistTemplatePath = Join-Path -Path $PSScriptRoot -ChildPath "../src/PicView.Core.MacOS/Info.plist"
  52. $infoPlistPath = Join-Path -Path $contentsPath -ChildPath "Info.plist"
  53. # Read template as text
  54. $infoPlistContent = Get-Content $infoPlistTemplatePath -Raw
  55. # Map platform identifier to proper macOS architecture identifier
  56. $macOSArchitecture = if ($Platform -eq "arm64") { "arm64" } else { "x86_64" }
  57. # Replace placeholders with actual values
  58. $infoPlistContent = $infoPlistContent -replace "{{appVersion}}", $appVersion
  59. $infoPlistContent = $infoPlistContent -replace "{{platform}}", $macOSArchitecture
  60. # Save Info.plist with UTF-8 encoding without BOM
  61. $utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
  62. [System.IO.File]::WriteAllText($infoPlistPath, $infoPlistContent, $utf8NoBomEncoding)
  63. # Copy build output to MacOS directory
  64. Copy-Item -Path "$tempBuildPath/*" -Destination $macOSPath -Recurse
  65. # Copy icon if it exists
  66. $iconSource = Join-Path -Path $PSScriptRoot -ChildPath "../src/PicView.Avalonia.MacOS/Assets/AppIcon.icns"
  67. if (Test-Path $iconSource) {
  68. Copy-Item -Path $iconSource -Destination $resourcesPath
  69. }
  70. # Remove PDB files
  71. Get-ChildItem -Path $macOSPath -Filter "*.pdb" -Recurse | Remove-Item -Force
  72. # Remove temporary build directory
  73. Remove-Item -Path $tempBuildPath -Recurse -Force
  74. # Set executable permissions on all binaries and dylibs
  75. Get-ChildItem -Path $macOSPath -Recurse | ForEach-Object {
  76. if ($_.Extension -in @('.dylib', '') -or $_.Name -eq 'PicView.Avalonia.MacOS') {
  77. chmod +x $_.FullName
  78. }
  79. }
  80. # Set proper ownership and permissions for the entire .app bundle
  81. chmod -R 755 $appBundlePath