如何在构建期间使用 msbuild 生成文件 [英] How to generate files during build using msbuild

查看:22
本文介绍了如何在构建期间使用 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),但直到现在我似乎没有使用正确的关键字.

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/.网络工具/扩展-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="..DynamicCodeGeneratorinDebugDynamicCodeGenerator.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) 项目组,以便 Clean 目标正常工作.

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