param(
|
[string]$Configuration = "Release",
|
[string]$Runtime = "win-x64",
|
[string]$TargetRoot = "Y:\应用程序",
|
[string]$AppName = "Moli.Desktop",
|
[switch]$SelfContained,
|
[switch]$SkipTests,
|
[switch]$NoZip,
|
[switch]$KeepStaging
|
)
|
|
$ErrorActionPreference = "Stop"
|
|
function Invoke-CheckedRobocopy {
|
param(
|
[Parameter(Mandatory = $true)]
|
[string]$Source,
|
[Parameter(Mandatory = $true)]
|
[string]$Destination
|
)
|
|
robocopy $Source $Destination /MIR /FFT /R:2 /W:2 /XD ".git" ".vs" /XF "*.pdb"
|
$exitCode = $LASTEXITCODE
|
if ($exitCode -gt 7) {
|
throw "robocopy failed with exit code $exitCode"
|
}
|
}
|
|
function Stop-PublishedApplication {
|
param(
|
[Parameter(Mandatory = $true)]
|
[string]$ProcessName,
|
[Parameter(Mandatory = $true)]
|
[string]$ExecutablePath
|
)
|
|
$normalizedExecutablePath = [System.IO.Path]::GetFullPath($ExecutablePath)
|
$processes = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue
|
foreach ($process in $processes) {
|
$matchesTarget = $false
|
try {
|
$processPath = [System.IO.Path]::GetFullPath($process.MainModule.FileName)
|
$matchesTarget = [string]::Equals($processPath, $normalizedExecutablePath, [System.StringComparison]::OrdinalIgnoreCase)
|
}
|
catch {
|
Write-Warning "Cannot inspect process path for $ProcessName (PID $($process.Id)); skipping it."
|
}
|
|
if ($matchesTarget) {
|
Write-Host "Stopping running application: $ProcessName (PID $($process.Id))"
|
Stop-Process -Id $process.Id -Force
|
$process.WaitForExit(10000)
|
}
|
}
|
}
|
|
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
|
$solutionPath = Join-Path $repoRoot "Moli.Desktop.slnx"
|
$projectPath = Join-Path $repoRoot "src\Moli.Desktop\Moli.Desktop.csproj"
|
$stagingRoot = Join-Path $repoRoot "artifacts\release"
|
$publishDir = Join-Path $stagingRoot "publish\$AppName"
|
$targetRootPath = Join-Path $TargetRoot ""
|
$targetDir = Join-Path $targetRootPath $AppName
|
$targetExePath = Join-Path $targetDir "$AppName.exe"
|
$zipPath = Join-Path $targetRootPath "$AppName-$Runtime.zip"
|
|
if (-not (Test-Path -LiteralPath $projectPath)) {
|
throw "Project file not found: $projectPath"
|
}
|
|
if (-not (Test-Path -LiteralPath $TargetRoot)) {
|
New-Item -ItemType Directory -Force -Path $TargetRoot | Out-Null
|
}
|
|
$resolvedTargetRoot = (Resolve-Path -LiteralPath $TargetRoot).Path
|
New-Item -ItemType Directory -Force -Path $targetDir | Out-Null
|
$resolvedTargetDir = (Resolve-Path -LiteralPath $targetDir).Path
|
|
if ($resolvedTargetDir -eq $resolvedTargetRoot) {
|
throw "Refusing to publish directly into target root: $resolvedTargetRoot"
|
}
|
|
if (-not $resolvedTargetDir.StartsWith($resolvedTargetRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
|
throw "Resolved target is outside target root: $resolvedTargetDir"
|
}
|
|
New-Item -ItemType Directory -Force -Path $stagingRoot | Out-Null
|
if (Test-Path -LiteralPath $publishDir) {
|
Remove-Item -LiteralPath $publishDir -Recurse -Force
|
}
|
|
if (-not $SkipTests) {
|
dotnet test $solutionPath --configuration $Configuration
|
}
|
|
$selfContainedValue = if ($SelfContained) { "true" } else { "false" }
|
dotnet publish $projectPath `
|
--configuration $Configuration `
|
--runtime $Runtime `
|
--self-contained $selfContainedValue `
|
-p:PublishSingleFile=false `
|
--output $publishDir
|
|
Stop-PublishedApplication -ProcessName $AppName -ExecutablePath $targetExePath
|
|
if (Test-Path -LiteralPath $targetDir) {
|
Write-Host "Removing old published files:"
|
Write-Host $targetDir
|
Remove-Item -LiteralPath $targetDir -Recurse -Force
|
}
|
New-Item -ItemType Directory -Force -Path $targetDir | Out-Null
|
|
Invoke-CheckedRobocopy -Source $publishDir -Destination $targetDir
|
|
if (-not $NoZip) {
|
if (Test-Path -LiteralPath $zipPath) {
|
Remove-Item -LiteralPath $zipPath -Force
|
}
|
Compress-Archive -Path (Join-Path $publishDir "*") -DestinationPath $zipPath -Force
|
}
|
|
if (-not $KeepStaging) {
|
Remove-Item -LiteralPath $stagingRoot -Recurse -Force
|
}
|
|
Write-Host "Starting application:"
|
Write-Host $targetExePath
|
Start-Process -FilePath $targetExePath -WorkingDirectory $targetDir
|
|
Write-Host "Published application:"
|
Write-Host $targetDir
|
if (-not $NoZip) {
|
Write-Host "Portable package:"
|
Write-Host $zipPath
|
}
|