Important alert: (current site time 7/15/2013 8:23:52 AM EDT)
 

VB icon

Bulk CopyRight generator macro for Visual Studio .NET

Email
Submitted on: 5/1/2005 10:00:15 PM
By: ivancoder34  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET, ASP.NET, C++.NET
Views: 11697
author picture
(About the author)
 
     I was tired of adding the copyright headers to my source code files manually, so I created this macro to do it for me. You may apply (or delete) a custom copyright header to: * The active source file (the one you have open currently) * All source files from the current project * All source files from all projects from the current solution More info: http://www.iloire.com/PermaLink,guid,c9a6e3b4-0cb3-4fe3-9f17-44cbe77b5a45.aspx

 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Bulk CopyRight generator macro for Visual Studio .NET
//**************************************
Licence: 
1. Released under GPL licence. You are free to use this macro for any purpose. 
2. This software is provided "as is" with no warranties of any kind.
3. You may use and/or distribute this software, but this copyright notice may NOT be removed, obscured or modified from source code.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Bulk CopyRight generator macro for Visual Studio .NET
// Description:I was tired of adding the copyright headers to my source code files manually, so I created this macro to do it for me. You may apply (or delete) a custom copyright header to:
 * The active source file (the one you have open currently)
 * All source files from the current project
 * All source files from all projects from the current solution
More info:
http://www.iloire.com/PermaLink,guid,c9a6e3b4-0cb3-4fe3-9f17-44cbe77b5a45.aspx
// By: ivancoder34
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3564&lngWId=10//for details.//**************************************

' --------------------------------------------------------
' Bulk CopyRight generator macro for Visual Studio .NET
' Version: 1.0 Beta
' Author: Iván Loire - ivan/at/iloire.com
' Licence: 
'1. Released under GPL licence. You are free to use this macro for any purpose. 
'2. This software is provided "as is" with no warranties of any kind.
'3. You may use and/or distribute this software, but this copyright notice may NOT be removed, obscured or modified from source code.
' --------------------------------------------------------
Imports EnvDTE
Imports System
Imports System.Diagnostics
Imports System.Collections
Imports System.Text.RegularExpressions
Public Module CopyRight
 Class Results
Public Succeed As Integer
Public Failed As Integer
Public Sub Merge(ByVal results As Results)
 Succeed = Succeed + results.Succeed
 Failed = Failed + results.Failed
End Sub
 End Class
 Enum Action
AddCopyRight = 0
DeleteCopyRight = 1
 End Enum
 'IMPORTANT-1: This tags are used as marks to FIND and REPLACE the copyright notice, 
 'so don't use a simple string that could be repeated somewhere else in the code
 'IMPORTANT-2: Don't change this marks once you have stamped the copyright within the files
 'Otherwise, the macro will not be able to find back those copyrights notices.
 Dim StartCopyRightTag As String = "--- Copyright (c) 2001-2005 Ivan Loire ---"
 Dim EndCopyRightTag As String = "--- End copyright notice --- " & vbCrLf & vbCrLf
 Private Function GetCopyRightText(ByVal language As String)
Dim text As String
text = GetLine(StartCopyRightTag, language)
text += GetLine("This sofware is written and copyrighted by Iván Loire (www.iloire.com). All rights are reserved.", language)
text += GetLine("Author contact: ivan/at/iloire.com", language)
text += GetLine("------------------------------------------------------------------------", language)
text += GetLine("NO WARRANTY: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ""AS IS"" AND ANY EXPRESS", language)
text += GetLine("OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ", language)
text += GetLine("AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR ", language)
text += GetLine("CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ", language)
text += GetLine("DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ", language)
text += GetLine("DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ", language)
text += GetLine("IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ", language)
text += GetLine("OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", language)
text += GetLine("------------------------------------------------------------------------", language)
text += GetLine("COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified ", language)
text += GetLine("without written consent from the author.", language)
text += GetLine(EndCopyRightTag, language)
Return text
 End Function
 Private Function GetLine(ByVal text As String, ByVal language As String)
Dim line As String
If language = "{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}" Then 'VB
 line = "' " & text
ElseIf language = "{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}" Then 'C#
 line = "// " & text
ElseIf language = "{B5E9BD32-6D3E-4B5D-925E-8A43B79820B4}" Then 'C related VC++
 line = "// " & text
ElseIf language = "{B5E9BD36-6D3E-4B5D-925E-8A43B79820B4}" Then 'C related (managed VC++)
 line = "// " & text
Else
 Throw New Exception("Language not implemented")
End If
Return line & vbCrLf
 End Function
 'Check for CopyRight header existence on target item
 Private Function ExistCopyRightWithInDocument(ByVal pitem As ProjectItem) As Boolean
Dim isOpen = pitem.IsOpen
Dim w As Window = pitem.Open() ' need to open the window. otherwise will raise an error
If Not isOpen Then
 w.Visible = True
End If
Dim ts As TextSelection = pitem.Document.Selection
ts.Cancel()
ts.StartOfDocument(True)
Dim result As Boolean = ts.FindText(GetLine(StartCopyRightTag, pitem.FileCodeModel.Language))
ts.Cancel()
If Not isOpen Then w.Close() 'if we opened, let's close it.
Return result
 End Function
 Private Sub DeleteCopyRightFromDocument(ByVal pitem As ProjectItem)
If Not ExistCopyRightWithInDocument(pitem) Then Exit Sub
Dim isOpen = pitem.IsOpen
Dim w As Window = pitem.Open()
If Not isOpen Then
 w.Visible = True
End If
Dim ts As TextSelection = pitem.Document.Selection
ts.Cancel()
Dim reg As Regex = New Regex(Regex.Escape(GetLine(StartCopyRightTag, pitem.FileCodeModel.Language)) & ".*" & Regex.Escape(GetLine(EndCopyRightTag, pitem.FileCodeModel.Language)), RegexOptions.Singleline)
ts.StartOfDocument(True)
ts.SelectAll()
Dim strPageOut = reg.Replace(ts.Text, "")
ts.Delete()
ts.Insert(strPageOut)
pitem.Save()
If Not isOpen Then w.Close() Else ts.StartOfDocument()
 End Sub
 'Adds CopyRight header to target project item (must be a valid code file: FileCodeModel!=null)
 Private Sub AddCopyRightToDocument(ByVal pitem As ProjectItem)
Dim isOpen = pitem.IsOpen
Dim w As Window = pitem.Open()
If Not isOpen Then
 w.Visible = True
End If
Dim ts As TextSelection = pitem.Document.Selection
ts.Cancel()
ts.StartOfDocument(True)
ts.Insert(GetCopyRightText(pitem.FileCodeModel.Language))
pitem.Save()
If Not isOpen Then w.Close() Else ts.StartOfDocument()
 End Sub
#Region "Process items"
 Private Function ProcessProjectItems(ByVal pitems As ProjectItems, ByVal action As Action) As Results
Dim results As New Results
For i As Integer = 1 To pitems.Count
 Dim pitem As ProjectItem = pitems.Item(i)
 Try
If Not IsNothing(pitem.FileCodeModel) Then
 DTE.StatusBar.Text = "Processing code file " & pitem.Name & " ..."
 If action = action.AddCopyRight Then
If ExistCopyRightWithInDocument(pitem) Then
 DeleteCopyRightFromDocument(pitem)
 DTE.StatusBar.Text = "Copyright already exists in file " & pitem.Name
End If
DTE.StatusBar.Text = "Adding copyright to file " & pitem.Name
AddCopyRightToDocument(pitem)
 Else
DTE.StatusBar.Text = "Deleting copyright from file " & pitem.Name
DeleteCopyRightFromDocument(pitem)
 End If
 results.Succeed = results.Succeed + 1
 'MsgBox(pitem.Name & " " & results.Succeed)
Else
 DTE.StatusBar.Text = pitem.Name & " is not a code file. Skipping..."
End If
 Catch ex As Exception
results.Failed = results.Failed + 1
 End Try
 If Not IsNothing(pitem.ProjectItems) Then
If pitem.ProjectItems.Count > 0 Then results.Merge(ProcessProjectItems(pitem.ProjectItems, action))
 Else
MsgBox(pitem.Name & " has no project items")
 End If
Next
Return results
 End Function
#End Region
 Private Function ProcessProject(ByVal p As Project, ByVal action As Action) As Results
Dim results As New Results
If Not IsNothing(p.ProjectItems) Then
 DTE.StatusBar.Text = "Processing project " & p.Name & " items ..."
 results = ProcessProjectItems(p.ProjectItems, action)
Else
 DTE.StatusBar.Text = p.Name & " has no project items"
End If
Return results
 End Function
 Private Function ProcessSolution(ByVal s As Solution, ByVal action As Action) As Results
Dim results As New Results
Try 
 For i As Integer = 1 To s.Projects.Count
results.Merge(ProcessProject(s.Projects.Item(i), action))
 Next
Catch ex As Exception
 MsgBox("Error: " & ex.Message)
