Advertisement
Guest User

VSCONVERT Macro for VS2005+

a guest
Nov 25th, 2013
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System
  2. Imports EnvDTE
  3. Imports EnvDTE80
  4. Imports EnvDTE90
  5. Imports System.Diagnostics
  6.  
  7. Public Module ExtractWinFormsDesignerFile
  8.  
  9.     ' -------------------------------------------------------------------------
  10.    ' Extract WinForms Designer File Visual Studio 2005/2008 Macro
  11.    ' -------------------------------------------------------------------------
  12.    ' Extracts the InitializeComponent() and Dispose() methods and control
  13.    ' field delarations from a .NET 1.x VS 2003 project into a VS 2005/8
  14.    ' style .NET 2.0 partial class in a *.Designer.VB file. (This tested to work
  15.    ' with VB only.)
  16.    '
  17.    ' To use:
  18.    '  * Copy the methods below into a Visual Studio Macro Module (use
  19.    '    ALT+F11 to show the Macro editor)
  20.    '  * Select a Windows Form in the Solution Explorer
  21.    '  * Run the macro by showing the Macro Explorer (ALT+F8) and double
  22.    '    clicking the 'ExtractWinFormsDesignerFile' macro.
  23.    '
  24.    ' Duncan Smart, InfoBasis, 2007
  25.    ' From: http://blog.dotsmart.net/2008/05/20/converting-visual-studio-2003-winforms-to-visual-studio-20052008-partial-classes/
  26.    ' Modified and updated by Nathan Jones, 2010
  27.    ' See: http://www.nathanpjones.com/
  28.    ' -------------------------------------------------------------------------
  29.  
  30.     Sub ExtractWinFormsDesignerFile()
  31.  
  32.         Dim item As ProjectItem = DTE.SelectedItems.Item(1).ProjectItem
  33.         Dim sourceFileName As String = item.FileNames(1)
  34.         Dim sourceDir As String = System.IO.Path.GetDirectoryName(sourceFileName)
  35.         Dim origCode As String
  36.         Dim bareName As String = System.IO.Path.GetFileNameWithoutExtension(sourceFileName)
  37.         Dim newDesignerFile As String = sourceDir & "\" & bareName & ".Designer.vb"
  38.  
  39.         If IO.File.Exists(newDesignerFile) Then
  40.             MsgBox("Designer file already exists!")
  41.             Exit Sub
  42.         Else
  43.             If MsgBox("You are about to extract designer code from:" + vbCrLf + _
  44.                       vbCrLf + _
  45.                       "    " + sourceFileName + vbCrLf + _
  46.                       vbCrLf + _
  47.                       "To the following file:" + vbCrLf + _
  48.                       vbCrLf + _
  49.                       "    " + newDesignerFile + vbCrLf + _
  50.                       vbCrLf + _
  51.                       "MAKE SURE TO BACK UP FIRST!", _
  52.                       MsgBoxStyle.OkCancel Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Question, _
  53.                       "Confirm") = MsgBoxResult.Cancel Then Exit Sub
  54.         End If
  55.         origCode = System.IO.File.ReadAllText(sourceFileName)
  56.  
  57.         Dim codeClass As CodeClass
  58.         Dim namespaceName As String
  59.  
  60.         codeClass = findClass(item.FileCodeModel.CodeElements)
  61.         namespaceName = ""
  62.         If codeClass.Namespace IsNot Nothing Then
  63.             namespaceName = codeClass.Namespace.FullName
  64.         End If
  65.  
  66.  
  67.         Dim initComponentText As String
  68.         Dim disposeText As String
  69.         Dim fieldDecls() As String
  70.         Try
  71.             initComponentText = extractMember(codeClass.Members.Item("InitializeComponent"))
  72.         Catch
  73.             MsgBox("Error extracting InitializeComponent!")
  74.             Exit Sub
  75.         End Try
  76.         Try
  77.             disposeText = extractMember(codeClass.Members.Item("Dispose"))
  78.         Catch
  79.             MsgBox("Error extracting Dispose! Will restore source file.")
  80.             System.IO.File.WriteAllText(sourceFileName, origCode)
  81.             Exit Sub
  82.         End Try
  83.         Try
  84.             fieldDecls = extractWinFormsFields(codeClass, initComponentText)
  85.         Catch
  86.             MsgBox("Error extracting field declares! Will restore source file.")
  87.             System.IO.File.WriteAllText(sourceFileName, origCode)
  88.             Exit Sub
  89.         End Try
  90.  
  91.         Dim finalCode As String
  92.  
  93.         finalCode = IIf(namespaceName <> "", "Namespace " + namespaceName + vbCrLf + vbCrLf, "") + _
  94.                     "<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _" + vbCrLf + _
  95.                     "Partial Class " + codeClass.Name + vbCrLf + _
  96.                     "    Inherits System.Windows.Forms.Form" + vbCrLf + _
  97.                     vbCrLf + _
  98.                     disposeText + vbCrLf + _
  99.                     vbCrLf + _
  100.                     fieldDecls(0) + vbCrLf + _
  101.                     vbCrLf + _
  102.                     initComponentText + vbCrLf + _
  103.                     fieldDecls(1) + vbCrLf + _
  104.                     "End Class" + vbCrLf + _
  105.                     IIf(namespaceName <> "", vbCrLf + "End Namespace" + vbCrLf, "")
  106.  
  107.         ' Now write the new designer file
  108.  
  109.         System.IO.File.WriteAllText(newDesignerFile, finalCode)
  110.  
  111.         Dim newProjItem As ProjectItem = item.ProjectItems.AddFromFile(newDesignerFile)
  112.         Try
  113.             newProjItem.Open()
  114.         Catch
  115.         End Try
  116.         Try
  117.             DTE.ExecuteCommand("Edit.FormatDocument")
  118.         Catch
  119.         End Try
  120.  
  121.         MsgBox("Code separated successfully!" + vbCrLf + _
  122.                "You may have residual comments or code regions in the source " + vbCrLf + _
  123.                "class that should be deleted or moved to the new Designer.vb class.", _
  124.                MsgBoxStyle.Information, _
  125.                "Complete")
  126.  
  127.     End Sub
  128.  
  129.     Function findClass(ByVal items As System.Collections.IEnumerable) As CodeClass
  130.  
  131.         For Each codeEl As CodeElement In items
  132.  
  133.             If codeEl.Kind = vsCMElement.vsCMElementClass Then
  134.  
  135.                 Return codeEl
  136.  
  137.             ElseIf codeEl.Children.Count > 0 Then
  138.  
  139.                 Dim cls As CodeClass = findClass(codeEl.Children)
  140.                 If cls IsNot Nothing Then
  141.                     Return findClass(codeEl.Children)
  142.                 End If
  143.  
  144.             End If
  145.  
  146.         Next
  147.  
  148.         Return Nothing
  149.  
  150.     End Function
  151.  
  152.     Function extractWinFormsFields(ByVal codeClass As CodeClass, ByVal initComponentsCode As String) As String()
  153.  
  154.         Dim member As CodeElement
  155.         Dim fieldsCode As String
  156.         Dim components As String
  157.         Dim initComponentsCodeForComp As String = initComponentsCode.ToLower
  158.  
  159.         fieldsCode = ""
  160.         For i As Integer = codeClass.Members.Count To 1 Step -1
  161.  
  162.             member = codeClass.Members.Item(i)
  163.  
  164.             If member.Kind = vsCMElement.vsCMElementVariable Then
  165.  
  166.                 Dim field As CodeVariable = member
  167.  
  168.                 If field.Type.TypeKind <> vsCMTypeRef.vsCMTypeRefArray Then
  169.  
  170.                     If field.Name.ToLower = "components" Then
  171.  
  172.                         ' We'll insert this separately
  173.                        components = extractMember(field)
  174.  
  175.                     ElseIf initComponentsCodeForComp Like ("* Me." + field.Name + " = New *").ToLower Then
  176.  
  177.                         fieldsCode = extractMember(field) + vbCrLf + _
  178.                                      fieldsCode
  179.  
  180.                     End If
  181.  
  182.                 End If
  183.  
  184.             End If
  185.  
  186.         Next
  187.  
  188.         Return New String() {components, fieldsCode}
  189.  
  190.     End Function
  191.  
  192.     Function extractMember(ByVal memberElement As CodeElement) As String
  193.  
  194.         Dim memberStart As EditPoint = memberElement.GetStartPoint().CreateEditPoint()
  195.         Dim memberText As String = String.Empty
  196.  
  197.         memberText += memberStart.GetText(memberElement.GetEndPoint())
  198.         memberStart.Delete(memberElement.GetEndPoint())
  199.  
  200.         Return memberText
  201.  
  202.     End Function
  203.  
  204. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement