forked from microsoft/TemplateStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakeGenShell.cs
335 lines (271 loc) · 10.8 KB
/
FakeGenShell.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Microsoft.Templates.Core.Diagnostics;
using Microsoft.Templates.Core.Gen;
using Microsoft.Templates.Utilities.Services;
using Microsoft.VisualStudio.TemplateWizard;
namespace Microsoft.Templates.Fakes
{
public class FakeGenShell : GenShell
{
private readonly Action<string> _changeStatus;
private readonly Action<string> _addLog;
private readonly Window _owner;
private string _language;
private string _platform;
public string SolutionPath
{
get
{
if (GenContext.Current != null)
{
return Path.Combine(Path.GetDirectoryName(GenContext.Current.DestinationPath), $"{GenContext.Current.ProjectName}.sln");
}
return null;
}
}
public FakeGenShell(string platform, string language, Action<string> changeStatus = null, Action<string> addLog = null, Window owner = null)
{
_platform = platform;
_language = language;
_changeStatus = changeStatus;
_addLog = addLog;
_owner = owner;
}
public void SetCurrentLanguage(string language)
{
_language = language;
}
public void SetCurrentPlatform(string platform)
{
_platform = platform;
}
private void AddItems(string projectPath, IEnumerable<string> filesToAdd)
{
var msbuildProj = FakeMsBuildProject.Load(projectPath);
if (msbuildProj != null)
{
foreach (var file in filesToAdd)
{
msbuildProj.AddItem(file);
}
msbuildProj.Save();
}
}
public override void AddContextItemsToSolution(ProjectInfo projectInfo)
{
var filesByProject = ResolveProjectFiles(projectInfo.ProjectItems);
var filesForExistingProjects = filesByProject.Where(k => !projectInfo.Projects.Any(p => p == k.Key));
var nugetsForExistingProjects = projectInfo.NugetReferences.Where(n => !projectInfo.Projects.Any(p => p == n.Project)).GroupBy(n => n.Project, n => n);
var sdksForExistingProjects = projectInfo.SdkReferences.Where(n => !projectInfo.Projects.Any(p => p == n.Project)).GroupBy(n => n.Project, n => n);
foreach (var files in filesForExistingProjects)
{
if (!IsCpsProject(files.Key))
{
AddItems(files.Key, files.Value);
}
}
foreach (var project in nugetsForExistingProjects)
{
AddNugetsForProject(project.Key, project);
}
foreach (var sdk in sdksForExistingProjects)
{
AddSdksForProject(sdk.Key, sdk);
}
foreach (var project in projectInfo.Projects)
{
var msbuildProj = FakeMsBuildProject.Load(project);
var solutionFile = FakeSolution.LoadOrCreate(_platform, SolutionPath);
var projectRelativeToSolutionPath = project.Replace(Path.GetDirectoryName(SolutionPath) + Path.DirectorySeparatorChar, string.Empty);
solutionFile.AddProjectToSolution(_platform, msbuildProj.Name, msbuildProj.Guid ?? Guid.NewGuid().ToString(), projectRelativeToSolutionPath, IsCpsProject(project));
if (!IsCpsProject(project) && filesByProject.ContainsKey(project))
{
AddItems(project, filesByProject[project]);
}
AddNugetsForProject(project, projectInfo.NugetReferences.Where(n => n.Project == project));
AddSdksForProject(project, projectInfo.SdkReferences.Where(n => n.Project == project));
}
AddReferencesToProjects(projectInfo.ProjectReferences);
}
private void AddNugetsForProject(string projectPath, IEnumerable<NugetReference> nugetReferences)
{
var msbuildProj = FakeMsBuildProject.Load(projectPath);
foreach (var nugetPackages in nugetReferences)
{
msbuildProj.AddNugetReference(nugetPackages);
}
msbuildProj.Save();
}
public override string GetActiveProjectNamespace()
{
return GenContext.Current.ProjectName;
}
public override void ShowStatusBarMessage(string message)
{
_changeStatus?.Invoke(message);
}
public override string GetActiveProjectTypeGuids()
{
var projectFileName = FindProject(GenContext.Current.DestinationPath);
if (string.IsNullOrEmpty(projectFileName))
{
throw new Exception($"There is not project file in {GenContext.Current.DestinationPath}");
}
var msbuildProj = FakeMsBuildProject.Load(projectFileName);
return msbuildProj.ProjectTypeGuids;
}
public override string GetActiveProjectName()
{
return GenContext.Current.ProjectName;
}
public override string GetActiveProjectPath()
{
return (GenContext.Current != null) ? GenContext.Current.DestinationPath : string.Empty;
}
public override string GetActiveProjectLanguage()
{
return _language;
}
private static string FindProject(string path)
{
return Directory.EnumerateFiles(path, "*proj", SearchOption.AllDirectories).FirstOrDefault();
}
public override void ChangeSolutionConfiguration(IEnumerable<ProjectConfiguration> projectConfiguration)
{
}
public override void SetDefaultSolutionConfiguration(string configurationName, string platformName, string projectName)
{
}
public override void ShowTaskList()
{
}
public override void ShowModal(IWindow shell)
{
if (shell is Window dialog)
{
dialog.Owner = _owner;
dialog.ShowDialog();
}
}
public override void CancelWizard(bool back = true)
{
if (back)
{
throw new WizardBackoutException();
}
else
{
throw new WizardCancelledException();
}
}
public override void WriteOutput(string data)
{
_addLog?.Invoke(data);
}
public override void CloseSolution()
{
}
public override Guid GetProjectGuidByName(string projectName)
{
var projectFileName = FindProject(GenContext.Current.DestinationPath);
var msbuildProj = FakeMsBuildProject.Load(projectFileName);
var guid = msbuildProj.Guid;
if (string.IsNullOrEmpty(guid))
{
var solution = FakeSolution.LoadOrCreate(_platform, SolutionPath);
guid = solution.GetProjectGuids().First(p => p.Key == projectName).Value;
}
Guid.TryParse(guid, out Guid parsedGuid);
return parsedGuid;
}
public override void OpenItems(params string[] itemsFullPath)
{
}
public override bool IsDebuggerEnabled()
{
return false;
}
public override bool IsBuildInProgress()
{
return false;
}
public override void OpenProjectOverview()
{
}
private void AddReferencesToProjects(IEnumerable<ProjectReference> projectReferences)
{
var solution = FakeSolution.LoadOrCreate(_platform, SolutionPath);
var projectGuids = solution.GetProjectGuids();
var groupedReferences = projectReferences.GroupBy(n => n.Project, n => n);
foreach (var project in groupedReferences)
{
var parentProject = FakeMsBuildProject.Load(project.Key);
foreach (var referenceToAdd in project)
{
var referenceProject = FakeMsBuildProject.Load(referenceToAdd.ReferencedProject);
var name = referenceProject.Name;
var guid = projectGuids[name];
if (guid == "{}")
{
guid = "{" + Guid.NewGuid().ToString() + "}";
}
parentProject.AddProjectReference(referenceToAdd.ReferencedProject, guid, name);
}
parentProject.Save();
}
}
private void AddSdksForProject(string projectPath, IEnumerable<SdkReference> sdkReferences)
{
var project = FakeMsBuildProject.Load(projectPath);
foreach (var referenceValue in sdkReferences)
{
project.AddSDKReference(referenceValue);
}
project.Save();
}
private bool IsCpsProject(string projectPath)
{
string[] targetFrameworkTags = { "</TargetFramework>", "</TargetFrameworks>" };
return targetFrameworkTags.Any(t => File.ReadAllText(projectPath).Contains(t));
}
public override string CreateCertificate(string publisherName)
{
return CertificateService.Instance.CreateCertificate(publisherName);
}
public override VSTelemetryInfo GetVSTelemetryInfo()
{
return new VSTelemetryInfo()
{
OptedIn = true,
VisualStudioCulture = string.Empty,
VisualStudioEdition = string.Empty,
VisualStudioExeVersion = string.Empty,
VisualStudioManifestId = string.Empty,
};
}
public override void SafeTrackProjectVsTelemetry(Dictionary<string, string> properties, string pages, string features, string services, string testing, Dictionary<string, double> metrics, bool success = true)
{
}
public override void SafeTrackNewItemVsTelemetry(Dictionary<string, string> properties, string pages, string features, string services, string testing, Dictionary<string, double> metrics, bool success = true)
{
}
public override void SafeTrackWizardCancelledVsTelemetry(Dictionary<string, string> properties, bool success = true)
{
}
public override bool GetActiveProjectIsWts()
{
return true;
}
public override bool IsSdkInstalled(string name)
{
return true;
}
}
}