Kaynağa Gözat

build(script): 优化构建脚本以支持版本化输出

- 从项目文件提取版本号并重命名客户端可执行文件
- 统一服务端与客户端发布目录至 publish/
- 同步更新构建文档中的路径与说明
yangkaixiang 1 ay önce
ebeveyn
işleme
8b08d75722
2 değiştirilmiş dosya ile 47 ekleme ve 8 silme
  1. 42 3
      build.ps1
  2. 5 5
      docs/08-构建与编译.md

+ 42 - 3
build.ps1

@@ -6,7 +6,8 @@ $windowsDir = Join-Path $repoRoot "windows"
 $serverDir = Join-Path $repoRoot "server"
 $windowsProject = Join-Path $windowsDir "NetworkTool.Client\NetworkTool.Client.csproj"
 $windowsPublishOutput = Join-Path $repoRoot "publish\win-x64"
-$serverLinuxOutput = Join-Path $serverDir "networktool-server-linux-amd64"
+$serverPublishOutput = Join-Path $repoRoot "publish\linux-amd64"
+$serverConfig = Join-Path $serverDir "internal\config\config.go"
 
 if (-not (Test-Path -LiteralPath $windowsProject)) {
     throw "Windows project not found: $windowsProject"
@@ -16,6 +17,30 @@ if (-not (Test-Path -LiteralPath $serverDir)) {
     throw "Server directory not found: $serverDir"
 }
 
+if (-not (Test-Path -LiteralPath $serverConfig)) {
+    throw "Server config not found: $serverConfig"
+}
+
+$windowsProjectXml = [xml](Get-Content -Raw -LiteralPath $windowsProject)
+$windowsClientVersion = ($windowsProjectXml.Project.PropertyGroup | ForEach-Object { $_.InformationalVersion } | Where-Object { $_ } | Select-Object -First 1)
+if ([string]::IsNullOrWhiteSpace($windowsClientVersion)) {
+    throw "InformationalVersion not found in: $windowsProject"
+}
+
+$windowsDefaultExe = Join-Path $windowsPublishOutput "NetworkTool.Client.exe"
+$windowsVersionedExeName = "NetworkTool.Client-$windowsClientVersion-win-x64.exe"
+$windowsVersionedExe = Join-Path $windowsPublishOutput $windowsVersionedExeName
+
+$serverConfigText = Get-Content -Raw -LiteralPath $serverConfig
+$serverVersionMatch = [regex]::Match($serverConfigText, 'const\s+ServerVersion\s*=\s*"([^"]+)"')
+if (-not $serverVersionMatch.Success) {
+    throw "ServerVersion not found in: $serverConfig"
+}
+
+$serverVersion = $serverVersionMatch.Groups[1].Value
+$serverLinuxOutputName = "networktool-server-$serverVersion-linux-amd64"
+$serverLinuxOutput = Join-Path $serverPublishOutput $serverLinuxOutputName
+
 Write-Host "[1/2] Publishing Windows client (Release, self-contained win-x64)..."
 dotnet publish $windowsProject `
     -c Release `
@@ -26,6 +51,20 @@ dotnet publish $windowsProject `
     -p:EnableCompressionInSingleFile=true `
     -o $windowsPublishOutput
 
+if (-not (Test-Path -LiteralPath $windowsDefaultExe)) {
+    throw "Published executable not found: $windowsDefaultExe"
+}
+
+if (Test-Path -LiteralPath $windowsVersionedExe) {
+    Remove-Item -LiteralPath $windowsVersionedExe -Force
+}
+
+Rename-Item -LiteralPath $windowsDefaultExe -NewName $windowsVersionedExeName
+
+if (-not (Test-Path -LiteralPath $serverPublishOutput)) {
+    New-Item -ItemType Directory -Path $serverPublishOutput | Out-Null
+}
+
 $previousGoos = $env:GOOS
 $previousGoarch = $env:GOARCH
 $previousCgoEnabled = $env:CGO_ENABLED
@@ -46,5 +85,5 @@ finally {
 }
 
 Write-Host "Build completed."
-Write-Host "Windows client output: publish/win-x64/"
-Write-Host "Server Linux output: server/networktool-server-linux-amd64"
+Write-Host "Windows client output: publish/win-x64/$windowsVersionedExeName"
+Write-Host "Server Linux output: publish/linux-amd64/$serverLinuxOutputName"

+ 5 - 5
docs/08-构建与编译.md

@@ -91,8 +91,8 @@ powershell -ExecutionPolicy Bypass -File .\build.ps1
 
 该脚本会依次完成:
 
-1. 编译 Windows 客户端到默认 `Debug` 目录
-2. 编译 Linux amd64 版 Server 到 `server\networktool-server-linux-amd64`
+1. 发布 Windows 客户端自包含单文件到 `publish\win-x64\NetworkTool.Client-<InformationalVersion>-win-x64.exe`
+2. 编译 Linux amd64 版 Server 到 `publish\linux-amd64\networktool-server-<ServerVersion>-linux-amd64`
 
 ## 5. 上传与启动
 
@@ -104,10 +104,10 @@ powershell -ExecutionPolicy Bypass -File .\build.ps1
 
 ### 5.1 上传 Linux 版 Server
 
-如果当前已经在 `server\` 目录执行:
+如果当前已经在仓库根目录执行:
 
 ```powershell
-scp .\networktool-server-linux-amd64 x@192.168.229.136:/home/x/networktool-server
+scp .\publish\linux-amd64\networktool-server-<ServerVersion>-linux-amd64 x@192.168.229.136:/home/x/networktool-server
 ```
 
 说明:
@@ -188,7 +188,7 @@ tail -n 50 /home/x/networktool-server-run.log
 本机 PowerShell:
 
 ```powershell
-scp .\networktool-server-linux-amd64 x@192.168.229.136:/home/x/networktool-server
+scp .\publish\linux-amd64\networktool-server-<ServerVersion>-linux-amd64 x@192.168.229.136:/home/x/networktool-server
 ssh x@192.168.229.136
 ```