StyleCop MS 构建魔法?谁在称呼 StyleCop 为目标?

发布于 2024-08-22 17:22:38 字数 1285 浏览 5 评论 0原文

在我们的项目文件中,我们使用 StyleCop 并在构建过程中运行它。我们修改了项目文件以包含 StyleCop 目标,如下所示:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="..\..\Tools\Microsoft\StyleCop\v4.3\Microsoft.StyleCop.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
     Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

问题是为什么这有效?谁在调用 Microsoft.StyleCop.targets 文件中定义的 StyleCop 目标?

据我所知,开始构建时运行的唯一目标是“Build”目标。我在 Microsoft.StyleCop.targets 文件之外的任何地方都找不到对“StyleCop”目标的任何引用。话又说回来,为什么会这样称呼它呢?

我想知道的原因是,如果我们可以为我们自己的自定义任务做类似的事情,那就太酷了。因此,我们不必编辑所有 78 个 csproj 文件,而是可以导入我们自己的一般目标,例如:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="Common.targets" />

然后只需在该文件中导入 StyleCop,以及需要在每个项目的基础上完成的所有其他任务。

请帮助我理解。

In our project files we are using StyleCop and are running it during the build process. We've modified our project files to include the StyleCop targets like so:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="..\..\Tools\Microsoft\StyleCop\v4.3\Microsoft.StyleCop.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
     Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

The question is why does this work? Who is calling the StyleCop target defined in the Microsoft.StyleCop.targets file?

As far as I can tell, the only target being run when you start the build, is the "Build" target. I can't find any references to the "StyleCop" target anywhere outside the Microsoft.StyleCop.targets file. So again, why is it being called?

The reason I want to know is because it would be cool if we could do something similar for our own custom tasks. So instead of editing all of our 78 csproj files, we could just import our own general target like:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="Common.targets" />

And then just import StyleCop in that file instead, along with all our other tasks that need to be done on a per project basis.

Please help me understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋千易 2024-08-29 17:22:38

尤里卡!

秘密武器是 StyleCop 目标文件中的以下几行:

<PropertyGroup>
  <BuildDependsOn>$(BuildDependsOn);StyleCop</BuildDependsOn>
  <RebuildDependsOn>StyleCopForceFullAnalysis;$(RebuildDependsOn)</RebuildDependsOn>
</PropertyGroup>

Microsoft.Common.targets 中的“Build”目标声明如下:

<Target Name="Build"
        Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
        DependsOnTargets="$(BuildDependsOn)"
        Outputs="$(TargetPath)" />

这意味着“BuildDependsOn”属性中列出的任何目标都将在构建过程中被调用。这不是很好吗? :)

Eureka!

The secret sauce is the following lines in the StyleCop targets file:

<PropertyGroup>
  <BuildDependsOn>$(BuildDependsOn);StyleCop</BuildDependsOn>
  <RebuildDependsOn>StyleCopForceFullAnalysis;$(RebuildDependsOn)</RebuildDependsOn>
</PropertyGroup>

The "Build" target in Microsoft.Common.targets is declared like so:

<Target Name="Build"
        Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
        DependsOnTargets="$(BuildDependsOn)"
        Outputs="$(TargetPath)" />

This means that any target listed in the "BuildDependsOn" property will get called during the build. Isn't that nice? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文