Files
GSS2Rework/tools/packages_bulk_download.ps1
T

179 lines
13 KiB
PowerShell

$packageListFile = "packages_list.txt"
$localFeedDirectory = ".\nuget_packages"
function Test-PackageIntegrity ($filePath) {
try {
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead($filePath)
$zip.Dispose()
$process = Start-Process -FilePath "./nuget.exe" -ArgumentList "verify -All $filePath -NonInteractive -Verbosity quiet" -Wait -PassThru -NoNewWindow
if (-not ($process.ExitCode -eq 0)) {
return $false
}
return $true
}
catch {
return $false
}
}
function Invoke-DownloadWithProgress {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Uri,
[Parameter(Mandatory = $true)]
[string]$OutFile,
[Parameter(Mandatory = $false)]
[string]$ActivityName = "Скачивание файла"
)
Add-Type -AssemblyName System.Net.Http
$httpClient = New-Object System.Net.Http.HttpClient
try {
$response = $httpClient.GetAsync($Uri, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).GetAwaiter().GetResult()
if (-not $response.IsSuccessStatusCode) {
throw "Сервер вернул код ошибки: $([int]$response.StatusCode) ($($response.StatusCode))"
}
$totalBytes = $response.Content.Headers.ContentLength
$totalMB = if ($totalBytes) { [Math]::Round($totalBytes / 1MB, 3) } else { "Неизвестно" }
$downloadStream = $response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
$fileStream = [System.IO.File]::Create($OutFile)
$fileStream.Close()
$buffer = New-Object byte[] 8192
$bytesRead = 0
$totalBytesRead = 0
while (($bytesRead = $downloadStream.Read($buffer, 0, $buffer.Length)) -gt 0) {
$fileStream = [System.IO.File]::Open($OutFile, [System.IO.FileMode]::Append)
$fileStream.Write($buffer, 0, $bytesRead)
$fileStream.Close();
$totalBytesRead += $bytesRead
$currentMB = [Math]::Round($totalBytesRead / 1MB, 3)
$progressParams = @{
Activity = $ActivityName
Status = "Скачано: " + $currentMB.ToString("F3", [System.Globalization.CultureInfo]::InvariantCulture) + " МБ из " + $totalMB.ToString("F3", [System.Globalization.CultureInfo]::InvariantCulture) + " МБ"
}
if ($totalBytes) {
$progressParams.PercentComplete = [int](($totalBytesRead / $totalBytes) * 100)
}
Write-Progress @progressParams
}
$downloadStream.Close()
return [PSCustomObject]@{
StatusCode = [int]$response.StatusCode
StatusDescription = $response.StatusCode.ToString()
ContentLength = $totalBytes
Path = $OutFile
}
}
catch {
if (Test-Path $OutFile) { Remove-Item $OutFile -Force }
Write-Error $_.Exception.Message
return $null
}
finally {
if ($httpClient) { $httpClient.Dispose() }
Write-Progress -Activity $ActivityName -Completed
}
}
# if (-not (Test-Path "nuget.exe")) {
# Write-Host "Скачивание: nuget.exe" -ForegroundColor Cyan
# $res = Invoke-DownloadWithProgress -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile "nuget.exe" -ActivityName "Скачивание: nuget.exe"
# if (-not ($res -and $res.StatusCode -eq 200)) {
# Write-Error "Не удалось скачать файл или сервер вернул ошибку."
# }
# }
if (-not (Test-Path $localFeedDirectory)) {
New-Item -ItemType Directory -Path $localFeedDirectory | Out-Null
}
$allPackagesOk = 1;
if (-not (Test-Path $packageListFile)) {
Write-Host "Файл со списком пакетов не найден: $packageListFile" -ForegroundColor Red
$allPackagesOk = 0
} else {
Get-Content $packageListFile | ForEach-Object {
$line = $_.Trim()
if ($line.StartsWith("#")) {
return
}
if (-not [string]::IsNullOrEmpty($line)) {
if ($line -match "==") {
$parts = $line -split "=="
$name = $parts[0].Trim()
$version = $parts[1].Trim()
$filename = "$name.$version.nupkg"
$outFile = Join-Path $localFeedDirectory $filename
$url = "https://api.nuget.org/v3-flatcontainer/$($name.ToLower())/$version/$($name.ToLower()).$version.nupkg"
if (Test-Path $outFile) {
if (Test-PackageIntegrity $outFile) {
Write-Host "Пакет цел: $name v$version" -ForegroundColor Green
return
} else {
Write-Host "Пакет поврежден: $name v$version" -ForegroundColor Red
Remove-Item $outFile -Force
}
}
Write-Host "Скачивание: $name v$version" -ForegroundColor Cyan
$res = Invoke-DownloadWithProgress -Uri $url -OutFile $outFile -ActivityName "Скачивание: $name v$version"
if (-not ($res -and $res.StatusCode -eq 200)) {
Write-Error "Не удалось скачать файл или сервер вернул ошибку."
}
}
}
}
}
Write-Host "Проверка пакетов" -ForegroundColor Cyan
Get-Content $packageListFile | ForEach-Object {
$line = $_.Trim()
if ($line.StartsWith("#")) {
return
}
if (-not [string]::IsNullOrEmpty($line)) {
if ($line -match "==") {
$parts = $line -split "=="
$name = $parts[0].Trim()
$version = $parts[1].Trim()
$filename = "$name.$version.nupkg"
$outFile = Join-Path $localFeedDirectory $filename
if (Test-Path $outFile) {
if (-not (Test-PackageIntegrity $outFile)) {
Write-Host "Пакет поврежден: $name v$version" -ForegroundColor Red
$allPackagesOk = 0
}
} else {
Write-Host "Пакет не найден: $name v$version" -ForegroundColor Red
$allPackagesOk = 0
}
}
}
}
if ($allPackagesOk) {
Write-Host "Все пакеты прошли проверку" -ForegroundColor Green
} else {
Write-Host "Некоторые пакеты не прошли проверку, проверьте сообщения выше" -ForegroundColor Red
}