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

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

问题描述

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

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

不幸的是,它当前覆盖现有的生成后事件. 如果构建事件尚不存在,如何将Powershell脚本修改为追加?

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

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

只给出:

System .__ ComObject.Value xcopy"$(ProjectDir)MyFolder""$(OutDir)"

解决方案

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

例如,您有一个名为MyNuGet的软件包.您创建一个文件build\MyNuGet.targets,其中包含:

<?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>

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

您不需要在install.ps1中进行任何其他操作.当用户安装您的软件包时,NuGet将自动将导入添加到用户的proj文件中,并且您的目标将在运行Build目标之后运行.

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)`"" }

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?

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)`""

just gives:

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

解决方案

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.

For example you have a package called MyNuGet. You create a file build\MyNuGet.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.

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天全站免登陆