Repository navigation
Expand file tree
/
Copy pathProgram.vb
More file actions
52 lines (40 loc) · 2.55 KB
/
Copy pathProgram.vb
File metadata and controls
52 lines (40 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.MSBuild
' This program will format all Visual Basic and C# source files for an entire solution.
Module Program
Sub Main(args As String())
' The test solution is copied to the output directory when you build this sample.
Dim workspace As MSBuildWorkspace = MSBuildWorkspace.Create()
' Open the solution within the workspace.
Dim originalSolution As Solution = workspace.OpenSolutionAsync("TestSolutionForVB\Test.sln").Result
' Declare a variable to store the intermediate solution snapshot at each step.
Dim newSolution As Solution = originalSolution
' Note how we can't simply iterate over originalSolution.Projects or project.Documents
' because it will return objects from the unmodified originalSolution, Not from the newSolution.
' We need to use the ProjectIds And DocumentIds (that don't change) to look up the corresponding
' snapshots in the newSolution.
For Each projectId As ProjectId In originalSolution.ProjectIds
' Look up the snapshot for the original project in the latest forked solution.
Dim project As Project = newSolution.GetProject(projectId)
For Each documentId As DocumentId In project.DocumentIds
' Look up the snapshot for the original document in the latest forked solution.
Dim document As Document = newSolution.GetDocument(documentId)
' Get a transformed version of the document (a new solution snapshot is created
' under the covers to contain it - none of the existing objects are modified).
Dim newDocument As Document = Formatter.FormatAsync(document).Result
' Store the solution implicitly constructed in the previous step as the latest
' one so we can continue building it up in the next iteration.
newSolution = newDocument.Project.Solution
Next
Next
' Actually apply the accumulated changes And save them to disk. At this point
' workspace.CurrentSolution Is updated to point to the New solution.
If workspace.TryApplyChanges(newSolution) Then
Console.WriteLine("Solution updated.")
Else
Console.WriteLine("Update failed!")
End If
End Sub
End Module