Create a NuGet package that shows update notifications

.net, c#, nuget, powershell

Solution

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)
}

Problem

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 - This solution is acceptable, or if there is a better and "standard" way to do this (rather than brewing my own solution). - There is a better way to show a notification, as the `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.

Original source