2012-06-15 26 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. 你可以用寫主機將其推到包管理器控制檯。