clearCursor.ps1 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # Check for admin rights and handle elevation
  2. $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
  3. if (-NOT $isAdmin) {
  4. # Detect PowerShell version and path
  5. $pwshPath = if (Get-Command "pwsh" -ErrorAction SilentlyContinue) {
  6. (Get-Command "pwsh").Source # PowerShell 7+
  7. } elseif (Test-Path "$env:ProgramFiles\PowerShell\7\pwsh.exe") {
  8. "$env:ProgramFiles\PowerShell\7\pwsh.exe"
  9. } else {
  10. "powershell.exe" # Windows PowerShell
  11. }
  12. try {
  13. Write-Host "`nRequesting administrator privileges..." -ForegroundColor Cyan
  14. $scriptPath = $MyInvocation.MyCommand.Path
  15. $argList = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
  16. Start-Process -FilePath $pwshPath -Verb RunAs -ArgumentList $argList -Wait
  17. exit
  18. }
  19. catch {
  20. Write-Host "`nError: Administrator privileges required" -ForegroundColor Red
  21. Write-Host "Please run this script from an Administrator PowerShell window" -ForegroundColor Yellow
  22. Write-Host "`nTo do this:" -ForegroundColor Cyan
  23. Write-Host "1. Press Win + X" -ForegroundColor White
  24. Write-Host "2. Click 'Windows Terminal (Admin)' or 'PowerShell (Admin)'" -ForegroundColor White
  25. Write-Host "3. Run the installation command again" -ForegroundColor White
  26. Write-Host "`nPress enter to exit..."
  27. $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  28. exit 1
  29. }
  30. }
  31. # Set TLS to 1.2
  32. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  33. # Create temporary directory
  34. $TmpDir = Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString())
  35. New-Item -ItemType Directory -Path $TmpDir | Out-Null
  36. # Cleanup function
  37. function Cleanup {
  38. if (Test-Path $TmpDir) {
  39. Remove-Item -Recurse -Force $TmpDir
  40. }
  41. }
  42. # Error handler
  43. trap {
  44. Write-Host "Error: $_" -ForegroundColor Red
  45. Cleanup
  46. Write-Host "Press enter to exit..."
  47. $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  48. exit 1
  49. }
  50. # Detect system architecture
  51. function Get-SystemArch {
  52. if ([Environment]::Is64BitOperatingSystem) {
  53. return "x86_64"
  54. } else {
  55. return "i386"
  56. }
  57. }
  58. # Download with progress
  59. function Get-FileWithProgress {
  60. param (
  61. [string]$Url,
  62. [string]$OutputFile
  63. )
  64. try {
  65. $webClient = New-Object System.Net.WebClient
  66. $webClient.Headers.Add("User-Agent", "PowerShell Script")
  67. $webClient.DownloadFile($Url, $OutputFile)
  68. return $true
  69. }
  70. catch {
  71. Write-Host "Failed to download: $_" -ForegroundColor Red
  72. return $false
  73. }
  74. }
  75. # Main installation function
  76. function Install-CursorModifier {
  77. Write-Host "Starting installation..." -ForegroundColor Cyan
  78. # Detect architecture
  79. $arch = Get-SystemArch
  80. Write-Host "Detected architecture: $arch" -ForegroundColor Green
  81. # Set installation directory
  82. $InstallDir = "$env:ProgramFiles\CursorModifier"
  83. if (!(Test-Path $InstallDir)) {
  84. New-Item -ItemType Directory -Path $InstallDir | Out-Null
  85. }
  86. # Get latest release
  87. try {
  88. $latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/yuaotian/go-cursor-help/releases/latest"
  89. Write-Host "Found latest release: $($latestRelease.tag_name)" -ForegroundColor Cyan
  90. # Look for Windows binary with our architecture
  91. $version = $latestRelease.tag_name.TrimStart('v')
  92. Write-Host "Version: $version" -ForegroundColor Cyan
  93. $possibleNames = @(
  94. "cursor-id-modifier_${version}_windows_x86_64.exe",
  95. "cursor-id-modifier_${version}_windows_$($arch).exe"
  96. )
  97. $asset = $null
  98. foreach ($name in $possibleNames) {
  99. Write-Host "Checking for asset: $name" -ForegroundColor Cyan
  100. $asset = $latestRelease.assets | Where-Object { $_.name -eq $name }
  101. if ($asset) {
  102. Write-Host "Found matching asset: $($asset.name)" -ForegroundColor Green
  103. break
  104. }
  105. }
  106. if (!$asset) {
  107. Write-Host "`nAvailable assets:" -ForegroundColor Yellow
  108. $latestRelease.assets | ForEach-Object { Write-Host "- $($_.name)" }
  109. throw "Could not find appropriate Windows binary for $arch architecture"
  110. }
  111. $downloadUrl = $asset.browser_download_url
  112. }
  113. catch {
  114. Write-Host "Failed to get latest release: $_" -ForegroundColor Red
  115. exit 1
  116. }
  117. # Download binary
  118. Write-Host "`nDownloading latest release..." -ForegroundColor Cyan
  119. $binaryPath = Join-Path $TmpDir "cursor-id-modifier.exe"
  120. if (!(Get-FileWithProgress -Url $downloadUrl -OutputFile $binaryPath)) {
  121. exit 1
  122. }
  123. # Install binary
  124. Write-Host "Installing..." -ForegroundColor Cyan
  125. try {
  126. Copy-Item -Path $binaryPath -Destination "$InstallDir\cursor-id-modifier.exe" -Force
  127. # Add to PATH if not already present
  128. $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
  129. if ($currentPath -notlike "*$InstallDir*") {
  130. [Environment]::SetEnvironmentVariable("Path", "$currentPath;$InstallDir", "Machine")
  131. }
  132. }
  133. catch {
  134. Write-Host "Failed to install: $_" -ForegroundColor Red
  135. exit 1
  136. }
  137. Write-Host "Installation completed successfully!" -ForegroundColor Green
  138. Write-Host "Running cursor-id-modifier..." -ForegroundColor Cyan
  139. # Run the program
  140. try {
  141. & "$InstallDir\cursor-id-modifier.exe"
  142. if ($LASTEXITCODE -ne 0) {
  143. Write-Host "Failed to run cursor-id-modifier" -ForegroundColor Red
  144. exit 1
  145. }
  146. }
  147. catch {
  148. Write-Host "Failed to run cursor-id-modifier: $_" -ForegroundColor Red
  149. exit 1
  150. }
  151. }
  152. # Run installation
  153. try {
  154. Install-CursorModifier
  155. }
  156. catch {
  157. Write-Host "Installation failed: $_" -ForegroundColor Red
  158. Cleanup
  159. Write-Host "Press enter to exit..."
  160. $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  161. exit 1
  162. }
  163. finally {
  164. Cleanup
  165. if ($LASTEXITCODE -ne 0) {
  166. Write-Host "Press enter to exit..." -ForegroundColor Green
  167. $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  168. }
  169. }