Stylecop ビルド設定編

Stylecopでビルド時にチェックを行ないたい時の設定
NuGetからインストールした場合は自動的にこの状態に設定される。
チェックを行ないたくない場合は逆に削除する。

次の設定をプロジェクトファイルに追記するとビルド時にワーニングが出るようになる

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(ProgramFiles(x86))\MSBuild\StyleCop\v4.7\StyleCop.targets" />

1行目:既にある場合は不要
2行目:Stylecopのバージョンに合わせて変更する(この場合は4.7)
以下の行の直前に挿入する

<!-- 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>
  -->

追記後にビルドするとワーニングが表示されるようになる。

 

更にコンパイルエラーで停止させたい時は以下を追記する。

<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>

追記例(14行目)

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{98A05456-A314-4C0F-92AD-E9DFA3FC7D4F}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ConsoleApplication20131127</RootNamespace>
    <AssemblyName>ConsoleApplication20131127</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
  </PropertyGroup>

追記後にビルドするとワーニング検出時にコンパイルが停止する。

 

参考サイト

OPC Diary  StyleCopの導入と使用