End Try
Return results
DTE.StatusBar.Text = "Operation complete with " & results.Failed & " errors."
 End Function
 Private Function ShowResults(ByVal results As Results) As String
Dim output As String = ""
output += results.Succeed & " source files processed successfully. " & results.Failed & " errors encountered"
Return output
 End Function
 'Public methods ----------------------------------------------
 '-- To document
 Sub AddCopyRightToActiveDocument()
If ExistCopyRightWithInDocument(DTE.ActiveDocument.ProjectItem) Then
 If MsgBox("A copyright header matching our fingerprint already exist. Do you want to replace it?", MsgBoxStyle.OKCancel, "Copyright already exists in target document") = MsgBoxResult.OK Then
DeleteCopyRightFromDocument(DTE.ActiveDocument.ProjectItem)
 Else
Exit Sub
 End If
End If
Try
 AddCopyRightToDocument(DTE.ActiveDocument.ProjectItem)
 'MsgBox("Succeed!", MsgBoxStyle.Information, "Process completed")
Catch ex As Exception
 MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical, "Process completed with errors")
End Try
 End Sub
 Sub DeleteCopyRightFromActiveDocument()
DeleteCopyRightFromDocument(DTE.ActiveDocument.ProjectItem)
 End Sub
 '-- Add to Project
 Sub AddCopyRightToCurrentProject()
Dim p As Project = DTE.ActiveDocument.ProjectItem.ProjectItems.ContainingProject
If MsgBox("This action will modify all the valid source files from your selected PROJECT """ & p.Name & """, adding the customized copyright header. " & vbCrLf & vbCrLf & "This action can NOT be undone. You are encourage to create backups of your source files before running this macro." & vbCrLf & vbCrLf & "Do you want to continue?", MsgBoxStyle.OKCancel, "Confirmation dialog") = MsgBoxResult.OK Then
 MsgBox(ShowResults(ProcessProject(p, Action.AddCopyRight)))
End If
 End Sub
 '-- Delete from Project
 Sub DeleteCopyRightFromCurrentProject()
Dim p As Project = DTE.ActiveDocument.ProjectItem.ProjectItems.ContainingProject
If MsgBox("This action will modify all the valid source files from your selected PROJECT """ & p.Name & """, deleting the customized copyright header. " & vbCrLf & vbCrLf & "This action can NOT be undone. You are encourage to create backups of your source files before running this macro." & vbCrLf & vbCrLf & "Do you want to continue?", MsgBoxStyle.OKCancel, "Confirmation dialog") = MsgBoxResult.OK Then
 MsgBox(ShowResults(ProcessProject(p, Action.DeleteCopyRight)))
End If
 End Sub
 '-- Add to Solution
 Sub AddCopyRightToCurrentSolution()
Dim s As Solution = Application.Solution
If MsgBox("This action will modify all the valid source files from your selected SOLUTION """ & s.FullName & """, adding the customized copyright header in them. " & vbCrLf & vbCrLf & "This action can NOT be undone. You are encourage to create backups of your source files before running this macro." & vbCrLf & vbCrLf & "Do you want to continue?", MsgBoxStyle.OKCancel, "Confirmation dialog") = MsgBoxResult.OK Then
 MsgBox(ShowResults(ProcessSolution(s, Action.AddCopyRight)))
End If
 End Sub
 '-- Delete from Solution
 Sub DeleteCopyRightFromCurrentSolution()
Dim s As Solution = Application.Solution
If MsgBox("This action will modify all the valid source files from your selected SOLUTION """ & s.FullName & """, deleting the customized copyright header in them. " & vbCrLf & vbCrLf & "This action can NOT be undone. You are encourage to create backups of your source files before running this macro." & vbCrLf & vbCrLf & "Do you want to continue?", MsgBoxStyle.OKCancel, "Confirmation dialog") = MsgBoxResult.OK Then
 MsgBox(ShowResults(ProcessSolution(s, Action.DeleteCopyRight)))
End If
 End Sub
End Module


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/1/2005 10:03:09 PMIvan Loire

The code has lost the format when I pasted it to PSC, so you may want to get it from here:
http://www.iloire.com/PermaLink,guid,c9a6e3b4-0cb3-4fe3-9f17-44cbe77b5a45.aspx< /span>
(If this comment was disrespectful, please report it.)

 
12/16/2006 5:16:30 AMNoName

Site link dead. Anyone know how to install this?

(If this comment was disrespectful, please report it.)

 
9/1/2008 5:41:49 AM;'l

,;
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.