Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
290 lines (229 loc) · 7.87 KB
/
Copy pathinstall.ps1
File metadata and controls
290 lines (229 loc) · 7.87 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
# claude-code-tool (cct) installer for Windows
# Usage: irm https://raw.githubusercontent.com/punisher1/claude-code-tool/main/install.ps1 | iex
# .\install.ps1 -Version 0.1.5 -Proxy http://127.0.0.1:11224
#Requires -Version 5.1
# TLS 1.2 兼容
# 参数
[CmdletBinding()]
param(
[string]$Version = "",
[string]$InstallDir = "",
[string]$Proxy = "",
[switch]$SkipChecksum,
[switch]$Help
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ── 常量 ──
$Repo = "punisher1/claude-code-tool"
$BinaryName = "cct.exe"
$GitHubApiBase = "https://api.github.com/repos/$Repo/releases"
$DefaultInstallDir = Join-Path $env:USERPROFILE ".local\bin"
$ArtifactName = "cct-Windows-x86_64.zip"
# ── 输出函数 ──
function Write-Info($message) {
Write-Host -ForegroundColor Green "[INFO] $message"
}
function Write-Warn($message) {
Write-Host -ForegroundColor Yellow "[WARN] $message"
}
function Write-Err($message) {
Write-Host -ForegroundColor Red "[ERROR] $message"
exit 1
}
function Write-Banner {
Write-Host "============================================"
Write-Host " claude-code-tool (cct) Installer"
Write-Host "============================================"
Write-Host ""
}
# ── 帮助 ──
function Show-Help {
$helpText = @(
"Usage: install.ps1 [options]"
""
"Options:"
" -Version STRING Install a specific version (e.g., 0.1.5)"
" -InstallDir STRING Override installation directory"
" -Proxy STRING Use HTTP proxy for downloads"
" -SkipChecksum Skip SHA256 checksum verification"
" -Help Show this help message"
""
"Examples:"
" .\install.ps1"
" .\install.ps1 -Version 0.1.5"
" .\install.ps1 -Proxy http://127.0.0.1:11224"
)
$helpText | ForEach-Object { Write-Host $_ }
exit 0
}
# ── 版本解析 ──
function Resolve-CctVersion {
if ($Version -ne "") {
$script:VersionTag = "v$(($Version -replace '^v',''))"
$apiUrl = "$GitHubApiBase/tags/$script:VersionTag"
} else {
$apiUrl = "$GitHubApiBase/latest"
}
Write-Info "Querying GitHub API for release information..."
$headers = @{ "User-Agent" = "cct-installer" }
try {
$proxyParam = @{}
if ($Proxy -ne "") {
$proxyParam = @{ Proxy = $Proxy }
}
$response = Invoke-RestMethod -Uri $apiUrl -Headers $headers @proxyParam -ErrorAction Stop
} catch {
$statusCode = 0
if ($_.Exception.Response) {
$statusCode = [int]$_.Exception.Response.StatusCode
}
if ($statusCode -eq 403) {
Write-Err "GitHub API rate limit exceeded. Try again later or use -Version to specify a tag."
} else {
Write-Err "Failed to fetch release info: $_"
}
}
$script:VersionTag = $response.tag_name
$script:DownloadBaseUrl = "https://github.com/$Repo/releases/download/$script:VersionTag"
Write-Info "Installing version: $script:VersionTag"
}
# ── 下载 ──
function Download-File {
param(
[string]$Url,
[string]$OutputPath
)
$proxyParam = @{}
if ($Proxy -ne "") {
$proxyParam = @{ Proxy = $Proxy }
}
try {
Invoke-WebRequest -Uri $Url -OutFile $OutputPath @proxyParam -ErrorAction Stop
} catch {
Write-Err "Failed to download ${Url}: $_"
}
}
function Download-Artifacts {
$script:TempDir = Join-Path $env:TEMP "cct-install-$(Get-Random)"
New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
$script:ArchivePath = Join-Path $script:TempDir $ArtifactName
$script:ChecksumPath = Join-Path $script:TempDir "$ArtifactName.sha256"
$archiveUrl = "$script:DownloadBaseUrl/$ArtifactName"
$checksumUrl = "$script:DownloadBaseUrl/$ArtifactName.sha256"
Write-Info "Downloading $ArtifactName..."
Download-File -Url $archiveUrl -OutputPath $script:ArchivePath
if (-not $SkipChecksum) {
Write-Info "Downloading checksum..."
Download-File -Url $checksumUrl -OutputPath $script:ChecksumPath
}
}
# ── 校验 ──
function Verify-Checksum {
if ($SkipChecksum) {
Write-Warn "Checksum verification skipped."
return
}
$expectedHash = (Get-Content $script:ChecksumPath -Raw).Trim()
# 处理两种格式:裸 hash 或 "hash filename"
$expectedHash = ($expectedHash -split '\s+')[0]
$actualHash = (Get-FileHash -Path $script:ArchivePath -Algorithm SHA256).Hash.ToLower()
$expectedHash = $expectedHash.ToLower()
if ($actualHash -ne $expectedHash) {
Write-Err "Checksum mismatch!`n Expected: $expectedHash`n Got: $actualHash`n The file may be corrupted."
}
Write-Info "Checksum verified."
}
# ── 安装目录 ──
function Resolve-InstallDir {
if ($InstallDir -eq "") {
$script:InstallDir = $DefaultInstallDir
} else {
$script:InstallDir = $InstallDir
}
if (-not (Test-Path $script:InstallDir)) {
New-Item -ItemType Directory -Path $script:InstallDir -Force | Out-Null
Write-Info "Created directory: $($script:InstallDir)"
}
}
# ── 安装二进制 ──
function Install-Binary {
Write-Info "Extracting archive..."
Expand-Archive -Path $script:ArchivePath -DestinationPath $script:TempDir -Force
$extractedBinary = Join-Path $script:TempDir $BinaryName
if (-not (Test-Path $extractedBinary)) {
Write-Err "Binary not found in archive: $extractedBinary"
}
$destPath = Join-Path $script:InstallDir $BinaryName
if (Test-Path $destPath) {
Write-Info "Replacing existing installation at $destPath"
}
Copy-Item -Path $extractedBinary -Destination $destPath -Force
Write-Info "Installed $BinaryName to $destPath"
}
# ── PATH 管理 ──
function Ensure-Path {
$installDir = $script:InstallDir
$pathParts = $env:PATH -split [System.IO.Path]::PathSeparator
if ($installDir -in $pathParts) {
Write-Info "$installDir is already in PATH."
return
}
# 添加到当前会话
$env:PATH = "$installDir$([System.IO.Path]::PathSeparator)$env:PATH"
# 持久化到用户环境变量
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User")
if ($null -eq $userPath) {
$userPath = ""
}
if ($userPath -notlike "*$installDir*") {
try {
[System.Environment]::SetEnvironmentVariable(
"PATH",
"$installDir$([System.IO.Path]::PathSeparator)$userPath",
"User"
)
Write-Info "Added $installDir to user PATH (persistent)."
} catch {
Write-Warn "Could not update user PATH automatically: $_"
Write-Warn "Add this directory to PATH manually: $installDir"
return
}
}
Write-Info "Added $installDir to PATH for this session."
}
# ── 安装后验证 ──
function Verify-Installation {
$cctPath = Join-Path $script:InstallDir $BinaryName
if (Test-Path $cctPath) {
try {
$ver = & $cctPath --version 2>$null
Write-Info "Verification: cct $ver"
} catch {
Write-Info "Verification: $cctPath exists."
}
} else {
Write-Warn "Installation verification failed."
}
}
# ── 清理 ──
function Cleanup {
if ($script:TempDir -and (Test-Path $script:TempDir)) {
Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
# ── 主流程 ──
if ($Help) { Show-Help }
try {
Write-Banner
Resolve-InstallDir
Resolve-CctVersion
Download-Artifacts
Verify-Checksum
Install-Binary
Ensure-Path
Verify-Installation
Write-Host ""
Write-Info "Installation complete! Run 'cct --help' to get started."
} finally {
Cleanup
}