|
| 1 | +param( |
| 2 | + [string]$Command = 'help' |
| 3 | +) |
| 4 | + |
| 5 | +$ProgramName = 'timesplit' |
| 6 | +$InstallDir = Join-Path -Path ${env:ProgramFiles} -ChildPath $ProgramName |
| 7 | +$InstallPath = Join-Path -Path $InstallDir -ChildPath "$ProgramName.exe" |
| 8 | +$TaskName = 'TimeSplit' |
| 9 | +$GhApi = 'https://api.github.com/repos/ImShyMike/timesplit/releases/latest' |
| 10 | + |
| 11 | +function Write-Info([string]$m){ Write-Host "→ $m" -ForegroundColor Yellow } |
| 12 | +function Write-Success([string]$m){ Write-Host "✓ $m" -ForegroundColor Green } |
| 13 | +function Write-Err([string]$m){ Write-Host "✗ $m" -ForegroundColor Red } |
| 14 | + |
| 15 | +function Ensure-Admin { |
| 16 | + $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) |
| 17 | + if (-not $isAdmin) { |
| 18 | + Write-Err "This script must be run as Administrator (open PowerShell as Admin)." |
| 19 | + exit 1 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function Get-DownloadInfo { |
| 24 | + Write-Info "Querying GitHub for latest release..." |
| 25 | + try { |
| 26 | + $release = Invoke-RestMethod -Uri $GhApi -UseBasicParsing -ErrorAction Stop |
| 27 | + } catch { |
| 28 | + Write-Err "Failed to fetch release information from GitHub: $_" |
| 29 | + return $null |
| 30 | + } |
| 31 | + |
| 32 | + $tag = $release.tag_name |
| 33 | + if (-not $tag) { |
| 34 | + Write-Err "Unable to determine release tag name from GitHub response." |
| 35 | + return $null |
| 36 | + } |
| 37 | + |
| 38 | + $version = $tag.TrimStart('v') |
| 39 | + $expectedName = "$ProgramName-$version-x86_64-pc-windows-gnu.exe" |
| 40 | + |
| 41 | + $asset = $release.assets | Where-Object { $_.name -eq $expectedName } | Select-Object -First 1 |
| 42 | + |
| 43 | + if ($null -eq $asset) { |
| 44 | + Write-Err "Expected asset '$expectedName' not found in release '$tag'." |
| 45 | + return $null |
| 46 | + } |
| 47 | + |
| 48 | + Write-Info "Selected asset: $($asset.name)" |
| 49 | + return [PSCustomObject]@{ |
| 50 | + Url = $asset.browser_download_url |
| 51 | + Name = $asset.name |
| 52 | + Version = $version |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function Create-ScheduledTask-PowerShell { |
| 57 | + param([string]$exePath) |
| 58 | + try { |
| 59 | + $action = New-ScheduledTaskAction -Execute $exePath -Argument 'run' |
| 60 | + $trigger = New-ScheduledTaskTrigger -AtStartup |
| 61 | + Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -RunLevel Highest -User 'SYSTEM' -Force | Out-Null |
| 62 | + Write-Success "Scheduled Task '$TaskName' created (PowerShell ScheduledTask API)." |
| 63 | + Start-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue |
| 64 | + return $true |
| 65 | + } catch { |
| 66 | + Write-Err "PowerShell ScheduledTask creation failed: $_" |
| 67 | + return $false |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function Create-ScheduledTask-Schtasks { |
| 72 | + param([string]$exePath) |
| 73 | + try { |
| 74 | + $quoted = '"' + $exePath + '" run' |
| 75 | + $cmd = "schtasks /Create /SC ONSTART /TN \"$TaskName\" /TR $quoted /RL HIGHEST /F /RU SYSTEM" |
| 76 | + Write-Info "Running: $cmd" |
| 77 | + $proc = Start-Process -FilePath schtasks -ArgumentList "/Create","/SC","ONSTART","/TN","$TaskName","/TR",$quoted,"/RL","HIGHEST","/F","/RU","SYSTEM" -NoNewWindow -PassThru -Wait -ErrorAction Stop |
| 78 | + Write-Success "Scheduled Task '$TaskName' created (schtasks)." |
| 79 | + return $true |
| 80 | + } catch { |
| 81 | + Write-Err "schtasks creation failed: $_" |
| 82 | + return $false |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function Install-Program { |
| 87 | + Ensure-Admin |
| 88 | + |
| 89 | + if (-not [Environment]::Is64BitOperatingSystem) { |
| 90 | + Write-Err "Unsupported architecture. Windows releases are currently available only for x86_64." |
| 91 | + exit 1 |
| 92 | + } |
| 93 | + |
| 94 | + $downloadInfo = Get-DownloadInfo |
| 95 | + if (-not $downloadInfo) { exit 1 } |
| 96 | + |
| 97 | + $tmp = Join-Path -Path $env:TEMP -ChildPath $downloadInfo.Name |
| 98 | + Write-Info "Downloading $ProgramName $($downloadInfo.Version) from $($downloadInfo.Url) to $tmp" |
| 99 | + try { |
| 100 | + Invoke-WebRequest -Uri $downloadInfo.Url -OutFile $tmp -UseBasicParsing -ErrorAction Stop |
| 101 | + } catch { |
| 102 | + Write-Err "Download failed: $_" |
| 103 | + exit 1 |
| 104 | + } |
| 105 | + |
| 106 | + Write-Info "Installing to $InstallDir" |
| 107 | + New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null |
| 108 | + try { |
| 109 | + Move-Item -Path $tmp -Destination $InstallPath -Force |
| 110 | + } catch { |
| 111 | + Write-Err "Failed to move binary into place: $_" |
| 112 | + exit 1 |
| 113 | + } |
| 114 | + |
| 115 | + Write-Success "Binary installed to $InstallPath (version $($downloadInfo.Version))" |
| 116 | + |
| 117 | + # Try PowerShell ScheduledTask API first, fallback to schtasks.exe |
| 118 | + if (Get-Command -Name Register-ScheduledTask -ErrorAction SilentlyContinue) { |
| 119 | + if (-not (Create-ScheduledTask-PowerShell -exePath $InstallPath)) { |
| 120 | + Write-Info "Falling back to schtasks.exe" |
| 121 | + Create-ScheduledTask-Schtasks -exePath $InstallPath | Out-Null |
| 122 | + } |
| 123 | + } else { |
| 124 | + Write-Info "Register-ScheduledTask not available; using schtasks.exe" |
| 125 | + Create-ScheduledTask-Schtasks -exePath $InstallPath | Out-Null |
| 126 | + } |
| 127 | + |
| 128 | + Write-Success "$ProgramName installed and scheduled to run at startup." |
| 129 | + Write-Info "Use 'install.ps1 status' to check the installation." |
| 130 | +} |
| 131 | + |
| 132 | +function Uninstall-Program { |
| 133 | + Ensure-Admin |
| 134 | + |
| 135 | + Write-Info "Removing scheduled task (if present)" |
| 136 | + if (Get-Command -Name Unregister-ScheduledTask -ErrorAction SilentlyContinue) { |
| 137 | + try { Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue } |
| 138 | + catch { } |
| 139 | + } |
| 140 | + |
| 141 | + # Also try schtasks delete |
| 142 | + try { schtasks /Delete /TN $TaskName /F > $null 2>&1 } catch { } |
| 143 | + |
| 144 | + if (Test-Path $InstallDir) { |
| 145 | + try { |
| 146 | + Remove-Item -Path $InstallDir -Recurse -Force |
| 147 | + Write-Success "Removed $InstallDir" |
| 148 | + } catch { |
| 149 | + Write-Err "Failed to remove $InstallDir: $_" |
| 150 | + } |
| 151 | + } else { |
| 152 | + Write-Info "$InstallDir not present" |
| 153 | + } |
| 154 | + |
| 155 | + Write-Success "$ProgramName uninstalled." |
| 156 | +} |
| 157 | + |
| 158 | +function Show-Status { |
| 159 | + if (Test-Path $InstallPath) { |
| 160 | + Write-Success "$ProgramName is installed at $InstallPath" |
| 161 | + } else { |
| 162 | + Write-Err "$ProgramName is not installed" |
| 163 | + } |
| 164 | + |
| 165 | + Write-Info "Scheduled task status:" |
| 166 | + if (Get-Command -Name Get-ScheduledTask -ErrorAction SilentlyContinue) { |
| 167 | + try { |
| 168 | + $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction Stop |
| 169 | + $task | Format-List | Out-Host |
| 170 | + } catch { |
| 171 | + Write-Info "Scheduled task '$TaskName' not found via PowerShell API." |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + try { |
| 176 | + schtasks /Query /TN $TaskName 2>$null |
| 177 | + } catch { |
| 178 | + Write-Info "No schtasks entry for $TaskName" |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +function Show-Usage { |
| 183 | + Write-Host "Usage: install.ps1 [install|uninstall|update|status|help]" |
| 184 | +} |
| 185 | + |
| 186 | +switch ($Command.ToLower()) { |
| 187 | + 'install' { Install-Program } |
| 188 | + 'uninstall' { Uninstall-Program } |
| 189 | + 'update' { Uninstall-Program; Install-Program } |
| 190 | + 'status' { Show-Status } |
| 191 | + 'help' { Show-Usage } |
| 192 | + default { Write-Err "Invalid command: $Command`n"; Show-Usage; exit 1 } |
| 193 | +} |
0 commit comments