/
SiteBuilderFactory.cs
347 lines (301 loc) · 15.9 KB
/
SiteBuilderFactory.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using Kudu.Contracts.Settings;
using Kudu.Contracts.Tracing;
using Kudu.Core.Infrastructure;
using Kudu.Core.SourceControl;
using Kudu.Core.Tracing;
namespace Kudu.Core.Deployment.Generator
{
public class SiteBuilderFactory : ISiteBuilderFactory
{
private readonly IEnvironment _environment;
private readonly IBuildPropertyProvider _propertyProvider;
public SiteBuilderFactory(IBuildPropertyProvider propertyProvider, IEnvironment environment)
{
_propertyProvider = propertyProvider;
_environment = environment;
}
public ISiteBuilder CreateBuilder(ITracer tracer, ILogger logger, IDeploymentSettingsManager settings, IFileFinder fileFinder)
{
string repositoryRoot = _environment.RepositoryPath;
// Use the cached vs projects file finder for: a. better performance, b. ignoring solutions/projects under node_modules
fileFinder = new CachedVsProjectsFileFinder(fileFinder);
// If there's a custom deployment file then let that take over.
var command = settings.GetValue(SettingsKeys.Command);
if (!String.IsNullOrEmpty(command))
{
return new CustomBuilder(_environment, settings, _propertyProvider, repositoryRoot, command);
}
// If the user provided specific generator arguments, that overrides any detection logic
string scriptGeneratorArgs = settings.GetValue(SettingsKeys.ScriptGeneratorArgs);
if (!String.IsNullOrEmpty(scriptGeneratorArgs))
{
return new CustomGeneratorCommandSiteBuilder(_environment, settings, _propertyProvider, repositoryRoot, scriptGeneratorArgs);
}
// If the repository has an explicit pointer to a project path to be deployed
// then use it.
string targetProjectPath = settings.GetValue(SettingsKeys.Project);
if (!String.IsNullOrEmpty(targetProjectPath))
{
tracer.Trace("Specific project was specified: " + targetProjectPath);
targetProjectPath = Path.GetFullPath(Path.Combine(repositoryRoot, targetProjectPath.TrimStart('/', '\\')));
// Try to resolve the project
return ResolveProject(repositoryRoot,
targetProjectPath,
settings,
fileFinder,
tryWebSiteProject: true,
searchOption: SearchOption.TopDirectoryOnly);
}
// Get all solutions in the current repository path
var solutions = VsHelper.GetSolutions(repositoryRoot, fileFinder).ToList();
if (!solutions.Any())
{
return ResolveProject(repositoryRoot,
settings,
fileFinder,
searchOption: SearchOption.AllDirectories);
}
// More than one solution is ambiguous
if (solutions.Count > 1)
{
// TODO: Show relative paths in error messages
ThrowAmbiguousSolutionsError(solutions);
}
// We have a solution
VsSolution solution = solutions[0];
// We need to determine what project to deploy so get a list of all web projects and
// figure out with some heuristic, which one to deploy.
// TODO: Pick only 1 and throw if there's more than one
VsSolutionProject project = solution.Projects.Where(p => p.IsWap || p.IsWebSite || p.IsAspNetCore).FirstOrDefault();
if (project == null)
{
// Try executable type project
project = solution.Projects.Where(p => p.IsExecutable).FirstOrDefault();
if (project != null)
{
return new DotNetConsoleBuilder(_environment,
settings,
_propertyProvider,
repositoryRoot,
project.AbsolutePath,
solution.Path);
}
logger.Log(Resources.Log_NoDeployableProjects, solution.Path);
return ResolveNonAspProject(repositoryRoot, null, settings);
}
if (project.IsWap)
{
return new WapBuilder(_environment,
settings,
_propertyProvider,
repositoryRoot,
project.AbsolutePath,
solution.Path);
}
if (project.IsAspNetCore)
{
return new AspNetCoreBuilder(_environment,
settings,
_propertyProvider,
repositoryRoot,
project.AbsolutePath,
solution.Path);
}
return new WebSiteBuilder(_environment,
settings,
_propertyProvider,
repositoryRoot,
project.AbsolutePath,
solution.Path);
}
private ISiteBuilder ResolveNonAspProject(string repositoryRoot, string projectPath, IDeploymentSettingsManager perDeploymentSettings)
{
string sourceProjectPath = projectPath ?? repositoryRoot;
if (IsNodeSite(sourceProjectPath))
{
return new NodeSiteBuilder(_environment, perDeploymentSettings, _propertyProvider, repositoryRoot, projectPath);
}
else if (IsPythonSite(sourceProjectPath))
{
return new PythonSiteBuilder(_environment, perDeploymentSettings, _propertyProvider, repositoryRoot, projectPath);
}
else if (IsGoSite(sourceProjectPath))
{
return new GoSiteBuilder(_environment, perDeploymentSettings, _propertyProvider, repositoryRoot, projectPath);
}
else if (IsFunctionApp(sourceProjectPath))
{
return new FunctionAppBuilder(_environment, perDeploymentSettings, _propertyProvider, repositoryRoot, projectPath);
}
return new BasicBuilder(_environment, perDeploymentSettings, _propertyProvider, repositoryRoot, projectPath);
}
private static bool IsGoSite(string projectPath)
{
return GoSiteEnabler.LooksLikeGo(projectPath);
}
private static bool IsNodeSite(string projectPath)
{
return NodeSiteEnabler.LooksLikeNode(projectPath);
}
private static bool IsPythonSite(string projectPath)
{
return PythonSiteEnabler.LooksLikePython(projectPath);
}
private static bool IsFunctionApp(string projectPath)
{
return FunctionAppEnabler.LooksLikeFunctionApp(projectPath);
}
private ISiteBuilder ResolveProject(string repositoryRoot, IDeploymentSettingsManager perDeploymentSettings, IFileFinder fileFinder, bool tryWebSiteProject = false, SearchOption searchOption = SearchOption.AllDirectories)
{
return ResolveProject(repositoryRoot, repositoryRoot, perDeploymentSettings, fileFinder, tryWebSiteProject, searchOption, specificConfiguration: false);
}
private ISiteBuilder ResolveProject(string repositoryRoot, string targetPath, IDeploymentSettingsManager perDeploymentSettings, IFileFinder fileFinder, bool tryWebSiteProject, SearchOption searchOption = SearchOption.AllDirectories, bool specificConfiguration = true)
{
if (DeploymentHelper.IsProject(targetPath))
{
// needs to check for project file existence
if (!File.Exists(targetPath))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
Resources.Error_ProjectDoesNotExist,
targetPath));
}
return DetermineProject(repositoryRoot, targetPath, perDeploymentSettings, fileFinder);
}
// Check for loose projects
var projects = DeploymentHelper.GetProjects(targetPath, fileFinder, searchOption);
if (projects.Count > 1)
{
// Can't determine which project to build
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
Resources.Error_AmbiguousProjects,
String.Join(", ", projects)));
}
else if (projects.Count == 1)
{
return DetermineProject(repositoryRoot, projects[0], perDeploymentSettings, fileFinder);
}
// Check for ASP.NET Core project without VS solution or project
string projectJson;
if (AspNetCoreHelper.TryAspNetCoreWebProject(targetPath, fileFinder, out projectJson))
{
return new AspNetCoreBuilder(_environment,
perDeploymentSettings,
_propertyProvider,
repositoryRoot,
projectJson);
}
if (tryWebSiteProject)
{
// Website projects need a solution to build so look for one in the repository path
// that has this website in it.
var solutions = VsHelper.FindContainingSolutions(repositoryRoot, targetPath, fileFinder);
// More than one solution is ambiguous
if (solutions.Count > 1)
{
ThrowAmbiguousSolutionsError(solutions);
}
else if (solutions.Count == 1)
{
// Unambiguously pick the root
return new WebSiteBuilder(_environment,
perDeploymentSettings,
_propertyProvider,
repositoryRoot,
targetPath,
solutions[0].Path);
}
}
// This should only ever happen if the user specifies an invalid directory.
// The other case where the method is called we always resolve the path so it's a non issue there.
if (specificConfiguration && !Directory.Exists(targetPath))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
Resources.Error_ProjectDoesNotExist,
targetPath));
}
// If there's none then use the basic builder (the site is xcopy deployable)
return ResolveNonAspProject(repositoryRoot, targetPath, perDeploymentSettings);
}
private ISiteBuilder DetermineProject(string repositoryRoot, string targetPath, IDeploymentSettingsManager perDeploymentSettings, IFileFinder fileFinder)
{
var solution = VsHelper.FindContainingSolution(repositoryRoot, targetPath, fileFinder);
string solutionPath = solution?.Path;
var projectTypeGuids = VsHelper.GetProjectTypeGuids(targetPath);
if (VsHelper.IsWap(projectTypeGuids))
{
return new WapBuilder(_environment,
perDeploymentSettings,
_propertyProvider,
repositoryRoot,
targetPath,
solutionPath);
}
else if (AspNetCoreHelper.IsDotnetCorePreview3(targetPath, projectTypeGuids) || targetPath.EndsWith(".xproj", StringComparison.OrdinalIgnoreCase))
{
return new AspNetCoreBuilder(_environment,
perDeploymentSettings,
_propertyProvider,
repositoryRoot,
targetPath,
solutionPath);
}
else if (VsHelper.IsExecutableProject(targetPath))
{
// This is a console app
return new DotNetConsoleBuilder(_environment,
perDeploymentSettings,
_propertyProvider,
repositoryRoot,
targetPath,
solutionPath);
}
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
Resources.Error_ProjectNotDeployable,
targetPath));
}
private static void ThrowAmbiguousSolutionsError(IList<VsSolution> solutions)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
Resources.Error_AmbiguousSolutions,
String.Join(", ", solutions.Select(s => s.Path))));
}
private class CachedVsProjectsFileFinder : IFileFinder
{
// The way CachedVsProjectsFileFinder works is it looks up all the files in this CachedExtensions list the first time
// then it's the looked up list is filtered with the passed in lookupList to ListFiles()
private static readonly string[] CachedExtensions = DeploymentHelper.ProjectFileLookup.Concat(VsHelper.SolutionsLookupList).Concat(AspNetCoreHelper.ProjectJsonLookupList).ToArray();
private const string NodeModulesDirectory = "\\node_modules\\";
private IFileFinder _fileFinder;
private string _path;
private List<string> _cachedResults;
public CachedVsProjectsFileFinder(IFileFinder fileFinder)
{
_fileFinder = fileFinder;
}
public IEnumerable<string> ListFiles(string path, SearchOption searchOption, params string[] lookupList)
{
if (searchOption == SearchOption.AllDirectories && (_path == null || PathUtilityFactory.Instance.PathsEquals(_path, path)))
{
if (_cachedResults == null)
{
_path = path;
_cachedResults =
_fileFinder.ListFiles(path, searchOption, CachedExtensions)
.Where(filePath => !filePath.Contains(NodeModulesDirectory))
.ToList();
}
lookupList = lookupList.Select(l => l.TrimStart('*')).ToArray();
return _cachedResults.Where(filePath => lookupList.Any(lookup => filePath.EndsWith(lookup, StringComparison.OrdinalIgnoreCase)));
}
return _fileFinder.ListFiles(path, searchOption, lookupList);
}
}
}
}