|
| 1 | +# Skern installer - Windows / PowerShell counterpart to scripts/install.sh |
| 2 | +# Usage: |
| 3 | +# irm https://raw.githubusercontent.com/devrimcavusoglu/skern/main/scripts/install.ps1 | iex |
| 4 | +# |
| 5 | +# Environment variables: |
| 6 | +# SKERN_INSTALL_DIR - override install directory (default: %LOCALAPPDATA%\skern\bin) |
| 7 | +# SKERN_VERSION - install a specific version (default: latest) |
| 8 | + |
| 9 | +$ErrorActionPreference = 'Stop' |
| 10 | + |
| 11 | +$Repo = 'devrimcavusoglu/skern' |
| 12 | +$Binary = 'skern' |
| 13 | +$DefaultInstallDir = Join-Path $env:LOCALAPPDATA 'skern\bin' |
| 14 | + |
| 15 | +# PowerShell 5.1 default protocol may be TLS 1.0/1.1; GitHub requires 1.2+. |
| 16 | +[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 |
| 17 | + |
| 18 | +function Write-Info($msg) { |
| 19 | + Write-Host "[skern] $msg" |
| 20 | +} |
| 21 | + |
| 22 | +function Stop-WithError($msg) { |
| 23 | + Write-Host "[skern] ERROR: $msg" -ForegroundColor Red |
| 24 | + exit 1 |
| 25 | +} |
| 26 | + |
| 27 | +function Get-Arch { |
| 28 | + $procArch = $env:PROCESSOR_ARCHITECTURE |
| 29 | + $procArchW6432 = $env:PROCESSOR_ARCHITEW6432 |
| 30 | + if ($procArch -eq 'ARM64' -or $procArchW6432 -eq 'ARM64') { |
| 31 | + return 'arm64' |
| 32 | + } |
| 33 | + if ([System.Environment]::Is64BitOperatingSystem) { |
| 34 | + return 'amd64' |
| 35 | + } |
| 36 | + Stop-WithError "Unsupported architecture: $procArch. Skern supports amd64 and arm64." |
| 37 | +} |
| 38 | + |
| 39 | +function Resolve-Version { |
| 40 | + if ($env:SKERN_VERSION) { |
| 41 | + Write-Info "Using specified version: $($env:SKERN_VERSION)" |
| 42 | + return $env:SKERN_VERSION |
| 43 | + } |
| 44 | + Write-Info 'Fetching latest release...' |
| 45 | + try { |
| 46 | + $release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" -UseBasicParsing |
| 47 | + } catch { |
| 48 | + Stop-WithError "Could not fetch latest release: $_" |
| 49 | + } |
| 50 | + if (-not $release.tag_name) { |
| 51 | + Stop-WithError "Could not determine latest version. Set SKERN_VERSION manually or use 'go install'." |
| 52 | + } |
| 53 | + Write-Info "Latest version: $($release.tag_name)" |
| 54 | + return $release.tag_name |
| 55 | +} |
| 56 | + |
| 57 | +function Get-AssetWithChecksum($version, $arch, $tmpDir) { |
| 58 | + $versionNoV = $version -replace '^v','' |
| 59 | + $assetName = "skern_${versionNoV}_windows_${arch}.zip" |
| 60 | + $downloadUrl = "https://github.com/$Repo/releases/download/$version/$assetName" |
| 61 | + $checksumsUrl = "https://github.com/$Repo/releases/download/$version/checksums.txt" |
| 62 | + |
| 63 | + $zipPath = Join-Path $tmpDir $assetName |
| 64 | + $checksumsPath = Join-Path $tmpDir 'checksums.txt' |
| 65 | + |
| 66 | + Write-Info "Downloading $downloadUrl ..." |
| 67 | + try { |
| 68 | + Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing |
| 69 | + } catch { |
| 70 | + Stop-WithError "Download failed: $_" |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + Invoke-WebRequest -Uri $checksumsUrl -OutFile $checksumsPath -UseBasicParsing |
| 75 | + } catch { |
| 76 | + Write-Info 'Warning: checksums file not available, skipping verification.' |
| 77 | + $checksumsPath = $null |
| 78 | + } |
| 79 | + |
| 80 | + if ($checksumsPath -and (Test-Path $checksumsPath)) { |
| 81 | + $expectedLine = Get-Content $checksumsPath | Where-Object { $_ -match [regex]::Escape($assetName) } | Select-Object -First 1 |
| 82 | + if ($expectedLine) { |
| 83 | + $expectedSum = ($expectedLine -split '\s+')[0] |
| 84 | + $actualSum = (Get-FileHash -Path $zipPath -Algorithm SHA256).Hash.ToLower() |
| 85 | + if ($expectedSum.ToLower() -ne $actualSum) { |
| 86 | + Stop-WithError "Checksum mismatch! Expected $expectedSum, got $actualSum. Aborting." |
| 87 | + } |
| 88 | + Write-Info 'Checksum verified.' |
| 89 | + } else { |
| 90 | + Write-Info "Warning: could not find checksum for $assetName, skipping verification." |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $zipPath |
| 95 | +} |
| 96 | + |
| 97 | +function Install-Binary($zipPath, $tmpDir) { |
| 98 | + $installDir = if ($env:SKERN_INSTALL_DIR) { $env:SKERN_INSTALL_DIR } else { $DefaultInstallDir } |
| 99 | + if (-not (Test-Path $installDir)) { |
| 100 | + New-Item -ItemType Directory -Path $installDir -Force | Out-Null |
| 101 | + } |
| 102 | + |
| 103 | + $extractDir = Join-Path $tmpDir 'extracted' |
| 104 | + Expand-Archive -Path $zipPath -DestinationPath $extractDir -Force |
| 105 | + |
| 106 | + $exeName = "$Binary.exe" |
| 107 | + $sourceExe = Get-ChildItem -Path $extractDir -Filter $exeName -Recurse | Select-Object -First 1 |
| 108 | + if (-not $sourceExe) { |
| 109 | + Stop-WithError "Binary $exeName not found after extraction. Archive may be corrupt." |
| 110 | + } |
| 111 | + |
| 112 | + $targetPath = Join-Path $installDir $exeName |
| 113 | + Move-Item -Path $sourceExe.FullName -Destination $targetPath -Force |
| 114 | + Write-Info "Installed $exeName to $targetPath" |
| 115 | + |
| 116 | + # Check if installDir is on PATH (user or system). |
| 117 | + $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') |
| 118 | + $machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine') |
| 119 | + $combinedPath = "$userPath;$machinePath" |
| 120 | + $onPath = $combinedPath.Split(';') | Where-Object { $_.TrimEnd('\') -ieq $installDir.TrimEnd('\') } |
| 121 | + |
| 122 | + if (-not $onPath) { |
| 123 | + Write-Info '' |
| 124 | + Write-Info "WARNING: $installDir is not on your PATH." |
| 125 | + Write-Info 'Add it persistently with:' |
| 126 | + Write-Info '' |
| 127 | + Write-Info " [Environment]::SetEnvironmentVariable('Path', `"`$env:Path;$installDir`", 'User')" |
| 128 | + Write-Info '' |
| 129 | + Write-Info 'Then open a new shell. Or, for the current shell only:' |
| 130 | + Write-Info '' |
| 131 | + Write-Info " `$env:Path += ';$installDir'" |
| 132 | + Write-Info '' |
| 133 | + } |
| 134 | + |
| 135 | + return $targetPath |
| 136 | +} |
| 137 | + |
| 138 | +function Main { |
| 139 | + Write-Info 'Skern installer' |
| 140 | + Write-Info '' |
| 141 | + |
| 142 | + $arch = Get-Arch |
| 143 | + Write-Info "Detected platform: windows_$arch" |
| 144 | + |
| 145 | + $version = Resolve-Version |
| 146 | + |
| 147 | + $tmpDir = Join-Path $env:TEMP "skern-install-$([System.Guid]::NewGuid().ToString('N'))" |
| 148 | + New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null |
| 149 | + |
| 150 | + try { |
| 151 | + $zipPath = Get-AssetWithChecksum -version $version -arch $arch -tmpDir $tmpDir |
| 152 | + $targetPath = Install-Binary -zipPath $zipPath -tmpDir $tmpDir |
| 153 | + |
| 154 | + Write-Info '' |
| 155 | + try { |
| 156 | + $output = & $targetPath version 2>$null |
| 157 | + if ($LASTEXITCODE -eq 0) { |
| 158 | + Write-Info "Success! Installed $output" |
| 159 | + } else { |
| 160 | + Write-Info 'Installation complete. You may need to open a new shell to use skern.' |
| 161 | + } |
| 162 | + } catch { |
| 163 | + Write-Info 'Installation complete. You may need to open a new shell to use skern.' |
| 164 | + } |
| 165 | + } finally { |
| 166 | + if (Test-Path $tmpDir) { |
| 167 | + Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +Main |
0 commit comments