我正在编辑 Owen Johnson 在 GitHub 上编写的 Powershell 脚本,用于迁移 MSBuild 集成解决方案以使用带有 Nuget 的自动包还原。这是原始的迁移脚本:
########################################
# Regex Patterns for Really Bad Things!
$listOfBadStuff = @(
#sln regex
"\s*(\.nuget\\NuGet\.(exe|targets)) = \1",
#*proj regexes
"\s*<Import Project=""\$\(SolutionDir\)\\\.nuget\\NuGet\.targets"".*?/>",
"\s*<Target Name=""EnsureNuGetPackageBuildImports"" BeforeTargets=""PrepareForBuild"">(.|\n)*?</Target>"
"\s*<RestorePackages>\w*</RestorePackages>"
)
#######################
# Delete NuGet.targets
ls -Recurse -include 'NuGet.exe','NuGet.targets' |
foreach {
remove-item $_ -recurse -force
write-host deleted $_
}
#########################################################################################
# Fix Project and Solution Files to reverse damage done by "Enable NuGet Package Restore
ls -Recurse -include *.csproj, *.sln, *.fsproj, *.vbproj, *.wixproj, *.vcxproj |
foreach {
sp $_ IsReadOnly $false
$content = cat $_.FullName | Out-String
$origContent = $content
foreach($badStuff in $listOfBadStuff){
$content = $content -replace $badStuff, ""
}
if ($origContent -ne $content)
{
$content | out-file -encoding "UTF8" $_.FullName
write-host messed with $_.Name
}
}
我还想删除<Target>
Name="EnsureBclBuildImported" 的标签及其内容。我对正则表达式不是很有经验,不幸的是,我最初尝试让它工作的尝试都失败了。我尝试将目标标签的正则表达式更改为"\s*<Target Name=""(EnsureNuGetPackageBuildImports|EnsureBclBuildImported)""/.+?(?=>)/(.|\n)*?</Target>"
我还尝试制作一个新的正则表达式,如下所示:"\s*<Target Name=""EnsureBclBuildImported"" ^[^\>]*(.|\n)*?</Target>"