2012-06-15 29 views
5

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

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

要做到这一点,我在包中添加这在我init.ps1文件:

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的检查,以避免显示通知正在安装新的软件包时。

我想知道,如果

  1. 该解决方案是可以接受的,或者如果有一个更好,“标准”的方式做到这一点(而不是酝酿自己的解决方案)。
  2. 有一个更好的方式来显示通知,因为MessageBox是相当烦人的:当我打开项目时,它隐藏在“准备解决方案”对话框后面,并且操作直到我单击确定才完成。

回答

3

最后,我发现没有比通过init.ps1文件更好的显示通知的方法。
我还发现只有在程序包管理器控制台可见的情况下才运行init脚本,这对于此目的来说并不完美,但仍然找不到更好的东西。

关于通知用户的方式,我发现了一些方法,我在这里发布这些方法以防他们可能对别人有用。

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) 
} 
0
  1. 真的没有错...
  2. 你可以用写主机将其推到包管理器控制台。