Command Line Options

Aug 10, 2010 at 4:53 PM
It would be really cool if the StyleCop team could add features to enable Build Server automation. In the past I have used StyleCopCmd and this has worked, but it appears that the project has been abandoned. http://sourceforge.net/projects/stylecopcmd/develop
Aug 11, 2010 at 2:09 AM

Have you tried connecting StyleCop to your projects using their included build tasks? If you install the MSBuild support from the StyleCop installation package you can do so by adding the following to your csproj file:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\StyleCop\v4.4\Microsoft.StyleCop.targets" />

It needs to be placed underneath the CSharp targets import statement, which means it should look like:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\StyleCop\v4.4\Microsoft.StyleCop.targets" />

As long as you install StyleCop with MSBuild support on your build server it will work just fine. Our team used this method before we switched to using a TFS check-in policy and never had any difficulty with it.

Hope that helps,

- Jeff

Aug 11, 2010 at 1:24 PM

We actually have this setup up in each of our projects for the devs on our team and it works as expected.  However, on the build server, I've never seen the build fail because of StyleCop violations.  Maybe I should investigate that further since I think that would work for us.  Thanks for your advice!

Aug 11, 2010 at 4:20 PM

Binding to MSbuild is a much better alternative than using any sort of command line tool. The reason for this is that your code may have one or more #if\#endif regions. StyleCop needs to know which of these regions to include, and which to ignore. The most reliable way is to grab these flags from msbuild. This way you ensure that StyleCop is running under the same set of #define flags as the compiler. If the compiler is able to successfully parse the code, then StyleCop will too.

For example, you could have this code in place

#if SILVERLIGHT
public class MyClass : ISomeSilverlightInterface
#elif DESKTOP
public class MyClass
#endif
{
}

If neither the SILVERLIGHT nor DESKTOP flags are defined, this code becomes unparsable. If you ran StyleCop through something like StyleCopCMD, you wouldn't be telling it which flags are defined, and StyleCop would fail to correctly parse this code.

May 3, 2012 at 12:48 PM

I want to chime in here, and explain why we need a command line version.  Like probably many places that people want to bring StyleCop in, we have a large legacy code base with thousands of violations.  While we clean up as we go, we cannot fix them nor go through and add SuppressMessages everywhere.

What we've done is as part of our build process, we keep a ceiling count of violations.  As long as there are X number of violations the build passes.  If we end up with X + 1, it fails.  If the number of violations falls below X, the lower count is saved as the new X.  Basically as long as we are not adding anymore we pass a build, and as we do improve the code the ceiling lowers.

This seems to be impossible with the MsBuild integration as it is, which is why we've been using StyleCopCmd.  Unfortunately as pointed out above, that project has since been abandoned.

The current all or nothing approach is a high barrier to entry for those that want to use a tool such as StyleCop.  If there's another way to accomplish our goals, please let me know.  Otherwise I think a command line client would be a great addition to your project.

Coordinator
May 3, 2012 at 1:07 PM

Wow. This is an old thread. In your situation can't you use the msbuild integration and in the StyleCop.targets:

<PropertyGroup>
    <!-- Specifying 0 will cause StyleCop to use the default violation count limit.
         Specifying any positive number will cause StyleCop to use that number as the violation count limit.
         Specifying any negative number will cause StyleCop to allow any number of violations without limit. -->
    <StyleCopMaxViolationCount Condition="'$(StyleCopMaxViolationCount)' == ''">0</StyleCopMaxViolationCount>
  </PropertyGroup>

Set the StyleCopMaxViolationCount to the number of issues you currently have?

May 3, 2012 at 4:03 PM

I have to agree a command line version of StyleCop would be great.  I write CLR Stored procedures/functions ect. for use in SQL Server and frankly I use Textpad and compile using csc.exe manually.  It would be great to be able to use StyleCop on those files from the command line.

Coordinator
May 3, 2012 at 5:08 PM

So for the storedprocs cant you use the cmdline of msbuild.exe passing it a proj file with your cs files referred too also?

Developer
May 17, 2012 at 7:30 AM

There is also an article describing how to make such console app by your own:

http://stylecopplus.codeplex.com/wikipage?title=Running%20StyleCop%20from%20Your%20Code

 

Best regards,
Oleg Shuruev

May 21, 2012 at 7:35 PM
andyr wrote:

Wow. This is an old thread. In your situation can't you use the msbuild integration and in the StyleCop.targets:

 

<PropertyGroup>
    <!-- Specifying 0 will cause StyleCop to use the default violation count limit.
         Specifying any positive number will cause StyleCop to use that number as the violation count limit.
         Specifying any negative number will cause StyleCop to allow any number of violations without limit. -->
    <StyleCopMaxViolationCount Condition="'$(StyleCopMaxViolationCount)' == ''">0</StyleCopMaxViolationCount>
  </PropertyGroup>

 

Set the StyleCopMaxViolationCount to the number of issues you currently have?


This gives us a way to cap, but our current process lets us do a "falling ceiling" approach, which automatically lowers the limit as code is cleaned up.  We'd like to get to no violations at some point.  Also, I'm not sure how this would work as we run StyleCop against solutions, which have multiple projects, and this seems like a project level setting.  Again, maybe I'm missing something.

