In work today, I needed to add a copyright header to a large number of C# classes. Instead of simply pasting the header across the 20+ files, I took a look at the various tooling options.
The Code Header Designer looked like a possible option. However, I wanted something lightweight and easily configurable, so I settled on using a VS macro to insert the headers. A quick Google revealed a number of example macros already written to do exactly what I required. This is my version of James Welch’s code:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module AddCopyrightHeader
Sub FileHeader()
Dim doc As Document
Dim docName As String
Dim companyName As String = "My Company Name"
Dim authorName As String = "Andy Parkhill"
Dim copyrightText As String = "Copyright (c) " + DateString.ToString() + " All Right Reserved"
Dim summaryText As String = "Class representing a {0} entity."
' Get the name of this object from the file name
doc = DTE.ActiveDocument
' Get the name of the current document
docName = doc.Name
' Set selection to top of document
DTE.ActiveDocument.Selection.StartOfDocument()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.LineUp()
' Write copyright tag
DTE.ActiveDocument.Selection.Text = "// <copyright file=""" + docName + """ company=""" + companyName + """>"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "// " + copyrightText
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "// </copyright>"
' Write author name tag (optional)
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "// <author>" + authorName + "</author>"
' Write date tag (optional)
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "// <date>" + DateString.ToString() + "</date>"
' Write summary tag (optional)
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "// <summary>" + String.Format(summaryText, docName) + "</summary>"
DTE.ActiveDocument.Selection.NewLine()
End Sub
End Module
I then followed the instructions in this article to add a context menu entry to call the macro by right-clicking the target file. This resulted in the following header comment being inserted in each file:
1: // <copyright file="Sample.cs" company="My Company Name">
2: // Copyright (c) 19/07/2010 23:01:05 All Right Reserved
3: // </copyright>
4: // <author>Andy Parkhill</author>
5: // <date>19/07/2010 23:01:05 </date>
6: // <summary>Class representing a Sample entity</summary>
It is worth noting that by taking 20 minutes to research the possible options, I was able to find modify an existing macro that is significantly faster than copying and pasting boilerplate text across the various files, and that can be modified for other uses.
If I had to do this on a new project, I would instead create a custom class template specifically for the project with required header and class structure.
hey andy i`ll follow u from now!!
ReplyDeletei`d like learn C# visual studio :D!!