Created
February 5, 2019 13:40
-
-
Save oceanexplorer/35e0f26962018dc8578c745060365c15 to your computer and use it in GitHub Desktop.
Helper methods for dealing with NuGet packages such as versioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$compressDirectory = | |
{ | |
param($directoryPath, $destinationPath) | |
Compress-Archive -path "$directoryPath\*" -DestinationPath $destinationPath -Force | |
} | |
function Compress-NuGetPackages | |
{ | |
Param( | |
[Parameter(mandatory=$true)] | |
[string]$packagesDirectory | |
) | |
Write-Output "Searching for .nuspec files in $packagesDirectory" | |
$files = gci "$packagesDirectory" -recurse | ?{ $_.Extension -eq ".nuspec" } | foreach { gci -Path $_.FullName -Recurse -include *.nuspec } | |
if($files) | |
{ | |
foreach ($nuspecFile in $files) | |
{ | |
$zipDestinationPath = Join-Path -Path $nuspecFile.Directory.Parent.FullName -ChildPath "$($nuspecFile.Directory.Name).zip" | |
Write-Output "Starting compression of $($nuspecFile.Directory.FullName)" | |
$job = Start-Job -ScriptBlock $compressDirectory -ArgumentList $nuspecFile.Directory.FullName, $zipDestinationPath | Wait-Job | |
if($job.State -ne "Completed") | |
{ | |
$errorReason = $job.ChildJobs[0].JobStateInfo.Reason.Message | |
Write-Error "There was an issue compressing the directory at $ because: $errorReason" | |
} | |
else | |
{ | |
Write-Output "Successfully compressed the directory to $zipDestinationPath" | |
} | |
# Rename the nuget package from a .nupkg to .zip extension and extract the archive | |
$newName = $zipDestinationPath -replace ".zip", ".nupkg" | |
Write-Output "Renaming $zipDestinationPath to $newName" | |
Rename-Item -Path $zipDestinationPath -NewName $newName | |
Write-Output "Deleting directory: $($nuspecFile.Directory.FullName)" | |
Remove-Item $nuspecFile.Directory -Recurse | |
} | |
} | |
else | |
{ | |
Write-Warning "Found no *.nuspec files." | |
} | |
} | |
$expandZipFile = | |
{ | |
param($zipFile, $zipDestinationPath) | |
Expand-Archive -path "$zipFile" -DestinationPath $zipDestinationPath -Force | |
} | |
function Expand-NuGetPackages | |
{ | |
Param( | |
[Parameter(mandatory=$true)] | |
[string]$packagesDirectory | |
) | |
Write-Output "Searching for .nupkg files in $packagesDirectory" | |
$files = gci "$packagesDirectory" -recurse | ?{ $_.Extension -eq ".nupkg" } | foreach { gci -Path $_.FullName -Recurse -include *.nupkg } | |
if($files) | |
{ | |
foreach ($nugetPackageFile in $files) | |
{ | |
$zipFilename = $nugetPackageFile.Name -replace ".nupkg", ".zip" | |
$packageName = [io.path]::GetFileNameWithoutExtension($nugetPackageFile) | |
$zipDestinationDirectory = Join-Path -Path $nugetPackageFile.Directory -ChildPath $packageName | |
$zipFilepath = Join-Path -Path $nugetPackageFile.Directory -ChildPath $zipFilename | |
# Rename the nuget package from a .nupkg to .zip extension and extract the archive | |
Write-Output "Renaming $($nugetPackageFile.Name) to $zipFilename" | |
Rename-Item -Path $nugetPackageFile.FullName -NewName $zipFilename | |
if(!$(Test-Path $packagesDirectory)) { Write-Error "Could not find the packages directory at $packagesDirectory" } | |
if(!$(Test-Path $zipFilepath)) { Write-Error "Could not find a zip file at $zipFilepath" } | |
$zipFile = Get-ChildItem $zipFilepath | |
Write-Output "Starting extraction of $zipFile" | |
$job = Start-Job -ScriptBlock $expandZipFile -ArgumentList $zipFile, $zipDestinationDirectory | Wait-Job | |
if($job.State -ne "Completed") | |
{ | |
$errorReason = $job.ChildJobs[0].JobStateInfo.Reason.Message | |
Write-Error "There was an issues unzipping the file at $zipFile because: $errorReason" | |
} | |
else | |
{ | |
Write-Output "Successfully extracted the zip file to $zipDestinationDirectory" | |
} | |
} | |
} | |
else | |
{ | |
Write-Warning "Found no *.nupkg files." | |
} | |
} | |
function Set-Mode | |
{ | |
if ($env:SYSTEM_TEAMFOUNDATIONSERVERURI -like '*visualstudio*') { | |
Write-Verbose "VSTS MODE" | |
$account = ($env:SYSTEM_TEAMFOUNDATIONSERVERURI -replace "https://(.*)\.visualstudio\.com/", '$1').split('.')[0] | |
$script:basepackageurl = ("https://{0}.pkgs.visualstudio.com/DefaultCollection/_apis/packaging/feeds" -f $account) | |
$script:basefeedsurl = ("https://{0}.feeds.visualstudio.com/DefaultCollection/_apis/packaging/feeds" -f $account) | |
} | |
else { | |
write-Verbose "ONPREM MODE" | |
$script:basepackageurl = $env:SYSTEM_TEAMFOUNDATIONSERVERURI + "_apis/packaging/feeds"; | |
$script:basefeedsurl = $env:SYSTEM_TEAMFOUNDATIONSERVERURI + "_apis/packaging/feeds"; | |
} | |
Write-Verbose "BasePackageUrl: $script:basepackageurl" | |
Write-Verbose "BaseFeedUrl: $script:basefeedsurl" | |
} | |
<# | |
.Synopsis | |
Creates either a Basic Authentication token or a Bearer token depending on where the method is called from VSTS. | |
When you send a Personal Access Token that you generate in VSTS it uses this one. Within the VSTS pipeline it uses env:System_AccessToken | |
#> | |
function New-VSTSAuthenticationToken | |
{ | |
[CmdletBinding()] | |
[OutputType([object])] | |
$accesstoken = ""; | |
if([string]::IsNullOrEmpty($env:System_AccessToken)) | |
{ | |
if([string]::IsNullOrEmpty($env:PersonalAccessToken)) | |
{ | |
throw "No token provided. Use either env:PersonalAccessToken for Localruns or use in VSTS Build/Release (System_AccessToken)" | |
} | |
Write-Debug $($env:PersonalAccessToken) | |
$userpass = ":$($env:PersonalAccessToken)" | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($userpass)) | |
$accesstoken = "Basic $encodedCreds" | |
} | |
else | |
{ | |
$accesstoken = "Bearer $env:System_AccessToken" | |
} | |
return $accesstoken; | |
} | |
function Set-PackageQuality | |
{ | |
[CmdletBinding()] | |
[OutputType([object])] | |
param | |
( | |
[string] $feedType="nuget", | |
[string] $feedName="", | |
[string] $packageId="", | |
[string] $packageVersion="", | |
[string] $packageQuality="" | |
) | |
Set-Mode | |
$token = New-VSTSAuthenticationToken | |
#API URL is slightly different for npm vs. nuget... | |
switch($feedType) | |
{ | |
"npm" { $releaseViewURL = "$script:basepackageurl/$feedName/npm/$packageId/versions/$($packageVersion)?api-version=3.0-preview.1" } | |
"nuget" { $releaseViewURL = "$script:basepackageurl/$feedName/nuget/packages/$packageId/versions/$($packageVersion)?api-version=3.0-preview.1" } | |
default { $releaseViewURL = "$script:basepackageurl/$feedName/nuget/packages/$packageId/versions/$($packageVersion)?api-version=3.0-preview.1" } | |
} | |
$json = @{ | |
views = @{ | |
op = "add" | |
path = "/views/-" | |
value = "$packageQuality" | |
} | |
} | |
Write-Host $releaseViewURL | |
$response = Invoke-RestMethod -Uri $releaseViewURL -Headers @{Authorization = $token} -ContentType "application/json" -Method Patch -Body (ConvertTo-Json $json) | |
return $response | |
} | |
Export-ModuleMember -Function Expand-NuGetPackages, Compress-NuGetPackages, Set-PackageQuality |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment