79 lines
3.2 KiB
PowerShell
79 lines
3.2 KiB
PowerShell
|
function Get-PackageFile {
|
|||
|
<#
|
|||
|
.SYNOPSIS
|
|||
|
Downloads a specific file from proget and returns it. Throws an exception if the file doesn't exist.
|
|||
|
This should only be used to download string/config files.
|
|||
|
#>
|
|||
|
[CmdletBinding()]
|
|||
|
Param(
|
|||
|
[Parameter(Mandatory = $true)]
|
|||
|
[string]$FeedSource,
|
|||
|
[Parameter(Mandatory = $true)]
|
|||
|
[string]$Name,
|
|||
|
[Parameter(Mandatory = $true)]
|
|||
|
[string]$Version,
|
|||
|
[Parameter(Mandatory = $true)]
|
|||
|
[string]$PackagePath,
|
|||
|
[Parameter(Mandatory = $false)]
|
|||
|
[PSCredential]$Credential = $null
|
|||
|
)
|
|||
|
|
|||
|
$loglead = (Get-LogLeadName);
|
|||
|
|
|||
|
|
|||
|
# Make sure the input feed URL is a nuget feed.
|
|||
|
$url = $feedSource.Replace("\", "/")
|
|||
|
if ($url -notlike "https://*/nuget/*") {
|
|||
|
Write-Error "$loglead : Feed URL must be in the format of `"https://feed.com/nuget/feed.name`""
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
# If a PSCredential is specified build a basic authentication header.
|
|||
|
$headers = (Get-BasicAuthHeader -Credential $Credential)
|
|||
|
|
|||
|
# Replace package path back slashes with forward slashes, to play nicely in the URL.
|
|||
|
$packagePath = $packagePath.Replace("\", "/")
|
|||
|
|
|||
|
# Parse out the base proget URL and the feed name.
|
|||
|
$searchString = "/nuget/"
|
|||
|
$index = $url.IndexOf($searchString)
|
|||
|
$baseUrl = $url.Substring(0, $index)
|
|||
|
$feedName = $url.Substring($index + $searchString.Length)
|
|||
|
$feedName = $feedName.TrimEnd('/')
|
|||
|
Write-Verbose "$loglead : Base URL: $baseUrl"
|
|||
|
Write-Verbose "$loglead : Feed Name: $feedname"
|
|||
|
|
|||
|
# Make sure the case sensitivity of the package name is correct by querying for package files, and
|
|||
|
# looking for the specific file in a case insensitive manner.
|
|||
|
$filesUrl = "$baseUrl/package-files/list?packageId=$($name)&version=$($version)&feedName=$($feedName)"
|
|||
|
Write-Host "$loglead : Querying for package files at endpoint: $filesUrl"
|
|||
|
try {
|
|||
|
$filesResponse = Invoke-ProgetRequest -URI $filesUrl -Headers $headers
|
|||
|
$files = [System.IO.StreamReader]::new($filesResponse.RawContentStream).ReadToEnd() | ConvertFrom-Json
|
|||
|
$fileSearch = $files | Where-Object { $_.fullPath -like $packagePath }
|
|||
|
} catch {
|
|||
|
$fileSearch = $null
|
|||
|
}
|
|||
|
Write-Host "$loglead : Done Querying for package files at endpoint: $filesUrl"
|
|||
|
|
|||
|
if ($null -eq $fileSearch) {
|
|||
|
Write-Warning "Package file `"$($packagePath)`" could not be found in $name $version"
|
|||
|
return $null
|
|||
|
}
|
|||
|
$packagePath = $fileSearch | Select-Object -First 1 -ExpandProperty "fullPath"
|
|||
|
|
|||
|
# Note: This is a query to download the package .nuspec file. This is a not a query for the filenames/contents of the package.
|
|||
|
$nuspecUrl = "$baseUrl/package-files/download?packageId=$name&version=$version&feedName=$feedName&path=$packagePath"
|
|||
|
|
|||
|
# Download.
|
|||
|
Write-Host "$loglead : Querying for nuspec file at endpoint: $nuspecUrl"
|
|||
|
$response = Invoke-ProgetRequest -URI $nuspecUrl -Headers $headers
|
|||
|
|
|||
|
|
|||
|
# StreamReader interprets byte order marks and skips it, if it exists.
|
|||
|
$result = [System.IO.StreamReader]::new($response.RawContentStream).ReadToEnd()
|
|||
|
Write-Host "$loglead : Completed querying nuspec file at endpoint: $nuspecUrl"
|
|||
|
|
|||
|
return $result
|
|||
|
}
|