Create a NuGet package that shows update notifications

后端 未结 3 1136
滥情空心
滥情空心 2021-01-18 14:05

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 rep

3条回答
  •  萌比男神i
    2021-01-18 14:42

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

提交回复
热议问题