Last active
February 23, 2022 22:57
-
-
Save AArnott/fa48cd4079b42e19a818d5e99b400271 to your computer and use it in GitHub Desktop.
Repack a NuGet package with a new ID
This file contains hidden or 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
<# | |
.SYNOPSIS | |
Creates a package with the same content as another package, but with a new ID. | |
.PARAMETER Id | |
The original ID of the package to download. | |
.PARAMETER NewId | |
The ID of the package to create. | |
.PARAMETER Version | |
The version of the package to download. | |
.PARAMETER OutDir | |
The directory to write the new nupkg file to. | |
.PARAMETER Source | |
The URL to the NuGet server to download the original package from. | |
#> | |
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Low')] | |
Param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[string]$Id, | |
[Parameter(Mandatory=$true,Position=2)] | |
[string]$NewId, | |
[Parameter(Mandatory=$true,Position=3)] | |
[string]$Version, | |
[Parameter()] | |
[string]$OutDir = '.', | |
[Parameter()] | |
[string]$Source = 'https://api.nuget.org/v3/index.json' | |
) | |
# Intentionally use a nonsense framework so that no dependencies will be downloaded. | |
$Framework = 'net42' | |
if (Test-Path $Id) { | |
Remove-Item -Recurse $Id | |
} | |
if ($PSCmdlet.ShouldProcess("$($Id).$Version", "nuget install")) { | |
$script:ProgressPreference = 'SilentlyContinue' | |
try { | |
$NuGetToolPath = Get-Command nuget.exe | |
} catch { | |
Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile nuget.exe | |
$NuGetToolPath = '.\nuget.exe' | |
} | |
Write-Progress -Activity "Installing $Id $Version" | |
& $NuGetToolPath install $Id -version $Version -source $Source -packagesavemode nuspec -excludeversion -framework $Framework -Verbosity quiet | |
if ($LASTEXITCODE -ne 0) { | |
exit $LASTEXITCODE | |
} | |
} | |
# Delete the signature file, if there is one. | |
$SignatureFilePath = "$Id\.signature.p7s" | |
If (Test-Path $SignatureFilePath) { | |
Remove-Item $SignatureFilePath | |
} | |
$NuspecFilePath = Join-Path $PWD "$Id\$Id.nuspec" | |
if ($PSCmdlet.ShouldProcess("Replace package ID `"$Id`" with `"$NewId`"?")) { | |
[xml]$nuspec = Get-Content $NuspecFilePath | |
$nuspec.package.metadata.id = $NewId | |
$nuspec.Save($NuspecFilePath) | |
} | |
if ($PSCmdlet.ShouldProcess("Pack `"$NewId`"?")) { | |
Write-Progress -Activity "Packing $NewId $Version" | |
& $NuGetToolPath pack $NuspecFilePath -OutputDirectory $OutDir | |
} | |
# Clean up unpacked directory | |
Remove-Item $Id -Recurse | |
Write-Progress -Activity Repack -Completed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment