From 180c3fd2752ec983632792d4ca1907847753dbf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=85=8E=E9=A5=BC=E6=9E=9C=E5=AD=90=E5=8D=B7=E9=B2=A8?= =?UTF-8?q?=E9=B1=BC=E8=BE=A3=E6=A4=92?= Date: Sun, 15 Dec 2024 02:00:31 +0800 Subject: [PATCH] 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. --- scripts/install.ps1 | 118 +++++++++++++++++++++++++++++++++++++++----- scripts/install.sh | 64 ++++++++++++++++++++---- 2 files changed, 159 insertions(+), 23 deletions(-) diff --git a/scripts/install.ps1 b/scripts/install.ps1 index b7101ed..cd4d5dd 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -1,7 +1,8 @@ # Auto-elevate to admin rights if not already running as admin if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Host "Requesting administrator privileges..." - Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs + $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -ExecutionFromElevated" + Start-Process powershell.exe -ArgumentList $arguments -Verb RunAs Exit } @@ -30,7 +31,7 @@ $EN_MESSAGES = @( "Adding to PATH...", "Cleaning up...", "Installation completed successfully!", - "You can now use 'cursor-id-modifier' from any terminal (you may need to restart your terminal first)", + "You can now use 'cursor-id-modifier' directly", "Checking for running Cursor instances...", "Found running Cursor processes. Attempting to close them...", "Successfully closed all Cursor instances", @@ -53,7 +54,7 @@ $CN_MESSAGES = @( "正在添加到PATH...", "正在清理...", "安装成功完成!", - "现在可以在任何终端中使用 'cursor-id-modifier' 了(可能需要重启终端)", + "现在可以直接使用 'cursor-id-modifier' 了", "正在检查运行中的Cursor进程...", "发现正在运行的Cursor进程,尝试关闭...", "成功关闭所有Cursor实例", @@ -133,6 +134,94 @@ function Get-LatestVersion { return $release.tag_name } +# 在文件开头添加日志函数 +function Write-Log { + param( + [string]$Message, + [string]$Level = "INFO" + ) + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $logMessage = "[$timestamp] [$Level] $Message" + $logFile = "$env:TEMP\cursor-id-modifier-install.log" + Add-Content -Path $logFile -Value $logMessage + + # 同时输出到控制台 + switch ($Level) { + "ERROR" { Write-Error $Message } + "WARNING" { Write-Warning $Message } + "SUCCESS" { Write-Success $Message } + default { Write-Status $Message } + } +} + +# 添加安装前检查函数 +function Test-Prerequisites { + Write-Log "Checking prerequisites..." "INFO" + + # 检查PowerShell版本 + if ($PSVersionTable.PSVersion.Major -lt 5) { + Write-Log "PowerShell 5.0 or higher is required" "ERROR" + return $false + } + + # 检查网络连接 + try { + $testConnection = Test-Connection -ComputerName "github.com" -Count 1 -Quiet + if (-not $testConnection) { + Write-Log "No internet connection available" "ERROR" + return $false + } + } catch { + Write-Log "Failed to check internet connection: $_" "ERROR" + return $false + } + + return $true +} + +# 添加文件验证函数 +function Test-FileHash { + param( + [string]$FilePath, + [string]$ExpectedHash + ) + + $actualHash = Get-FileHash -Path $FilePath -Algorithm SHA256 + return $actualHash.Hash -eq $ExpectedHash +} + +# 修改下载函数,添加进度条 +function Download-File { + param( + [string]$Url, + [string]$OutFile + ) + + try { + $webClient = New-Object System.Net.WebClient + $webClient.Headers.Add("User-Agent", "PowerShell Script") + + $webClient.DownloadFileAsync($Url, $OutFile) + + while ($webClient.IsBusy) { + Write-Progress -Activity "Downloading..." -Status "Progress:" -PercentComplete -1 + Start-Sleep -Milliseconds 100 + } + + Write-Progress -Activity "Downloading..." -Completed + return $true + } + catch { + Write-Log "Download failed: $_" "ERROR" + return $false + } + finally { + if ($webClient) { + $webClient.Dispose() + } + } +} + # Main installation process / 主安装过程 Write-Status (Get-Message 0) @@ -156,7 +245,8 @@ Write-Status "$(Get-Message 3) $version" # Set up paths / 设置路径 $installDir = "$env:ProgramFiles\cursor-id-modifier" -$binaryName = "cursor_id_modifier_${version}_windows_amd64.exe" +$versionWithoutV = $version.TrimStart('v') # 移除版本号前面的 'v' 字符 +$binaryName = "cursor_id_modifier_${versionWithoutV}_windows_amd64.exe" $downloadUrl = "https://github.com/yuaotian/go-cursor-help/releases/download/$version/$binaryName" $tempFile = "$env:TEMP\$binaryName" @@ -169,7 +259,9 @@ if (-not (Test-Path $installDir)) { # Download binary / 下载二进制文件 Write-Status "$(Get-Message 5) $downloadUrl" try { - Invoke-WebRequest -Uri $downloadUrl -OutFile $tempFile + if (-not (Download-File -Url $downloadUrl -OutFile $tempFile)) { + Write-Error "$(Get-Message 6)" + } } catch { Write-Error "$(Get-Message 6) $_" } @@ -198,13 +290,6 @@ if ($userPath -notlike "*$installDir*") { ) } -# Create shortcut in Start Menu / 在开始菜单创建快捷方式 -$startMenuPath = "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\cursor-id-modifier.lnk" -$shell = New-Object -ComObject WScript.Shell -$shortcut = $shell.CreateShortcut($startMenuPath) -$shortcut.TargetPath = "$installDir\cursor-id-modifier.exe" -$shortcut.Save() - # Cleanup / 清理 Write-Status (Get-Message 11) if (Test-Path $tempFile) { @@ -213,4 +298,11 @@ if (Test-Path $tempFile) { Write-Success (Get-Message 12) Write-Success (Get-Message 13) -Write-Host "" \ No newline at end of file +Write-Host "" + +# 直接运行程序 +try { + Start-Process "$installDir\cursor-id-modifier.exe" -NoNewWindow +} catch { + Write-Warning "Failed to start cursor-id-modifier: $_" +} \ No newline at end of file diff --git a/scripts/install.sh b/scripts/install.sh index ae2ff6c..7bec268 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -177,12 +177,22 @@ get_binary_name() { esac } -# Install the binary / 安装二进制文件 +# Add download progress display function +download_with_progress() { + local url="$1" + local output_file="$2" + + curl -L -f --progress-bar "$url" -o "$output_file" + return $? +} + +# Optimize installation function install_binary() { OS=$(detect_os) - BINARY_NAME=$(get_binary_name) - REPO="yuaotian/go-cursor-help" VERSION=$(get_latest_version) + VERSION_WITHOUT_V=${VERSION#v} # Remove 'v' from version number + BINARY_NAME="cursor_id_modifier_${VERSION_WITHOUT_V}_${OS}_$(get_arch)" + REPO="yuaotian/go-cursor-help" DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${BINARY_NAME}" TMP_DIR=$(mktemp -d) FINAL_BINARY_NAME="cursor-id-modifier" @@ -190,30 +200,61 @@ install_binary() { print_status "$(get_message 2)" print_status "$(get_message 3) ${DOWNLOAD_URL}" - if ! curl -L -f "$DOWNLOAD_URL" -o "$TMP_DIR/$BINARY_NAME"; then + if ! download_with_progress "$DOWNLOAD_URL" "$TMP_DIR/$BINARY_NAME"; then + rm -rf "$TMP_DIR" print_error "$(get_message 8) $DOWNLOAD_URL" fi if [ ! -f "$TMP_DIR/$BINARY_NAME" ]; then + rm -rf "$TMP_DIR" print_error "$(get_message 9)" fi print_status "$(get_message 4)" INSTALL_DIR="/usr/local/bin" - # Create directory if it doesn't exist / 如果目录不存在则创建 + # Create directory if it doesn't exist mkdir -p "$INSTALL_DIR" - # Move binary to installation directory / 移动二进制文件到安装目录 - mv "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$FINAL_BINARY_NAME" - chmod +x "$INSTALL_DIR/$FINAL_BINARY_NAME" + # Move binary to installation directory + if ! mv "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$FINAL_BINARY_NAME"; then + rm -rf "$TMP_DIR" + print_error "Failed to move binary to installation directory" + fi + + if ! chmod +x "$INSTALL_DIR/$FINAL_BINARY_NAME"; then + rm -rf "$TMP_DIR" + print_error "Failed to set executable permissions" + fi - # Cleanup / 清理 + # Cleanup print_status "$(get_message 5)" rm -rf "$TMP_DIR" print_success "$(get_message 6)" printf "${GREEN}[✓]${NC} $(get_message 7)\n" "$FINAL_BINARY_NAME" + + # Try to run the program directly + if [ -x "$INSTALL_DIR/$FINAL_BINARY_NAME" ]; then + "$INSTALL_DIR/$FINAL_BINARY_NAME" & + else + print_warning "Failed to start cursor-id-modifier" + fi +} + +# Optimize architecture detection function +get_arch() { + case "$(uname -m)" in + x86_64) + echo "amd64" + ;; + aarch64|arm64) + echo "arm64" + ;; + *) + print_error "$(get_message 13) $(uname -m)" + ;; + esac } # Check for required tools / 检查必需工具 @@ -234,6 +275,9 @@ main() { # Check root privileges / 检查root权限 check_root "$@" + # Check required tools / 检查必需工具 + check_requirements + # Close Cursor instances / 关闭Cursor实例 close_cursor_instances @@ -243,7 +287,7 @@ main() { OS=$(detect_os) print_status "$(get_message 1) $OS" - check_requirements + # Install the binary / 安装二进制文件 install_binary }