如何将 StyleCop 与 TeamCity 结合使用

发布于 2024-11-15 20:32:29 字数 347 浏览 5 评论 0原文

有人从 TeamCity 运行 StyleCop 取得过成功吗?

我知道 StyleCop 支持命令行模式,但我不确定这将如何集成到 TeamCity 的报告输出中。

我检查了在这里找到的这个插件: https://bitbucket.org/metaman/teamcitydotnetcontrib/src/753712db5df7 /stylecop/

但是无法让它运行。

我正在使用 TeamCity 6.5.1(最新)。

Has anyone had any success with running StyleCop from TeamCity?

I know StyleCop supports a command line mode, however i am not sure how this will integrate into the report output by TeamCity.

I've checked out this plugin found here: https://bitbucket.org/metaman/teamcitydotnetcontrib/src/753712db5df7/stylecop/

However could not get it running.

I am using TeamCity 6.5.1 (latest).

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

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

发布评论

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

评论(4

赤濁 2024-11-22 20:32:29

我不知道您对 MSBuild 有多熟悉,但您应该能够在 TC 6 及更高版本中添加新的构建步骤,并将 MSBuild 设置为构建运行程序,并将其指向一个 .proj 文件,该文件执行类似于如下:

<Target Name="StyleCop">

  <!-- Create a collection of files to scan -->
  <CreateItem Include="$(SourceFolder)\**\*.cs">
    <Output TaskParameter="Include" ItemName="StyleCopFiles" />
  </CreateItem>

  <StyleCopTask
    ProjectFullPath="$(MSBuildProjectFile)"
    SourceFiles="@(StyleCopFiles)"
    ForceFullAnalysis="true"
    TreatErrorsAsWarnings="true"
    OutputFile="StyleCopReport.xml"
    CacheResults="true" />

  <Xslt Inputs="StyleCopReport.xml"
     RootTag="StyleCopViolations" 
     Xsl="tools\StyleCop\StyleCopReport.xsl"
     Output="StyleCopReport.html" />

  <XmlRead XPath="count(//Violation)" XmlFileName="StyleCopReport.xml">
    <Output TaskParameter="Value" PropertyName="StyleCopViolations" />
  </XmlRead>

  <Error Condition="$(StyleCopViolations) > 0" Text="StyleCop found $(StyleCopViolations) broken rules!" />

</Target>

如果您不想因 StyleCop 错误而导致构建失败,请将 Error 任务设置为 Warning

您还需要将以下内容添加到 .proj 文件中:

<UsingTask TaskName="StyleCopTask" AssemblyFile="$(StyleCopTasksPath)\Microsoft.StyleCop.dll" />

Microsoft.StyleCop.dll 包含在 StyleCop 安装中,并且您需要适当地设置路径。

要在 TeamCity 中查看输出的 StyleCop 结果,您需要使用适当的 .xsl 文件(在上面的脚本中称为 StyleCopReport.xsl)将 .xml StyleCop 报告转换为 HTML。

要在 TeamCity 中显示 HTML 文件,您需要创建一个 .html 输出中的工件,然后 将该工件包含在构建结果中

.NET 中的持续集成一书是一个很好的资源。

I don't know how familiar you are with MSBuild, but you should be able to add a new Build Step in TC 6 and above, and set MSBuild as the build runner, and point it to a .proj file which does something similar to the following:

<Target Name="StyleCop">

  <!-- Create a collection of files to scan -->
  <CreateItem Include="$(SourceFolder)\**\*.cs">
    <Output TaskParameter="Include" ItemName="StyleCopFiles" />
  </CreateItem>

  <StyleCopTask
    ProjectFullPath="$(MSBuildProjectFile)"
    SourceFiles="@(StyleCopFiles)"
    ForceFullAnalysis="true"
    TreatErrorsAsWarnings="true"
    OutputFile="StyleCopReport.xml"
    CacheResults="true" />

  <Xslt Inputs="StyleCopReport.xml"
     RootTag="StyleCopViolations" 
     Xsl="tools\StyleCop\StyleCopReport.xsl"
     Output="StyleCopReport.html" />

  <XmlRead XPath="count(//Violation)" XmlFileName="StyleCopReport.xml">
    <Output TaskParameter="Value" PropertyName="StyleCopViolations" />
  </XmlRead>

  <Error Condition="$(StyleCopViolations) > 0" Text="StyleCop found $(StyleCopViolations) broken rules!" />

</Target>

If you don't want to fail the build on a StyleCop error, then set the Error task to be Warning instead.

You'll also need to add the following to your .proj file:

<UsingTask TaskName="StyleCopTask" AssemblyFile="$(StyleCopTasksPath)\Microsoft.StyleCop.dll" />

Microsoft.StyleCop.dll is included in the StyleCop installation, and you'll need to set your paths appropriately.

To see the outputted StyleCop results in TeamCity, you will need to transform the .xml StyleCop report to HTML using an appropriate .xsl file (called StyleCopReport.xsl in the script above).

To display the HTML file in TeamCity, you'll need to create an artifact from this .html output, and then include that artifact in the build results.

The Continuous Integration in .NET book is a great resource.

摘星┃星的人 2024-11-22 20:32:29

您是否知道 teamcity 仅从 msbuild 提供特定属性?
无需服务消息,请参阅:
http://confluence.jetbrains.net/display/TCD65/MSBuild+Service+Tasks

所以您不必添加自定义报告页面。
使用构建统计信息,例如,

<TeamCitySetStatus Status="$(AllPassed)" Text="Violations: $(StyleCopViolations)" />

您也可以记录统计信息:

<TeamCityReportStatsValue Key="StyleCopViolations" Value="$(StyleCopViolations)" />

然后创建一个要显示的自定义图表,并且您的 msbuild 输出中已经存在违规情况。
编辑 main-config.xml 并添加:

<graph title="Style Violations" seriesTitle="Warning">
   <valueType key="StyleCopViolations" title="Violations" buildTypeId="bt20"/>
 </graph>

其中 buildTypeId="bt20" bt20 是您的样式构建。

Did you know that teamcity provides specific properties just from msbuild?
No need for the service messages, see:
http://confluence.jetbrains.net/display/TCD65/MSBuild+Service+Tasks

So you dont have to add a custom report page.
Use the build stats e.g.

<TeamCitySetStatus Status="$(AllPassed)" Text="Violations: $(StyleCopViolations)" />

you can then log the statistic too:

<TeamCityReportStatsValue Key="StyleCopViolations" Value="$(StyleCopViolations)" />

And then create a custom graph to display, and you already have the violations in your msbuild output.
edit main-config.xml and add:

<graph title="Style Violations" seriesTitle="Warning">
   <valueType key="StyleCopViolations" title="Violations" buildTypeId="bt20"/>
 </graph>

Where buildTypeId="bt20" bt20 is your style build.

千纸鹤 2024-11-22 20:32:29

我迟到了,但实现此目的的一个非常简单的方法是安装 StyleCop.MSBuild 您想要使用 StyleCop 分析的任何项目中的 NuGet 包。

安装软件包后,StyleCop 分析将在您执行的每个构建上运行,无论在何处或如何调用,例如 VS、命令行、msbuild、psake、rake、fake、bake、nant、build服务器等。无需特殊操作。

如果您希望在 StyleCop 规则被破坏时构建失败,您只需在每个适当的构建配置下将以下元素添加到项目文件中,例如

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
    ...

,这将适用于每个构建,无论在哪里以及如何调用它。

I'm late to the show here but a very easy way to achieve this is to install the StyleCop.MSBuild NuGet package in any project which you want to analyse with StyleCop.

After installing the package, StyleCop analysis will run on every build you do, regardless of where or how it is invoked, e.g VS, command line, msbuild, psake, rake, fake, bake, nant, build server, etc. No special actions are required.

If you want the build to fail when StyleCop rules are broken you just need to add the following element to your project file under each appropriate build configuration, E.g.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
    ...

Again, this will work on every build, regardless of where and how it is invoked.

绝不放开 2024-11-22 20:32:29

这里有一个(新的?)StyleCop 第三方 TeamCity 插件(尽管我还没有还尝试过)。

更新:据我所知,最新版本仅适用于 TeamCity 7(或者我做错了什么)。另外,我的(虚拟)构建服务器非常慢,因此即使重新启动服务后,StyleCop 运行程序也需要一段时间才能出现在 Web 界面中。

我做的另一件愚蠢的事情是没有正确阅读自述文件:您必须解压下载的 zip,并使用内部的 zip。

我最初也只是从“包含”选项(用于构建步骤)中的 .cs 文件列表开始,但这不起作用;不过,到 sln 或 csproj 文件的链接确实有效。

There's a (new?) third-party TeamCity plugin for StyleCop here, (though I haven't tried it yet).

UPDATE: as far as I can tell, the latest version only works with TeamCity 7 (or I did something wrong). Also, I have a very slow (virtual) build server, so even after the services were restarted, it took a while for the StyleCop runner to appear in the web interface.

Another stupid thing I did was not read the readme properly: you have to unzip the downloaded zip, and use the zip inside.

I also originally started with just a list of .cs files in the "Include" option (for the build step), but that didn't work; links to sln or csproj files do work though.

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