gpt4 book ai didi

c# - 如何在编译开始之前但在生成中间文件之后运行 MSBuild 任务?

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:14 26 4
gpt4 key购买 nike

背景:StyleCop 提示自动生成的文件格式不正确,导致在我尝试构建项目时出现许多警告。自动生成的文件位于 obj/ 中我的项目目录,我想创建一个 MSBuild 任务,它在 // <auto-generated/> 之前在编译之前(但在生成之后)到这个文件,这样 StyleCop 就不会提示。

问题:我有以下 MSBuild 代码

<!-- StyleCop complains about a file that's auto-generated by the designer,
so we need to prepend 'auto-generated' to it beforehand. -->
<Target Name="BeforeCompile" DependsOnTargets="MarkGeneratedFiles" />

<Target Name="MarkGeneratedFiles">
<PropertyGroup>
<GeneratedFilePath>$(MSBuildThisFileDirectory)obj\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).Program.cs</GeneratedFilePath>
</PropertyGroup>

<InsertIntoFile FilePath="$(GeneratedFilePath)" LineNumber="1" Text="// &lt;auto-generated/&gt;" />
</Target>

<!-- Code taken from http://stackoverflow.com/a/21500030/4077294 -->
<UsingTask
TaskName="InsertIntoFile"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<FilePath ParameterType="System.String" Required="true" />
<LineNumber ParameterType="System.Int32" Required="true" />
<Text ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Code Type="Fragment" Language="cs">
<![CDATA[
// By tradition, text file line numbering is 1-based
var lines = File.Exists(FilePath)
? File.ReadAllLines(FilePath).ToList()
: new List<String>(1);
lines.Insert(Math.Min(LineNumber - 1, lines.Count), Text);
File.WriteAllLines(FilePath, lines);
return true;
]]>
</Code>
</Task>
</UsingTask>

我要修改的文件的文件名是obj/Debug/netcoreapp1.0/BasicCompiler.Tests.Program.cs .在上面的片段中,我有一个 BeforeCompile取决于 MarkGeneratedFiles 的目标,它继续尝试插入 // <auto-generated/>在该文件的第一行之前。

我已经测试过,如果生成的文件已经存在,这似乎工作正常。但是,如果我删除 obj/目录或我从另一台机器构建,我得到这个错误:

"C:\cygwin64\home\james\Code\cs\BasicCompiler\src\BasicCompiler.Tests\BasicCompiler.Tests.csproj" (default target) (1) ->
(MarkGeneratedFiles target) ->
C:\cygwin64\home\james\Code\cs\BasicCompiler\src\BasicCompiler.Tests\BasicCompiler.Tests.csproj(68,5): error MSB4018: The "InsertIntoFile" task failed unexpectedly.\r
C:\cygwin64\home\james\Code\cs\BasicCompiler\src\BasicCompiler.Tests\BasicCompiler.Tests.csproj(68,5): error MSB4018: System.IO.DirectoryNotFoundException: Could not find
a part of the path 'C:\cygwin64\home\james\Code\cs\BasicCompiler\src\BasicCompiler.Tests\obj\Debug\netcoreapp1.0\BasicCompiler.Tests.Program.cs'.\r

基本上看起来目标在文件生成之前就已经运行了,所以没有什么可以放在文本前面的。有没有办法在生成此文件之后编译之前运行它?

附加说明:到目前为止,我已经查看了所有特殊目标名称 here并尝试同时使用 BeforeBuildBeforeCompile .

此外,因为我使用的是"new"StyleCop,所以我不能输入 <ExcludeFromStyleCop>在我的项目文件中。参见 https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1145

最佳答案

我设法解决了这个问题;查看@stijn 的 super 有用的评论 here .

  • 首先,我从命令行运行了 msbuild/v:detailed。这会增加 MSBuild 的冗长程度,以便您更详细地了解正在发生的事情,例如您可以看到正在运行的每个目标的名称。

  • 我在日志中搜索了生成文件的目标名称。在我的例子中,结果是 GenerateProgramFiles

  • 我用 AfterTargets="GenerateProgramFiles" 标记了我的自定义目标,因此它会在文件生成后运行。

关于c# - 如何在编译开始之前但在生成中间文件之后运行 MSBuild 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41318530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com