In this blog post I am going to share how you can add a document header of xml comments with code file automatically. This is going to be an very interesting and helpful for all of you who are using styleCop to maintain coding standard. Actually extensive using of StyleCop helped me to think to make it automate. There are many tools available which can helps us in this case to do this automatically. But I always prefer let’s Visual studio do the job for me. Here I am going to share with you two different approaches to deal with this issue. Now it’s up to you which one to use.
Problem : StyleCop warning due to file header missing or mismatch for any “.cs” file.
Warning Message : “The file has no header,the header xml is invalid or the header is not located at the top of the file.”
Two Different approaches to automatically add document header Well, we have two different approaches to automate this stuff.
1. Using Templates
2. Using Macro
2. Using Macro : Macro can do this job very well. If you are very new in this macro world, here is an good article which says How To Create and Run Macro in Visual Studio .NET .
What I did here is, I created small macro which will add the file header automatically with proper file name. What you need to is to, just “Right Click > Add document header” or Just click on “Toolbar” button.
Below I am describing the bit details of implementation
Step 1: Open the Macro IDE from Tools > Macros > Macros IDE
Inside macro window, Right Click on MyMacro and add New module as shown in below
Give the name as “AddDocumentHeader” ( It can be anything ). Copy Paste below code block as it is.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module AddDocumentHeader
Sub AddDocumentHeader()
Dim document As Document
Dim companyName As String = "My Company"
Dim copyrightInformation As String = "Copyright statement. All right reserved"
document = DTE.ActiveDocument
Dim Matching As String = "// <copyright file=""" + document.Name + """ company=""" + companyName + """>"
document.Selection.StartOfDocument()
document.Selection.LineDown(True, 2)
Dim content As String = document.Selection.Text
'Check for already hedader exist or not. Here I am only checking with Copyright Text with in second line of code.
Dim MatchFound = System.Text.RegularExpressions.Regex.IsMatch(content, Matching)
If (Not MatchFound) Then
document.Selection.StartOfDocument()
document.Selection.LineUp()
document.Selection.Text = "// ----------------------------------------------------------------------"
document.Selection.NewLine()
document.Selection.Text = "// <copyright file=""" + document.Name + """ company=""" + companyName + """>"
document.Selection.NewLine()
document.Selection.Text = "// " + copyrightInformation
document.Selection.NewLine()
document.Selection.Text = "// </copyright>"
document.Selection.NewLine()
Read more: Abhijit's World of .NET
1 comments:
Hi there everyone, it's my first pay a visit at this website, and piece of writing is truly fruitful in favor of me, keep up posting these content.
my blog post - airplanes games
Post a Comment