创建一个显示更新通知的 NuGet 包 [英] Create a NuGet package that shows update notifications

查看:14
本文介绍了创建一个显示更新通知的 NuGet 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 NuGet 包,并且我希望该包在存储库(这是一个私有存储库,而不是官方 NuGet 存储库)中存在该包的更新时显示通知.

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.

为此,我在我的 init.ps1 文件中添加了这个:

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
}

需要检查 $update.Version -gt $package.Version 以避免在安装较新的软件包时显示通知.

The check on $update.Version -gt $package.Version is needed to avoid showing the notification when the newer package is being installed.

我想知道

  1. 这个解决方案是可以接受的,或者如果有更好和标准"的方式来做到这一点(而不是酝酿我自己的解决方案).
  2. 有一个更好的方法来显示通知,因为 MessageBox 相当烦人:当我打开项目时它隐藏在准备解决方案"对话框的后面,并且操作直到我完成点击确定.
  1. This solution is acceptable, or if there is a better and "standard" way to do this (rather than brewing my own solution).
  2. 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.

推荐答案

最后,我发现没有比通过 init.ps1 文件更好的方式来显示通知.
我还发现只有在包管理器控制台可见时才会运行初始化脚本,这对于此目的并不完全理想,但我仍然找不到更好的东西.

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

这篇关于创建一个显示更新通知的 NuGet 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