stylecop breaking down the barriers to entry gary ewan park [email protected] twitter: @gep13@gep13...
TRANSCRIPT
![Page 1: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/1.jpg)
StyleCopBreaking down the barriers to entry
Gary Ewan [email protected]: @gep13Blog: http://www.gep13.co.uk/blog
![Page 2: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/2.jpg)
Agenda
• What is StyleCop?• Visual Studio Integration• MSBuild Integration• Using a Custom Task• StyleCop Compliance• Extending StyleCop• Working with “Legacy” Applications• Additional Tooling Support
![Page 3: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/3.jpg)
Source Code
http://gep13.me/StyleCopDemos
![Page 4: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/4.jpg)
What is StyleCop?
![Page 5: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/5.jpg)
A Definition…
“StyleCop is an open source static code analysis tool from Microsoft that checks C# code for conformance to StyleCop's recommended coding styles and a subset of Microsoft's .NET Framework Design Guidelines. StyleCop analyzes the source code, allowing it to enforce a different set of rules from FxCop.” *
Reference http://stylecop.codeplex.com/
* http://en.wikipedia.org/wiki/StyleCop
![Page 6: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/6.jpg)
What does that mean?
• StyleCop is a “plugin” for Visual Studio 2008, 2010, and VS 11 Preview (and in a way, 2005* and Express)
• It provides 162 rules over 7 categories of “style”
* http://gep13.me/ie1T0V
![Page 7: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/7.jpg)
Visual Studio Integration
![Page 8: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/8.jpg)
Visual Studio Integration
![Page 9: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/9.jpg)
Demo 1
Visual Studio Integration
Using StyleCop on a New Project
![Page 10: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/10.jpg)
MSBuild Integration
![Page 11: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/11.jpg)
This gets installed by default…
During the installation
you have the option
to install MSBuild files. These files will be required if you want to integrate StyleCop analysis with the Visual Studio build process or a Continuous Integration Server.
![Page 12: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/12.jpg)
MSBuild Integration – Visual Studio
To add StyleCop as part of the “Build” process in Visual Studio follow these steps:
1. Use the StyleCop.MSBuild Nuget package to install the necessary files and make file edits.
2. This edits the project file to include:
3. Add the following property in the project file to force all warnings to be treated as errors:
![Page 13: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/13.jpg)
Demo 2
MSBuild Integration
Including StyleCop Analysis as part of the Build Process
![Page 14: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/14.jpg)
Using a Custom Task
![Page 15: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/15.jpg)
MSBuild Integration – Custom Task
• StyleCop also includes a MSBuild task that can be used to create a custom build step
• Manning Publications – Continuous Integration in .NET• http://gep13.me/MuNEL9
![Page 16: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/16.jpg)
Demo 3
Using a Custom Task
Running StyleCop Analysis as a custom task with MSBuild and TeamCity
![Page 17: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/17.jpg)
StyleCop Compliance
![Page 18: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/18.jpg)
Visual Studio Templates
During the installation
you have the option to install a new set of Visual Studio Templates, which will already by StyleCop “compliant”
![Page 19: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/19.jpg)
Going further with more templates…
• Use StyleCop compliant code snippets• http://vssnippets.codeplex.com/
• Use StyleCop compliant project/item templates• http://vstemplates.codeplex.com/
![Page 20: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/20.jpg)
Talk to your Team
• Get Buy-In from your Team Members• Only enable the StyleCop rules that you want to
follow• Have a team meeting to decide
![Page 21: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/21.jpg)
Demo 4
StyleCop Compliance
Using StyleCop Compliant Project/Item Templates and Code Snippets
![Page 22: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/22.jpg)
Extending StyleCop
![Page 23: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/23.jpg)
Add More StyleCop Rules
• There are a number of projects that are extending the built-in StyleCop rules:
StyleCop+o http://stylecopplus.codeplex.com/
JSL StyleCopo http://jslstylecop.codeplex.com/
![Page 24: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/24.jpg)
Create your own Custom StyleCop Rule
1. Create a .Net Framework 3.5 Class Library Project in Visual Studio
2. Add a reference to StyleCop.dll and StyleCop.Csharp.dll
3. Create your analyzer class that inherits from SourceAnalyzer
4. Add an XML file with the same name as the analyzer class. Set the properties of the XML file to be an Embedded Resource and not copy to the output directory
5. Override the AnalyzeDocument method
6. You are ready to write your own analysis code
• http://gep13.me/JIn0xL
![Page 25: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/25.jpg)
StyleCop Contrib
This CodePlex Project started out life as another Custom StyleCop Rule Library, however, now it is a Unit Testing Framework for Custom StyleCop Rules
http://stylecopcontrib.codeplex.com
Allows you to quickly begin testing your Rules, without the need to debug multiple instances of Visual Studio
![Page 26: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/26.jpg)
Demo 5
Extending StyleCop
Create a Custom StyleCop Rule and then test it with Unit Tests
![Page 27: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/27.jpg)
Working with “Legacy”
Applications
![Page 28: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/28.jpg)
Using StyleCop in an Existing Project
• Turn off analysis for generated code
• Surround code in a region with the text ‘generated code’ within the title
• Turn off StyleCop Analysis for a particular file using SourceFileList in Settings.StyleCop file
• Turn off StyleCop Analysis for an entire project using Settings.StyleCop file
![Page 29: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/29.jpg)
Demo 6
Working with “Legacy” Applications
Turning StyleCop off for an existing large project using Settings file
![Page 30: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/30.jpg)
Additional Tooling Support
![Page 31: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/31.jpg)
Refactoring StyleCop Rules
• There are plugins available for both CodeRush and ReSharper to help refactor code to be StyleCop Compliant
• StyleCop For ReSharper Now included as part of the core StyleCop installation Automatically fixes 128 StyleCop issues during
ReSharper “CleanUp Code…”
• Style Ninja for CodeRush http://code.google.com/p/dxcorecommunityplugins/wiki/
CR_StyleNinja Fixes 23 StyleCop Issues
![Page 32: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/32.jpg)
ReSharper Integration
During the installation
you have the option
to install ReSharper
Integration files, these
will be required if you
want to be able to analyse and correct StyleCop warnings using ReSharper Refactorings.
![Page 33: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/33.jpg)
Demo 7
Additional Tooling Support
Using ReSharper to improve StyleCop Compliance
![Page 34: StyleCop Breaking down the barriers to entry Gary Ewan Park gep13@gep13.co.uk Twitter: @gep13@gep13 Blog:](https://reader030.vdocuments.net/reader030/viewer/2022032605/56649e735503460f94b72706/html5/thumbnails/34.jpg)
Questions?
Feel free to email me any additional questions at