One of the side effects of this automation is that every time you build a new version of the assembly, a new package will be created and pushed out. NuGet package names follow the convention
<assembly name>.<major version number>.<minor version number>.<revision>.<build>.nupkgso the package name will change every time you build a new version of the DLL. Before you know it, you're swamped with various package versions, like this screenshot of the repository folder shows:
So, what to do about it? PowerShell comes to the rescue again. Here's the contents of a purge.ps1 file I created:
param ( [string]$workingDir = "."
)
pushd $workingDir
echo "Purging $workingDir"
[string]$pattern = "(?<rootname>.*)\.(?<revision>\d+\.\d+\.\d+\.\d+)\.nupkg";
$results = dir .| Sort-Object Name -Descending | Where-Object {$_.Name -match $pattern} | foreach-object {
new-object PSObject -Property @{
rootname = $matches.rootname
revision = $matches.Revision
File = $_
}
}
$filesToKeep = $results | sort rootname, revision -Descending | group rootname |
ForEach-Object { $_.group | select -first 1 -ExpandProperty File}
Remove-Item *.nupkg -Exclude $filesToKeep
Write-Output "Files retained are"
dir .
popd
echo "Purge completed"
Copy this file to your repository folder, fire up a PowerShell console and run it. It will delete all but the most recent version of the package.
How does it work? Pretty simply as it happens:
- It creates a regex which matches sections of the filenames
- It then applies the regex to the current directory, and creates a stream of PowerShell objects that capture the name of the package and the version information separately
- It groups these objects by package name and keeps the most recent version in a list called $filesToKeep
- It then deletes all packages from the target directory except those in the list $filesToKeep
PowerShell has a pretty arcane syntax, and it's easy to lose sight of what a script is intended to do. But it's easy to translate a process expressed in plain English into PowerShell, so my advice is simply to write down what you are trying to achieve in words first before plunging into script.
Happy scripting!
This comment has been removed by the author.
ReplyDeletePlease don't call the command prompt a DOS. You're a professional and this is late 2013.
ReplyDeleteI don't believe I did, as it happens. I referred to the DOS *command language*, which for want of a better term it still is.
ReplyDelete