Friday, November 11, 2011

Reformatting All .xaml Files in Visual Studio Solution

 

Today I decided to format all .xaml documents in a solution so that all attributes are placed on a separate line and also to line nested tags on correct positions.

Actually I already setup settings in Visual Studio Tools menu a long time ago, but some developers working on the same project forget to format documents before check in – the result is - to comfortably read the file in standard format I need to check out the file and re-format it.

I decided to format all files in a solution so at least some time I don’t need to do this Smile

I have found very good answer on Stack Overflow:

auto format all files in solution in visual studio 2010

This works fine in Visual Studio 2008 too.

I had to modify a script a little to allow iterating over solution folders as well.

My mods allowed only 1 hierarchy level of solution folders to be supported – but this can be extended by recursive function call in first method resolving projects.

 

Here is the script:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Module1

    Sub IterateFiles()
        Dim solution As Solution = DTE.Solution

        For Each prj As Project In solution.Projects

            If (prj.ConfigurationManager IsNot Nothing) Then
                ' It's a project!
                If (prj.UniqueName = prj.Name) Then IterateProjectFiles(prj.ProjectItems)
            Else
                If (prj.ProjectItems IsNot Nothing) Then
                    For Each projectItem In prj.ProjectItems
                        If (projectItem.SubProject IsNot Nothing) Then
                            IterateProjectFiles(projectItem.SubProject.ProjectItems)
                        End If
                    Next
                End If
            End If
        Next
    End Sub

    Private Sub IterateProjectFiles(ByVal prjItms As ProjectItems)
        For Each file As ProjectItem In prjItms
            If file.SubProject IsNot Nothing Then
                FormatFile(file)
                If file.ProjectItems IsNot Nothing Then
                    IterateProjectFiles(file.ProjectItems)
                End If

            ElseIf file.ProjectItems IsNot Nothing AndAlso file.ProjectItems.Count > 0 Then
                FormatFile(file)
                If file.ProjectItems IsNot Nothing Then
                    IterateProjectFiles(file.ProjectItems)
                End If
            Else
                FormatFile(file)
            End If
        Next
    End Sub

    Private Sub FormatFile(ByVal file As ProjectItem)
        'Exit Sub
        DTE.ExecuteCommand("View.SolutionExplorer")
        If file.Name.EndsWith(".xaml") Then
            file.Open()
            file.Document.Activate()

            DTE.ExecuteCommand("Edit.FormatDocument", "")

            file.Document.Save()
            file.Document.Close()
        End If
    End Sub

End Module

Happy Coding

Kirill

1 comment:

Magento Development said...

I have no words for this great post such a awe-some information I got gathered. Thanks to Author.