Files
Better-Seewo/Publish/build.ps1
miao-moe bb0c8832a0 feat: 新增一键构建脚本 (v2.0.1)
- 添加 build.ps1 PowerShell 构建脚本
- 添加 build.bat 批处理入口(双击一键构建)
- 支持 Debug/Release 配置切换
- 支持自包含模式发布
- 自动检测 .NET SDK、清理输出、编译打包
2026-06-28 07:14:37 +00:00

197 lines
6.7 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.
#Requires -Version 5.1
<#
.SYNOPSIS
Better-Seewo 一键构建脚本
.DESCRIPTION
自动还原 NuGet 包、编译 Manager 和 Installer 项目,并打包为发布版本。
支持 Debug / Release 配置,支持框架依赖和自包含两种发布模式。
.PARAMETER Configuration
构建配置Debug 或 Release默认 Release
.PARAMETER SelfContained
是否自包含 .NET Runtime默认 $false需要用户安装 .NET 6 Desktop Runtime
.PARAMETER OutputPath
输出目录(默认 ./Publish
.EXAMPLE
.\build.ps1
.\build.ps1 -Configuration Release -SelfContained $true
#>
param(
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Release",
[bool]$SelfContained = $false,
[string]$OutputPath = "$PSScriptRoot\Publish"
)
$ErrorActionPreference = "Stop"
# ========== 颜色输出辅助函数 ==========
function Write-Info { param([string]$Message) Write-Host "[INFO] $Message" -ForegroundColor Cyan }
function Write-Success { param([string]$Message) Write-Host "[OK] $Message" -ForegroundColor Green }
function Write-Warning { param([string]$Message) Write-Host "[WARN] $Message" -ForegroundColor Yellow }
function Write-Error { param([string]$Message) Write-Host "[ERR] $Message" -ForegroundColor Red }
# ========== 标题 ==========
Write-Host ""
Write-Host "========================================" -ForegroundColor Magenta
Write-Host " Better-Seewo 一键构建脚本 v2.0.1" -ForegroundColor Magenta
Write-Host " 雾生万象,启以为光" -ForegroundColor Magenta
Write-Host "========================================" -ForegroundColor Magenta
Write-Host ""
# ========== 检测 .NET SDK ==========
Write-Info "检测 .NET SDK ..."
$dotnetVersion = dotnet --version 2>$null
if (-not $dotnetVersion) {
Write-Error ".NET SDK 未找到!请先安装 .NET 6.0 SDK"
Write-Error " https://dotnet.microsoft.com/download/dotnet/6.0"
exit 1
}
Write-Success ".NET SDK 版本: $dotnetVersion"
# ========== 检测解决方案路径 ==========
$SolutionDir = $PSScriptRoot
$SolutionFile = Join-Path $SolutionDir "Better-Seewo.sln"
if (-not (Test-Path $SolutionFile)) {
Write-Error "未找到解决方案文件: $SolutionFile"
exit 1
}
Write-Info "解决方案路径: $SolutionFile"
# ========== 清理旧输出 ==========
Write-Info "清理旧构建输出 ..."
$OutputManager = Join-Path $OutputPath "Manager"
$OutputInstaller = Join-Path $OutputPath "Installer"
if (Test-Path $OutputPath) {
Remove-Item -Recurse -Force $OutputPath
}
New-Item -ItemType Directory -Path $OutputManager -Force | Out-Null
New-Item -ItemType Directory -Path $OutputInstaller -Force | Out-Null
Write-Success "输出目录已清理: $OutputPath"
# ========== 还原 NuGet 包 ==========
Write-Info "还原 NuGet 包 ..."
$restoreArgs = @("restore", $SolutionFile)
# 如果 EasiPlugin 私有源不可用,跳过它
$NugetConfig = Join-Path $SolutionDir "NuGet.config"
if (Test-Path $NugetConfig) {
[xml]$nugetXml = Get-Content $NugetConfig
$easiPluginSource = $nugetXml.configuration.packageSources.add | Where-Object { $_.key -eq "EasiPlugin" }
if ($easiPluginSource) {
Write-Warning "检测到 EasiPlugin 私有 NuGet 源Better-EN5 项目可能无法编译"
}
}
& dotnet @restoreArgs
if ($LASTEXITCODE -ne 0) {
Write-Warning "NuGet 还原部分失败EasiPlugin SDK 可能无法访问),继续构建可用项目 ..."
}
# ========== 构建 Manager ==========
Write-Info "构建 Manager 项目 [$Configuration] ..."
$managerArgs = @(
"publish",
(Join-Path $SolutionDir "Manager\Manager.csproj"),
"-c", $Configuration,
"--no-restore",
"-o", $OutputManager,
"-p:SolutionDir=$SolutionDir\"
)
if ($SelfContained) {
$managerArgs += "--self-contained"
$managerArgs += "true"
$managerArgs += "-r"
$managerArgs += "win-x64"
}
& dotnet @managerArgs
if ($LASTEXITCODE -ne 0) {
Write-Error "Manager 项目构建失败!"
exit 1
}
Write-Success "Manager 构建完成 -> $OutputManager"
# ========== 构建 Installer ==========
Write-Info "构建 Installer 项目 [$Configuration] ..."
$installerArgs = @(
"publish",
(Join-Path $SolutionDir "Installer\Installer.csproj"),
"-c", $Configuration,
"--no-restore",
"-o", $OutputInstaller,
"-p:SolutionDir=$SolutionDir\"
)
if ($SelfContained) {
$installerArgs += "--self-contained"
$installerArgs += "true"
$installerArgs += "-r"
$installerArgs += "win-x64"
}
& dotnet @installerArgs
if ($LASTEXITCODE -ne 0) {
Write-Error "Installer 项目构建失败!"
exit 1
}
Write-Success "Installer 构建完成 -> $OutputInstaller"
# ========== 复制构建脚本到输出目录 ==========
Write-Info "复制构建脚本 ..."
Copy-Item (Join-Path $SolutionDir "build.ps1") $OutputPath
Copy-Item (Join-Path $SolutionDir "build.bat") $OutputPath
Write-Success "构建脚本已复制"
# ========== 生成版本信息 ==========
Write-Info "生成版本信息 ..."
$VersionInfo = @"
Better-Seewo v2.0.1
: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
: $Configuration
: $SelfContained
.NET SDK: $dotnetVersion
:
- Better-Seewo.Manager.exe ()
- Better-Seewo.Installer.exe ()
:
- Windows 10/11
$(if (-not $SelfContained) { " - .NET 6.0 Desktop Runtime`n" })
- EN5 v5.2.4.9855
:
"@
$VersionInfo | Out-File -Encoding UTF8 (Join-Path $OutputPath "VERSION.txt")
Write-Success "版本信息已生成"
# ========== 统计输出文件 ==========
Write-Info "统计构建产物 ..."
$managerExe = Get-ChildItem $OutputManager -Filter "Better-Seewo.Manager*.exe" -ErrorAction SilentlyContinue
$installerExe = Get-ChildItem $OutputInstaller -Filter "Better-Seewo.Installer*.exe" -ErrorAction SilentlyContinue
if ($managerExe) {
Write-Success "Manager 可执行文件: $($managerExe.Name) ($([math]::Round($managerExe.Length/1MB, 2)) MB)"
} else {
Write-Warning "Manager .exe 未生成(可能为 DLL-only 输出)"
}
if ($installerExe) {
Write-Success "Installer 可执行文件: $($installerExe.Name) ($([math]::Round($installerExe.Length/1MB, 2)) MB)"
} else {
Write-Warning "Installer .exe 未生成(可能为 DLL-only 输出)"
}
# ========== 完成 ==========
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " 构建成功!" -ForegroundColor Green
Write-Host " 输出目录: $OutputPath" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
# 可选:自动打开输出目录
$openDir = Read-Host "是否打开输出目录? (Y/n)"
if ($openDir -eq "" -or $openDir -match "^[Yy]") {
Invoke-Item $OutputPath
}