Skip to content

Commit dc90015

Browse files
committed
fix: 修复了 路径变量为空 的问题
1 parent 173baeb commit dc90015

1 file changed

Lines changed: 77 additions & 30 deletions

File tree

install.ps1

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,26 @@ $AUTO_CONFIRM = $yes
5050
$OPT_LANG = $lang
5151
$OPT_DRY_RUN = $dryrun
5252

53-
# 日志目录
54-
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
55-
$logDir = Join-Path $scriptRoot "logs"
56-
$backupDir = Join-Path $scriptRoot "backups"
53+
# 确定工作目录(兼容远程执行)
54+
function Get-WorkingDirectory {
55+
# 尝试获取脚本真实路径
56+
$scriptPath = $MyInvocation.MyCommand.Definition
57+
if (Test-Path -LiteralPath $scriptPath -PathType Leaf) {
58+
# 本地执行,使用脚本所在目录
59+
return Split-Path -Parent $scriptPath
60+
} else {
61+
# 远程执行,使用临时目录
62+
$tempDir = Join-Path $env:TEMP "devboost"
63+
if (-not (Test-Path $tempDir)) {
64+
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
65+
}
66+
return $tempDir
67+
}
68+
}
69+
70+
$DEVBOOST_ROOT = Get-WorkingDirectory
71+
$logDir = Join-Path $DEVBOOST_ROOT "logs"
72+
$backupDir = Join-Path $DEVBOOST_ROOT "backups"
5773
$logFile = Join-Path $logDir "devboost.log"
5874
$manifestFile = Join-Path $backupDir "manifest.txt"
5975

@@ -71,7 +87,7 @@ function Write-Log {
7187
else { Write-Host $line -ForegroundColor Green }
7288
}
7389

