Files
md-preview/agent/scripts/publish.ps1

115 lines
4.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
<#
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