Repository navigation
Expand file tree
/
Copy pathSolutionFileBuilder.cs
More file actions
208 lines (170 loc) · 8.11 KB
/
Copy pathSolutionFileBuilder.cs
File metadata and controls
208 lines (170 loc) · 8.11 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// --------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// --------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Construction;
using Microsoft.Build.Graph;
using Microsoft.Build.Graph.UnitTests;
using Microsoft.Build.UnitTests;
using Shouldly;
#nullable disable
namespace Microsoft.Build.Engine.UnitTests
{
internal sealed class SolutionFileBuilder
{
public static readonly IReadOnlyCollection<SolutionConfigurationInSolution> SolutionConfigurationPlatformsDefaults = new[]
{
new SolutionConfigurationInSolution("Debug", "AnyCPU"),
new SolutionConfigurationInSolution("Release", "AnyCPU")
};
/// <summary>
/// projectName -> projectPath
/// </summary>
public IReadOnlyDictionary<string, string> Projects { get; set; }
public IReadOnlyCollection<SolutionConfigurationInSolution> SolutionConfigurationPlatforms { get; set; } = SolutionConfigurationPlatformsDefaults;
public Dictionary<string, Dictionary<SolutionConfigurationInSolution, ProjectConfigurationInSolution>> ProjectConfigurations { get; set; }
public (string, string)[] SolutionDependencies { get; set; }
public (string, string[])[] SolutionDependenciesProjectNameToGuids { get; set; }
private readonly struct ProjectInfo
{
private readonly List<ProjectInfo> _dependencies;
private readonly List<string> _dependencyGuids;
public string Name { get; }
public string Path { get; }
public string ProjectTypeGuid { get; }
public string Guid { get; }
public IReadOnlyCollection<ProjectInfo> Dependencies => _dependencies;
public IReadOnlyCollection<string> DependencyGuids => _dependencyGuids;
public ProjectInfo(string name, string path, string projectTypeGuid, string guid)
{
Name = name;
Path = path;
ProjectTypeGuid = projectTypeGuid;
Guid = guid;
_dependencies = new List<ProjectInfo>();
_dependencyGuids = new List<string>();
}
public void AddDependency(ProjectInfo dependency)
{
_dependencies.Add(dependency);
}
internal void AddGuidDependencies(string[] dependencyGuids)
{
_dependencyGuids.AddRange(dependencyGuids);
}
}
public static SolutionFileBuilder FromGraphEdges(
TestEnvironment env,
Dictionary<int, int[]> edges,
(string, string[])[] solutionDependenciesProjectNameToGuids = null)
{
return FromGraph(Helpers.CreateProjectGraph(env, edges), solutionDependenciesProjectNameToGuids: solutionDependenciesProjectNameToGuids);
}
public static SolutionFileBuilder FromGraph(
ProjectGraph graph,
Dictionary<string, Dictionary<SolutionConfigurationInSolution, ProjectConfigurationInSolution>> projectConfigurations = null,
(string, string)[] solutionDependencies = null,
(string, string[])[] solutionDependenciesProjectNameToGuids = null)
{
return new SolutionFileBuilder
{
Projects = graph.ProjectNodes.ToDictionary(
n => GraphTestingUtilities.GetProjectNumber((ProjectGraphNode)n).ToString(),
n => n.ProjectInstance.FullPath),
ProjectConfigurations = projectConfigurations,
SolutionDependencies = solutionDependencies,
SolutionDependenciesProjectNameToGuids = solutionDependenciesProjectNameToGuids
};
}
public string BuildSolution()
{
var projectInfos = Projects.ToDictionary(
kvp => kvp.Key,
kvp => new ProjectInfo(
kvp.Key,
kvp.Value,
"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}",
Guid.NewGuid()
.ToString("B")));
if (SolutionDependencies?.Length > 0)
{
foreach (var (parent, dependency) in SolutionDependencies)
{
projectInfos.ShouldContainKey(parent);
projectInfos.ShouldContainKey(dependency);
projectInfos[parent].AddDependency(projectInfos[dependency]);
}
}
if (SolutionDependenciesProjectNameToGuids?.Length > 0)
{
foreach (var (parent, dependencyGuids) in SolutionDependenciesProjectNameToGuids)
{
projectInfos.ShouldContainKey(parent);
projectInfos[parent].AddGuidDependencies(dependencyGuids);
}
}
ProjectConfigurations ??= projectInfos.ToDictionary(
p => p.Key,
p => SolutionConfigurationPlatforms.ToDictionary(
sc => sc,
sc => new ProjectConfigurationInSolution(
sc.ConfigurationName,
sc.PlatformName,
true)));
var sb = new StringBuilder();
sb.AppendLine(@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2009
MinimumVisualStudioVersion = 10.0.40219.1");
foreach (var project in projectInfos.Values)
{
sb.Append(@"
Project(""").Append(project.ProjectTypeGuid).Append(@""") = """).Append(project.Name).Append(@""", """).Append(project.Path).Append(@""", """).Append(project.Guid).AppendLine(@"""");
if (project.Dependencies.Count > 0 || project.DependencyGuids.Count > 0)
{
sb.AppendLine("\tProjectSection(ProjectDependencies) = postProject");
foreach (var dependency in project.Dependencies)
{
sb.Append("\t\t").Append(dependency.Guid).Append(" = ").AppendLine(dependency.Guid);
}
foreach (var dependencyGuid in project.DependencyGuids)
{
sb.Append("\t\t").Append(dependencyGuid).Append(" = ").AppendLine(dependencyGuid);
}
sb.AppendLine("\tEndProjectSection");
}
sb.AppendLine(@"
EndProject");
}
sb.AppendLine("Global");
sb.AppendLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution");
foreach (var solutionConfigurationPlatform in SolutionConfigurationPlatforms)
{
sb.Append("\t\t").Append(solutionConfigurationPlatform.FullName).Append(" = ").AppendLine(solutionConfigurationPlatform.FullName);
}
sb.AppendLine("\tEndGlobalSection");
sb.AppendLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
foreach (var project in projectInfos)
{
foreach (var configurationMapping in ProjectConfigurations[project.Key])
{
var solutionConfiguration = configurationMapping.Key;
var projectConfiguration = configurationMapping.Value;
sb.Append("\t\t").Append(project.Value.Guid).Append('.').Append(solutionConfiguration.FullName).Append(".ActiveCfg = ").AppendLine(projectConfiguration.FullName);
sb.Append("\t\t").Append(project.Value.Guid).Append('.').Append(solutionConfiguration.FullName).Append(".Build.0 = ").AppendLine(projectConfiguration.FullName);
}
}
sb.AppendLine("\tEndGlobalSection");
sb.AppendLine("EndGlobal");
return sb.ToString();
}
}
}