File size: 2,435 Bytes
6d60378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<#
.SYNOPSIS
  WorkBuddy2API 引导脚本(Windows)。

.DESCRIPTION
  只做一件事:**没有二进制就编译,然后把控制权交给 wb2api-launcher.exe**。

  真正的启动逻辑(先同步凭证 → 再拉起网关 → 后台定时对账 → 转发中断信号)
  只有一份,在 cmd/launcher 里,不在脚本里重复一遍——两套实现迟早会漂移。

  不想依赖 Go?直接从发布页下载 `wb2api-launcher-windows-amd64.exe` 与
  `wb2api.exe` 放进同一目录,把启动器重命名为 wb2api-launcher.exe,然后双击即可。
  本脚本检测到二进制已存在时会自动跳过编译。

.EXAMPLE
  .\start.ps1                  # 同步 + 启动网关 + 后台对账
  .\start.ps1 -status          # 只看两边差异,不启动网关
  .\start.ps1 -no-sync         # 离线启动
  .\start.ps1 -h               # 看启动器全部选项
#>
[CmdletBinding()]
param(
    # 其余参数原样透传给 wb2api-launcher.exe
    [Parameter(ValueFromRemainingArguments = $true)]
    [string[]]$Args
)

$ErrorActionPreference = 'Stop'
$root = if ($PSScriptRoot) { $PSScriptRoot } else { (Get-Location).Path }
Set-Location $root

# Windows 控制台默认代码页会把中文写成乱码(尤其在重定向日志时),显式切 UTF-8。
try {
    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
    $OutputEncoding = [System.Text.Encoding]::UTF8
} catch {
    # 某些宿主不允许改编码,忽略即可,不影响功能。
}

$serverBin = Join-Path $root 'wb2api.exe'
$launcherBin = Join-Path $root 'wb2api-launcher.exe'

function Need-Go([string]$what) {
    if (-not (Get-Command go -ErrorAction SilentlyContinue)) {
        throw "找不到 go,无法编译 $what。请安装 Go,或直接下载预编译二进制放到 $root"
    }
}

if (-not (Test-Path $serverBin)) {
    Need-Go 'wb2api'
    Write-Host '[start] 编译网关 wb2api.exe ...' -ForegroundColor Cyan
    & go build -o $serverBin ./cmd/server
    if ($LASTEXITCODE -ne 0) { throw '编译 wb2api 失败' }
}

if (-not (Test-Path $launcherBin)) {
    Need-Go 'wb2api-launcher'
    Write-Host '[start] 编译启动器 wb2api-launcher.exe ...' -ForegroundColor Cyan
    & go build -o $launcherBin ./cmd/launcher
    if ($LASTEXITCODE -ne 0) { throw '编译 wb2api-launcher 失败' }
}

# 把控制权完全交给启动器;它的退出码原样透出。
& $launcherBin @Args
exit $LASTEXITCODE