Last active
December 30, 2023 18:46
-
-
Save AArnott/45d884642684046c81cb to your computer and use it in GitHub Desktop.
PowerShell cmdlets for publishing NuGet packages in a fast directory structure, and upgrading from an old flat directory listing of nupkg's to the faster one.
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
Function Create-NuPkgSha512File { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[string]$Path, | |
[Parameter()] | |
[string]$Sha512Path="$Path.sha512" | |
) | |
$sha512 = [Security.Cryptography.SHA512]::Create() | |
$fileBytes = [IO.File]::ReadAllBytes($Path) | |
$hashBytes = $sha512.ComputeHash($fileBytes) | |
$hashBase64 = [convert]::ToBase64String($hashBytes) | |
Set-Content -Path $Sha512Path -Value $hashBase64 | |
} | |
Function Publish-Package { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[string]$Path, | |
[Parameter(Mandatory=$true,Position=2)] | |
[string]$PackageSource | |
) | |
$nupkgLeafName = Split-Path $Path -Leaf | |
$shell = New-Object -ComObject Shell.Application | |
$TempFolder = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath ([IO.Path]::GetRandomFileName()) | |
$null = mkdir $TempFolder | |
$tempZipPath = "$TempFolder\$nupkgLeafName.zip" | |
Copy-Item -Path $Path -Destination $tempZipPath | |
$zipItems = $shell.NameSpace($tempZipPath).Items() | |
$nuSpecItem = @($zipItems |? { $_.Path -like '*.nuspec' })[0] | |
$tempNuSpecPath = "$tempFolder\$([IO.Path]::GetFileName($nuspecItem.Path))" | |
$Shell.NameSpace($tempFolder).CopyHere($nuspecItem) | |
$nuspec = [xml](Get-Content $tempNuSpecPath) | |
$id = $nuspec.package.metadata.id | |
$version = $nuspec.package.metadata.version | |
If (-not (Test-Path "$PackageSource\$id\$version")) { $null = mkdir "$PackageSource\$id\$version" } | |
Copy-Item -Path $tempZipPath -Destination "$PackageSource\$id\$version\$nupkgLeafName" | |
Create-NuPkgSha512File -Path $tempZipPath -Sha512Path "$PackageSource\$id\$version\$nupkgLeafName.sha512" | |
Copy-Item -Path $tempNuSpecPath -Destination "$PackageSource\$id\$version\$id.nuspec" | |
Remove-Item $TempFolder -Recurse -Force | |
} | |
Function Upgrade-PackageSource { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[string]$NuGet2Source, | |
[Parameter(Mandatory=$true,Position=2)] | |
[string]$NuGet3Source | |
) | |
# Exclude symbols packages. NuGet clients don't use them from package sources anyway. | |
Get-ChildItem "$NuGet2Source\*.nupkg" -Exclude *.symbols.nupkg |% { | |
Publish-Package -Path $_ -PackageSource $NuGet3Source | |
Write-Host "Published $($_.Name)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script has known bug NuGet/Home#1807.
The recommendation is to use nuget.exe v3.3, which includes new commands that replace the need for this PowerShell script: