NuGet安装后运行PowerShell脚本
问题描述:
我试图通过PowerShell在csproj中编辑当前安装的NuGet包库的<Reference>
XML节点,以添加Condition="..."
属性。NuGet安装后运行PowerShell脚本
我能够在包安装期间调用PowerShell Install.ps1脚本,但<Reference>
节点在那时不存在。
如果我手动将所需的Condition="..."
属性添加到csproj,它将在更新NuGet包时被删除。
有没有办法在之后自动运行PowerShell脚本NuGet添加已安装库的<Reference>
节点?或者以某种方式修改NuGet在安装软件包时添加的节点?
答
一个选项是没有NuGet默认添加引用。删除你的lib文件夹并将程序集中的其他地方放在包中(参考?)。创建一个{NuGetPkgName} .targets文件并将其放入包build文件夹中。该目标文件将被“导入”到您添加NuGet pkg的任何项目中。然后在您的目标文件中,您可以使用所需的条件属性将引用添加到您的程序集。
答
我还没有时间去实施Keith Hill的解决方案,只好通过批处理文件手动调用我的脚本。这是PowerShell脚本,以帮助某人。
目标是在从MyProjectName开始的所有项目中的Debug中排除对MyPackageName的引用。
# Get a list of matching csproj files recursively
$csprojs = gci $PSScriptRoot -rec -include MyProjectName.*.csproj
# The value of the condition attribute we want to set
$condition = @'
'$(Configuration)' != 'Debug'
'@
foreach ($csproj in $csprojs)
{
[xml]$xml = Get-Content $csproj;
foreach ($reference in $xml.Project.ItemGroup.Reference | ? { $_.Include -match '^MyPackageName' })
{
$reference.SetAttribute('Condition', $condition);
}
$xml.Save($csproj);
}
NuGet将停止在未来的版本中支持PowerShell。 –