feat: 添加便携模式和打包脚本,精简打包大小

This commit is contained in:
2026-01-17 17:58:37 +08:00
parent c276e9e2b9
commit b0e785bd06
8 changed files with 332 additions and 20 deletions

View File

@@ -0,0 +1,91 @@
param(
[string]$Rid = "win-x64",
[bool]$SelfContained = $false,
[bool]$SingleFile = $true,
[bool]$EnableCompressionInSingleFile = $true,
[bool]$ReadyToRun = $false,
[bool]$Trim = $false,
[bool]$InvariantGlobalization = $false
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
$project = Join-Path $repoRoot "TimerApp.csproj"
$artifactsDir = Join-Path $repoRoot "artifacts"
$distDir = Join-Path $repoRoot "dist"
New-Item -ItemType Directory -Force -Path $distDir | Out-Null
$EnableCompressionInSingleFile = $EnableCompressionInSingleFile -and $SelfContained
$variantParts = @()
$variantParts += $(if ($SelfContained) { "sc" } else { "fd" })
$variantParts += $(if ($SingleFile) { "single" } else { "multi" })
if ($SingleFile -and $EnableCompressionInSingleFile) { $variantParts += "comp" }
if ($ReadyToRun) { $variantParts += "r2r" }
if ($Trim) { $variantParts += "trim" }
if ($InvariantGlobalization) { $variantParts += "invglob" }
$variant = ($variantParts -join "-")
$defaultVariant = "fd-single"
$publishBaseDir = Join-Path $artifactsDir "publish"
$publishDir = Join-Path (Join-Path $publishBaseDir $Rid) $variant
New-Item -ItemType Directory -Force -Path $publishDir | Out-Null
$props = @(
"-p:SelfContained=$SelfContained",
"-p:PublishSingleFile=$SingleFile",
"-p:PublishReadyToRun=$ReadyToRun",
"-p:DebugType=None",
"-p:DebugSymbols=false"
)
if ($SingleFile) {
$props += "-p:IncludeNativeLibrariesForSelfExtract=true"
if ($SelfContained) {
$props += "-p:EnableCompressionInSingleFile=$EnableCompressionInSingleFile"
}
}
if ($Trim) {
$props += "-p:PublishTrimmed=true"
$props += "-p:TrimMode=partial"
}
if ($InvariantGlobalization) {
$props += "-p:InvariantGlobalization=true"
}
dotnet publish $project `
-c Release `
-r $Rid `
@props `
-o $publishDir
if ($LASTEXITCODE -ne 0) {
throw "dotnet publish failed with exit code $LASTEXITCODE"
}
$zipName = if ($variant -eq $defaultVariant) { "TimerApp-Portable-$Rid.zip" } else { "TimerApp-Portable-$Rid-$variant.zip" }
$zipPath = Join-Path $distDir $zipName
if (Test-Path $zipPath) {
try { [System.IO.File]::Delete($zipPath) } catch { }
}
Compress-Archive -Path (Join-Path $publishDir "*") -DestinationPath $zipPath -Force
$mainExe = Join-Path $publishDir "TimerApp.exe"
if (Test-Path $mainExe) {
$exeMiB = [Math]::Round(((Get-Item $mainExe).Length / 1MB), 1)
Write-Host "EXE size: $exeMiB MiB"
}
$zipMiB = [Math]::Round(((Get-Item $zipPath).Length / 1MB), 1)
Write-Host "ZIP size: $zipMiB MiB"
Write-Host "Portable zip created:"
Write-Host " $zipPath"