简体   繁体   English

在安装NuGet之后运行PowerShell脚本

[英]Run PowerShell script after NuGet install

I am trying to edit the <Reference> XML node of the currently installing NuGet package library in the csproj via PowerShell to add a Condition="..." attribute. 我正在尝试通过PowerShell编辑csproj中当前安装的NuGet软件包库的<Reference> XML节点,以添加Condition="..."属性。

I was able to call my PowerShell Install.ps1 script during the package's installation but the <Reference> node is not present at that moment. 我可以在软件包的安装过程中调用PowerShell Install.ps1脚本,但此时<Reference>节点不存在。

If I manually add the desired Condition="..." attribute to the csproj it gets removed when updating the NuGet package. 如果我手动将所需的Condition="..."属性添加到csproj,则在更新NuGet软件包时将其删除。

Is there a way to automatically run a PowerShell script after NuGet adds the <Reference> node of the installed library? NuGet添加已安装库的<Reference>节点之后,是否可以自动运行PowerShell脚本? Or a way to somehow modify the node that NuGet adds when installing the package? 还是一种在安装软件包时以某种方式修改NuGet添加的节点的方法?

One option is to not have NuGet add the references by default. 一种选择是默认情况下不让NuGet添加引用。 Remove your lib folder and put the assemblies elsewhere in the package (refs?). 删除您的lib文件夹,然后将程序集放在程序包中的其他位置(引用?)。 Create a {NuGetPkgName}.targets file and put that in the packages build folder. 创建一个{NuGetPkgName} .targets文件,并将其放在程序包的build文件夹中。 That targets file will get "imported" into any project you add the NuGet pkg to. 该目标文件将“导入”到您添加NuGet pkg的任何项目中。 Then in your targets file you can add the references to your assemblies with the condition attributes you want. 然后,在目标文件中,可以使用所需的条件属性将引用添加到程序集中。

I haven't had time to implement Keith Hill's solution and had to settle on calling my script manually via a batch file. 我还没有时间实施Keith Hill的解决方案,因此不得不通过批处理文件手动调用我的脚本。 Here is the PowerShell script in case it helps someone. 如果有帮助,请使用PowerShell脚本。

The goal is to exclude the reference to MyPackageName when in Debug from all projects starting with MyProjectName. 目标是在调试中从MyProjectName开头的所有项目中排除对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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM