如何在使用msbuild的生成过程中生成文件 [英] How to generate files during build using msbuild

查看:133
本文介绍了如何在使用msbuild的生成过程中生成文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何以某种方式修改csproj文件以在生成过程中生成代码文件而无需实际引用这些文件吗?

类似这样的过程:

  • 创建文件,
  • 在构建过程中动态引用临时文件
  • 编译后的程序集具有其他成员,具体取决于在构建过程中创建的文件

这样做的目的是创建一种使用roslyn而不是t4模板生成代码文件的方法,一旦尝试根据属性来做某事,这将非常尴尬.

The purpose of this is to create a way of generating code files using roslyn instead of using t4 templates, which are very awkward to use once you're trying to do something depending on attributes.

因此,我正计划提供一种使用特殊csharp文件(出于对语法的全面支持)的方式,根据该特殊文件的内容以编程方式生成文件.

Hence i am planning on providing a way to use a special csharp file (for full syntax support) to generate files programatically based on the contents of that special file.

我花了几周时间(以msbuild为主题)调查了Internet资源,但直到现在我似乎都没有使用正确的关键字.

I've spent a couple of weeks looking into resources on the internet (with the topic msbuild), but until now it seems i didn't use the right keywords.

这个对我来说是最有见地的: https://www.simple-talk.com/dotnet/. net-tools/extending-msbuild/

This one has been the most insightful one to me yet: https://www.simple-talk.com/dotnet/.net-tools/extending-msbuild/

我的猜测是,出于我的目的,正确的构建目标应该是"BeforeCompile",以便以某种方式使用自定义代码文件填充构建过程.

My guess is, that the correct build target for my purpose should be "BeforeCompile" in order to somehow populate the build process with custom code files.

是否有人对我的问题有经验,或者知道处理该任务的任何特殊资源?

Does anyone have experience with my issue, or is aware of any particular resources which deal with the task?

解决方案,我可以使用它:

<UsingTask TaskName="DynamicCodeGenerator.DynamicFileGeneratorTask" AssemblyFile="..\DynamicCodeGenerator\bin\Debug\DynamicCodeGenerator.dll" />
<Target Name="DynamicCodeGeneratorTarget" BeforeTargets="BeforeBuild;BeforeRebuild">
    <DynamicFileGeneratorTask>
        <Output ItemName="Generated" TaskParameter="GeneratedFilePaths" />
    </DynamicFileGeneratorTask>
    <ItemGroup>
        <Compile Include="@(Generated)" />
        <FileWrites Include="@(Generated)" />
        <!-- For clean to work properly -->
    </ItemGroup>
</Target>

不幸的是,我没有像建议的那样使用它来与属性组替代一起使用

Unfortunately i did not get it to work with a propertygroup override as suggested

更新:此链接也很有趣: https: //github.com/firstfloorsoftware/xcc/blob/master/FirstFloor.Xcc/Targets/Xcc.targets

Update: This link is interesting too: https://github.com/firstfloorsoftware/xcc/blob/master/FirstFloor.Xcc/Targets/Xcc.targets

推荐答案

可以通过 msbuild任务 msbuild内联任务.取决于您是否生成正确的代码.您必须注意的一件事是创建输出项目参数,以将其附加到@(Compile)项目.您可以使用$(IntDir)位置查找新生成的文件,并将它们添加到@(FileWrites)项组中,以使清理目标正常工作.

Generating the code file can be achieved by msbuild task or msbuild inline task. It is up to you to generate the proper code. One thing that you must care of is creating output item parameter in order to append it to the @(Compile) item. You can use $(IntDir) location to locate your newly generated file, and add them to the @(FileWrites) item group in order for Clean target work properly.

完成任务编写后,必须像下面这样在项目中使用它:

When you finish writing your task, you must use it in your project like this:

<UsingTask TaskName="TaskTypeFullName" AssemblyFile="YourAssembly.dll"/>
<PropertyGroup>
<!-- Here you need to experiment with [Build/Compile/SomeOther]DependsOn property -->
    <BuildDependsOn>
        MyCodeGenerator;
        $(BuildDependsOn)
    </BuildDependsOn>
</PropertyGroup>

<Target Name="MyCodeGenerator">
    <YourTaskName>
        <Output ItemName="Generated" TaskParameter="GeneratedFiles" />
    </YourTaskName>
    <ItemGroup>
        <Compile Include="@(Generated)" /> 
        <FileWrites Include="@(Generated)" /> <!-- For clean to work properly -->
    </ItemGroup>
</Target>

这篇关于如何在使用msbuild的生成过程中生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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