2013-06-24 62 views
3

對Nude包使用Install.ps1腳本時,我試圖將代碼添加到Global.asax.cs文件中以進行自定義。使用我的Install.ps1腳本,-replace命令不起作用。事實上,我不能將任何文本分配給我正在使用的變量$ a - 替換並將其寫入「Global.asax.cs」文件。涉及-replace和剪貼板的3行腳本可以在nuget之外的「Windows PowerShell」中使用。我知道變量$ a正在傳遞剪貼板中的內容,因爲註釋掉「#$ customGlobalAsax.Document.Selection.Copy()」會將無論發生在剪貼板中的任何內容寫入Global.asax.cs文件。 有何建議?謝謝。在nuget包中使用PowerShell編輯Global.asax

param($installPath, $toolsPath, $package, $project) 

$customGlobalAsax = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" } 
$customGlobalAsax.Open() 
$customGlobalAsax.Document.Activate() 
$customGlobalAsax.Document.Selection.SelectAll() 
$customGlobalAsax.Document.Selection.Copy() 

$a = [System.Windows.Forms.Clipboard]::GetText() 
$a = $a -replace "using Company.Web.Mvc.ViewBase;","using Company.Web.Mvc.ViewBase;`r`nusing Company.Web.Address;" 
[System.Windows.Forms.Clipboard]::SetText($a) 

$customGlobalAsax.Document.Selection.Delete() 
$customGlobalAsax.Document.Selection.Paste() 
+0

是有一些原因,你不能使用[WebActivator(HTTP://的NuGet。 org/packages/WebActivatorEx /)掛鉤你想使用的事件? –

+1

Steven,我嘗試過WebActivator,並且爲了編輯Global.asax.cs而工作......謝謝! ..我仍然有興趣修改代碼文件。我有一些我想添加到現有控制器方法的屬性。 – user2517322

回答

0

從我所看到的,有一些問題,這條線,因爲它會返回任何結果:

$customGlobalAsax = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" } 

以下是我的測試:

$directory = dir * 

$directory | foreach {$_.name} 

以上成功運行。

但下面沒有回報。

$directory = dir * 

$directory.name | foreach {$_.name} 

我相信,在後一種情況下$ _名代表$ directory.name.name不存在。

所以看起來這行腳本

$customGlobalAsax = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" } 

應改爲:

$customGlobalAsax = $project | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" } 
+0

彼得,我試了一下你的建議。不幸的是,刪除'.ProjectItems'會刪除「Global.asas.cs」文件中的句柄。我通過註釋掉'#$ customGlobalAsax.Document.Selection.Paste()'「來測試這個,看看Global.asax.cs是否被清除,只有當'.ProjectItems'被留下時才清除。 – user2517322

+0

我試了'$ customGlobalAsax = $ project.ProjectItems |其中{$ _。名稱-eq「Global.asax.cs」}'和'$ customGlobalAsax = $ project |其中{$ _。名稱-eq「Global.asax.cs」}「。我都失去了文件的句柄。我檢查了內容是否已經到達剪貼板,但沒有。其目的的原始線似乎工作正常。 – user2517322

+0

測試,失去原來的線已經提供。 – user2517322