Skip to content

Commit 180c3fd

Browse files
committed
feat: Enhance installation scripts with logging, progress display, and prerequisite checks
- Added logging functionality to track installation progress and errors in both PowerShell and Bash scripts. - Implemented a download progress display for better user experience during file downloads. - Introduced prerequisite checks to ensure the required PowerShell version and internet connectivity before installation. - Modified the installation process to run the installed program directly after completion. - Updated messages to clarify usage instructions for the 'cursor-id-modifier' tool. These changes improve the robustness and user-friendliness of the installation process across platforms.
1 parent 3316f8b commit 180c3fd

File tree

2 files changed

+159
-23
lines changed

2 files changed

+159
-23
lines changed

scripts/install.ps1

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Auto-elevate to admin rights if not already running as admin
22
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
33
Write-Host "Requesting administrator privileges..."
4-
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
4+
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -ExecutionFromElevated"
5+
Start-Process powershell.exe -ArgumentList $arguments -Verb RunAs
56
Exit
67
}
78

@@ -30,7 +31,7 @@ $EN_MESSAGES = @(
3031
"Adding to PATH...",
3132
"Cleaning up...",
3233
"Installation completed successfully!",
33-
"You can now use 'cursor-id-modifier' from any terminal (you may need to restart your terminal first)",
34+
"You can now use 'cursor-id-modifier' directly",
3435
"Checking for running Cursor instances...",
3536
"Found running Cursor processes. Attempting to close them...",
3637
"Successfully closed all Cursor instances",
@@ -53,7 +54,7 @@ $CN_MESSAGES = @(
5354
"正在添加到PATH...",
5455
"正在清理...",
5556
"安装成功完成!",
56-
"现在可以在任何终端中使用 'cursor-id-modifier' 了(可能需要重启终端)",
57+
"现在可以直接使用 'cursor-id-modifier' 了",
5758
"正在检查运行中的Cursor进程...",
5859
"发现正在运行的Cursor进程,尝试关闭...",
5960
"成功关闭所有Cursor实例",
@@ -133,6 +134,94 @@ function Get-LatestVersion {
133134
return $release.tag_name
134135
}
135136

137+
# 在文件开头添加日志函数
138+
function Write-Log {
139+
param(
140+
[string]$Message,
141+
[string]$Level = "INFO"
142+
)
143+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
144+
$logMessage = "[$timestamp] [$Level] $Message"
145+
$logFile = "$env:TEMP\cursor-id-modifier-install.log"
146+
Add-Content -Path $logFile -Value $logMessage
147+
148+
# 同时输出到控制台
149+
switch ($Level) {
150+
"ERROR" { Write-Error $Message }
151+
"WARNING" { Write-Warning $Message }
152+
"SUCCESS" { Write-Success $Message }
153+
default { Write-Status $Message }
154+
}
155+
}
156+
157+
# 添加安装前检查函数
158+
function Test-Prerequisites {
159+
Write-Log "Checking prerequisites..." "INFO"
160+
161+
# 检查PowerShell版本
162+
if ($PSVersionTable.PSVersion.Major -lt 5) {
163+
Write-Log "PowerShell 5.0 or higher is required" "ERROR"
164+
return $false
165+
}
166+
167+
# 检查网络连接
168+
try {
169+
$testConnection = Test-Connection -ComputerName "github.com" -Count 1 -Quiet
170+
if (-not $testConnection) {
171+
Write-Log "No internet connection available" "ERROR"
172+
return $false
173+
}
174+
} catch {
175+
Write-Log "Failed to check internet connection: $_" "ERROR"
176+
return $false
177+
}
178+
179+
return $true
180+
}
181+
182+
# 添加文件验证函数
183+
function Test-FileHash {
184+
param(
185+
[string]$FilePath,
186+
[string]$ExpectedHash
187+
)
188+
189+
$actualHash = Get-FileHash -Path $FilePath -Algorithm SHA256
190+
return $actualHash.Hash -eq $ExpectedHash
191+
}
192+
193+
# 修改下载函数,添加进度条
194+
function Download-File {
195+
param(
196+
[string]$Url,
197+
[string]$OutFile
198+
)
199+
200+
try {
201+
$webClient = New-Object System.Net.WebClient
202+
$webClient.Headers.Add("User-Agent", "PowerShell Script")
203+
204+
$webClient.DownloadFileAsync($Url, $OutFile)
205+
206+
while ($webClient.IsBusy) {
207+
Write-Progress -Activity "Downloading..." -Status "Progress:" -PercentComplete -1
208+
Start-Sleep -Milliseconds 100
209+
}
210+
211+
Write-Progress -Activity "Downloading..." -Completed
212+
return $true
213+
}
214+
catch {
215+
Write-Log "Download failed: $_" "ERROR"
216+
return $false
217+
}
218+
finally {
219+
if ($webClient) {
220+
$webClient.Dispose()
221+
}
222+
}
223+
}
224+
136225
# Main installation process / 主安装过程
137226
Write-Status (Get-Message 0)
138227

@@ -156,7 +245,8 @@ Write-Status "$(Get-Message 3) $version"
156245

157246
# Set up paths / 设置路径
158247
$installDir = "$env:ProgramFiles\cursor-id-modifier"
159-
$binaryName = "cursor_id_modifier_${version}_windows_amd64.exe"
248+
$versionWithoutV = $version.TrimStart('v') # 移除版本号前面的 'v' 字符
249+
$binaryName = "cursor_id_modifier_${versionWithoutV}_windows_amd64.exe"
160250
$downloadUrl = "https://github.com/yuaotian/go-cursor-help/releases/download/$version/$binaryName"
161251
$tempFile = "$env:TEMP\$binaryName"
162252

@@ -169,7 +259,9 @@ if (-not (Test-Path $installDir)) {
169259
# Download binary / 下载二进制文件
170260
Write-Status "$(Get-Message 5) $downloadUrl"
171261
try {
172-
Invoke-WebRequest -Uri $downloadUrl -OutFile $tempFile
262+
if (-not (Download-File -Url $downloadUrl -OutFile $tempFile)) {
263+
Write-Error "$(Get-Message 6)"
264+
}
173265
} catch {
174266
Write-Error "$(Get-Message 6) $_"
175267
}
@@ -198,13 +290,6 @@ if ($userPath -notlike "*$installDir*") {
198290
)
199291
}
200292

201-
# Create shortcut in Start Menu / 在开始菜单创建快捷方式
202-
$startMenuPath = "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\cursor-id-modifier.lnk"
203-
$shell = New-Object -ComObject WScript.Shell
204-
$shortcut = $shell.CreateShortcut($startMenuPath)
205-
$shortcut.TargetPath = "$installDir\cursor-id-modifier.exe"
206-
$shortcut.Save()
207-
208293
# Cleanup / 清理
209294
Write-Status (Get-Message 11)
210295
if (Test-Path $tempFile) {
@@ -213,4 +298,11 @@ if (Test-Path $tempFile) {
213298

214299
Write-Success (Get-Message 12)
215300
Write-Success (Get-Message 13)
216-
Write-Host ""
301+
Write-Host ""
302+
303+
# 直接运行程序
304+
try {
305+
Start-Process "$installDir\cursor-id-modifier.exe" -NoNewWindow
306+
} catch {
307+
Write-Warning "Failed to start cursor-id-modifier: $_"
308+
}

scripts/install.sh

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,43 +177,84 @@ get_binary_name() {
177177
esac
178178
}
179179

180-
# Install the binary / 安装二进制文件
180+
# Add download progress display function
181+
download_with_progress() {
182+
local url="$1"
183+
local output_file="$2"
184+
185+
curl -L -f --progress-bar "$url" -o "$output_file"
186+
return $?
187+
}
188+
189+
# Optimize installation function
181190
install_binary() {
182191
OS=$(detect_os)
183-
BINARY_NAME=$(get_binary_name)
184-
REPO="yuaotian/go-cursor-help"
185192
VERSION=$(get_latest_version)
193+
VERSION_WITHOUT_V=${VERSION#v} # Remove 'v' from version number
194+
BINARY_NAME="cursor_id_modifier_${VERSION_WITHOUT_V}_${OS}_$(get_arch)"
195+
REPO="yuaotian/go-cursor-help"
186196
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${BINARY_NAME}"
187197
TMP_DIR=$(mktemp -d)
188198
FINAL_BINARY_NAME="cursor-id-modifier"
189199

190200
print_status "$(get_message 2)"
191201
print_status "$(get_message 3) ${DOWNLOAD_URL}"
192202

193-
if ! curl -L -f "$DOWNLOAD_URL" -o "$TMP_DIR/$BINARY_NAME"; then
203+
if ! download_with_progress "$DOWNLOAD_URL" "$TMP_DIR/$BINARY_NAME"; then
204+
rm -rf "$TMP_DIR"
194205
print_error "$(get_message 8) $DOWNLOAD_URL"
195206
fi
196207

197208
if [ ! -f "$TMP_DIR/$BINARY_NAME" ]; then
209+
rm -rf "$TMP_DIR"
198210
print_error "$(get_message 9)"
199211
fi
200212

201213
print_status "$(get_message 4)"
202214
INSTALL_DIR="/usr/local/bin"
203215

204-
# Create directory if it doesn't exist / 如果目录不存在则创建
216+
# Create directory if it doesn't exist
205217
mkdir -p "$INSTALL_DIR"
206218

207-
# Move binary to installation directory / 移动二进制文件到安装目录
208-
mv "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$FINAL_BINARY_NAME"
209-
chmod +x "$INSTALL_DIR/$FINAL_BINARY_NAME"
219+
# Move binary to installation directory
220+
if ! mv "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$FINAL_BINARY_NAME"; then
221+
rm -rf "$TMP_DIR"
222+
print_error "Failed to move binary to installation directory"
223+
fi
224+
225+
if ! chmod +x "$INSTALL_DIR/$FINAL_BINARY_NAME"; then
226+
rm -rf "$TMP_DIR"
227+
print_error "Failed to set executable permissions"
228+
fi
210229

211-
# Cleanup / 清理
230+
# Cleanup
212231
print_status "$(get_message 5)"
213232
rm -rf "$TMP_DIR"
214233

215234
print_success "$(get_message 6)"
216235
printf "${GREEN}[✓]${NC} $(get_message 7)\n" "$FINAL_BINARY_NAME"
236+
237+
# Try to run the program directly
238+
if [ -x "$INSTALL_DIR/$FINAL_BINARY_NAME" ]; then
239+
"$INSTALL_DIR/$FINAL_BINARY_NAME" &
240+
else
241+
print_warning "Failed to start cursor-id-modifier"
242+
fi
243+
}
244+
245+
# Optimize architecture detection function
246+
get_arch() {
247+
case "$(uname -m)" in
248+
x86_64)
249+
echo "amd64"
250+
;;
251+
aarch64|arm64)
252+
echo "arm64"
253+
;;
254+
*)
255+
print_error "$(get_message 13) $(uname -m)"
256+
;;
257+
esac
217258
}
218259

219260
# Check for required tools / 检查必需工具
@@ -234,6 +275,9 @@ main() {
234275
# Check root privileges / 检查root权限
235276
check_root "$@"
236277

278+
# Check required tools / 检查必需工具
279+
check_requirements
280+
237281
# Close Cursor instances / 关闭Cursor实例
238282
close_cursor_instances
239283

@@ -243,7 +287,7 @@ main() {
243287
OS=$(detect_os)
244288
print_status "$(get_message 1) $OS"
245289

246-
check_requirements
290+
# Install the binary / 安装二进制文件
247291
install_binary
248292
}
249293

0 commit comments

Comments
 (0)