33 lines
3.0 KiB
PowerShell
33 lines
3.0 KiB
PowerShell
param([switch]$Apply)
|
|
$ErrorActionPreference='Stop'
|
|
$projectRoot=Split-Path -Parent $PSScriptRoot
|
|
$stageRoot=[IO.Path]::GetFullPath((Join-Path $projectRoot 'integration/kefu'))
|
|
$sourceRoot=[IO.Path]::GetFullPath('C:\kefu')
|
|
$manifest=Get-Content -LiteralPath (Join-Path $stageRoot 'source-manifest.json') -Raw | ConvertFrom-Json
|
|
if($manifest.version -ne 1 -or $manifest.source_root -ne $sourceRoot){throw '客服部署清单来源不符'}
|
|
$plans=@()
|
|
foreach($item in $manifest.files){
|
|
$source=[IO.Path]::GetFullPath((Join-Path $sourceRoot $item.path))
|
|
$staged=[IO.Path]::GetFullPath((Join-Path $stageRoot $item.path))
|
|
if(-not $source.StartsWith($sourceRoot+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase) -or -not $staged.StartsWith($stageRoot+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase)){throw '客服清单路径越界'}
|
|
if([IO.Path]::GetExtension($source) -notin @('.py','.ts','.vue','.mjs','.yaml','.md')){throw '客服清单存在非源码或说明文件'}
|
|
if((Get-FileHash -LiteralPath $staged).Hash.ToLowerInvariant() -ne $item.staged_sha256){throw ('暂存源码已变化: '+$item.path)}
|
|
if((Test-Path -LiteralPath $source) -and (Get-FileHash -LiteralPath $source).Hash.ToLowerInvariant() -eq $item.staged_sha256){continue}
|
|
if($null -eq $item.source_sha256){if(Test-Path -LiteralPath $source){throw ('目标新文件已存在: '+$item.path)}}
|
|
elseif(-not(Test-Path -LiteralPath $source) -or (Get-FileHash -LiteralPath $source).Hash.ToLowerInvariant() -ne $item.source_sha256){throw ('目标已有修改,拒绝覆盖: '+$item.path)}
|
|
$plans+=@{source=$source;staged=$staged;relative=$item.path;sha=$item.staged_sha256;existing=($null -ne $item.source_sha256)}
|
|
}
|
|
if(-not $Apply){Write-Output ('检查通过:'+$plans.Count+' 个待应用文件;其余已与交付清单一致,未写入客服项目。');return}
|
|
if(-not $plans.Count){Write-Output '客服源码已与交付清单一致,无需重复应用。';return}
|
|
$backupRoot=Join-Path $projectRoot ('.test-runtime/kefu-source-before-'+(Get-Date -Format 'yyyyMMdd-HHmmss'))
|
|
foreach($plan in $plans){
|
|
if($plan.existing){$backup=Join-Path $backupRoot $plan.relative;New-Item -ItemType Directory -Path (Split-Path -Parent $backup) -Force | Out-Null;Copy-Item -LiteralPath $plan.source -Destination $backup}
|
|
}
|
|
foreach($plan in $plans){
|
|
New-Item -ItemType Directory -Path (Split-Path -Parent $plan.source) -Force | Out-Null
|
|
Copy-Item -LiteralPath $plan.staged -Destination $plan.source
|
|
if((Get-FileHash -LiteralPath $plan.source).Hash.ToLowerInvariant() -ne $plan.sha){throw ('复制校验失败: '+$plan.relative)}
|
|
}
|
|
@{files=$plans.Count;backup=$backupRoot;source_root=$sourceRoot;applied_at=(Get-Date).ToString('o')} | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $projectRoot '.test-runtime/kefu-source-applied.json') -Encoding utf8
|
|
Write-Output ('客服源码已应用并校验:'+$plans.Count+' 个文件;原源码备份位于 '+$backupRoot)
|