May 21, 2012 at 7:37 PM
shuruev wrote:

There is also an article describing how to make such console app by your own:

http://stylecopplus.codeplex.com/wikipage?title=Running%20StyleCop%20from%20Your%20Code

 

Best regards,
Oleg Shuruev


I suppose we can do that, but it'd only really be helping us out, and we're not keen on needing to maintain code for tools as well as our own code.  Thanks for the link though, I'll give building our own a go as we're pretty far behind in versions.

May 22, 2012 at 6:57 AM

ajj3085: our current process lets us do a "falling ceiling" approach

This is an excellent approach!

May 28, 2012 at 7:31 PM

I can't understand why a command-line approach (with a stycop configuration file) is a bad choose. Nant is a pretty good tool for build scripts everybody knows that. 

Why? Because the build script is separated of my project, the architect could customize without changes at SLN and CSPROJ files.

I think many build server tools are waiting for it (Hudson, Sonar, Nant, Etc)! We got stuck in 4.3.3 version! 

IMHO, could you provide a way to execute MSBuild file without change the .SLN/.CSPROJ or Command-Line version of StyleCop?
I just want a way to create my build script (msbuild or not), run from Nant and get the Xml report file. 
Thanks in advance!

May 28, 2012 at 7:36 PM

Create a custom "runner" for Stylecop without reports, without parameters for me, isn't a nice solution. This is just a way to fix something of we could have by default.

 

I can help create this command-line approach, but I need some start directions: Where do I start to begin the contribuition? Which programming guidelines shoud I follow?

Thanks!

Coordinator
May 28, 2012 at 7:41 PM

Sorry, maybe I've misunderstood your point. We already support running StyleCop by using an MsBuild Task that we ship and support. So my question is why can't you use this?

May 30, 2012 at 12:40 AM
Edited May 30, 2012 at 12:42 AM

Hi andyr,  thanks!

The folllowing text was extracted from main page:

"For example, to integrate StyleCop to the project SampleProject, open the project file SampleProject.csproj within your favorite text editor. Scroll down to the bottom of the file and add a new tag to import the Microsoft.StyleCop.targets file. This import tag should be added just below the import of Microsoft.CSharp.targets:"

I'm sorry but I couldn't figure out a way to run the Stylecop without change .sln/.csproj. Also, i'm not a MSBuild master yet :-(....

The main point is: Do you have a RAW MSBuild File just to run a Stylecop config file against a Solution?

I wanna make the proccess easiest I can :): 

MSBuild.exe Stylecop.proj /property:StyleCopConfigFile="C:\build\acme.stylecop" /property:Solution="c:\mysolution.sln"

Something like that (above)...

I really wanna solve this problem by myself, but I couldn't figure out a line to follow....

Best regards,

Thanks

Coordinator
May 30, 2012 at 12:53 PM

1. Create a proj file called StyleCopCheck.proj that contains the following:

<Project
    DefaultTargets="Full"
    xmlns= "http://schemas.microsoft.com/developer/msbuild/2003"
    ToolsVersion="4.0">

    <UsingTask AssemblyFile="StyleCop.dll" TaskName="StyleCopTask"/>

    <PropertyGroup>
        <FolderToAnalyse>C:\Dev\SA1023\SA1023FalsePositive</FolderToAnalyse>
        <ProjectPath>C:\Dev\SA1023\SA1023FalsePositive\SA1023FalsePositive.csproj</ProjectPath>
        <StyleCopForceFullAnalysis>true</StyleCopForceFullAnalysis>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
        <StyleCopCacheResults>true</StyleCopCacheResults>
        <StyleCopOverrideSettingsFile> </StyleCopOverrideSettingsFile>
        <StyleCopOutputFile>StyleCopViolations.xml</StyleCopOutputFile>
        <StyleCopMaxViolationCount>1000</StyleCopMaxViolationCount>
    </PropertyGroup>
    
    <Target Name="Full">

    <CreateItem Include="$(FolderToAnalyse)\**\*.cs">
      <Output TaskParameter="Include" ItemName="StyleCopFiles"/>
    </CreateItem>


    <StyleCopTask
      ProjectFullPath="$(ProjectPath)"
      SourceFiles="@(StyleCopFiles)"
      AdditionalAddinPaths="@(StyleCopAdditionalAddinPaths)"
      ForceFullAnalysis="$(StyleCopForceFullAnalysis)"
      DefineConstants="$(DefineConstants)"
      TreatErrorsAsWarnings="$(StyleCopTreatErrorsAsWarnings)"
      CacheResults="$(StyleCopCacheResults)"
      OverrideSettingsFile="$(StyleCopOverrideSettingsFile)"
      OutputFile="$(StyleCopOutputFile)"
      MaxViolationCount="$(StyleCopMaxViolationCount)"
            />

    </Target>
   
</Project>

2. Save this file in a folder like c:\StyleCopCheck

3. Copy into this folder: StyleCop.dll, StyleCop.CSharp.dll, and StyleCop.CSharp.Rules.dll

