|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# Based on Flyctl installer. |
| 3 | +# Copyright 2018 the Deno authors. All rights reserved. MIT license. |
| 4 | +# TODO(everyone): Keep this script simple and easily auditable. |
| 5 | + |
| 6 | +$ErrorActionPreference = 'Stop' |
| 7 | + |
| 8 | +$Version = if ($v) { |
| 9 | + $v |
| 10 | +} |
| 11 | +elseif ($args.Length -eq 1) { |
| 12 | + $args.Get(0) |
| 13 | +} |
| 14 | +else { |
| 15 | + "latest" |
| 16 | +} |
| 17 | + |
| 18 | +$WokwiInstall = $env:WOKWI_INSTALL |
| 19 | +$BinDir = if ($WokwiInstall) { |
| 20 | + "$WokwiInstall\bin" |
| 21 | +} |
| 22 | +else { |
| 23 | + "$Home\.wokwi\bin" |
| 24 | +} |
| 25 | + |
| 26 | +$TempExe = "$BinDir\wokwi-cli.exe.new" |
| 27 | +$WokwiCLIExe = "$BinDir\wokwi-cli.exe" |
| 28 | + |
| 29 | +# GitHub require TLS 1.2 |
| 30 | +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
| 31 | + |
| 32 | +# Determine the URL for downloading wokwi-cli |
| 33 | +$WokwiUri = if ($Version -eq "latest") { |
| 34 | + "https://github.com/wokwi/wokwi-cli/releases/latest/download/wokwi-cli-win-x64.exe" |
| 35 | +} |
| 36 | +else { |
| 37 | + "https://github.com/wokwi/wokwi-cli/releases/download/v$Version/wokwi-cli-win-x64.exe" |
| 38 | +} |
| 39 | + |
| 40 | +# Check if the URL is valid |
| 41 | +try { |
| 42 | + Invoke-WebRequest $WokwiUri -Method Head -UseBasicParsing | Out-Null |
| 43 | +} |
| 44 | +catch { |
| 45 | + $StatusCode = $_.Exception.Response.StatusCode.value__ |
| 46 | + if ($StatusCode -eq 404) { |
| 47 | + Write-Error "Unable to find a wokwi-cli release on GitHub for version: $Version - see https://github.com/wokwi/wokwi-cli/releases for all versions" |
| 48 | + Exit 1 |
| 49 | + } |
| 50 | + else { |
| 51 | + $Request = $_.Exception |
| 52 | + Write-Error "Error while fetching releases: $Request" |
| 53 | + Exit 1 |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +if (!(Test-Path $BinDir)) { |
| 58 | + New-Item $BinDir -ItemType Directory | Out-Null |
| 59 | +} |
| 60 | + |
| 61 | +$prevProgressPreference = $ProgressPreference |
| 62 | +try { |
| 63 | + # Invoke-WebRequest on older powershell versions has severe transfer |
| 64 | + # performance issues due to progress bar rendering - the screen updates |
| 65 | + # end up throttling the download itself. Disable progress on these older |
| 66 | + # versions. |
| 67 | + if ($PSVersionTable.PSVersion.Major -lt 7) { |
| 68 | + Write-Output "Downloading wokwi-cli..." |
| 69 | + $ProgressPreference = "SilentlyContinue" |
| 70 | + } |
| 71 | + |
| 72 | + Invoke-WebRequest $WokwiUri -OutFile $TempExe -UseBasicParsing |
| 73 | +} |
| 74 | +finally { |
| 75 | + $ProgressPreference = $prevProgressPreference |
| 76 | +} |
| 77 | + |
| 78 | +if (Test-Path $WokwiCLIExe) { |
| 79 | + Remove-Item $WokwiCLIExe |
| 80 | +} |
| 81 | +Copy-Item $TempExe $WokwiCLIExe |
| 82 | +Remove-Item $TempExe |
| 83 | + |
| 84 | +$User = [EnvironmentVariableTarget]::User |
| 85 | +$Path = [Environment]::GetEnvironmentVariable('Path', $User) |
| 86 | +if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) { |
| 87 | + [Environment]::SetEnvironmentVariable('Path', "$Path;$BinDir", $User) |
| 88 | + $Env:Path += ";$BinDir" |
| 89 | +} |
| 90 | + |
| 91 | +Write-Output "wokwi-cli was installed successfully to $WokwiCLIExe" |
| 92 | +Write-Output "Run 'wokwi-cli --help' to get started" |
| 93 | +Write-Output "" |
| 94 | +Write-Output "Stuck? Join our Discord at https://wokwi.com/discord" |
0 commit comments