Show release download progress in installers
This commit is contained in:
@@ -19,6 +19,55 @@ $ErrorActionPreference = 'Stop'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
|
||||
|
||||
function Show-DownloadProgress([long]$Received, [long]$Total) {
|
||||
if ($Total -gt 0) {
|
||||
$Percent = [Math]::Min(100, [Math]::Floor(100.0 * $Received / $Total))
|
||||
$Line = ' Downloading... {0:0.#} MB / {1:0.#} MB ({2}%)' -f ($Received / 1MB), ($Total / 1MB), $Percent
|
||||
} else {
|
||||
$Line = ' Downloading... {0:0.#} MB' -f ($Received / 1MB)
|
||||
}
|
||||
Write-Host ("`r" + $Line.PadRight(64)) -NoNewline
|
||||
}
|
||||
|
||||
function Save-ReleaseArchive([string]$Uri, [string]$Path) {
|
||||
$Request = [Net.HttpWebRequest]::CreateHttp($Uri)
|
||||
$Request.Timeout = 120000
|
||||
$Request.ReadWriteTimeout = 120000
|
||||
$Response = $Request.GetResponse()
|
||||
try {
|
||||
$Stream = $Response.GetResponseStream()
|
||||
try {
|
||||
$File = [IO.File]::Create($Path)
|
||||
try {
|
||||
$Total = $Response.ContentLength
|
||||
$Received = 0L
|
||||
$Buffer = New-Object byte[] 65536
|
||||
$Clock = [Diagnostics.Stopwatch]::StartNew()
|
||||
Show-DownloadProgress $Received $Total
|
||||
while (($Count = $Stream.Read($Buffer, 0, $Buffer.Length)) -gt 0) {
|
||||
$File.Write($Buffer, 0, $Count)
|
||||
$Received += $Count
|
||||
if ($Clock.ElapsedMilliseconds -ge 150 -and $Received -ne $Total) {
|
||||
Show-DownloadProgress $Received $Total
|
||||
$Clock.Restart()
|
||||
}
|
||||
}
|
||||
if ($Total -ge 0 -and $Received -ne $Total) {
|
||||
throw 'The mio-code archive download ended early.'
|
||||
}
|
||||
Show-DownloadProgress $Received $Total
|
||||
} finally {
|
||||
$File.Dispose()
|
||||
Write-Host ''
|
||||
}
|
||||
} finally {
|
||||
$Stream.Dispose()
|
||||
}
|
||||
} finally {
|
||||
$Response.Dispose()
|
||||
}
|
||||
}
|
||||
|
||||
$Repo = 'https://git.hoshizukimio.com/HoshizukiMio/mio-code'
|
||||
$ReleaseApi = 'https://git.hoshizukimio.com/api/v1/repos/HoshizukiMio/mio-code/releases'
|
||||
$Architecture = $env:PROCESSOR_ARCHITEW6432
|
||||
@@ -28,6 +77,7 @@ if ($env:OS -ne 'Windows_NT' -or $Architecture -ne 'AMD64') {
|
||||
}
|
||||
if (-not $BinDir) { $BinDir = Join-Path $env:USERPROFILE '.local\bin' }
|
||||
if (-not $Version) {
|
||||
Write-Host 'Fetching latest stable version...'
|
||||
$Release = Invoke-RestMethod -Uri "$ReleaseApi/latest"
|
||||
if ($Release.draft -or $Release.prerelease -or $Release.tag_name -notmatch '^v') {
|
||||
throw 'Could not resolve the latest stable mio-code release.'
|
||||
@@ -47,14 +97,14 @@ $Archive = Join-Path $BinDir ".mio-install-$Nonce.gz"
|
||||
$Staged = Join-Path $BinDir ".mio-install-$Nonce.exe"
|
||||
$Backup = Join-Path $BinDir ".mio-install-$Nonce.old"
|
||||
$Asset = "$Repo/releases/download/v$Version/mio-windows-x86_64.exe"
|
||||
Write-Host "Installing mio $Version (windows-x86_64)..."
|
||||
|
||||
try {
|
||||
$Checksum = (Invoke-WebRequest -UseBasicParsing -Uri "$Asset.sha256").Content
|
||||
if ($Checksum -is [byte[]]) { $Checksum = [Text.Encoding]::UTF8.GetString($Checksum) }
|
||||
$Checksum = $Checksum.Trim()
|
||||
if ($Checksum -notmatch '^[0-9a-fA-F]{64}$') { throw 'Invalid mio-code release checksum.' }
|
||||
Write-Host "Downloading mio $Version for Windows x86-64..."
|
||||
Invoke-WebRequest -UseBasicParsing -Uri "$Asset.gz" -OutFile $Archive
|
||||
Save-ReleaseArchive "$Asset.gz" $Archive
|
||||
$InputStream = [IO.File]::OpenRead($Archive)
|
||||
try {
|
||||
$Decoder = [IO.Compression.GZipStream]::new($InputStream, [IO.Compression.CompressionMode]::Decompress)
|
||||
@@ -65,7 +115,14 @@ try {
|
||||
} finally { $Decoder.Dispose() }
|
||||
} finally { $InputStream.Dispose() }
|
||||
|
||||
if ((Get-FileHash -LiteralPath $Staged -Algorithm SHA256).Hash -ne $Checksum) {
|
||||
$Hasher = [Security.Cryptography.SHA256]::Create()
|
||||
try {
|
||||
$Binary = [IO.File]::OpenRead($Staged)
|
||||
try { $Digest = [BitConverter]::ToString($Hasher.ComputeHash($Binary)).Replace('-', '') }
|
||||
finally { $Binary.Dispose() }
|
||||
} finally { $Hasher.Dispose() }
|
||||
|
||||
if ($Digest -ne $Checksum) {
|
||||
throw 'mio-code release checksum did not match.'
|
||||
}
|
||||
$Reported = (& $Staged --version | Out-String).Trim()
|
||||
|
||||
@@ -28,6 +28,7 @@ for program in curl gzip sha256sum; do
|
||||
done
|
||||
|
||||
if [[ -z "$version" ]]; then
|
||||
printf '%s\n' 'Fetching latest stable version...' >&2
|
||||
latest=$(curl -fsSL --retry 2 -o /dev/null -w '%{url_effective}' "$repo/releases/latest")
|
||||
case "$latest" in
|
||||
"$repo/releases/tag/v"*) version=${latest##*/} ;;
|
||||
@@ -41,6 +42,7 @@ if [[ ! "$version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; th
|
||||
fi
|
||||
|
||||
asset="$repo/releases/download/v$version/mio-linux-x86_64"
|
||||
printf 'Installing mio %s (linux-x86_64)...\n' "$version" >&2
|
||||
mkdir -p -- "$bin_dir"
|
||||
bin_dir=$(cd -- "$bin_dir" && pwd -P)
|
||||
work=$(mktemp -d -- "$bin_dir/.mio-install.XXXXXXXX")
|
||||
@@ -53,8 +55,12 @@ if [[ ! "$checksum" =~ ^[[:xdigit:]]{64}$ ]]; then
|
||||
printf '%s\n' 'Invalid mio-code release checksum.' >&2
|
||||
exit 1
|
||||
fi
|
||||
printf 'Downloading mio %s for Linux x86-64...\n' "$version" >&2
|
||||
curl -fsSL --retry 2 --output "$archive" "$asset.gz"
|
||||
if [[ -t 2 ]]; then
|
||||
curl -fSL --progress-bar --retry 2 --output "$archive" "$asset.gz"
|
||||
else
|
||||
printf '%s\n' ' Downloading...' >&2
|
||||
curl -fsSL --retry 2 --output "$archive" "$asset.gz"
|
||||
fi
|
||||
gzip -dc -- "$archive" > "$staged"
|
||||
actual=$(sha256sum < "$staged")
|
||||
if [[ ${actual%% *} != "${checksum,,}" ]]; then
|
||||
|
||||
Reference in New Issue
Block a user