LoginSignup
6
5

More than 5 years have passed since last update.

StyleCop.AnalyzersをCLIで動かせるツールを.NET Coreで作った

Last updated at Posted at 2019-03-16

StyleCop.AnalyzersをCLIで使いたい
作った理由などについてはこちらで

ある程度動くようになったのでGitHubに公開しました
https://github.com/rookxx/StyleCopAnalyzers.CLI

なぜUnityタグついてるのか

筆者が直近で開発に関わっているプロジェクトがUnityのゲームという理由だけです
そして、UnityプロジェクトでStyleCop.Analyzersが扱いづらい理由があります
StyleCop.Analyzersの設定はcsprojに設定するのですが、Unityはcsprojファイルを自動で生成します
またcsproj/slnといったファイルはgitなどで共有されません(各PCのローカルパスなどが記述されてるため)

f3ea22c159f2e7536e1497e81e198b0c.png
こんな感じになってます

つまり、VisualStudio/RiderなどでStyleCop.Analyzersの設定を共有することはできません
個々人で設定することは可能ですが。

使い方

各自でビルドする必要があります。各プラットフォーム用のバイナリまでは出力していませんが、
.NET Core SDK 2.1以上の環境でリポジトリをcloneし、

dotnet publish -c release

と出力してもらえれば。
※不明な点などはここのコメントかPR/Issueなどで上げてもらえると助かります

使い方などはつたない英語でGitHub上のREADMEに記載していますのでそちらを見てもらえれば
Unity以外のプロジェクトでも使用できるように、sln/csprojファイル指定でもチェックできるようになっています
ディレクトリ指定でも内部のC#ファイルを全部走査してチェックを行います

実行結果

このようにコンソール上に違反しているルールなどが出力されます

# dotnet ./StyleCopAnalyzersCmd.dll check ./TestCSharpCodes/
SA1402 : StyleCopAnalyzersCmd/DiagnosticWriter/OutputKind.cs : 23: File may only contain a single type
SA1633 : StyleCopAnalyzersCmd/DiagnosticWriter/OutputKind.cs : 1: The file header is missing or not located at the top of the file.
SA1649 : StyleCopAnalyzersCmd/DiagnosticWriter/OutputKind.cs : 10: File name should match first type name.
SA1633 : StyleCopAnalyzersCmd/DiagnosticWriter/IDiagnosticWriter.cs : 1: The file header is missing or not located at the top of the file.
SA1633 : StyleCopAnalyzersCmd/Extensions.cs : 1: The file header XML is invalid.
SA1633 : StyleCopAnalyzersCmd/DiagnosticWriter/ConsoleWriter.cs : 1: The file header is missing or not located at the top of the file.
SA1633 : StyleCopAnalyzersCmd/DiagnosticWriter/XmlWriter.cs : 1: The file header is missing or not located at the top of the file.
SA1101 : StyleCopAnalyzersCmd/DiagnosticWriter/XmlWriter.cs : 32: Prefix local calls with this
SA1101 : StyleCopAnalyzersCmd/DiagnosticWriter/XmlWriter.cs : 33: Prefix local calls with this

StyleCopを使用しているプロジェクトの場合は、XML出力もサポートしたのでコマンド置き換えだけで対応できるようにもしてみました

# dotnet ./StyleCopAnalyzersCmd.dll check ./TestCSharpCodes/ -f xml
<?xml version="1.0" encoding="utf-8"?>
<StyleCopViolations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Violations>
    <Violation Section="StyleCopAnalyzersCmd.OutputKindExtensions" LineNumber="23" Source="/mnt/g/git/StyleCopAnalyzersCmd/StyleCopAnalyzersCmd/bin/release/netcoreapp2.1/ubuntu.16.04-x64/publish/TestCSharpCodes/StyleCopAnalyzersCmd/DiagnosticWriter/OutputKind.cs" RuleNamespace="StyleCop.CSharp.MaintainabilityRules" Rule="SA1402">
      <Message>File may only contain a single type</Message>
    </Violation>
    <Violation Section="StyleCopAnalyzersCmd.OutputKindExtensions" LineNumber="1" Source="/mnt/g/git/StyleCopAnalyzersCmd/StyleCopAnalyzersCmd/bin/release/netcoreapp2.1/ubuntu.16.04-x64/publish/TestCSharpCodes/StyleCopAnalyzersCmd/DiagnosticWriter/OutputKind.cs" RuleNamespace="StyleCop.CSharp.DocumentationRules" Rule="SA1633">
      <Message>The file header is missing or not located at the top of the file.</Message>
    </Violation>
    <Violation Section="StyleCopAnalyzersCmd.OutputKindExtensions" LineNumber="10" Source="/mnt/g/git/StyleCopAnalyzersCmd/StyleCopAnalyzersCmd/bin/release/netcoreapp2.1/ubuntu.16.04-x64/publish/TestCSharpCodes/StyleCopAnalyzersCmd/DiagnosticWriter/OutputKind.cs" RuleNamespace="StyleCop.CSharp.DocumentationRules" Rule="SA1649">
      <Message>File name should match first type name.</Message>
    </Violation>
    <Violation LineNumber="1" Source="/mnt/g/git/StyleCopAnalyzersCmd/StyleCopAnalyzersCmd/bin/release/netcoreapp2.1/ubuntu.16.04-x64/publish/TestCSharpCodes/StyleCopAnalyzersCmd/DiagnosticWriter/IDiagnosticWriter.cs" RuleNamespace="StyleCop.CSharp.DocumentationRules" Rule="SA1633">
      <Message>The file header is missing or not located at the top of the file.</Message>
    </Violation>
    <Violation Section="StyleCopTester.Extensions" LineNumber="1" Source="/mnt/g/git/StyleCopAnalyzersCmd/StyleCopAnalyzersCmd/bin/release/netcoreapp2.1/ubuntu.16.04-x64/publish/TestCSharpCodes/StyleCopAnalyzersCmd/Extensions.cs" RuleNamespace="StyleCop.CSharp.DocumentationRules" Rule="SA1633">
      <Message>The file header XML is invalid.</Message>
    </Violation>

欠点

既存のStyleCopと比べると実行速度が大分遅いです
理由は、StyleCop.AnalyzersがRoslynベースのアナライザであるため静的解析を行うためにコンパイルまで必要なためです
CLI上では表示していませんがコンパイルエラーの結果まで受け取っています
ですが、とりあえず動くようにはなったので公開してみた次第です

後書き

需要があるかはわかりませんが、StyleCop.AnalyzersでCLIでコーディング規約のチェックができるようにしてみました
次は自動的にコードの修正を行えるまでの機能追加を行おうかと思ってます

6
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5