-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.ps1
More file actions
438 lines (377 loc) · 15.8 KB
/
dev.ps1
File metadata and controls
438 lines (377 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
param(
[Parameter(Position=0)]
[string]$Choice = ""
)
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$ScriptPath = $MyInvocation.MyCommand.Definition
function Ensure-Admin {
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "[INFO] 需要管理员权限,正在请求提升..."
# 提权窗口中用 & 执行脚本,完成后 pause 防止窗口闪退
$cmd = "& '$ScriptPath' -Choice '$Choice'; Write-Host ''; Write-Host '按任意键关闭窗口...'; `$null = `$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')"
Start-Process powershell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"$cmd`""
exit 0
}
}
# ============ 正式版操作 ============
function Do-BuildRelease {
& "$ScriptDir\build_all.ps1" -WailsMode release
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-BuildDebug {
& "$ScriptDir\build_all.ps1" -WailsMode debug
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-Install {
& "$ScriptDir\installer\install.ps1"
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-Uninstall {
& "$ScriptDir\installer\uninstall.ps1"
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-BuildInstaller {
& "$ScriptDir\installer\build_nsis.ps1"
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-BuildInstallerSkip {
& "$ScriptDir\installer\build_nsis.ps1" -SkipBuild
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-BuildModule {
param([string[]]$Modules)
& "$ScriptDir\build_all.ps1" -Module $Modules -WailsMode release
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-DeployModule {
param([string[]]$Modules)
& "$ScriptDir\installer\install.ps1" -Module $Modules
if ($LASTEXITCODE -ne 0) { exit 1 }
}
# ============ 调试版操作 ============
function Do-BuildReleaseDebugVariant {
& "$ScriptDir\build_all.ps1" -WailsMode release -DebugVariant
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-BuildDebugDebugVariant {
& "$ScriptDir\build_all.ps1" -WailsMode debug -DebugVariant
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-InstallDebugVariant {
& "$ScriptDir\installer\install.ps1" -DebugVariant
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-UninstallDebugVariant {
& "$ScriptDir\installer\uninstall.ps1" -DebugVariant
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-BuildModuleDebugVariant {
param([string[]]$Modules)
& "$ScriptDir\build_all.ps1" -Module $Modules -WailsMode release -DebugVariant
if ($LASTEXITCODE -ne 0) { exit 1 }
}
function Do-DeployModuleDebugVariant {
param([string[]]$Modules)
& "$ScriptDir\installer\install.ps1" -Module $Modules -DebugVariant
if ($LASTEXITCODE -ne 0) { exit 1 }
}
# ============ 便携模式部署 ============
$PortableDir = "D:\Software\WindInputPortable"
$PortableDebugDir = "D:\Software\WindInputPortableDebug"
function Portable-ReplaceFile {
param([string]$TargetDir, [string]$FileName, [string]$SrcPath)
$dstPath = Join-Path $TargetDir $FileName
if (-not (Test-Path $dstPath)) {
Copy-Item -Path $SrcPath -Destination $dstPath -Force
Write-Host " - $FileName"
return
}
try {
Copy-Item -Path $SrcPath -Destination $dstPath -Force -ErrorAction Stop
Write-Host " - $FileName"
return
} catch { }
$randomSuffix = Get-Random -Maximum 99999999
$oldName = "${FileName}.old_${randomSuffix}"
try {
Rename-Item -Path $dstPath -NewName $oldName -Force -ErrorAction Stop
Copy-Item -Path $SrcPath -Destination $dstPath -Force
Write-Host " - $FileName (旧文件已重命名为 $oldName)"
} catch {
Write-Host " [错误] 无法替换 ${FileName}: $_" -ForegroundColor Red
}
}
function Do-PortableDeploy {
param([string]$TargetDir, [string]$BuildDir, [string]$PortableExe, [string]$ServiceExe, [string]$SettingExe)
Write-Host "======================================"
Write-Host "便携模式部署"
Write-Host "目标目录: $TargetDir"
Write-Host "======================================"
Write-Host ""
$portableProcName = [System.IO.Path]::GetFileNameWithoutExtension($PortableExe)
$serviceProcName = [System.IO.Path]::GetFileNameWithoutExtension($ServiceExe)
$settingProcName = [System.IO.Path]::GetFileNameWithoutExtension($SettingExe)
# 通过启动器优雅停止服务(注销 + 关闭)
Write-Host "[1/5] 停止旧服务..."
$oldPortable = Join-Path $TargetDir $PortableExe
if (Test-Path $oldPortable) {
Write-Host " - 通过启动器停止服务..."
& $oldPortable -stop 2>&1 | ForEach-Object { Write-Host " $_" }
Start-Sleep -Milliseconds 500
}
# 强制清理残留进程
Write-Host "[2/5] 清理残留进程..."
Get-Process -Name $settingProcName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Get-Process -Name $portableProcName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Get-Process -Name $serviceProcName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds 500
# 创建目录
Write-Host "[3/5] 准备目录..."
if (-not (Test-Path $TargetDir)) { New-Item -ItemType Directory -Path $TargetDir -Force | Out-Null }
# 复制文件(处理被占用的 DLL/EXE)
Write-Host "[4/5] 复制文件..."
foreach ($name in @($PortableExe, $ServiceExe, $SettingExe)) {
$src = Join-Path $BuildDir $name
if (Test-Path $src) {
Portable-ReplaceFile -TargetDir $TargetDir -FileName $name -SrcPath $src
} else {
Write-Host " [警告] 未找到 $name" -ForegroundColor Yellow
}
}
foreach ($dll in (Get-ChildItem -Path $BuildDir -Filter "wind_tsf*.dll" -ErrorAction SilentlyContinue)) {
Portable-ReplaceFile -TargetDir $TargetDir -FileName $dll.Name -SrcPath $dll.FullName
}
# 复制 data 目录
$dataDir = Join-Path $BuildDir "data"
if (Test-Path $dataDir) {
$targetDataDir = Join-Path $TargetDir "data"
if (Test-Path $targetDataDir) { Remove-Item -Path $targetDataDir -Recurse -Force -ErrorAction SilentlyContinue }
Copy-Item -Path $dataDir -Destination $TargetDir -Recurse -Force
Write-Host " - data\ (词库、方案、主题)"
}
# 清理旧备份文件
Get-ChildItem -Path $TargetDir -Filter "*.old_*" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
# 启动便携启动器
Write-Host "[5/5] 启动便携启动器..."
$newPortable = Join-Path $TargetDir $PortableExe
if (Test-Path $newPortable) {
Start-Process -FilePath $newPortable
Write-Host " - 已启动 $PortableExe"
}
Write-Host ""
Write-Host "======================================"
Write-Host "便携模式部署完成!"
Write-Host "目录: $TargetDir"
Write-Host "======================================"
}
function Do-PortableDeployModule {
param(
[string]$TargetDir,
[string]$BuildDir,
[string]$PortableExe,
[string]$ServiceExe,
[string]$SettingExe,
[string[]]$Modules
)
Write-Host "======================================"
Write-Host "便携模式 - 模块部署"
Write-Host "目标目录: $TargetDir"
Write-Host "模块: $($Modules -join ', ')"
Write-Host "======================================"
Write-Host ""
$portableProcName = [System.IO.Path]::GetFileNameWithoutExtension($PortableExe)
$serviceProcName = [System.IO.Path]::GetFileNameWithoutExtension($ServiceExe)
$settingProcName = [System.IO.Path]::GetFileNameWithoutExtension($SettingExe)
# 通过启动器优雅停止服务
Write-Host "[1/4] 停止旧服务..."
$oldPortable = Join-Path $TargetDir $PortableExe
if (Test-Path $oldPortable) {
& $oldPortable -stop 2>&1 | ForEach-Object { Write-Host " $_" }
Start-Sleep -Milliseconds 500
}
# 清理相关进程
Write-Host "[2/4] 清理残留进程..."
if ($Modules -contains "setting") { Get-Process -Name $settingProcName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue }
if ($Modules -contains "portable") { Get-Process -Name $portableProcName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue }
if ($Modules -contains "service" -or $Modules -contains "dll") {
Get-Process -Name $serviceProcName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
}
Start-Sleep -Milliseconds 500
# 复制模块文件
Write-Host "[3/4] 复制模块文件..."
foreach ($mod in $Modules) {
switch ($mod) {
"dll" {
foreach ($dll in (Get-ChildItem -Path $BuildDir -Filter "wind_tsf*.dll" -ErrorAction SilentlyContinue)) {
Portable-ReplaceFile -TargetDir $TargetDir -FileName $dll.Name -SrcPath $dll.FullName
}
}
"service" {
$src = Join-Path $BuildDir $ServiceExe
if (Test-Path $src) { Portable-ReplaceFile -TargetDir $TargetDir -FileName $ServiceExe -SrcPath $src }
else { Write-Host " [警告] 未找到 $ServiceExe" -ForegroundColor Yellow }
}
"setting" {
$src = Join-Path $BuildDir $SettingExe
if (Test-Path $src) { Portable-ReplaceFile -TargetDir $TargetDir -FileName $SettingExe -SrcPath $src }
else { Write-Host " [警告] 未找到 $SettingExe" -ForegroundColor Yellow }
}
"portable" {
$src = Join-Path $BuildDir $PortableExe
if (Test-Path $src) { Portable-ReplaceFile -TargetDir $TargetDir -FileName $PortableExe -SrcPath $src }
else { Write-Host " [警告] 未找到 $PortableExe" -ForegroundColor Yellow }
}
}
}
# 清理旧备份 + 启动
Get-ChildItem -Path $TargetDir -Filter "*.old_*" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
Write-Host "[4/4] 启动便携启动器..."
$newPortable = Join-Path $TargetDir $PortableExe
if (Test-Path $newPortable) {
Start-Process -FilePath $newPortable
Write-Host " - 已启动 $PortableExe"
}
Write-Host ""
Write-Host "======================================"
Write-Host "便携模式模块部署完成!"
Write-Host "目录: $TargetDir"
Write-Host "======================================"
}
# ============ 交互式菜单 ============
if (-not $Choice) {
Write-Host "======================================"
Write-Host "WindInput - Dev Menu"
Write-Host "======================================"
Write-Host ""
Write-Host " --- 基本操作 ---"
Write-Host " 1 卸载 + 构建(Release) + 安装"
Write-Host " 1d 卸载 + 构建(Debug) + 安装"
Write-Host " 1s 卸载 + 安装(跳过构建)"
Write-Host " 2 仅构建(Release)"
Write-Host " 2d 仅构建(Debug)"
Write-Host " 3 仅安装"
Write-Host " 4 仅卸载"
Write-Host " 8 生成安装包"
Write-Host " 8s 生成安装包(跳过编译)"
Write-Host ""
Write-Host " --- 模块快速部署 (1=DLL 2=服务 3=设置 4=便携) ---"
Write-Host " m[N] 构建 + 部署 (如: m1, m12, m1234)"
Write-Host ""
Write-Host " --- 便携模式 ---"
Write-Host " p 构建 + 全量部署 -> $PortableDir"
Write-Host " pm[N] 构建 + 模块部署 (如: pm2, pm12)"
Write-Host ""
Write-Host " 前缀 d = 调试版 (如: d1, d2d, dm12, dp, dpm12)"
Write-Host " 调试版便携目录: $PortableDebugDir"
Write-Host ""
$Choice = Read-Host "请选择"
if (-not $Choice) { exit 1 }
}
# ============ 命令解析与执行 ============
$cmd = $Choice.Trim().ToLower()
$isDebugVariant = $false
$isPortable = $false
# 解析调试版前缀 'd'
if ($cmd.Length -gt 1 -and $cmd[0] -eq 'd') {
$isDebugVariant = $true
$cmd = $cmd.Substring(1)
}
# 解析便携前缀 'p'
if ($cmd.Length -ge 1 -and $cmd[0] -eq 'p') {
$isPortable = $true
$cmd = $cmd.Substring(1)
}
# 便携模式辅助变量
if ($isPortable) {
if ($isDebugVariant) {
$pTargetDir = $PortableDebugDir
$pBuildDir = Join-Path $ScriptDir "build_debug"
$pPortableExe = "wind_portable.exe"
$pServiceExe = "wind_input_debug.exe"
$pSettingExe = "wind_setting_debug.exe"
} else {
$pTargetDir = $PortableDir
$pBuildDir = Join-Path $ScriptDir "build"
$pPortableExe = "wind_portable.exe"
$pServiceExe = "wind_input.exe"
$pSettingExe = "wind_setting.exe"
}
}
# 解析模块数字辅助函数
function Parse-Modules([string]$Digits) {
$mods = @()
if ($Digits -match '1') { $mods += "dll" }
if ($Digits -match '2') { $mods += "service" }
if ($Digits -match '3') { $mods += "setting" }
if ($Digits -match '4') { $mods += "portable" }
return ,$mods
}
# ---- 路由 ----
if ($isPortable -and $cmd -eq '') {
# p / dp: 便携全量部署
if ($isDebugVariant) { Do-BuildReleaseDebugVariant } else { Do-BuildRelease }
Do-PortableDeploy -TargetDir $pTargetDir -BuildDir $pBuildDir `
-PortableExe $pPortableExe -ServiceExe $pServiceExe -SettingExe $pSettingExe
} elseif ($isPortable -and $cmd -match '^m([1234]+)$') {
# pm[N] / dpm[N]: 便携模块部署
$modules = Parse-Modules $Matches[1]
if ($isDebugVariant) { Do-BuildModuleDebugVariant -Modules $modules } else { Do-BuildModule -Modules $modules }
Do-PortableDeployModule -TargetDir $pTargetDir -BuildDir $pBuildDir `
-PortableExe $pPortableExe -ServiceExe $pServiceExe -SettingExe $pSettingExe -Modules $modules
} elseif ($isPortable) {
Write-Host "[ERROR] 便携模式仅支持: p, pm[1234] (如: p, pm12, dpm123)" -ForegroundColor Red
exit 1
} elseif ($cmd -match '^m([1234]+)$') {
# m[N] / dm[N]: 系统模块部署
Ensure-Admin
$modules = Parse-Modules $Matches[1]
if ($isDebugVariant) {
Do-BuildModuleDebugVariant -Modules $modules
Do-DeployModuleDebugVariant -Modules $modules
} else {
Do-BuildModule -Modules $modules
Do-DeployModule -Modules $modules
}
} elseif ($cmd -match '^1([ds])?$') {
# 1 / 1d / 1s: 卸载 + [构建] + 安装
Ensure-Admin
$suffix = $Matches[1]
if ($isDebugVariant) {
Do-UninstallDebugVariant
if ($suffix -eq 'd') { Do-BuildDebugDebugVariant }
elseif ($suffix -ne 's') { Do-BuildReleaseDebugVariant }
Do-InstallDebugVariant
} else {
Do-Uninstall
if ($suffix -eq 'd') { Do-BuildDebug }
elseif ($suffix -ne 's') { Do-BuildRelease }
Do-Install
}
} elseif ($cmd -match '^2(d)?$') {
# 2 / 2d: 仅构建
if ($Matches[1] -eq 'd') {
if ($isDebugVariant) { Do-BuildDebugDebugVariant } else { Do-BuildDebug }
} else {
if ($isDebugVariant) { Do-BuildReleaseDebugVariant } else { Do-BuildRelease }
}
} elseif ($cmd -eq '3') {
# 3 / d3: 仅安装
Ensure-Admin
if ($isDebugVariant) { Do-InstallDebugVariant } else { Do-Install }
} elseif ($cmd -eq '4') {
# 4 / d4: 仅卸载
Ensure-Admin
if ($isDebugVariant) { Do-UninstallDebugVariant } else { Do-Uninstall }
} elseif ($cmd -match '^8(s)?$') {
# 8 / 8s: 生成安装包
if ($Matches[1] -eq 's') { Do-BuildInstallerSkip } else { Do-BuildInstaller }
} else {
Write-Host "[ERROR] 无效选项: $Choice" -ForegroundColor Red
Write-Host ""
Write-Host "格式: [d]<操作>"
Write-Host " 操作: 1[d|s], 2[d], 3, 4, 8[s], m[1234], p, pm[1234]"
Write-Host " 前缀 d = 调试版"
exit 1
}