清单格式:两种写法(软件名 / 手写目录)都支持 :+ 追加与 :- 排除;名录改用对象数组并逐条介绍目录
需求
- 支持两种条目写法:1) 直接写软件名录里的软件名;2) 用户手写目录。
- 两种写法都必须支持追加(:+)与排除(:-)。
- 软件名录要改进:scoop 合并成"一个软件 + 一个目录数组"。
- 运行时要把"分别是哪些目录、每个目录是干什么的、排除/追加的理由"讲清楚。
实现
- 名录(SoftwareCatalog.psd1 / Get-SoftwareCatalog)
* 一个软件挂多个目录时写成**对象数组**:@{ Path = '...'; Description = '...' };
也接受纯字符串数组与旧的 @{ Dirs = ... } / @{ Variants = ... }。
* 目录说明(Description)一路带到运行日志里。
* "声明了但当前不存在"的目录不再被丢掉:备份跳过,恢复仍然知道它该回到哪个位置。
* scoop 合并成一个数组条目(%UserProfile%\scoop\persist + %UserProfile%\.config\scoop);
ScoopApps-persist 保持独立条目 —— 它和前者末级名同为 persist,并进同一个归档会在包里撞名。
- 解析与解析结果(ConvertFrom-BackupListLine / Resolve-BackupEntry)
* `:+` 以前只对"软件名且能解析出目录"的写法生效,**手写目录的 :+ 会被整段丢掉**;
现在统一生效,且 :+ 后面写软件名会按名录展开。
* 行尾 `# 说明` 解析成 Comment,运行时打印。
- 归档与恢复
* 同一条目里两个同名目录:打包前明确报错(退出码 1),不再静默混成一棵树。
(7z 命令行没有"入库改名"的能力,归档内顶层名只能是文件系统上的那个名字。)
* 恢复时每个源只解出**它自己那棵子树**,不会再往别的父目录里复制兄弟目录。
- 可解释性
* 新增 Write-BackupEntryPlan:打包前打印条目的目录(含来源与介绍)以及排除/追加的出处;
Restore.ps1 同样打印"哪棵子树还原到哪、会新建还是覆盖"。
- 孤儿归档审计修正:判据只看当前清单,不再把 manifest 的历史记录当成"已知"。
否则"条目被合并/改名后留下的旧归档"会被历史记录遮住,永远不会报警。
验证
- Pester 84 项、零依赖单元 49 项、端到端 23 项,全部通过。
- 真实机器:合并后的 scoop.7z 233 MB / 29478 项 / 7z t 通过,manifest.roots=[persist|scoop];
恢复演练 26982/26982 逐字节一致(.ssh、legendary、Aria 同批通过)。
- 迁移提醒:scoop-config.7z 与 scoop-persist.7z 已无清单条目指向,会出现在孤儿审计里;
确认 scoop.7z 无误后可以自行删除。
This commit is contained in:
1 parent
e114cae8c8
commit
43fa4e52dd
10 files changed
+1083
-339
No files matched your search
+39
-8
@@ -158,7 +158,7 @@ function Get-7zExecutable {
|
||||
}
|
||||
|
||||
function Invoke-Extraction {
|
||||
param([object]$ArchiveFile, [string]$DestinationPath)
|
||||
param([object]$ArchiveFile, [string]$DestinationPath, [string]$RelativePath)
|
||||
|
||||
$extension = $ArchiveFile.Extension.ToLower()
|
||||
$destParent = Split-Path -Path $DestinationPath -Parent
|
||||
@@ -167,14 +167,17 @@ function Invoke-Extraction {
|
||||
New-Item -ItemType Directory -Path $destParent -Force | Out-Null
|
||||
}
|
||||
|
||||
# 归档布局与历史保持一致:根目录就是源目录名(软件名条目也一样,
|
||||
# 软件名只用于归档文件名),因此直接整包解压到目标的父目录即可。
|
||||
# 归档布局与历史保持一致:顶层就是**源目录名**(软件名只用于归档文件名)。
|
||||
# 一个条目可能打包了好几个目录(软件名录里的数组写法 / `:+` 追加),
|
||||
# 所以**不能整包往每个目标里倒** —— 那会把兄弟目录也复制到不相干的父目录下。
|
||||
# 这里只解出该目标自己那棵子树($RelativePath),其余不动。
|
||||
$sevenZip = Get-7zExecutable
|
||||
if ($sevenZip) {
|
||||
Write-Log '使用 7z 解压' -Level DEBUG
|
||||
$argument = @('x', '-bsp2', '-y', "-o$destParent")
|
||||
if ($password) { $argument += "-p$password" }
|
||||
$argument += $ArchiveFile.FullName
|
||||
if ($RelativePath) { $argument += $RelativePath }
|
||||
|
||||
$exitCode = Invoke-ExternalCommand -FilePath $sevenZip -ArgumentList $argument
|
||||
if ($exitCode -ne 0) { throw "7z 解压失败(退出码:$exitCode)" }
|
||||
@@ -187,18 +190,24 @@ function Invoke-Extraction {
|
||||
if (-not $rarExe) { throw '未找到 RAR 工具' }
|
||||
Write-Log '使用 RAR 解压' -Level DEBUG
|
||||
$argument = @('x', '-idp', '-idn', '-y', $ArchiveFile.FullName, "$destParent\")
|
||||
if ($RelativePath) { $argument += $RelativePath }
|
||||
$exitCode = Invoke-ExternalCommand -FilePath $rarExe -ArgumentList $argument
|
||||
if ($exitCode -ne 0) { throw "RAR 解压失败(退出码:$exitCode)" }
|
||||
}
|
||||
'.zip' {
|
||||
Write-Log '使用内置 ZIP 解压' -Level DEBUG
|
||||
if ($RelativePath) {
|
||||
Write-Log "内置 ZIP 不支持只解子树,将整包解压($RelativePath)" -Level WARN
|
||||
}
|
||||
Expand-Archive -LiteralPath $ArchiveFile.FullName -DestinationPath $destParent -Force
|
||||
}
|
||||
'.tar' {
|
||||
$tarExe = Get-Command tar -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source
|
||||
if (-not $tarExe) { throw '未找到 TAR 工具' }
|
||||
Write-Log '使用 TAR 解压' -Level DEBUG
|
||||
$exitCode = Invoke-ExternalCommand -FilePath $tarExe -ArgumentList @('-xf', $ArchiveFile.FullName, '-C', $destParent)
|
||||
$argument = @('-xf', $ArchiveFile.FullName, '-C', $destParent)
|
||||
if ($RelativePath) { $argument += $RelativePath }
|
||||
$exitCode = Invoke-ExternalCommand -FilePath $tarExe -ArgumentList $argument
|
||||
if ($exitCode -ne 0) { throw "TAR 解压失败(退出码:$exitCode)" }
|
||||
}
|
||||
default { throw "不支持的文件格式:$extension" }
|
||||
@@ -305,14 +314,26 @@ foreach ($line in $lines) {
|
||||
continue
|
||||
}
|
||||
|
||||
# 恢复目的地。清单条目可能带多个源(同名目录分散在多处),每个源各恢复各的。
|
||||
# 恢复目的地。一个条目可能带多个源(软件名录里的数组写法、`:+` 追加、
|
||||
# 或同名目录分散在多处),每个源只还原**它自己那棵子树**。
|
||||
$targets = @()
|
||||
if ($resolved.Sources.Count -gt 0) {
|
||||
foreach ($source in $resolved.Sources) {
|
||||
$targets += [pscustomobject]@{ DestPath = $source.SourcePath }
|
||||
$targets += [pscustomobject]@{
|
||||
DestPath = $source.SourcePath
|
||||
RelativePath = @($source.RelativePaths)[0]
|
||||
Description = $source.Description
|
||||
Origin = $source.Origin
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$targets += [pscustomobject]@{ DestPath = [Environment]::ExpandEnvironmentVariables($displayPath) }
|
||||
$expanded = [Environment]::ExpandEnvironmentVariables($displayPath)
|
||||
$targets += [pscustomobject]@{
|
||||
DestPath = $expanded
|
||||
RelativePath = (Split-Path -Path $expanded -Leaf)
|
||||
Description = $null
|
||||
Origin = 'path'
|
||||
}
|
||||
}
|
||||
|
||||
# 防御:解析不出目的地时明确失败,别把空字符串喂给 Split-Path/Test-Path
|
||||
@@ -388,6 +409,16 @@ foreach ($line in $lines) {
|
||||
}
|
||||
|
||||
$plannedTargets = @($targets | Where-Object { $_.DestPath })
|
||||
|
||||
# 说清楚"这条会把哪些目录还原到哪儿、为什么"
|
||||
Write-Log ("恢复计划:{0}(归档 {1})" -f $displayPath, $archiveFile.Name)
|
||||
foreach ($target in $plannedTargets) {
|
||||
$targetExists = Test-Path -LiteralPath $target.DestPath
|
||||
Write-Log (" 目标:{0}" -f $target.DestPath)
|
||||
Write-Log (" 归档内子树:{0};{1}" -f $target.RelativePath, $(if ($targetExists) { '已存在,将覆盖同名文件' } else { '不存在,将新建' }))
|
||||
if ($target.Description) { Write-Log (" 介绍:{0}" -f $target.Description) }
|
||||
}
|
||||
|
||||
foreach ($target in $plannedTargets) {
|
||||
if (Test-Path -LiteralPath $target.DestPath) { continue }
|
||||
# Split-Path -Parent 对根路径(如 "E:\")返回空串,此时无父目录可建
|
||||
@@ -415,7 +446,7 @@ foreach ($line in $lines) {
|
||||
$restoreFailed = $false
|
||||
try {
|
||||
foreach ($target in $plannedTargets) {
|
||||
if (-not (Invoke-Extraction -ArchiveFile $archiveFile -DestinationPath $target.DestPath)) {
|
||||
if (-not (Invoke-Extraction -ArchiveFile $archiveFile -DestinationPath $target.DestPath -RelativePath $target.RelativePath)) {
|
||||
$restoreFailed = $true
|
||||
break
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user