Imports EnvDTE
Imports System.Diagnostics
'These following macro code routines have been created using
'the assumption that there is one project in the involved
'solution and the project's base name matches the
'solution's name (from its filepath.)
'
'i.e. The .SLN full path "c:\MyProject.sln" causes
' these macro routines to consider the project name
' involved is "MyProject"
'
'Also note, that these rtn's applied on a newly created
'project from the IDE (using default settings) are not all
'encompassing changes from the start of things
'
'Immediately, when the change to make the Codebehind-type files
'become Src-type files, the global ASAX code file has references
'to EventArgs in several routines that are unqualified.
'Adding Imports System at the top of that file fixes that issue
'
'Considering the same concept just mentioned above, if a
'project is involved that has been worked on for a while out
'of the IDE, there could be several cases where other imports
'will be needed in 1 or many other code files, these statements
'need to be added manually as needed before or after the
'conversion process takes place to ensure the project can still
'run live from the web server as no DLL will be used that would
'have had by default all the needed imports for every class at
'a project level initially configured by the IDE
Public Module Proj_IDEMacros
'This function will examine the .SLN's full path and
'return the last part of the path beyond the last
'basckslash (\) and remove the .SLN extension
'
'i.e. If DTE.Solution.FullName = "c:\MyProject.sln" then
' GetProjectName() will return "MyProject"
'
Private Function GetProjectName() As String
Dim pname As String = DTE.Solution.FullName
Dim i As Integer = InStrRev(pname, "\")
If i > 0 Then
pname = Mid(pname, i + 1)
End If
pname = Replace(pname, ".sln", "", , , CompareMethod.Text)
Return pname
End Function
Sub ToSrc()
CloseAll()
'Look inside all .ASPX, .ASCX, and .ASAX files
'within the current project and replace the
'[Codebehind] attribute expected
'to be within the @page declaration and change
'it to the [Src] attribute
'
'* Note: The current project should be highlighted
' in the solution explorer or a file opened
' and active from that project so that these
' changes are assured to occur for the
' intended project being converted
DTE.Find.FindWhat = " Codebehind="""
DTE.Find.ReplaceWith = " Src="""
DTE.Find.SearchPath = "Current Project"
DTE.Find.FilesOfType = "*.as?x"
DTE.Find.Target = vsFindTarget.vsFindTargetFiles
DTE.Find.MatchInHiddenText = True
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
DTE.Find.SearchSubfolders = False
DTE.Find.KeepModifiedDocumentsOpen = False
DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Execute()
'Look inside all .ASPX, .ASCX, and .ASAX files
'within the current project and replace the
'[Inherits] attribute's value to remove the
'project name from the setting
'
' Inherits="SomeProject.Webform1"
'
' to
'
' Inherits="Webform1"
DTE.Find.FindWhat = " Inherits=""" & GetProjectName() & "."
DTE.Find.ReplaceWith = " Inherits="""
DTE.Find.Execute()
'Save all the files in the solution
DTE.ExecuteCommand("File.SaveAll")
End Sub
Sub ToCodebehind()
CloseAll()
'Look inside all .ASPX, .ASCX, and .ASAX files
'within the current project and replace the
'[Src] attribute expected
'to be within the @page declaration and change
'it to the [Codebehind] attribute
'
'* Note: The current project should be highlighted
' in the solution explorer or a file opened
' and active from that project so that these
' changes are assured to occur for the
' intended project being converted
DTE.Find.FindWhat = " Inherits="""
DTE.Find.ReplaceWith = " Inherits=""" & GetProjectName() & "."
DTE.Find.Target = vsFindTarget.vsFindTargetFiles
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.MatchInHiddenText = True
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
DTE.Find.SearchPath = "Current Project"
DTE.Find.SearchSubfolders = False
DTE.Find.KeepModifiedDocumentsOpen = False
DTE.Find.FilesOfType = "*.as?x"
DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Execute()
'The next section of code is repeated twice and
'is only involved to act as a safety net
'
'In the event the ToCodebehind conversion is initiated
'mulitple times in a row, the [Inherits] attribute
'could have been changed adding the project name to the
'the value more than once. For example:
'
' Inherits="Webform1"
'
' to Inherits="SomeProject.Webform1"
'
' and then to Inherits="SomeProject.SomeProject.Webform1"
'
'The next block of code is a self-cleaning piece of code
'that causes a replace of (in this case)
'
' Inherits="SomeProject.SomeProject.
'
' to
'
' Inherits="SomeProject.
'
' Thus fixing that possible problem
DTE.Find.FindWhat = " Inherits=""" & GetProjectName() & "." & GetProjectName() & "."
DTE.Find.ReplaceWith = " Inherits=""" & GetProjectName() & "."
DTE.Find.Target = vsFindTarget.vsFindTargetFiles
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.MatchInHiddenText = True
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
DTE.Find.SearchPath = "Current Project"
DTE.Find.SearchSubfolders = False
DTE.Find.KeepModifiedDocumentsOpen = False
DTE.Find.FilesOfType = "*.as?x"
DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Execute()
DTE.Find.FindWhat = " Inherits=""" & GetProjectName() & "." & GetProjectName() & "."
DTE.Find.ReplaceWith = " Inherits=""" & GetProjectName() & "."
DTE.Find.Target = vsFindTarget.vsFindTargetFiles
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.MatchInHiddenText = True
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
DTE.Find.SearchPath = "Current Project"
DTE.Find.SearchSubfolders = False
DTE.Find.KeepModifiedDocumentsOpen = False
DTE.Find.FilesOfType = "*.as?x"
DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Execute()
'Now this process simply changes all [Codebehind]
'attributes to [Src]
DTE.Find.FindWhat = " Src="""
DTE.Find.ReplaceWith = " Codebehind="""
DTE.Find.Execute()
'And finally, all the files in the solution are saved
DTE.ExecuteCommand("File.SaveAll")
End Sub
Private Sub CloseAll()
'Attempt up to 99 closes of the active windows in the
'environment before begining the conversion process
'
'The first IDE error on an attempt will cause the
'routine to end early and quietly without prompting
'any error message
'
'The purpose in closing all the files is that
'the "replace in all files" method should run cleaner
'that way and also it was found that when some
'files were changed back to Codebehind from Src types
'that they could not be immediately opened by the IDE's
'design mode until they and their correpsonding source
'VB files were closed, so...
'
'All the files will get closed (and auto-saved), the
'replacements applied, and re-saved all while
'they remain closed and end up ready to be
'opened for editing at the end of the conversion
On Error Resume Next
Dim i As Integer
For i = 1 To 99
DTE.ExecuteCommand("Window.CloseDocumentWindow")
DTE.ActiveWindow.Close(vsSaveChanges.vsSaveChangesYes)
If Err.Number <> 0 Then Exit For
Next
End Sub
End Module