support force post install

This commit is contained in:
2026-05-16 23:01:12 -04:00
parent 4b92de1b38
commit e9aa6024ff
3 changed files with 46 additions and 13 deletions

View File

@@ -19,6 +19,7 @@ if (-not (Test-Path $jsonPath)) {
Write-Host "Reading configurations..." -ForegroundColor Cyan
# 使用 UTF8 读取防止中文乱码
$apps = Get-Content $jsonPath -Encoding UTF8 | ConvertFrom-Json
$forcePostInstall = $false
# === 2.1 加载外部开关配置 (selection.ini) ===
# 尝试查找根目录下的 selection.ini (位于 bin 的上一级)
@@ -27,21 +28,44 @@ $selectionPath = Join-Path (Split-Path $PSScriptRoot -Parent) "selection.ini"
if (Test-Path $selectionPath) {
Write-Host "Loading selection overrides from selection.ini..." -ForegroundColor Cyan
try {
# 读取所有行,过滤掉注释和空行
$iniLines = Get-Content $selectionPath -Encoding UTF8 | Where-Object { $_ -match "=" -and $_ -notmatch "^;" -and $_ -notmatch "^\[" }
# 创建一个哈希表来存储 INI 配置
# 创建哈希表分别存储 App 开关和全局选项
$selectionConfig = @{}
foreach ($line in $iniLines) {
$optionsConfig = @{}
$currentSection = ""
foreach ($line in (Get-Content $selectionPath -Encoding UTF8)) {
$line = $line.Trim()
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith(";") -or $line.StartsWith("#")) {
continue
}
if ($line -match '^\[(.+)\]$') {
$currentSection = $matches[1].Trim()
continue
}
if ($line -notmatch "=") {
continue
}
# 按第一个等号分割,限制分割次数为 2
$parts = $line -split '=', 2
if ($parts.Count -eq 2) {
$key = $parts[0].Trim()
$value = $parts[1].Trim()
$selectionConfig[$key] = $value
if ($currentSection -eq "Options") {
$optionsConfig[$key] = $value
} else {
$selectionConfig[$key] = $value
}
}
}
if ($optionsConfig.ContainsKey("ForcePostInstall")) {
$forcePostInstall = $optionsConfig["ForcePostInstall"] -eq "1"
}
# 应用配置
foreach ($app in $apps) {
if ($selectionConfig.ContainsKey($app.Name)) {
@@ -153,8 +177,12 @@ foreach ($app in $apps) {
if ($exitCode -eq 0) {
Write-Host "[Success]" -ForegroundColor Green
} elseif ($exitCode -eq -1978335189) {
Write-Host "[Skip] Already installed" -ForegroundColor Yellow
continue # 已经安装的为了避免覆盖配置,也就不配置了
if ($forcePostInstall) {
Write-Host "[Skip] Already installed, running PostInstall..." -ForegroundColor Yellow
} else {
Write-Host "[Skip] Already installed" -ForegroundColor Yellow
continue # 已经安装的为了避免覆盖配置,也就不配置了
}
} else {
Write-Error "[Fail] Error code: $exitCode"
continue # 安装失败则跳过后续配置