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

编程入门 行业动态 更新时间:2024-10-22 18:48:43
如何使用msbuild在构建期间生成文件(How to generate files during build using msbuild)

有没有人知道如何在生成过程中生成代码文件的方式修改csproj文件而不实际引用文件?

一个过程如:

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

这样做的目的是创建一种使用roslyn而不是使用t4模板生成代码文件的方法,一旦您尝试根据属性执行某些操作,这些模板就非常难以使用。

因此,我计划提供一种方法来使用特殊的csharp文件(用于完整语法支持),以基于该特殊文件的内容以编程方式生成文件。

我花了几个星期的时间来研究互联网上的资源(主题为msbuild),但直到现在我似乎还没有使用正确的关键字。

这个对我来说是最具洞察力的一个: https : //www.simple-talk.com/dotnet/.net-tools/extending-msbuild/

我的猜测是,为了我的目的,正确的构建目标应该是“BeforeCompile”,以便以某种方式使用自定义代码文件填充构建过程。

有没有人对我的问题有经验,或者知道任何处理这项任务的特定资源?

我得到它的解决方案:

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

不幸的是,我没有按照建议使用属性组覆盖

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

Does anyone know how to modify a csproj file in a way to generate code files during build without actually referencing the files?

A process like :

create file, dynamically reference temporary file during build compiled assembly has additional members, depending on the files created during build

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.

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.

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.

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

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?

Solution i got it working with:

<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

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

最满意答案

生成代码文件可以通过msbuild任务或msbuild内联任务来实现。 由您来生成正确的代码。 您必须关注的一件事是创建输出项参数,以便将其附加到@(编译)项。 您可以使用$(IntDir)位置来查找新生成的文件,并将它们添加到@(FileWrites)项目组,以便Clean目标正常工作。

完成任务后,必须在项目中使用它,如下所示:

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

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>

更多推荐

文件,使用,files,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-23 18:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   msbuild   build   generate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!