74-
# 多语言输出(用于单行纯文本)
90+
# 多语言输出
7591
function Write-I18n {
7692
param([string]$en, [string]$zh)
7793
if ($OPT_LANG -eq "zh") { Write-Host $zh }
@@ -110,20 +126,39 @@ function Confirm-Action {
110126
return ($answer -eq "y" -or $answer -eq "Y")
111127
}
112128

113-
# 备份文件
129+
# 备份文件(支持注册表路径)
114130
function Backup-File {
115131
param([string]$Path, [string]$Tag)
116132
if (-not (Test-Path $Path)) { return $null }
133+
117134
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
118-
$backupPath = Join-Path $backupDir "$(Split-Path $Path -Leaf)_${Tag}_$timestamp"
119-
Copy-Item -Path $Path -Destination $backupPath
135+
if ($Path -match '^registry::') {
136+
# 注册表项:导出为 .reg 文件
137+
$regPath = $Path -replace '^registry::', ''
138+
$backupPath = Join-Path $backupDir "$(($regPath -replace '\\', '_') -replace ':', '')_${Tag}_$timestamp.reg"
139+
if ($OPT_DRY_RUN) {
140+
Write-Log "INFO" "[DRY-RUN] Would export registry $regPath to $backupPath"
141+
} else {
142+
reg export "$regPath" "$backupPath" /y > $null 2>&1
143+
Write-Log "INFO" "Registry exported: $regPath -> $backupPath"
144+
}
145+
} else {
146+
# 普通文件
147+
$backupPath = Join-Path $backupDir "$(Split-Path $Path -Leaf)_${Tag}_$timestamp"
148+
if ($OPT_DRY_RUN) {
149+
Write-Log "INFO" "[DRY-RUN] Would copy $Path to $backupPath"
150+
} else {
151+
Copy-Item -Path $Path -Destination $backupPath
152+
Write-Log "INFO" "Backup created: $Path -> $backupPath"
153+
}
154+
}
155+
120156
$manifestLine = "$Path|$backupPath|$Tag|$timestamp"
121157
Add-Content -Path $manifestFile -Value $manifestLine
122-
Write-Log "INFO" "Backup created: $Path -> $backupPath"
123158
return $backupPath
124159
}
125160

126-
# 恢复文件
161+
# 恢复文件(简化,此处仅用于演示,实际需根据类型处理)
127162
function Restore-File {
128163
param([string]$OriginalPath)
129164
if (-not (Test-Path $manifestFile)) { return $false }
@@ -147,7 +182,12 @@ function Restore-File {
147182
$backupPath = $parts[1]
148183

149184
if (Test-Path $backupPath) {
150-
Copy-Item -Path $backupPath -Destination $OriginalPath -Force
185+
if ($OriginalPath -match '^registry::') {
186+
# 注册表项:导入 .reg 文件
187+
reg import "$backupPath" > $null 2>&1
188+
} else {
189+
Copy-Item -Path $backupPath -Destination $OriginalPath -Force
190+
}
151191
Write-Log "INFO" "Restored: $OriginalPath"
152192
return $true
153193
}
@@ -212,7 +252,10 @@ function Optimize-DNS {
212252
if ($OPT_DRY_RUN) {
213253
Write-Log "INFO" "[DRY-RUN] Would set DNS on $($adapter.Name) to $($servers -join ', ')"
214254
} else {
215-
Backup-File -Path "registry::HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$($adapter.InterfaceGuid)" -Tag "dns"
255+
# 备份注册表项
256+
$regPath = "registry::HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$($adapter.InterfaceGuid)"
257+
Backup-File -Path $regPath -Tag "dns"
258+
# 设置 DNS
216259
Set-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -ServerAddresses $servers
217260
Write-Log "INFO" "DNS set on $($adapter.Name) to $($servers -join ', ')"
218261
}
@@ -224,35 +267,35 @@ function Optimize-DevTools {
224267

225268
# npm
226269
if (Get-Command npm -ErrorAction SilentlyContinue) {
227-
Write-I18n "npm installed, current registry:" "npm 已安装,当前 registry" (npm config get registry)
270+
Write-Host "npm 已安装,当前 registry: $(npm config get registry)"
228271
if (Confirm-Action "Configure npm mirror?" "配置 npm 镜像?") {
229272
$registry = "https://registry.npmmirror.com"
230273
if ($OPT_DRY_RUN) {
231274
Write-Log "INFO" "[DRY-RUN] npm config set registry $registry"
232275
} else {
233276
npm config set registry $registry
234-
Write-Log "INFO" "npm registry set to $registry"
277+
Write-Log "INFO" "npm registry 已设置为 $registry"
235278
}
236279
}
237280
}
238281

239282
# pip
240283
if (Get-Command pip -ErrorAction SilentlyContinue) {
241-
Write-I18n "pip installed" "pip 已安装"
284+
Write-Host "pip 已安装"
242285
if (Confirm-Action "Configure pip mirror?" "配置 pip 镜像?") {
243286
$indexUrl = "https://pypi.tuna.tsinghua.edu.cn/simple"
244287
if ($OPT_DRY_RUN) {
245288
Write-Log "INFO" "[DRY-RUN] pip config set global.index-url $indexUrl"
246289
} else {
247290
pip config set global.index-url $indexUrl
248-
Write-Log "INFO" "pip index-url set to $indexUrl"
291+
Write-Log "INFO" "pip index-url 已设置为 $indexUrl"
249292
}
250293
}
251294
}
252295

253296
# Docker
254297
if (Get-Command docker -ErrorAction SilentlyContinue) {
255-
Write-I18n "Docker installed" "Docker 已安装"
298+
Write-Host "Docker 已安装"
256299
$daemonPath = "$env:ProgramData\Docker\config\daemon.json"
257300
if (Confirm-Action "Configure Docker mirror?" "配置 Docker 镜像加速器?") {
258301
$mirror = "https://docker.mirrors.ustc.edu.cn"
@@ -270,7 +313,7 @@ function Optimize-DevTools {
270313
$config.'registry-mirrors' += $mirror
271314
Backup-File -Path $daemonPath -Tag "docker"
272315
$config | ConvertTo-Json -Depth 10 | Set-Content $daemonPath
273-
Write-Log "INFO" "Docker mirror added: $mirror"
316+
Write-Log "INFO" "Docker 镜像加速器已添加: $mirror"
274317
Restart-Service docker
275318
}
276319
}
@@ -315,7 +358,7 @@ function Optimize-GitHubHosts {
315358
} else {
316359
Backup-File -Path $hostsPath -Tag "github"
317360
Add-Content -Path $hostsPath -Value "`r`n$entries"
318-
Write-Log "INFO" "GitHub hosts updated"
361+
Write-Log "INFO" "GitHub hosts 已更新"
319362
}
320363
}
321364

@@ -324,13 +367,13 @@ function Optimize-GitHubProxy {
324367
if (-not $proxy) { return }
325368
[Environment]::SetEnvironmentVariable("http_proxy", $proxy, "User")
326369
[Environment]::SetEnvironmentVariable("https_proxy", $proxy, "User")
327-
Write-Log "INFO" "Proxy environment variables set. Restart shell to take effect."
370+
Write-Log "INFO" "代理环境变量已设置,重新打开命令行生效。"
328371
}
329372

330373
function Invoke-Rollback {
331374
Write-Log "INFO" "===== 回滚 ====="
332375
if (-not (Test-Path $manifestFile)) {
333-
Write-Log "WARN" "No backup records found." "未找到备份记录。"
376+
Write-Log "WARN" "未找到备份记录。"
334377
return
335378
}
336379
$lines = Get-Content $manifestFile
@@ -340,19 +383,23 @@ function Invoke-Rollback {
340383
Write-Host "$i. $($parts[0]) -> $($parts[1]) ($($parts[2]))"
341384
$i++
342385
}
343-
Write-Host "0. Cancel" "0. 取消"
344-
$choice = Read-Host "Select number to rollback"
386+
Write-Host "0. 取消"
387+
$choice = Read-Host "选择要回滚的序号"
345388
if ($choice -eq "0") { return }
346389
if ($choice -match '^\d+$' -and $choice -le $lines.Count) {
347390
$selected = $lines[$choice-1]
348391
$parts = $selected -split '\|'
349392
$backupPath = $parts[1]
350393
$original = $parts[0]
351394
if (Test-Path $backupPath) {
352-
Copy-Item -Path $backupPath -Destination $original -Force
353-
Write-Log "INFO" "Restored: $original"
395+
if ($original -match '^registry::') {
396+
reg import "$backupPath" > $null 2>&1
397+
} else {
398+
Copy-Item -Path $backupPath -Destination $original -Force
399+
}
400+
Write-Log "INFO" "已恢复: $original"
354401
} else {
355-
Write-Log "ERROR" "Backup file missing: $backupPath"
402+
Write-Log "ERROR" "备份文件丢失: $backupPath"
356403
}
357404
} else {
358405
Write-I18n "Invalid choice." "无效选择。"
@@ -393,17 +440,17 @@ function Show-Menu {
393440
}
394441

395442
function Main {
396-
Write-Log "INFO" "========== devboost (Windows) started =========="
397-
Write-Log "INFO" "Log file: $logFile"
398-
Write-Log "INFO" "Backup directory: $backupDir"
443+
Write-Log "INFO" "========== devboost (Windows) 启动 =========="
444+
Write-Log "INFO" "日志文件: $logFile"
445+
Write-Log "INFO" "备份目录: $backupDir"
399446

400447
# 交互模式下询问语言(如果没有通过参数指定且为默认英文)
401448
if (-not $dns -and -not $devtools -and -not $github -and -not $rollback -and $OPT_LANG -eq "en") {
402449
Ask-Language
403450
}
404451

405452
$sysInfo = Get-SystemInfo
406-
Write-Log "INFO" "System: $($sysInfo.OSName) $($sysInfo.OSVersion), WSL=$($sysInfo.IsWSL)"
453+
Write-Log "INFO" "系统信息: $($sysInfo.OSName) $($sysInfo.OSVersion), WSL=$($sysInfo.IsWSL)"
407454

408455
if ($rollback) {
409456
Invoke-Rollback

0 commit comments

Comments
 (0)