Files
ZonyLrcToolsX/.github/workflows/dotnet.yml
T
aL1CesT 7379571e2d build(desktop): 默认开启裁剪,体积从 108 MB 降到 51 MB
桌面端以前发布出来是 107.8 MB(单文件 exe 90.3 MB),其中绝大部分是随包携带的
.NET 运行时里用不到的部分。这次让 `dotnet publish -r <rid>` 默认打开裁剪:

    默认(裁剪)      51.3 MB   exe 33.9 MB
    加压缩            36.9 MB   exe 19.5 MB
    -p:PublishTrimmed=false   107.7 MB   exe 90.3 MB(和以前一样)

选择不裁剪的方式就是发布时加 `-p:PublishTrimmed=false`,输出与改动前完全一致。
普通 `dotnet build`(不带 RID)不受影响,因为裁剪需要 RID 才有意义。

裁剪在这里不是免费的,新增的 ILLink.Descriptors.xml 就是为了付这笔账。有两处
东西是靠反射拿到的,直接裁剪会安静地消失:

1. DI 容器。AutoDependencyInjectExtensions 用 `Assembly.GetTypes()` 扫描
   ZonyLrcTools.Common,把实现 ITransientDependency / ISingletonDependency 的
   类型注册进去。裁剪器看不到这些类型被实例化,会把它们删掉,然后程序能启动、
   一进页面就抛 "No service for type ... has been registered"。

2. Avalonia 的自动化 peer。Windows UI Automation 是按名字请求节点的,裁剪掉之后
   窗口照常渲染,但 UIA 树是空的 —— 读屏软件、讲述人、以及任何自动化脚本看到的
   都是一个空壳。

第 2 点踩过一次坑值得记下来:只保留 Avalonia.Controls 不够。第一版裁出来的 exe
能启动、界面完整,但 UIA 枚举返回 0 个元素。真正的 UIA provider 在 Win32 后端里,
通过 COM 激活而不是静态引用,所以要额外保留 Avalonia.Win32 和 Avalonia.Base。
补上之后 UIA 树恢复成 39 个元素,与不裁剪版本完全一致。

TrimMode 用 partial:只丢弃整个未被引用的程序集,不裁剪方法体。体积收益主要来自
框架,这样做不会去赌"某个方法没被反射用到"。

另外 ILLink.Descriptors.xml 里写的是程序集名不是包名 —— MusicDecrypto.Library
这个包发出的程序集叫 libmusicdecrypto,写包名会得到 IL2007 警告且不生效。

CI 的 Verify 步骤加了一条 90 MB 上限断言:裁剪可以被环境变量或命令行静默关掉,
而关掉之后唯一的症状就是包大了 55 MB,没人会注意到。用宽阈值而不是精确值是给
框架版本升级留余量。

实测(win-x64 自包含单文件):

    项目            总大小    主 exe    UIA 元素   .kgg 解密   歌词下载
    不裁剪          107.8 MB  90.3 MB   39        3/3         3/3
    裁剪             51.3 MB  33.9 MB   39        3/3         3/3
    裁剪 + 压缩      36.9 MB  19.5 MB   39        3/3         —
    Trimmed=false   107.7 MB  90.3 MB   —         —           —

.kgg 解密产物逐字节相同(3371722 / 51650043 / 48354702 字节);设置写回仍然只改
config.yaml 里受影响的那几行,60 行、35 条注释全保留;五个页面在裁剪版下截图与
不裁剪版无差异。启动耗时略慢:1048 ms → 1223 ms,因为裁剪后的程序集元数据要从
打包数据里反序列化;换来的是下载体积少 56 MB。

这段代码由 AI 辅助编写,发布前已在本机逐项跑过上面这张表。
2026-11-09 05:02:46 +08:00

