添加构建后事件而不覆盖现有事件 [英] Add post build event without overwriting existing events

查看:19
本文介绍了添加构建后事件而不覆盖现有事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Powershell 脚本(由我的 NuGet 包运行),它向用户的 Visual Studio 项目添加构建后事件.

I have a Powershell script (run by my NuGet package) that adds a post build event to a user's Visual Studio project.

$project.Properties | where { $_.Name -eq "PostBuildEvent" } | foreach { $_.Value = "xcopy `"`$(ProjectDir)MyFolder`" `"`$(OutDir)`"" }

不幸的是,它目前覆盖现有的构建后事件.如果 Powershell 脚本不存在,如何修改它以追加我的构建事件?

Unfortunately it currently overwrites the existing post build event. How can I modify the Powershell script to append my build event if it does not already exist?

我以前从未使用过 Powershell,但我尝试将其简单地附加到 foreach 中,但这不起作用:

I've never used Powershell before but I tried simply appending it inside the foreach, but this didn't work:

$_.Value = "$_.Value`nxcopy `"`$(ProjectDir)MyFolder`" `"`$(OutDir)`""

只是给出:

System.__ComObject.Value xcopy "$(ProjectDir)MyFolder" "$(OutDir)"

System.__ComObject.Value xcopy "$(ProjectDir)MyFolder" "$(OutDir)"

推荐答案

我认为编辑 PostBuildEvent 属性是将构建后操作添加到用户项目的错误方法.我相信推荐的方法是将您的自定义操作放入目标中,然后将其导入项目文件中.从 NuGet 2.5 开始,如果您在包中包含构建"文件夹(与内容和工具处于同一级别)并且它包含一个 {packageid}.targets 文件或 {packageid}.props 文件,NuGet 会在您安装包时自动将 Import 添加到项目文件中.

I think editing the PostBuildEvent property is the wrong way to go about adding a post build action to a user's project. I believe the recommended way is to put your custom action into a target that is then imported into the project file. As of NuGet 2.5 if you include a 'build' folder in your package (at the same level as content and tools) and it contains a {packageid}.targets file or {packageid}.props file, NuGet will automatically add an Import to the project file when you install the package.

例如,您有一个名为 MyNuGet 的包.您创建一个文件 buildMyNuGet.targets 包含:

For example you have a package called MyNuGet. You create a file buildMyNuGet.targets containing:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <MySourceFiles Include="$(ProjectDir)MyFolder**" />
    </ItemGroup>
    <Target Name="MyNuGetCustomTarget" AfterTargets="Build">
        <Copy SourceFiles="@(MySourceFiles)" DestinationFolder="$(OutDir)" />
    </Target>
</Project>

这将创建一个配置为在标准构建目标之后运行的自定义目标.它会将一些文件复制到输出目录.

This creates a custom target that is configured to run after the standard Build target. It will copy some files to the output directory.

您无需在 install.ps1 中执行任何其他操作.当用户安装你的包时,NuGet 会自动添加一个 Import 到用户的 proj 文件中,并且你的目标将在 Build 目标运行后运行.

You do not need to do anything else in install.ps1. When the user installs your package, NuGet will automatically add an Import to the user's proj files and your target will run after the Build target is run.

这篇关于添加构建后事件而不覆盖现有事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