创建的NuGet包,显示更新通知通知、NuGet

2023-09-03 17:11:05 作者:那份伤、早已释怀

我创建的NuGet包,我想包时为包的更新资源库中的present(这是一个私人存储库,而不是官方的NuGet库),以显示通知。

请注意,我不希望程序包自动更新(如果新版本可能会引入一些问题),但只是通知用户。

要做到这一点,我说这在包我的 init.ps1 文件:

 参数($ INSTALLPATH,$ toolsPath,$包,$项目)
$ PACKAGENAME =MyPackage的
$更新=获取-包-Updates |位置对象{$ _。标识-eq $ PACKAGENAME}
如果($更新-ne $空 - 和$ update.Version -gt $ package.Version){
    [System.Windows.Forms.MessageBox] ::显示(新版本$($ update.Version)提供$($ PACKAGENAME))|外空
}
 
搭建NuGet服务器

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

我想知道,如果

在该解决方案是可以接受的,或者有更好的和标准的方式做到这一点(而不是酝酿自己的解决方案)。 有一个更好的方式来显示一个通知,作为的MessageBox 是相当恼人的:它隐藏了preparing解决方案对话框后面,当我打开项目,并且操作无法完成,直到我点击确定。 解决方案

在最后,我发现没有更好的方式来显示一个通知不是通过 init.ps1 文件。 我还发现,在init脚本运行仅当程序包管理器控制台是可见的,这是不完全适合于这个目的,但我仍然不能找到更好的东西。

关于通知用户的方式,我已经找到了一些方法,这些方法我张贴在这里的情况,他们可能对别人有用的。

 参数($ INSTALLPATH,$ toolsPath,$包,$项目)
如果($项目-eq $空){
    $谟=获取项目
}

$ PACKAGENAME =MyPackage的
$更新=获取-包-Updates -SourceMYSOURCE|位置对象{$ _。标识-eq $ PACKAGENAME}
#在$ u.Version -gt $ package.Version检查是必要的,以避免显示的通知
当正在安装#较新的包
如果($更新-ne $空 - 和$ update.Version -gt $ package.Version){

    $味精=有可用的更新程序包$($ PACKAGENAME):版本$($ update.Version)

    #方法1:一个消息
    [System.Windows.Forms.MessageBox] ::显示($味精)|外空
    #方法2:写主机
    写主机$味精
    #方法3:导航到EnvDTE网页
    $ project.DTE.ItemOperations.Navigate(一些-url.html,[EnvDTE.vsNavigateOptions] :: vsNavigateOptionsNewWindow)|外空
    #方法4:显示调试消息/编译窗口
    赢$ = $ project.DTE.Windows.Item([EnvDTE.Constants] :: vsWindowKindOutput)
    $ win.Object.OutputWindowPanes.Item(构建)OutputString(更新)。
    $ win.Object.OutputWindowPanes.Item(构建)。OutputString([环境] ::新行)
}
 

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.

解决方案

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