Files
WCTDataMiner/scripts/publish.ps1

128 lines
5.0 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<#
.SYNOPSIS
发布 Gpulse.WCT.DataAnalyzer生成两种包
1) Framework-Dependent需要目标机器装 .NET 8 运行时)
2) Self-Contained自带 .NET 8 运行时,无需预装)
两者均为单文件 exe附带 appsettings.json。
.PARAMETER Version
版本号,用于产物目录命名。默认从 csproj 读取 <Version>,若 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 的 <Version> 节点读取
# SDK 风格的 csproj 没有 XML 命名空间,直接用 XPath 查询
[xml]$csprojXml = Get-Content $ProjectCs -Raw
$versionNode = $csprojXml.SelectSingleNode("//Version")
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"
"-p:DebugType=none"
"-p:DebugSymbols=false"
)
# ── 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 ""