From 348d11c4806a4a6f46c8d9be28c7bcd5bffb55f4 Mon Sep 17 00:00:00 2001 From: Scottxjw <13374147+scottxjw@user.noreply.gitee.com> Date: Mon, 17 Aug 2026 10:48:33 +0800 Subject: [PATCH] =?UTF-8?q?chore(scripts):=20=E6=B7=BB=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8F=91=E5=B8=83=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- .gitignore | 5 +- scripts/publish.ps1 | 126 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 scripts/publish.ps1 diff --git a/.gitignore b/.gitignore index 09c5b87..ded2fc3 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,7 @@ test_logs/ .idea/ # 包目录(由 nuget restore 生成) -packages/ \ No newline at end of file +packages/ + +# 发布产物 +publish/ \ No newline at end of file diff --git a/scripts/publish.ps1 b/scripts/publish.ps1 new file mode 100644 index 0000000..149d8c9 --- /dev/null +++ b/scripts/publish.ps1 @@ -0,0 +1,126 @@ +<# +.SYNOPSIS + 发布 Gpulse.WCT.DataAnalyzer,生成两种包: + 1) Framework-Dependent(需要目标机器装 .NET 8 运行时) + 2) Self-Contained(自带 .NET 8 运行时,无需预装) + 两者均为单文件 exe,附带 appsettings.json。 + +.PARAMETER Version + 版本号,用于产物目录命名。默认从 csproj 读取 ,若 csproj 未定义则 fallback 1.0.0。 + +.PARAMETER OutputBase + 产物输出根目录,默认 .\publish + +.EXAMPLE + .\scripts\publish.ps1 + .\scripts\publish.ps1 -Version 1.2.0 + .\scripts\publish.ps1 -Version 1.2.0 -OutputBase D:\release +#> +param( + [string]$Version, + [string]$OutputBase = ".\publish" +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +# ── 定位项目 ────────────────────────────────────────────────────────────────── +$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..") +$ProjectDir = Join-Path $RepoRoot "src\Gpulse.WCT.DataAnalyzer" +$ProjectCs = Join-Path $ProjectDir "Gpulse.WCT.DataAnalyzer.csproj" +$AppName = "Gpulse.WCT.DataAnalyzer" + +if (-not (Test-Path $ProjectCs)) { + Write-Error "找不到项目文件: $ProjectCs" + exit 1 +} + +# ── 解析版本号 ───────────────────────────────────────────────────────────────── +if (-not $Version) { + # 尝试从 csproj 的 节点读取 + [xml]$csprojXml = Get-Content $ProjectCs -Raw + $xmlNs = New-Object System.Xml.XmlNamespaceManager($csprojXml.NameTable) + $xmlNs.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003") + $versionNode = $csprojXml.SelectSingleNode("//ms:Version", $xmlNs) + if ($versionNode) { + $Version = $versionNode.InnerText + } else { + $Version = "1.0.0" + } +} +Write-Host "版本号: $Version" -ForegroundColor Cyan + +# ── 目录准备 ─────────────────────────────────────────────────────────────────── +$Timestamp = Get-Date -Format "yyyyMMdd-HHmmss" + +$FdDir = Join-Path $OutputBase "framework-dependent\$AppName-$Version-fd" +$ScDir = Join-Path $OutputBase "self-contained\$AppName-$Version-sc" + +foreach ($dir in @($FdDir, $ScDir)) { + if (Test-Path $dir) { Remove-Item $dir -Recurse -Force } + New-Item -ItemType Directory -Path $dir -Force | Out-Null +} + +# ── 公共发布参数 ──────────────────────────────────────────────────────────────── +$CommonPublishArgs = @( + "publish" + $ProjectCs + "-c", "Release" + "-r", "win-x64" + "--nologo" + "-p:PublishSingleFile=true" + "-p:IncludeNativeLibrariesForSelfExtract=true" +) + +# ── 1) Framework-Dependent(不含运行时)───────────────────────────────────────── +Write-Host "" +Write-Host ">>> 构建 Framework-Dependent(不含运行时)..." -ForegroundColor Green + +$fdArgs = $CommonPublishArgs + @( + "--no-self-contained" + "-o", $FdDir +) + +& dotnet @fdArgs +if ($LASTEXITCODE -ne 0) { + Write-Error "Framework-Dependent 构建失败 (exit code $LASTEXITCODE)" + exit $LASTEXITCODE +} + +Write-Host " 产物: $FdDir\$AppName.exe" -ForegroundColor Gray + +# ── 2) Self-Contained(含运行时)──────────────────────────────────────────────── +Write-Host "" +Write-Host ">>> 构建 Self-Contained(含 .NET 8 运行时)..." -ForegroundColor Green + +$scArgs = $CommonPublishArgs + @( + "--self-contained" + "-o", $ScDir + "-p:EnableCompressionInSingleFile=true" +) + +& dotnet @scArgs +if ($LASTEXITCODE -ne 0) { + Write-Error "Self-Contained 构建失败 (exit code $LASTEXITCODE)" + exit $LASTEXITCODE +} + +Write-Host " 产物: $ScDir\$AppName.exe" -ForegroundColor Gray + +# ── 汇总 ─────────────────────────────────────────────────────────────────────── +$FdSize = [math]::Round((Get-Item "$FdDir\$AppName.exe").Length / 1MB, 1) +$ScSize = [math]::Round((Get-Item "$ScDir\$AppName.exe").Length / 1MB, 1) + +Write-Host "" +Write-Host "============================================================" -ForegroundColor Cyan +Write-Host " 发布完成 v$Version $Timestamp" +Write-Host "============================================================" -ForegroundColor Cyan +Write-Host "" +Write-Host " Framework-Dependent(需预装 .NET 8 运行时)" +Write-Host " 目录: $FdDir" +Write-Host " 大小: ${FdSize} MB" +Write-Host "" +Write-Host " Self-Contained(自带 .NET 8 运行时)" +Write-Host " 目录: $ScDir" +Write-Host " 大小: ${ScSize} MB" +Write-Host ""