[Tool][Memo]如何編輯某個 StyleCop 設定檔

  • 6138
  • 0

[Tool][Memo]如何編輯某個 StyleCop 設定檔

前言

使用 .NET 進行開發的朋友們,應該對 StyleCop 不會太陌生 (如果你還很陌生,請參考: [Tool]程式碼風格分析-StyleCop),而在實務上使用時,多多少少會因為 legacy code ,或是團隊的開發方式來客製化自己的 rule 。

大家想必應該很常在 Visual Studio 上,直接調整 StyleCop 的 setting ,但是這樣設定所產生的 setting 檔,其實基本上只有 base on default 的 setting ,再額外增加的 rule 而已。產生的 setting 檔案,也會被額外置放在專案的目錄中,並且 Visual Studio 的 setting 會變成 merge setting 。

什麼意思?我們來看一下 Setting.StyleCop 的檔案內容結構,用 notepad++ 打開,其實就是 xml 的結構而已,如下所示:

<StyleCopSettings Version="105">
  <Parsers>
    <Parser ParserId="StyleCop.CSharp.CsParser">
      <ParserSettings>
        <CollectionProperty Name="GeneratedFileFilters">
          <Value>\.g\.cs$</Value>
          <Value>\.generated\.cs$</Value>
          <Value>\.g\.i\.cs$</Value>
        </CollectionProperty>
      </ParserSettings>
    </Parser>
  </Parsers>
  <Analyzers>
    <Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
      <Rules>
        <Rule Name="FieldNamesMustNotBeginWithUnderscore">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
        <Rule Name="VariableNamesMustNotBePrefixed">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
        <Rule Name="FieldNamesMustNotUseHungarianNotation">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
      </Rules>
      <AnalyzerSettings>
        <CollectionProperty Name="Hungarian">
          <Value>as</Value>
          <Value>do</Value>
          <Value>id</Value>
          <Value>if</Value>
          <Value>in</Value>
          <Value>is</Value>
          <Value>my</Value>
          <Value>no</Value>
          <Value>on</Value>
          <Value>to</Value>
          <Value>ui</Value>
        </CollectionProperty>
      </AnalyzerSettings>
    </Analyzer>
    <Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
      <Rules>
        <Rule Name="DocumentationTextMustContainWhitespace">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
        <Rule Name="FileMustHaveHeader">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
        <Rule Name="PropertySummaryDocumentationMustMatchAccessors">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
      </Rules>
      <AnalyzerSettings />
    </Analyzer>
    <Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
      <Rules>
        <Rule Name="UsingDirectivesMustBePlacedWithinNamespace">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
      </Rules>
      <AnalyzerSettings />
    </Analyzer>
  </Analyzers>
</StyleCopSettings>

可以看到 AnalyzerId 就是 rule 的 category ,而 rule 則是調整的部份。也因為 setting 檔是 xml 格式,所以要針對多個設定檔做 merge ,自然不是一件難事。

 

那麼,假設使用 Visual Studio 所產生的 StyleCop 設定檔,是額外再客製化的 rule ,那我們應該怎麼樣直接對某一個 StyleCop 設定檔進行編輯呢?

 

透過 IDE 工具: StyleCopSettingsEditor.exe

有心一點的朋友,肯定可以找到這個 IDE 工具: StyleCopSettingsEditor.exe 。(不然 Visual Studio 要怎麼打開編輯設定檔的介面?)

預設安裝完 StyleCop 的路徑:C:\Program Files (x86)\StyleCop 4.7 ,這邊要注意一下,會依照 StyleCop 的版本不同而有所差異 (這一點我一直很納悶,一點都不人性化)。在同一層目錄下就可以找到 StyleCopSettingsEditor.exe 。

不過,這個 exe 檔,是 console mode 的,請在 [執行] 輸入 cmd ,移至 StyleCopSettingsEditor.exe 目錄下,執行 StyleCopSettingsEditor.exe ,第一個參數即為要編輯的 StyleCop 設定檔路徑。

舉例來說,我要編輯 C:\test\Settings123.StyleCop 這個設定檔,畫面如下:

image

編輯完存檔即可。

 

結論

會有這需求,其實是為了調整 CI 上每個專案的共同設定,這樣可以讓所有 project 的 style 一致,也可以省去大家需要把自己 project 所額外設定的 StyleCop 設定檔都簽入進版控,反而難以管理。

希望這一篇 memo ,會幫到未來也有此需求的朋友啦。


blog 與課程更新內容,請前往新站位置:http://tdd.best/