Macro or tip to have automatically updated file headers ?

  • Thread starter Thread starter BlueTrin
  • Start date Start date
B

BlueTrin

Is there any way to maintain automatically a header file like this:

/*
*
**********************************************************************************
* JJStep_RateInstance.cs
* Author: Anthony Nguyen on 02/11/07 11:10:00
* Last Modified by: Anthony Nguyen on 02/11/07 11:00:00
*
* Description:
* - ???
*
* Notes:
* -
*
* To do:
* -
*
* Known Bugs:
* -
*
*
**********************************************************************************
*/

which will automatically be created when you create a new file or when
you press a certain combination of keys at the top of the current
file.
It would also update the last modified line when saving.

I have no experience with Visual Studio macros.

Thanks very much for reading this !
Anthony
 
Anthony,

With new files, you can actually find the templates in the VS.NET
install directory (or create your own) which have the appropriate
information at the top.

However, when saving, you will have to actually create a add-in for
VS.NET which will update the information for you, so it's better to just
look into creating an add-in and doing all the work there. It's going to be
more work than just a macro, mind you.
 
Anthony,

With new files, you can actually find the templates in the VS.NET
install directory (or create your own) which have the appropriate
information at the top.

However, when saving, you will have to actually create a add-in for
VS.NET which will update the information for you, so it's better to just
look into creating an add-in and doing all the work there. It's going to be
more work than just a macro, mind you.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Is there any way to maintain automatically a header file like this:
/*
*
**********************************************************************************
* JJStep_RateInstance.cs
* Author: Anthony Nguyen on 02/11/07 11:10:00
* Last Modified by: Anthony Nguyen on 02/11/07 11:00:00
*
* Description:
* - ???
*
* Notes:
* -
*
* To do:
* -
*
* Known Bugs:
* -
*
*
**********************************************************************************
*/
which will automatically be created when you create a new file or when
you press a certain combination of keys at the top of the current
file.
It would also update the last modified line when saving.
I have no experience with Visual Studio macros.
Thanks very much for reading this !
Anthony

Thanks very much for the reply indeed, anybody has made already such
an addin ?
Or is there a commercial addin doing this :?

Anthony
 
However, when saving, you will have to actually create a add-in for
VS.NET which will update the information for you, so it's better to just
look into creating an add-in and doing all the work there. It's going to be
more work than just a macro, mind you.
You can basically do this with a macro. Open the macro explorer and
navigate the file EnvironmentEvents. Add the following event handler at
the bottom of the file:

Private Sub DocumentEvents_DocumentOpened(ByVal document As
EnvDTE.Document) Handles DocumentEvents.DocumentOpened
CheckAndInsertCopyright(document)
End Sub

It will get called whenever a document opens. I have attached my
CheckAndInsertCopyright() implementation that inserts the content of a
copyright file into every C# file of a given project at the end of the
posting. This should give you a good starting point.

HTH,
Andy


Imports System
Imports System.IO
Imports System.Text
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Copyright
Public Sub InsertCopyright()
CheckAndInsertCopyright(DTE.ActiveDocument)
End Sub
Friend Sub CheckAndInsertCopyright(ByVal document As EnvDTE.Document)
ClearOutput()
WriteToOutput("Checking Copyright, Version " + "46")
WriteToOutput("File: " + document.FullName)
WriteToOutput("")

'check if it is a C# file!!
If Not document.FullName.EndsWith(".cs",
StringComparison.OrdinalIgnoreCase) Then
Return
End If

'Check if the project is the NSTL
Dim sln As String = DTE.Solution.FullName
If Not sln.EndsWith("nstl.sln",
StringComparison.OrdinalIgnoreCase) Then
Return
End If

'Check if a copyright is present
If Not Copyright.CheckCopyright(document) Then
'if no, insert it
InsertCopyright(document)
WriteToOutput("-->Copyright Inserted.")
Else
WriteToOutput("-->Copyright OK.")
End If

'check year, if necessary repair it
End Sub
Friend copyrightRegion As String = "#region Copyright © 2003 - {0},
Andreas Mueller"

Friend Function CheckCopyright(ByVal document As EnvDTE.Document)
As Boolean
Dim selection As TextSelection = CType(document.Selection,
TextSelection)

selection.LineDown(True, 5)

Dim s As String = selection.Text()
selection.StartOfDocument()

If s.Contains(String.Format(copyrightRegion,
DateTime.Now.Year)) Then
Return True
End If
Return False
End Function

Friend Sub InsertCopyright(ByVal document As EnvDTE.Document)

Dim selection As TextSelection = CType(document.Selection,
TextSelection)

selection.StartOfDocument()

DTE.UndoContext.Open("NSTL Copyright")
Try
Dim sln As String = DTE.Solution.FullName

Dim copyright As String
Dim templateFileName As String =
Path.Combine(Path.GetDirectoryName(sln), "CopyrightTemplate.txt")
WriteToOutput("Inserting Copyright from" + vbNewLine +
templateFileName)

selection.InsertFromFile(templateFileName)

Finally
DTE.UndoContext.Close()
End Try

End Sub
Friend Sub ClearOutput()
Dim out As OutputWindowPane = GetOutputWindowPane("Copyright
Macro", True)
out.Clear()
End Sub
Friend Sub WriteToOutput(ByVal msg As String)
Dim out As OutputWindowPane = GetOutputWindowPane("Copyright
Macro", True)
out.OutputString(msg + vbNewLine)
End Sub
Private Function GetOutputWindowPane(ByVal Name As String, Optional
ByVal show As Boolean = True) As OutputWindowPane

Dim window As Window =
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
window.Visible = show

Dim outputWindow As OutputWindow
Dim outputWindowPane As OutputWindowPane

If show Then window.Visible = show
outputWindow = CType(window.Object, OutputWindow)
Try
outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
End Try
outputWindowPane.Activate()
Return outputWindowPane
End Function


End Module
 
What would you do for when the document is saved though? You could
attach to the DocumentSaved event, but you would have to parse the existing
header, set the values, and then resave the document, making sure to avoid
reentrancy.
 
Nicholas said:
What would you do for when the document is saved though? You could
attach to the DocumentSaved event, but you would have to parse the existing
header, set the values, and then resave the document, making sure to avoid
reentrancy.
Correct. I didn't want to say that just because it is a macro, it is
simple and there is no work to do. Here's the code doing what you have
described:

Private isIn As Boolean = False
Private Sub DocumentEvents_DocumentDaved(ByVal document As
EnvDTE.Document) Handles DocumentEvents.DocumentSaved

If isIn Then Return
isIn = True
Try
Dim selection As TextSelection = CType(document.Selection,
TextSelection)
selection.Text = "BooFoo"
document.Save()
Finally
isIn = False
End Try
End Sub

HTH,
Andy
 
Nicholas Paldino [.NET/C# MVP] wrote:> What would you do for when the document is saved though? You could
attach to the DocumentSaved event, but you would have to parse the existing
header, set the values, and then resave the document, making sure to avoid
reentrancy.

Correct. I didn't want to say that just because it is a macro, it is
simple and there is no work to do. Here's the code doing what you have
described:

Private isIn As Boolean = False
Private Sub DocumentEvents_DocumentDaved(ByVal document As
EnvDTE.Document) Handles DocumentEvents.DocumentSaved

If isIn Then Return
isIn = True
Try
Dim selection As TextSelection = CType(document.Selection,
TextSelection)
selection.Text = "BooFoo"
document.Save()
Finally
isIn = False
End Try
End Sub

HTH,
Andy

Thanks very much both of you for your help !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top