I am creating a NuGet package, and I would like the package to display a notification whenever an update for the package is present in the repository (which is a private repository, not the official NuGet repository).
Please note that I do not want the package to update itself automatically (in case the new version might introduce some problems), but just notify the user.
To do this, I added this in my init.ps1
file in the package:
param($installPath, $toolsPath, $package, $project)
$PackageName = "MyPackage"
$update = Get-Package -Updates | Where-Object { $_.Id -eq $PackageName }
if ($update -ne $null -and $update.Version -gt $package.Version) {
[System.Windows.Forms.MessageBox]::Show("New version $($update.Version) available for $($PackageName)") | Out-Null
}
The check on $update.Version -gt $package.Version
is needed to avoid showing the notification when the newer package is being installed.
I would like to know if
MessageBox
is rather annoying: it hides behind the "preparing solution" dialog when I open the project, and the operation does not complete until I click ok.Invoke the Package Manager dialog (select Tools > NuGet Package Manager > Manage NuGet Packages for Solution). Go to the Updates tab. Select the packages you want to update (or use the Select all packages to update all packages) and click Update.
Well, you should update whenever you are able to cope with it. So you need to think carefully about the regression updating the packages could cause to your application if already in production, or the extra tests you are going to need to carry on in order to verify everything seems to be working as expected.
In the end, I've found no better way to show a notification than through the init.ps1
file.
I also discovered that the init script is run only if the Package Manager Console is visible, which is not exactly ideal for this purpose, but still I couldn't find anything better.
About the way to notify the user, I've found some methods, which I'm posting here in case they might be useful for someone else.
param($installPath, $toolsPath, $package, $project)
if ($project -eq $null) {
$projet = Get-Project
}
$PackageName = "MyPackage"
$update = Get-Package -Updates -Source 'MySource' | Where-Object { $_.Id -eq $PackageName }
# the check on $u.Version -gt $package.Version is needed to avoid showing the notification
# when the newer package is being installed
if ($update -ne $null -and $update.Version -gt $package.Version) {
$msg = "An update is available for package $($PackageName): version $($update.Version)"
# method 1: a MessageBox
[System.Windows.Forms.MessageBox]::Show($msg) | Out-Null
# method 2: Write-Host
Write-Host $msg
# method 3: navigate to a web page with EnvDTE
$project.DTE.ItemOperations.Navigate("some-url.html", [EnvDTE.vsNavigateOptions]::vsNavigateOptionsNewWindow) | Out-Null
# method 4: show a message in the Debug/Build window
$win = $project.DTE.Windows.Item([EnvDTE.Constants]::vsWindowKindOutput)
$win.Object.OutputWindowPanes.Item("Build").OutputString("Update available");
$win.Object.OutputWindowPanes.Item("Build").OutputString([Environment]::NewLine)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With