-
Notifications
You must be signed in to change notification settings - Fork 214
/
DependencyInstaller.cs
178 lines (155 loc) · 6.25 KB
/
DependencyInstaller.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.Web.CodeGeneration.Contracts.ProjectModel;
using Microsoft.VisualStudio.Web.CodeGeneration.DotNet;
using Microsoft.VisualStudio.Web.CodeGeneration;
using Microsoft.VisualStudio.Web.CodeGeneration.Utils;
namespace Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Dependency
{
public abstract class DependencyInstaller
{
protected DependencyInstaller(
IProjectContext projectContext,
IApplicationInfo applicationInfo,
ILogger logger,
IPackageInstaller packageInstaller,
IServiceProvider serviceProvider)
{
if (projectContext == null)
{
throw new ArgumentNullException(nameof(projectContext));
}
if (applicationInfo == null)
{
throw new ArgumentNullException(nameof(applicationInfo));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (packageInstaller == null)
{
throw new ArgumentNullException(nameof(packageInstaller));
}
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
ProjectContext = projectContext;
ApplicationEnvironment = applicationInfo;
Logger = logger;
PackageInstaller = packageInstaller;
ServiceProvider = serviceProvider;
}
public async Task Execute()
{
if (MissingDepdencies.Any())
{
await GenerateCode();
}
}
public async Task InstallDependencies()
{
if (MissingDepdencies.Any())
{
var isReadMe = true;
var readMeGenerator = ActivatorUtilities.CreateInstance<ReadMeGenerator>(ServiceProvider);
if (IsMsBuildProject)
{
readMeGenerator.GenerateReadMeWithContent(GetMsBuildMissingDependencyReadMeText(MissingDepdencies));
}
else
{
await PackageInstaller.InstallPackages(MissingDepdencies);
isReadMe = await readMeGenerator.GenerateStartupOrReadme(StartupContents.ToList());
}
if (isReadMe)
{
Logger.LogMessage(MessageStrings.AdditionalStepsRequired);
Logger.LogMessage(string.Format(MessageStrings.CheckoutReadMe, Constants.ReadMeOutputFileName));
}
}
}
private string GetMsBuildMissingDependencyReadMeText(IEnumerable<PackageMetadata> missingDepdencies)
{
var contentBuilder = new StringBuilder(MessageStrings.InstallPackageMessage);
foreach (var dependency in missingDepdencies)
{
contentBuilder.Append($"{Environment.NewLine} {dependency.Name} :: {dependency.Version}");
}
return contentBuilder.ToString();
}
protected abstract Task GenerateCode();
protected IApplicationInfo ApplicationEnvironment { get; private set; }
protected ILogger Logger { get; private set; }
public IPackageInstaller PackageInstaller { get; private set; }
protected IServiceProvider ServiceProvider { get; private set; }
protected IProjectContext ProjectContext { get; private set; }
protected bool IsMsBuildProject => Path.GetExtension(ProjectContext.ProjectFullPath).Equals(".csproj", StringComparison.OrdinalIgnoreCase);
protected IEnumerable<string> TemplateFolders
{
get
{
return TemplateFoldersUtilities.GetTemplateFolders(
containingProject: Constants.ThisAssemblyName,
baseFolders: new[] { TemplateFoldersName },
applicationBasePath: ApplicationEnvironment.ApplicationBasePath,
projectContext: ProjectContext);
}
}
protected virtual IEnumerable<PackageMetadata> Dependencies
{
get
{
return Enumerable.Empty<PackageMetadata>();
}
}
protected virtual IEnumerable<StartupContent> StartupContents
{
get
{
return Enumerable.Empty<StartupContent>();
}
}
protected abstract string TemplateFoldersName { get; }
protected IEnumerable<PackageMetadata> MissingDepdencies
{
get
{
return Dependencies
.Where(dep => ProjectContext.GetPackage(dep.Name) == null);
}
}
// Copies files from given source directory to destination directory recursively
// Ignores any existing files
protected async Task CopyFolderContentsRecursive(string destinationPath, string sourcePath)
{
DirectoryInfo sourceDir = new DirectoryInfo(sourcePath);
Contract.Assert(sourceDir.Exists);
// Create the destination directory if it does not exist.
Directory.CreateDirectory(destinationPath);
// Copy the files only if they don't exist in the destination.
foreach (var fileInfo in sourceDir.GetFiles())
{
var destinationFilePath = Path.Combine(destinationPath, fileInfo.Name);
if (!File.Exists(destinationFilePath))
{
fileInfo.CopyTo(destinationFilePath);
}
}
// Copy sub folder contents
foreach (var subDirInfo in sourceDir.GetDirectories())
{
await CopyFolderContentsRecursive(Path.Combine(destinationPath, subDirInfo.Name), subDirInfo.FullName);
}
}
}
}