Files
md-preview/agent/scripts/build.ps1
Richard Wu 8221eb5908 chore: clear clippy warnings and tighten the build guard
- base64_encode uses div_ceil plus a slice loop (clippy: manual_div_ceil,
  chunks_exact_to_as_chunks); add known-vector tests.
- macOS-only UserEvent variants no longer warn on Windows.
- needless borrow, needless bool, and a collapsible match arm in the
  Destroyed handler.
- build.ps1 no longer treats the word "powershell" as an updater marker;
  it now checks Start-Process / Invoke-WebRequest instead.
2026-11-03 22:11:03 +08:00

97 lines
3.4 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
<#
MD Preview 开发构建(DSH workspace-write / Low 完整性可运行)
- cargo test + cargo build --release
- 只做校验,不复制到任何固定 exe 路径
- 产物:target\release\md-preview.exe(Low)
- 发布:用户在自己的普通 PowerShell 里运行 agent\scripts\publish.ps1
#>
[CmdletBinding()]
param(
[switch]$SkipTests
)
$ErrorActionPreference = 'Stop'
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
Set-Location $repoRoot
# 工作区规则(agent/AGENTS.md):临时文件留在 target\ 下,不污染 %TEMP%。
$tmpRoot = Join-Path $repoRoot 'target\tmp'
New-Item -ItemType Directory -Force $tmpRoot | Out-Null
$env:TEMP = $tmpRoot
$env:TMP = $tmpRoot
$cargo = Join-Path $env:USERPROFILE '.cargo\bin\cargo.exe'
if (-not (Test-Path $cargo)) {
$cargo = (Get-Command cargo -ErrorAction Stop).Source
}
function Step([string]$text) { Write-Host ('==> ' + $text) -ForegroundColor Cyan }
function Invoke-Cargo([string[]]$arguments) {
$previous = $ErrorActionPreference
$code = 0
try {
# cargo 把进度/警告写到 stderr;Stop 会把它们当成终止错误
$ErrorActionPreference = 'Continue'
& $cargo @arguments
$code = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previous
}
if ($code -ne 0) {
throw ('cargo ' + ($arguments -join ' ') + ' failed with exit code ' + $code)
}
}
$built = Join-Path $repoRoot 'target\release\md-preview.exe'
$running = @()
foreach ($process in (Get-Process -ErrorAction SilentlyContinue)) {
$path = $null
try { $path = $process.Path } catch { }
if ($path -eq $built) { $running += [string]$process.Id }
}
if ($running.Count -gt 0) {
throw ('开发版 MD Preview 正在运行(PID ' + ($running -join ', ') + '),请先关闭。')
}
if (-not $SkipTests) {
Step 'cargo test'
Invoke-Cargo @('test')
}
Step 'cargo build --release'
Invoke-Cargo @('build', '--release')
if (-not (Test-Path $built)) { throw ('missing build output: ' + $built) }
$info = Get-Item $built
$hash = (Get-FileHash $built -Algorithm SHA256).Hash
Step 'check removed update strings'
$bytes = [IO.File]::ReadAllBytes($built)
$ascii = [Text.Encoding]::ASCII.GetString($bytes)
# Guard the removed updater. 'powershell' used to be on this list, but that word
# can legitimately show up in user-facing text; the updater itself needs these.
$forbidden = @('api.github', 'btn-update', 'Check for Updates', 'Start-Process', 'Invoke-WebRequest', 'update-check-result', 'open-url')
$bad = @()
foreach ($needle in $forbidden) {
$count = ([regex]::Matches($ascii, [regex]::Escape($needle), 'IgnoreCase')).Count
Write-Host (' {0,-20} {1}' -f $needle, $count)
if ($count -ne 0) { $bad += $needle }
}
if ($bad.Count -gt 0) { throw ('removed features still present: ' + ($bad -join ', ')) }
Step 'report'
$acl = (icacls $built | Out-String)
$integrity = 'Unknown'
if ($acl -match 'Mandatory Label\\(Low|Medium|High) Mandatory Level') { $integrity = $Matches[1] }
Write-Host ('output : ' + $built)
Write-Host ('size : {0:N0} bytes' -f $info.Length)
Write-Host ('sha256 : ' + $hash)
Write-Host ('version : ' + $info.VersionInfo.FileVersion)
Write-Host ('integrity : ' + $integrity + ' (workspace-write builds are Low)')
Write-Host ''
Write-Host 'DONE (build only). Publish: .\agent\scripts\publish.ps1 (normal PowerShell, Medium)' -ForegroundColor Green