作爲一個巨大的重構的一部分,我刪除了一些重複的類和枚舉。我移動了命名空間並重構了一切,以便將來更容易維護。如何在使用PowerShell的文件中的特定行之後插入文本?
除了一件事情之外,所有更改都已經腳本化了。如果尚未插入數據協定名稱空間,則需要在每個使用其他名稱空間的文件中插入數據協定名稱空間。
我現在的代碼不起作用,但是我想我需要的就是這樣。
function Insert-Usings{
trap {
Write-Host ("ERROR: " + $_) -ForegroundColor Red
return $false
}
(Get-ChildItem $base_dir -Include *.asmx,*.ascx,*.cs,*.aspx -Force -Recurse -ErrorAction:SilentlyContinue) | % {
$fileName = $_.FullName
(Get-Content $fileName) |
Foreach-Object
{
$_
if ($_ -cmatch "using Company.Shared;") {
$_ -creplace "using Company.Shared;", "using Company.Common;"
}
elseif ($_ -cmatch "using Company") {
#Add Lines after the selected pattern
"using Company.Services.Contracts;"
}
else{
$_
}
}
} | Set-Content $fileName
}
編輯:的代碼往往輸出(覆蓋整個文件與 - ) 「使用Company.Services.Contracts」 語句。
它失敗了嗎?什麼是錯誤信息?還是它做了與你想要的不同的東西? – 2010-11-01 08:09:25
對不起原來的問題不夠清楚。我會更新一些更多的信息。 – mhenrixon 2010-11-01 08:11:14