chore: switch to Low workspace and Nutstore-only publish
- build.ps1: dev build only; no copy to a fixed exe - publish.ps1: user-run Medium publish to Nutstore - docs: workspace stays D:\Scripts\md-preview, repo under AgentLowWorking - .gitignore: local test/ scratch directory
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
#requires -Version 5.1
|
||||
<#
|
||||
MD Preview 发布构建脚本
|
||||
固定产物:D:\Scripts\md-preview\MD-Preview-windows-x64.exe
|
||||
|
||||
运行环境:普通 PowerShell(Medium),或 DSH 的 danger-full-access。
|
||||
workspace-write 是 Low 完整性级别,写不动本目录(EPERM / os error 5)。
|
||||
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(
|
||||
[string]$OutputPath = 'D:\Scripts\md-preview\MD-Preview-windows-x64.exe',
|
||||
[switch]$SkipTests
|
||||
)
|
||||
|
||||
@@ -16,7 +15,7 @@ $ErrorActionPreference = 'Stop'
|
||||
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
|
||||
Set-Location $repoRoot
|
||||
|
||||
# 工作区规则(agent/AGENTS.md):构建/测试的临时文件也留在 target\ 下,不污染 %TEMP%。
|
||||
# 工作区规则(agent/AGENTS.md):临时文件留在 target\ 下,不污染 %TEMP%。
|
||||
$tmpRoot = Join-Path $repoRoot 'target\tmp'
|
||||
New-Item -ItemType Directory -Force $tmpRoot | Out-Null
|
||||
$env:TEMP = $tmpRoot
|
||||
@@ -29,51 +28,49 @@ if (-not (Test-Path $cargo)) {
|
||||
|
||||
function Step([string]$text) { Write-Host ('==> ' + $text) -ForegroundColor Cyan }
|
||||
|
||||
function Assert-TargetNotRunning([string]$path) {
|
||||
$ids = @()
|
||||
foreach ($p in (Get-Process -ErrorAction SilentlyContinue)) {
|
||||
$pPath = $null
|
||||
try { $pPath = $p.Path } catch { }
|
||||
if ($pPath -eq $path) { $ids += $p.Id }
|
||||
function Invoke-Cargo([string[]]$arguments) {
|
||||
$previous = $ErrorActionPreference
|
||||
$code = 0
|
||||
try {
|
||||
# cargo 把进度/警告写到 stderr;Stop 会把它们当成终止错误
|
||||
$ErrorActionPreference = 'Continue'
|
||||
& $cargo @arguments
|
||||
$code = $LASTEXITCODE
|
||||
} finally {
|
||||
$ErrorActionPreference = $previous
|
||||
}
|
||||
if ($ids.Count -gt 0) {
|
||||
throw ('MD Preview 正在运行(PID ' + ($ids -join ', ') + '),请先关闭窗口再构建。')
|
||||
if ($code -ne 0) {
|
||||
throw ('cargo ' + ($arguments -join ' ') + ' failed with exit code ' + $code)
|
||||
}
|
||||
}
|
||||
|
||||
Assert-TargetNotRunning $OutputPath
|
||||
$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'
|
||||
& $cargo test
|
||||
if ($LASTEXITCODE -ne 0) { throw 'cargo test failed' }
|
||||
Invoke-Cargo @('test')
|
||||
}
|
||||
|
||||
Step 'cargo build --release'
|
||||
& $cargo build --release
|
||||
if ($LASTEXITCODE -ne 0) { throw 'cargo build failed' }
|
||||
Invoke-Cargo @('build', '--release')
|
||||
|
||||
$built = Join-Path $repoRoot 'target\release\md-preview.exe'
|
||||
if (-not (Test-Path $built)) { throw ('missing build output: ' + $built) }
|
||||
|
||||
Step ('copy -> ' + $OutputPath)
|
||||
$outDir = Split-Path -Parent $OutputPath
|
||||
if (-not (Test-Path $outDir)) { New-Item -ItemType Directory -Path $outDir | Out-Null }
|
||||
Assert-TargetNotRunning $OutputPath
|
||||
try {
|
||||
Copy-Item $built $OutputPath -Force -ErrorAction Stop
|
||||
} catch {
|
||||
throw ('复制失败(目标可能被占用):' + $_.Exception.Message)
|
||||
}
|
||||
|
||||
$info = Get-Item $OutputPath
|
||||
$hash = (Get-FileHash $OutputPath -Algorithm SHA256).Hash
|
||||
Write-Host ('size : {0:N0} bytes' -f $info.Length)
|
||||
Write-Host ('sha256 : ' + $hash)
|
||||
Write-Host ('version : ' + $info.VersionInfo.FileVersion)
|
||||
$info = Get-Item $built
|
||||
$hash = (Get-FileHash $built -Algorithm SHA256).Hash
|
||||
|
||||
Step 'check removed update strings'
|
||||
$bytes = [IO.File]::ReadAllBytes($OutputPath)
|
||||
$bytes = [IO.File]::ReadAllBytes($built)
|
||||
$ascii = [Text.Encoding]::ASCII.GetString($bytes)
|
||||
$forbidden = @('api.github', 'btn-update', 'Check for Updates', 'powershell', 'update-check-result', 'open-url')
|
||||
$bad = @()
|
||||
@@ -84,13 +81,14 @@ foreach ($needle in $forbidden) {
|
||||
}
|
||||
if ($bad.Count -gt 0) { throw ('removed features still present: ' + ($bad -join ', ')) }
|
||||
|
||||
Step 'check integrity level'
|
||||
$acl = (icacls $OutputPath | Out-String)
|
||||
if ($acl -match 'Medium Mandatory Level') {
|
||||
Write-Host ' Medium - OK'
|
||||
} else {
|
||||
Write-Warning '不是 Medium:启动时可能报 os error 5,见 agent/docs/HANDOFF.md 第 4 节'
|
||||
}
|
||||
|
||||
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 ' + $OutputPath) -ForegroundColor Green
|
||||
Write-Host 'DONE (build only). Publish: .\agent\scripts\publish.ps1 (normal PowerShell, Medium)' -ForegroundColor Green
|
||||
|
||||
114
agent/scripts/publish.ps1
Normal file
114
agent/scripts/publish.ps1
Normal file
@@ -0,0 +1,114 @@
|
||||
#requires -Version 5.1
|
||||
<#
|
||||
MD Preview 发布(必须由用户在自己的普通 PowerShell / Medium 运行)
|
||||
- 源:target\release\md-preview.exe(Low)
|
||||
- 目标:C:\Users\wucl1\Nutstore\1\Scripts\MD-Preview-windows-x64.exe(Medium)
|
||||
- 校验 SHA256 + Medium,刷新 .md 关联,最后删除旧 exe
|
||||
- 不要从 DSH 的 workspace-write shell 运行
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$Source = '',
|
||||
[string]$NutstorePath = 'C:\Users\wucl1\Nutstore\1\Scripts\MD-Preview-windows-x64.exe',
|
||||
[string]$OldExe = 'D:\Scripts\md-preview\MD-Preview-windows-x64.exe',
|
||||
[switch]$KeepOldExe
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
|
||||
Set-Location $repoRoot
|
||||
if (-not $Source) { $Source = Join-Path $repoRoot 'target\release\md-preview.exe' }
|
||||
|
||||
function Step([string]$text) { Write-Host ('==> ' + $text) -ForegroundColor Cyan }
|
||||
|
||||
function Assert-Medium {
|
||||
$groups = (whoami /groups | Out-String)
|
||||
if ($groups -match 'Low Mandatory Level') {
|
||||
throw '当前是 Low 完整性(DSH 沙箱)。请在自己的普通 PowerShell(Medium)里运行本脚本。'
|
||||
}
|
||||
}
|
||||
|
||||
function Assert-NotRunning([string[]]$paths) {
|
||||
$running = @()
|
||||
foreach ($process in (Get-Process -ErrorAction SilentlyContinue)) {
|
||||
$path = $null
|
||||
try { $path = $process.Path } catch { }
|
||||
if ($path -and ($paths -contains $path)) {
|
||||
$running += ($process.ProcessName + ' (PID ' + $process.Id + '): ' + $path)
|
||||
}
|
||||
}
|
||||
if ($running.Count -gt 0) {
|
||||
throw ('请先关闭 MD Preview:' + [Environment]::NewLine + ($running -join [Environment]::NewLine))
|
||||
}
|
||||
}
|
||||
|
||||
function Get-Integrity([string]$path) {
|
||||
$out = (icacls $path | Out-String)
|
||||
if ($out -match 'Mandatory Label\\(Low|Medium|High) Mandatory Level') { return $Matches[1] }
|
||||
return 'Unknown'
|
||||
}
|
||||
|
||||
function Set-RegistryDefault([string]$path, [string]$value) {
|
||||
if (-not (Test-Path $path)) { New-Item -Path $path -Force | Out-Null }
|
||||
Set-ItemProperty -Path $path -Name '(default)' -Value $value
|
||||
}
|
||||
|
||||
Assert-Medium
|
||||
if (-not (Test-Path -LiteralPath $Source)) {
|
||||
throw ('找不到构建产物:' + $Source + ';先运行 agent\scripts\build.ps1')
|
||||
}
|
||||
|
||||
Assert-NotRunning @($Source, $NutstorePath, $OldExe)
|
||||
|
||||
$sourceHash = (Get-FileHash -LiteralPath $Source -Algorithm SHA256).Hash
|
||||
|
||||
Step ('copy -> ' + $NutstorePath)
|
||||
$nutstoreDir = Split-Path -Parent $NutstorePath
|
||||
if (-not (Test-Path $nutstoreDir)) { New-Item -ItemType Directory -Path $nutstoreDir -Force | Out-Null }
|
||||
Copy-Item -LiteralPath $Source -Destination $NutstorePath -Force -ErrorAction Stop
|
||||
|
||||
Step 'set Nutstore exe integrity -> Medium'
|
||||
$previous = $ErrorActionPreference
|
||||
$ErrorActionPreference = 'Continue'
|
||||
icacls $NutstorePath /setintegritylevel Medium | Out-Null
|
||||
$icaclsCode = $LASTEXITCODE
|
||||
$ErrorActionPreference = $previous
|
||||
if ($icaclsCode -ne 0) { throw ('icacls failed with exit code ' + $icaclsCode) }
|
||||
|
||||
$integrity = Get-Integrity $NutstorePath
|
||||
if ($integrity -ne 'Medium') { throw ('完整性级别不是 Medium:' + $integrity) }
|
||||
|
||||
$targetHash = (Get-FileHash -LiteralPath $NutstorePath -Algorithm SHA256).Hash
|
||||
if ($sourceHash -ne $targetHash) {
|
||||
throw ('SHA256 不一致:source=' + $sourceHash + ' target=' + $targetHash)
|
||||
}
|
||||
|
||||
Step 'refresh .md association -> Nutstore'
|
||||
$progid = 'MDPreview.md'
|
||||
$command = '"' + $NutstorePath + '" "%1"'
|
||||
Set-RegistryDefault ('HKCU:\Software\Classes\' + $progid + '\shell\open\command') $command
|
||||
Set-RegistryDefault ('HKCU:\Software\Classes\' + $progid + '\DefaultIcon') ('"' + $NutstorePath + '",0')
|
||||
Set-RegistryDefault ('HKCU:\Software\Classes\Applications\MD-Preview-windows-x64.exe\shell\open\command') $command
|
||||
|
||||
$actual = (Get-ItemProperty ('HKCU:\Software\Classes\' + $progid + '\shell\open\command')).'(default)'
|
||||
if ($actual -ne $command) { throw ('关联写入失败:' + $actual) }
|
||||
|
||||
$userChoiceKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.md\UserChoiceLatest\ProgId'
|
||||
$userChoice = $null
|
||||
if (Test-Path $userChoiceKey) { $userChoice = (Get-ItemProperty $userChoiceKey).ProgId }
|
||||
if ($userChoice -ne $progid) {
|
||||
Write-Warning ('.md 的 UserChoice ProgId 是 ' + $userChoice + ',不是 ' + $progid + ';请在 设置 -> 默认应用 里选择一次 MD Preview。')
|
||||
}
|
||||
|
||||
if (-not $KeepOldExe -and (Test-Path -LiteralPath $OldExe)) {
|
||||
Step ('delete old exe -> ' + $OldExe)
|
||||
Remove-Item -LiteralPath $OldExe -Force -ErrorAction Stop
|
||||
}
|
||||
|
||||
Write-Host ''
|
||||
Write-Host ('size : {0:N0} bytes' -f (Get-Item -LiteralPath $NutstorePath).Length)
|
||||
Write-Host ('sha256 : ' + $targetHash)
|
||||
Write-Host ('integrity : ' + $integrity)
|
||||
Write-Host ('default : ' + $actual)
|
||||
Write-Host ('old exe : ' + $OldExe)
|
||||
Write-Host 'DONE' -ForegroundColor Green
|
||||
Reference in New Issue
Block a user