4. Edit StyleCopCheck.proj and set the Properties to values that suit your project. In particular change:<FolderToAnalyse>C:\Dev\SA1023\SA1023FalsePositive</FolderToAnalyse><ProjectPath>C:\Dev\SA1023\SA1023FalsePositive\SA1023FalsePositive.csproj</ProjectPath>

5. Open a .NET cmd prompt in c:\StyleCopCheck and type msbuild.


6. StyleCop will anaylse the files and project you've specified and save the results in the c:\StyleCopCheck folder.

May 30, 2012 at 1:49 PM

Thanks andyr,

By far this solution is the most complete I've ever seen.

I'm gonna spread this discussion URL for the build tools whose are'nt using the current Stylecop Integration.

Thanks again!

Jun 6, 2012 at 12:45 PM

@andyr, Ahh, I see what you're getting at.  I'll investigate this route and report back.  Thanks!

Jun 7, 2012 at 5:42 AM

Hello,

If you are interested, I recently did a talk on StyleCop, which included a section on how to use the StyleCop MSBuild Task, and then how you can extend this further to use TeamCity to automate the build, and include a StyleCopViolations.xml file.  If you are interested in the slides from this talk have a look here:

http://www.gep13.co.uk/blog/dddsw-4-stylecop-breaking-down-the-barriers-to-entry/

And the source code for the demos can be found here:

https://github.com/gep13/StyleCopDemos

Hope that helps!

Gary

Jul 16, 2012 at 5:41 PM
andyr wrote:

1. Create a proj file called StyleCopCheck.proj that contains the following:

 

<Project
    DefaultTargets="Full"
    xmlns= "http://schemas.microsoft.com/developer/msbuild/2003"
    ToolsVersion="4.0">

    <UsingTask AssemblyFile="StyleCop.dll" TaskName="StyleCopTask"/>

    <PropertyGroup>
        <FolderToAnalyse>C:\Dev\SA1023\SA1023FalsePositive</FolderToAnalyse>
        <ProjectPath>C:\Dev\SA1023\SA1023FalsePositive\SA1023FalsePositive.csproj</ProjectPath>
        <StyleCopForceFullAnalysis>true</StyleCopForceFullAnalysis>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
        <StyleCopCacheResults>true</StyleCopCacheResults>
        <StyleCopOverrideSettingsFile> </StyleCopOverrideSettingsFile>
        <StyleCopOutputFile>StyleCopViolations.xml</StyleCopOutputFile>
        <StyleCopMaxViolationCount>1000</StyleCopMaxViolationCount>
    </PropertyGroup>
    
    <Target Name="Full">

    <CreateItem Include="$(FolderToAnalyse)\**\*.cs">
      <Output TaskParameter="Include" ItemName="StyleCopFiles"/>
    </CreateItem>


    <StyleCopTask
      ProjectFullPath="$(ProjectPath)"
      SourceFiles="@(StyleCopFiles)"
      AdditionalAddinPaths="@(StyleCopAdditionalAddinPaths)"
      ForceFullAnalysis="$(StyleCopForceFullAnalysis)"
      DefineConstants="$(DefineConstants)"
      TreatErrorsAsWarnings="$(StyleCopTreatErrorsAsWarnings)"
      CacheResults="$(StyleCopCacheResults)"
      OverrideSettingsFile="$(StyleCopOverrideSettingsFile)"
      OutputFile="$(StyleCopOutputFile)"
      MaxViolationCount="$(StyleCopMaxViolationCount)"
            />

    </Target>
   
</Project>

 

2. Save this file in a folder like c:\StyleCopCheck

3. Copy into this folder: StyleCop.dll, StyleCop.CSharp.dll, and StyleCop.CSharp.Rules.dll

4. Edit StyleCopCheck.proj and set the Properties to values that suit your project. In particular change:<FolderToAnalyse>C:\Dev\SA1023\SA1023FalsePositive</FolderToAnalyse><ProjectPath>C:\Dev\SA1023\SA1023FalsePositive\SA1023FalsePositive.csproj</ProjectPath>

5. Open a .NET cmd prompt in c:\StyleCopCheck and type msbuild.


6. StyleCop will anaylse the files and project you've specified and save the results in the c:\StyleCopCheck folder.


Hi again.  I haven't had too much time to poke at this, but I'm still having trouble.  The main thing is that we want a single file which has all the violations it for our build server, and I'm also having trouble getting it to work like it does in Visual Studio when you right click the Solution node and click Run Stylecop.

If it helps, we have to following folder structure.

trunk/

  src/

    Settings.StyleCop (master settings file)

    projectA/

    projectB/

  test/

    Settings.StyleCop (master settings file; same as above mentioned [SVN external]

    projectATest/

      Settings.StyleCop (inherits settings from parent folder, overrides by turning off some more rules)

    projectBTest/

      Settings.StyleCop (inherits settings from parent folder, overrides by turning off some more rules)

All the settings files are honored, but with the snippit I quoted I can't get this same behavior; if I specify the Settings.StyleCop in src/ as the override, I still end up with violations in the test projects as those settings are relaxed on a per project basis. 

Any ideas how I can get this to work just like running at the solution level does and have a combined output?