[料理佳餚] Jenkins 增加 StyleCop Plugin

  • 1522
  • 0
  • CI
  • 2015-11-23

靜態程式碼分析工具可以協助團隊統一程式碼的風格,讓團隊在程式碼的撰寫上有統一的規範。

有關於 StyleCop 的相關說明,可以參考這篇文章 [如何提升系統品質-Day17]品質量測工具-StyleCop

安裝 Violations Plugin

Violations Plugin 可以讓 StyleCop 檢查到違反風格的部分,以較為友善的方式呈現出來,而且不只是 StyleCop 還支援 FxCop、CSSLint、JSLint…等多種靜態程式碼分析的 Plugin。

安裝很簡單,只要到「管理 Jenkins」->「管理外掛程式」->「可用的」,搜尋 Violations,打勾按「直接安裝」即可。

安裝 StyleCop

StyleCop CodePlex 下載 StyleCop 並在 Jenkins Server 上安裝起來。

添加建置步驟 - Build a Visual Studio project or solution using MSBuild

StyleCop 是利用專案建置的同時帶上來去分析專案內的程式碼,在這邊我們做了一個通用的專案檔給 MSBuild 去執行。

在 xml 裡面我將不要分析的檔案設定在 <AnalysisFileExcludes> 標籤內,最後將分析結果輸出到 StyleCopReport.xml。

<project defaulttargets="StyleCop" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <propertygroup>
    <!-- file want to scan -->
    <analysisfileincludes>$(MSBuildStartupDirectory)\**\*.cs</analysisfileincludes>
    <!-- file dont want to scan -->
    <analysisfileexcludes>
      $(MSBuildStartupDirectory)\**\*Test.cs;
      $(MSBuildStartupDirectory)\**\*AssemblyInfo.cs;
      $(MSBuildStartupDirectory)\**\*.Designer.cs;
      $(MSBuildStartupDirectory)\**\Reference.cs;
      $(MSBuildStartupDirectory)\**\Configuration.cs;
      $(MSBuildStartupDirectory)\**\*.feature.cs;
      $(MSBuildStartupDirectory)\**\*Steps.cs;
    </analysisfileexcludes>
  </propertygroup>

  <usingtask assemblyfile="$(MSBuildExtensionsPath)\..\StyleCop 4.7\StyleCop.dll" taskname="StyleCopTask">

    <target name="StyleCop">
      <!-- Create a collection of files to scan -->
      <createitem exclude="$(AnalysisFileExcludes)" include="$(AnalysisFileIncludes)">
        <output itemname="StyleCopFiles" taskparameter="Include">&nbsp;</output>
      </createitem>

      <!-- Execute stylecop scan -->
      <stylecoptask cacheresults="true" forcefullanalysis="true" outputfile="$(MSBuildStartupDirectory)\StyleCopReport.xml" projectfullpath="$(MSBuildProjectFile)" sourcefiles="@(StyleCopFiles)" treaterrorsaswarnings="true">
      </stylecoptask>
    </target>
  </usingtask>
</project>

把上面的 xml 的內容另存成新檔 - StyleCopSetting.xml 填入 MSBuild Build File 這個欄位就行了

添加建置後動作 - Report Violations

把輸出後的 StyleCopReport.xml 填入 stylecop 欄位。

Demo

只要有違反風格的程式碼檔案,Violations Plugin 都會幫忙整理出來。

點進去之後還能看到是哪一段程式碼違反了哪一種規定。

 

相關資源

C# 指南
ASP.NET 教學
ASP.NET MVC 指引
Azure SQL Database 教學
SQL Server 教學
Xamarin.Forms 教學