167 lines
6.7 KiB
YAML
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.
name: .NET
on:
push:
branches: [ dev ]
paths:
- "versions/release.md"
pull_request:
branches: [ dev ]
jobs:
# Windows 和 Linux 构建(保持在 Ubuntu 上)
build-linux-windows:
runs-on: ubuntu-latest
steps:
- name: Get build version
id: date
run: echo "date=$(date +'%Y%m%d')${{github.run_number}}" >> $GITHUB_OUTPUT
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Publish Windows and Linux
working-directory: ./src/ZonyLrcTools.Cli
run: |
for platform in win-x64 linux-x64 win-arm64 linux-arm64; do
dotnet publish -r "$platform" -c Release -p:PublishSingleFile=true -p:UseAppHost=true -p:DebugType=none --self-contained true
cd ./bin/Release/net10.0/"$platform"/publish/
zip -r ./ZonyLrcTools_"$platform"_"${{ steps.date.outputs.date }}".zip ./
cd ../../../../../
mkdir -p ./TempFiles
mv ./bin/Release/net10.0/"$platform"/publish/*.zip ./TempFiles/
done
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
retention-days: 90
name: release-files-linux-windows
path: ./src/ZonyLrcTools.Cli/TempFiles
outputs:
version: ${{ steps.date.outputs.date }}
# 桌面端构建(目前只发布 Windows,见 README 的说明)
build-desktop:
runs-on: windows-latest
steps:
- name: Get build version
id: date
run: echo "date=$(Get-Date -Format 'yyyyMMdd')${{github.run_number}}" >> $env:GITHUB_OUTPUT
shell: pwsh
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Publish Desktop (win-x64)
working-directory: ./src/ZonyLrcTools.Desktop
run: |
dotnet publish -r win-x64 -c Release -p:PublishSingleFile=true -p:UseAppHost=true -p:DebugType=none --self-contained true
New-Item -ItemType Directory -Force -Path ./TempFiles | Out-Null
Compress-Archive -Path ./bin/Release/net10.0/win-x64/publish/* -DestinationPath ./TempFiles/ZonyLrcTools.Desktop_win-x64_${{ steps.date.outputs.date }}.zip -Force
shell: pwsh
# Microsoft.Data.Sqlite 的原生库不会被打进单文件 exe,漏了它 .kgg 解密会在运行时直接失败。
# 这里在打包后做一次校验,让 CI 在发布环节而不是在用户机器上暴露问题。
#
# 顺带盯着裁剪有没有真的生效:项目默认开了 PublishTrimmed,但裁剪可以被环境变量、
# 命令行参数或者一次 csproj 手滑静默关掉,而关掉之后唯一的症状就是包变大 55 MB。
# 用一个宽阈值(90 MB)而不是精确值,是因为框架版本升级会让体积小幅波动。
- name: Verify Desktop publish output
working-directory: ./src/ZonyLrcTools.Desktop
run: |
$publish = './bin/Release/net10.0/win-x64/publish'
$exe = Join-Path $publish 'ZonyLrcTools.Desktop.exe'
$sqlite = Join-Path $publish 'e_sqlite3.dll'
if (-not (Test-Path $exe)) { throw "缺少主程序: $exe" }
if (-not (Test-Path $sqlite)) { throw "缺少 SQLite 原生库: $sqlite(.kgg 解密依赖它)" }
if (-not (Test-Path (Join-Path $publish 'config.yaml'))) { throw '缺少 config.yaml 模板' }
$totalMb = [math]::Round(((Get-ChildItem $publish -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1MB), 1)
Write-Host "publish 目录校验通过:$((Get-ChildItem $publish -File).Count) 个文件,共 $totalMb MB"
if ($totalMb -gt 90) {
throw "publish 目录有 $totalMb MB,超过 90 MB 上限;这通常意味着 PublishTrimmed 被关掉了"
}
shell: pwsh
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
retention-days: 90
name: release-files-desktop
path: ./src/ZonyLrcTools.Desktop/TempFiles
outputs:
version: ${{ steps.date.outputs.date }}
# macOS 构建(在 macOS runner 上,支持 ad-hoc 签名)
build-macos:
runs-on: macos-latest
steps:
- name: Get build version
id: date
run: echo "date=$(date +'%Y%m%d')${{github.run_number}}" >> $GITHUB_OUTPUT
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Publish macOS
working-directory: ./src/ZonyLrcTools.Cli
run: |
for platform in osx-x64 osx-arm64; do
dotnet publish -r "$platform" -c Release -p:PublishSingleFile=true -p:DebugType=none --self-contained true
# Ad-hoc 签名,解决 macOS Gatekeeper/XProtect 问题
codesign -s - --force --deep ./bin/Release/net10.0/"$platform"/publish/ZonyLrcTools.Cli
cd ./bin/Release/net10.0/"$platform"/publish/
zip -r ./ZonyLrcTools_"$platform"_"${{ steps.date.outputs.date }}".zip ./
cd ../../../../../
mkdir -p ./TempFiles
mv ./bin/Release/net10.0/"$platform"/publish/*.zip ./TempFiles/
done
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
retention-days: 90
name: release-files-macos
path: ./src/ZonyLrcTools.Cli/TempFiles
outputs:
version: ${{ steps.date.outputs.date }}
# Release Job - 合并所有平台的构建产物
release:
if: github.event_name == 'push'
needs: [build-linux-windows, build-macos, build-desktop]
runs-on: ubuntu-latest
steps:
- name: Checkout for release notes
uses: actions/checkout@v4
- name: Download Linux/Windows artifacts
uses: actions/download-artifact@v4
with:
name: release-files-linux-windows
path: ./TempFiles
- name: Download macOS artifacts
uses: actions/download-artifact@v4
with:
name: release-files-macos
path: ./TempFiles
- name: Download Desktop artifacts
uses: actions/download-artifact@v4
with:
name: release-files-desktop
path: ./TempFiles
- name: Upload Release
uses: ncipollo/release-action@v1
with:
artifacts: "./TempFiles/*.zip"
token: ${{ secrets.GITHUBACTIONS }}
tag: ZonyLrcToolsX_Alpha.${{ needs.build-linux-windows.outputs.version }}
commit: dev
bodyFile: ./versions/release.md