# Install diago from its latest GitHub release, in PowerShell: # # irm https://diago.dev/install.ps1 | iex # # $env:DIAGO_VERSION the release to install, such as v0.7.0 (default: the latest) # $env:DIAGO_INSTALL_DIR where to put diago.exe (default: ~\.local\bin) # # Windows on x64 or ARM64, in Windows PowerShell 5.1 or PowerShell 7. The # archive is checked against the release's checksums.txt, and the folder is # added to the user's Path when it is not there yet. macOS and Linux have # https://diago.dev/install.sh. # # Everything runs in one script block, so a download cut short runs nothing. & { $ErrorActionPreference = 'Stop' # Windows PowerShell draws a progress bar that slows every download down. $ProgressPreference = 'SilentlyContinue' if ($PSVersionTable.PSVersion.Major -lt 6) { # GitHub takes TLS 1.2, which Windows PowerShell may not offer by default. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } elseif (-not $IsWindows) { throw 'diago: on macOS and Linux, run: curl -fsSL https://diago.dev/install.sh | sh' } $repo = 'https://github.com/oxforge/diago' # A 32-bit PowerShell on 64-bit Windows names the machine in PROCESSOR_ARCHITEW6432. $cpu = @($env:PROCESSOR_ARCHITEW6432, $env:PROCESSOR_ARCHITECTURE) | Where-Object { $_ } | Select-Object -First 1 $arch = switch ($cpu) { 'AMD64' { 'amd64' } 'ARM64' { 'arm64' } default { throw "diago: no release for $cpu; see https://diago.dev/docs/#install" } } if ($env:DIAGO_VERSION) { $version = 'v' + $env:DIAGO_VERSION.TrimStart('v') } else { # Where releases/latest redirects names the tag (the API has a rate limit). $r = Invoke-WebRequest -Uri "$repo/releases/latest" -Method Head -UseBasicParsing $uri = if ($r.BaseResponse.ResponseUri) { $r.BaseResponse.ResponseUri } else { $r.BaseResponse.RequestMessage.RequestUri } $version = $uri.Segments[-1] if ($version -notmatch '^v\d') { throw "diago: no release found at $repo/releases" } } $archive = "diago_$($version.TrimStart('v'))_windows_$arch.zip" $dir = if ($env:DIAGO_INSTALL_DIR) { $env:DIAGO_INSTALL_DIR } else { Join-Path $HOME '.local\bin' } $tmp = Join-Path ([IO.Path]::GetTempPath()) ('diago-install-' + [Guid]::NewGuid()) New-Item -ItemType Directory -Path $tmp | Out-Null try { Write-Host "diago: downloading $archive ($version)" try { Invoke-WebRequest -Uri "$repo/releases/download/$version/$archive" -OutFile (Join-Path $tmp $archive) -UseBasicParsing } catch { throw "diago: release $version has no $archive; see $repo/releases" } Invoke-WebRequest -Uri "$repo/releases/download/$version/checksums.txt" -OutFile (Join-Path $tmp 'checksums.txt') -UseBasicParsing $want = Get-Content (Join-Path $tmp 'checksums.txt') | ForEach-Object { $f = -split $_; if ($f[1] -eq $archive) { $f[0] } } | Select-Object -First 1 if (-not $want) { throw "diago: checksums.txt names no $archive" } # -ne compares without case: Get-FileHash writes upper-case hex. if ((Get-FileHash -Algorithm SHA256 (Join-Path $tmp $archive)).Hash -ne $want) { throw "diago: $archive does not match its checksum" } Expand-Archive -LiteralPath (Join-Path $tmp $archive) -DestinationPath $tmp -Force New-Item -ItemType Directory -Force -Path $dir | Out-Null $dir = (Resolve-Path $dir).ProviderPath $exe = Join-Path $dir 'diago.exe' Move-Item -Force (Join-Path $tmp 'diago.exe') $exe } finally { Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue } Write-Host "diago: installed $(& $exe --version) in $dir" # The user's Path, read unexpanded so that entries such as %USERPROFILE%\bin # stay as they are when it is written back. $key = 'HKCU:\Environment' $path = (Get-Item $key).GetValue('Path', '', 'DoNotExpandEnvironmentNames') $entries = @($path -split ';' | Where-Object { $_ }) $expanded = $entries | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd('\') } if ($expanded -notcontains $dir.TrimEnd('\')) { Set-ItemProperty $key -Name Path -Type ExpandString -Value (($entries + $dir) -join ';') # Setting a user variable tells running programs, Explorer among them, # that the environment changed, so new terminals see the new Path. [Environment]::SetEnvironmentVariable('DIAGO_INSTALL', '1', 'User') [Environment]::SetEnvironmentVariable('DIAGO_INSTALL', $null, 'User') Write-Host "diago: added $dir to your Path" } if (($env:Path -split ';' | ForEach-Object { $_.TrimEnd('\') }) -notcontains $dir.TrimEnd('\')) { $env:Path = "$env:Path;$dir" } $found = Get-Command diago -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 if ($found -and $found.Path -ne $exe) { Write-Host "diago: warning: $($found.Path) comes first on Path" } if (-not (Get-Command resvg -ErrorAction SilentlyContinue)) { Write-Host 'diago: PNG output also needs resvg: https://diago.dev/docs/#install' } }