-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
StartProjectFinder.cs
204 lines (181 loc) · 7.15 KB
/
StartProjectFinder.cs
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
namespace SetStartupProjects;
/// <summary>
/// Helper class for extracting Visual Studio startup projects by looking at the projects contained in the solution directory and applying some conventions.
///
/// The current conventions are as follows:
/// <list type="bullet">
/// <item>
/// <description>'StartAction' is 'Program'.</description>
/// </item>
/// <item>
/// <description>'OutputType' is 'Exe'.</description>
/// </item>
/// <item>
/// <description>'OutputType' is 'WinExe'.</description>
/// </item>
/// <item>
/// <description>Project extension is `ccproj` ie an Azure Cloud Service.</description>
/// </item>
/// <item>
/// <description>Project type is 'ASP.NET MVC 1.0' ie '603C0E0B-DB56-11DC-BE95-000D561079B0'.</description>
/// </item>
/// <item>
/// <description>Project type is 'ASP.NET MVC 2.0' ie 'F85E285D-A4E0-4152-9332-AB1D724D3325'.</description>
/// </item>
/// <item>
/// <description>Project type is 'ASP.NET MVC 3.0' ie 'E53F8FEA-EAE0-44A6-8774-FFD645390401'.</description>
/// </item>
/// <item>
/// <description>Project type is 'ASP.NET MVC 4.0' ie 'E3E379DF-F4C6-4180-9B81-6769533ABE47'.</description>
/// </item>
/// <item>
/// <description>Project type is 'Web Application' ie '349C5851-65DF-11DA-9384-00065B846F21'.</description>
/// </item>
/// <item>
/// <description>Project type is 'Web Site' ie 'E24C65DC-7377-472B-9ABA-BC803B73C61A'.</description>
/// </item>
/// </list>
/// </summary>
public static class StartProjectFinder
{
static IEnumerable<string> GuessStartupProjects(string solutionFile) =>
from project in SolutionProjectExtractor.GetAllProjectFiles(solutionFile)
where ShouldIncludeProjectFile(project)
select project.Guid;
/// <summary>
/// Get the startup projects by looking at the projects contained in <paramref name="solutionFile"/>.
/// </summary>
public static IEnumerable<string> GetStartProjects(string solutionFile)
{
Guard.AgainstNullAndEmpty(solutionFile, nameof(solutionFile));
Guard.AgainstNonExistingFile(solutionFile, nameof(solutionFile));
var nameWithoutExtension = Path.GetFileNameWithoutExtension(solutionFile);
var solutionDirectory = Path.GetDirectoryName(solutionFile)!;
var defaultProjectsTextFile = Path.Combine(solutionDirectory, $"{nameWithoutExtension}.StartupProjects.txt");
if (!File.Exists(defaultProjectsTextFile))
{
foreach (var startProject in GuessStartupProjects(solutionFile))
{
yield return startProject;
}
yield break;
}
var allPossibleProjects = SolutionProjectExtractor.GetAllProjectFiles(solutionFile).ToList();
var defaultProjects = File.ReadAllLines(defaultProjectsTextFile)
.Where(_ => !string.IsNullOrWhiteSpace(_));
foreach (var startupProject in defaultProjects)
{
var project = allPossibleProjects.FirstOrDefault(_ => string.Equals(_.RelativePath, startupProject, StringComparison.OrdinalIgnoreCase));
if (project == null)
{
var error = $"Could not find the relative path to the default startup project '{startupProject}'. Ensure `{defaultProjectsTextFile}` contains relative (to the solution directory) paths to project files.";
throw new(error);
}
yield return project.Guid;
}
}
static bool ShouldIncludeProjectFile(Project project)
{
var projectFile = project.FullPath;
Guard.AgainstNonExistingFile(projectFile, "Project");
try
{
if (ShouldIncludeForFileExtension(Path.GetExtension(projectFile)))
{
return true;
}
using var reader = File.OpenText(projectFile);
var xDocument = XDocument.Load(reader);
return ShouldIncludeProjectXml(xDocument, projectFile);
}
catch (Exception exception)
{
throw new($"Failed {nameof(ShouldIncludeProjectFile)}: {projectFile}", exception);
}
}
static bool ShouldIncludeForFileExtension(string extension) =>
extension == ".ccproj" ||
extension == ".sfproj";
internal static bool ShouldIncludeProjectXml(XDocument xDocument, string projectFile)
{
var directoryName = Path.GetDirectoryName(projectFile)!;
var netCoreLaunchSettingsFile = Path.Combine(directoryName, "Properties", "launchSettings.json");
if (File.Exists(netCoreLaunchSettingsFile))
{
return true;
}
xDocument.StripNamespace();
if (ShouldIncludeForStartAction(xDocument))
{
return true;
}
if (ShouldIncludeForWebSdk(xDocument))
{
return true;
}
var xElement = xDocument.Root!;
var propertyGroups = xElement
.Elements("PropertyGroup");
foreach (var propertyGroup in propertyGroups)
{
if (ShouldIncludeForOutputType(propertyGroup))
{
return true;
}
if (ShouldIncludeFromProjectTypeGuids(propertyGroup))
{
return true;
}
}
return false;
}
static bool ShouldIncludeForStartAction(XDocument document) =>
document.Descendants("StartAction")
.Any(_ => _.Value == "Program");
static bool ShouldIncludeForWebSdk(XDocument document)
{
var attribute = document.Root!.Attribute("Sdk");
if (attribute == null)
{
return false;
}
return attribute.Value == "Microsoft.NET.Sdk.Web";
}
static bool ShouldIncludeForOutputType(XElement propertyGroup)
{
var xElement = propertyGroup.Element("OutputType");
// OutputType can be null for xprojs
if (xElement != null)
{
var outputType = xElement.Value;
if (
string.Equals(outputType, "Exe", StringComparison.OrdinalIgnoreCase) ||
string.Equals(outputType, "WinExe", StringComparison.OrdinalIgnoreCase)
)
{
return true;
}
}
return false;
}
static bool ShouldIncludeFromProjectTypeGuids(XElement propertyGroup)
{
var projectTypes = propertyGroup.Element("ProjectTypeGuids");
if (projectTypes != null)
{
return projectTypes.Value.Split(';')
.Select(_ => _.Trim('{', '}'))
.Any(typeGuid => DefaultIncludedGuids.Any(_ => string.Equals(_, typeGuid, StringComparison.OrdinalIgnoreCase)));
}
return false;
}
static List<string> DefaultIncludedGuids =
[
"603C0E0B-DB56-11DC-BE95-000D561079B0", //ASP.NET MVC 1.0
"F85E285D-A4E0-4152-9332-AB1D724D3325", //ASP.NET MVC 2.0
"E53F8FEA-EAE0-44A6-8774-FFD645390401", //ASP.NET MVC 3.0
"E3E379DF-F4C6-4180-9B81-6769533ABE47", //ASP.NET MVC 4.0
"349C5851-65DF-11DA-9384-00065B846F21", //Web Application
"E24C65DC-7377-472B-9ABA-BC803B73C61A" //Web Site
];
}