build.ps1 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Set-StrictMode -Version Latest
  2. $ErrorActionPreference = "Stop"
  3. $repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
  4. $windowsDir = Join-Path $repoRoot "windows"
  5. $serverDir = Join-Path $repoRoot "server"
  6. $windowsProject = Join-Path $windowsDir "NetworkTool.Client\NetworkTool.Client.csproj"
  7. $windowsPublishOutput = Join-Path $repoRoot "publish\win-x64"
  8. $serverPublishOutput = Join-Path $repoRoot "publish\linux-amd64"
  9. $serverConfig = Join-Path $serverDir "internal\config\config.go"
  10. if (-not (Test-Path -LiteralPath $windowsProject)) {
  11. throw "Windows project not found: $windowsProject"
  12. }
  13. if (-not (Test-Path -LiteralPath $serverDir)) {
  14. throw "Server directory not found: $serverDir"
  15. }
  16. if (-not (Test-Path -LiteralPath $serverConfig)) {
  17. throw "Server config not found: $serverConfig"
  18. }
  19. $windowsProjectXml = [xml](Get-Content -Raw -LiteralPath $windowsProject)
  20. $windowsClientVersion = ($windowsProjectXml.Project.PropertyGroup | ForEach-Object { $_.InformationalVersion } | Where-Object { $_ } | Select-Object -First 1)
  21. if ([string]::IsNullOrWhiteSpace($windowsClientVersion)) {
  22. throw "InformationalVersion not found in: $windowsProject"
  23. }
  24. $windowsDefaultExe = Join-Path $windowsPublishOutput "NetworkTool.Client.exe"
  25. $windowsVersionedExeName = "NetworkTool.Client-$windowsClientVersion-win-x64.exe"
  26. $windowsVersionedExe = Join-Path $windowsPublishOutput $windowsVersionedExeName
  27. $serverConfigText = Get-Content -Raw -LiteralPath $serverConfig
  28. $serverVersionMatch = [regex]::Match($serverConfigText, 'const\s+ServerVersion\s*=\s*"([^"]+)"')
  29. if (-not $serverVersionMatch.Success) {
  30. throw "ServerVersion not found in: $serverConfig"
  31. }
  32. $serverVersion = $serverVersionMatch.Groups[1].Value
  33. $serverLinuxOutputName = "networktool-server-$serverVersion-linux-amd64"
  34. $serverLinuxOutput = Join-Path $serverPublishOutput $serverLinuxOutputName
  35. Write-Host "[1/2] Publishing Windows client (Release, self-contained win-x64)..."
  36. dotnet publish $windowsProject `
  37. -c Release `
  38. -r win-x64 `
  39. --self-contained true `
  40. -p:PublishSingleFile=true `
  41. -p:IncludeNativeLibrariesForSelfExtract=true `
  42. -p:EnableCompressionInSingleFile=true `
  43. -o $windowsPublishOutput
  44. if (-not (Test-Path -LiteralPath $windowsDefaultExe)) {
  45. throw "Published executable not found: $windowsDefaultExe"
  46. }
  47. if (Test-Path -LiteralPath $windowsVersionedExe) {
  48. Remove-Item -LiteralPath $windowsVersionedExe -Force
  49. }
  50. Rename-Item -LiteralPath $windowsDefaultExe -NewName $windowsVersionedExeName
  51. if (-not (Test-Path -LiteralPath $serverPublishOutput)) {
  52. New-Item -ItemType Directory -Path $serverPublishOutput | Out-Null
  53. }
  54. $previousGoos = $env:GOOS
  55. $previousGoarch = $env:GOARCH
  56. $previousCgoEnabled = $env:CGO_ENABLED
  57. try {
  58. Write-Host "[2/2] Building server for Linux amd64..."
  59. Set-Location -LiteralPath $serverDir
  60. $env:GOOS = "linux"
  61. $env:GOARCH = "amd64"
  62. $env:CGO_ENABLED = "0"
  63. go build -o $serverLinuxOutput ./cmd/networktool-server
  64. }
  65. finally {
  66. $env:GOOS = $previousGoos
  67. $env:GOARCH = $previousGoarch
  68. $env:CGO_ENABLED = $previousCgoEnabled
  69. Set-Location -LiteralPath $repoRoot
  70. }
  71. Write-Host "Build completed."
  72. Write-Host "Windows client output: publish/win-x64/$windowsVersionedExeName"
  73. Write-Host "Server Linux output: publish/linux-amd64/$serverLinuxOutputName"