-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCreateTemplateTask.cs
399 lines (318 loc) · 17.3 KB
/
CreateTemplateTask.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.Build.Construction;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace LigerShark.TemplateBuilder.Tasks {
public class CreateTemplateTask : Task
{
private const string VsTemplateSchema = "http://schemas.microsoft.com/developer/vstemplate/2005";
private List<string> _defaultNonFileTypesList;
private List<string> _filesToCopy { get; set; }
private List<string> _filesToExclude { get; set; }
public string ProjectFile { get; set; }
[Required]
public string VsTemplateShell { get; set; }
[Required]
public string DestinationTemplateLocation { get; set; }
public ITaskItem[] FilesExclude { get; set; }
public ITaskItem[] FilesExcludeRegex { get; set; }
public ITaskItem[] NonFileTypes { get; set; }
public bool UpdateProjectElement { get; set; }
[Output]
public ITaskItem[] FilesToCopy { get; set; }
[Output]
public ITaskItem[] FilesToExclude { get; set; }
public CreateTemplateTask() {
_defaultNonFileTypesList = new List<string> {
"Reference",
"ProjectReference",
"AppConfigFileDestination",
"IntermediateAssembly",
"ApplicationManifest",
"DeployManifest",
"BuiltProjectOutputGroupKeyOutput",
"DebugSymbolsProjectOutputGroupOutput",
"AvailableItemName",
"Clean",
"XamlBuildTaskTypeGenerationExtensionName",
"WebPublishExtnsionsToExcludeItem",
"COMReference",
"DocumentationProjectOutputGroupOutput"
};
UpdateProjectElement = true;
}
private HashSet<string> GetFiles(string projectDirectoryPath) {
HashSet<string> files = new HashSet<string>();
foreach (string filePath in Directory.EnumerateFiles(projectDirectoryPath, "*", SearchOption.AllDirectories)) {
var filePathLower = filePath
.Replace(projectDirectoryPath, string.Empty)
.TrimStart('\\');
files.Add(filePathLower);
}
return files;
}
private void RecurseItems(XElement projectItemContainer, HashSet<string> takenSourceFileNames, HashSet<string> takenTargetFileNames) {
RecurseItems(projectItemContainer, null, null, takenSourceFileNames, takenTargetFileNames);
}
private void RecurseItems(XElement projectItemContainer, string sourcePrefix, string targetPrefix, HashSet<string> takenSourceFileNames, HashSet<string> takenTargetFileNames) {
foreach (var projectItem in projectItemContainer.Elements(XName.Get("ProjectItem", VsTemplateSchema))) {
var sourceFileName = projectItem.Value.ToLowerInvariant();
var targetFileName = projectItem.Attribute(XName.Get("TargetFileName")).Value.ToLowerInvariant();
takenSourceFileNames.Add(String.IsNullOrWhiteSpace(sourcePrefix) ? sourceFileName : Path.Combine(sourcePrefix, sourceFileName));
takenTargetFileNames.Add(String.IsNullOrWhiteSpace(sourcePrefix) ? targetFileName : Path.Combine(sourcePrefix, targetFileName));
}
foreach (var folder in projectItemContainer.Elements(XName.Get("Folder", VsTemplateSchema))) {
var sourceFolderName = folder.Attribute(XName.Get("Name")).Value.ToLower();
var targetFolderName = folder.Attribute(XName.Get("TargetFolderName")).Value.ToLower();
var sourceFolder = !string.IsNullOrWhiteSpace(sourcePrefix) ? Path.Combine(sourcePrefix, sourceFolderName) : sourceFolderName;
var targetFolder = !string.IsNullOrWhiteSpace(targetPrefix) ? Path.Combine(targetPrefix, targetFolderName) : targetFolderName;
RecurseItems(folder, sourceFolder, targetFolder, takenSourceFileNames, takenTargetFileNames);
}
}
private string GetProjectFile(XDocument vstemplate) {
string result = ProjectFile;
if (string.IsNullOrEmpty(result) && vstemplate != null) {
XNamespace ns = @"http://schemas.microsoft.com/developer/vstemplate/2005";
string projfilename = vstemplate.Root.Element(ns + "TemplateContent").Element(ns + "Project").Attribute("File").Value;
// assume that this is a relative path to the .vstemplate file passed in
FileInfo vsTemplateFi = new FileInfo(VsTemplateShell);
result = System.IO.Path.Combine(vsTemplateFi.Directory.FullName, projfilename);
}
return result;
}
public override bool Execute() {
var vstemplate = XDocument.Load(VsTemplateShell);
var workingTemplate = XDocument.Load(VsTemplateShell);
// var workingTemplate = XDocument.Parse(@"<VSTemplate Version=""3.0.0"" xmlns=""http://schemas.microsoft.com/developer/vstemplate/2005"" Type=""Project"" />");
if (vstemplate.Root == null || workingTemplate.Root == null) {
return false;
}
// var templateData = new XElement(XName.Get("TemplateData", VsTemplateSchema));
// workingTemplate.Root.Add(templateData);
// MergeTemplateData(templateData, vstemplate.Root.Element(XName.Get("TemplateData", VsTemplateSchema)));
var project = ProjectRootElement.Open(GetProjectFile(vstemplate));
var realProjectFile = Path.GetFileName(project.FullPath);
if (realProjectFile == null) {
return false;
}
var projectExtension = Path.GetExtension(project.FullPath);
if (projectExtension == null) {
return false;
}
var templateContentElement = vstemplate.Root.Element(XName.Get("TemplateContent", VsTemplateSchema));
XElement projectElement = null;
XElement customParameters = null;
if (templateContentElement != null) {
var element = templateContentElement.Element(XName.Get("Project", VsTemplateSchema));
if (element != null) {
projectElement = XElement.Parse(element.ToString());
}
var customParamElement = templateContentElement.Element(XName.Get("CustomParameters", VsTemplateSchema));
if (customParamElement != null) {
customParameters = XElement.Parse(customParamElement.ToString());
}
templateContentElement.Remove();
}
var tcToRemove = workingTemplate.Root.Element(XName.Get("TemplateContent", VsTemplateSchema));
if (tcToRemove != null) {
tcToRemove.Remove();
}
templateContentElement = new XElement(XName.Get("TemplateContent", VsTemplateSchema));
templateContentElement.Add(projectElement);
if (projectElement == null) {
projectElement = new XElement(XName.Get("Project", VsTemplateSchema));
templateContentElement.Add(projectElement);
}
//else {
// projectElement.RemoveAttributes();
//}
// instead of calling RemoveAttribute and then Add we can just call SetAttributeValue below so that we don't
// remove any attributes we are not aware of
if (customParameters != null) {
templateContentElement.Add(customParameters);
}
if (UpdateProjectElement) {
projectElement.SetAttributeValue(XName.Get("TargetFileName"), "$safeprojectname$" + projectExtension);
projectElement.SetAttributeValue(XName.Get("File"), realProjectFile);
projectElement.SetAttributeValue(XName.Get("ReplaceParameters"), true);
}
// projectElement.Add(new XAttribute(XName.Get("TargetFileName"), "$safeprojectname$" + projectExtension));
// projectElement.Add(new XAttribute(XName.Get("File"), realProjectFile));
// projectElement.Add(new XAttribute(XName.Get("ReplaceParameters"), true));
workingTemplate.Root.Add(templateContentElement);
_filesToCopy = new List<string>();
_filesToExclude = new List<string>();
var itemsToMerge = new List<string>();
if (string.Equals(projectExtension, ".xproj", StringComparison.OrdinalIgnoreCase) ||
(string.Equals(projectExtension, ".csproj", StringComparison.OrdinalIgnoreCase) && IsNewCsproj(project.FullPath))) {
var files = GetFiles(Path.GetDirectoryName(project.FullPath));
foreach (var file in files) {
if (!CanExcludeFile(file.ToLower(), _filesToExclude) &&
!string.Equals(Path.GetExtension(file), ".xproj", StringComparison.OrdinalIgnoreCase)) {
_filesToCopy.Add(file);
itemsToMerge.Add(file);
}
}
}
else {
var sourceFileNames = new HashSet<string>();
var targetFileNames = new HashSet<string>();
RecurseItems(projectElement, sourceFileNames, targetFileNames);
foreach (var item in project.Items) {
if (!IsPotentialSourceFile(item)) {
continue;
}
var name = item.Include;
var lowerName = name.ToLower();
if (targetFileNames.Contains(lowerName)) {
continue;
}
if (CanExcludeFile(lowerName, _filesToExclude)) {
continue;
}
if (item.ItemType != "Folder") {
_filesToCopy.Add(name);
}
if (!sourceFileNames.Contains(lowerName)) {
itemsToMerge.Add(name);
}
}
}
//Copy all non-mutated sections
var mutatedTemplateSections = new[] {"TemplateContent", "TemplateData"};
// In commit f668a11df0f403520ae1457d3fd5a872ace2107d the entire file is copied first,
// so this should no longer be needed.
// var elementsToCopyDirectly = vstemplate.Root.Elements().Where(x => !mutatedTemplateSections.Contains(x.Name.LocalName));
//foreach (var element in elementsToCopyDirectly) {
// var clonedElement = XElement.Parse(element.ToString());
// workingTemplate.Root.Add(clonedElement);
//}
Merge(projectElement, itemsToMerge);
workingTemplate.Save(DestinationTemplateLocation);
FilesToCopy = new ITaskItem[_filesToCopy.Count];
for (int i = 0; i < FilesToCopy.Length; i++) {
FilesToCopy[i] = new TaskItem(_filesToCopy[i]);
}
FilesToExclude = new ITaskItem[_filesToExclude.Count];
for (int i = 0; i < FilesToExclude.Length; i++) {
FilesToExclude[i] = new TaskItem(_filesToExclude[i]);
}
return true;
}
private bool IsNewCsproj(string filePath)
{
var xml = File.ReadAllText(filePath);
var document = XDocument.Parse(xml);
var sdk = document.Element("Project")?.Attribute("Sdk")?.Value;
return !string.IsNullOrEmpty(sdk);
}
private bool CanExcludeFile(string fileName, IList<string> filesToExclude) {
bool exclude = false;
if (FilesExclude != null) {
foreach (var item in FilesExclude) {
if (item != null &&
!string.IsNullOrEmpty(item.ItemSpec) &&
string.Equals(fileName, item.ItemSpec.ToLower(), StringComparison.Ordinal)) {
exclude = true;
if (!filesToExclude.Contains(fileName)) {
filesToExclude.Add(fileName.ToLower());
}
break;
}
}
}
if (FilesExcludeRegex != null && !exclude) {
foreach (var item in FilesExcludeRegex) {
if (item != null &&
!string.IsNullOrEmpty(item.ItemSpec) &&
Regex.IsMatch(fileName, item.ItemSpec)) {
exclude = true;
if (!filesToExclude.Contains(fileName)) {
filesToExclude.Add(fileName.ToLower());
}
break;
}
}
}
return exclude;
}
private static void MergeTemplateData(XElement target, XElement source, string childName, object defaultValue) {
var element = source.Element(XName.Get(childName, VsTemplateSchema));
var value = defaultValue;
if (element != null) {
value = element.Value;
}
target.Add(new XElement(XName.Get(childName, VsTemplateSchema), value));
}
private static void MergeTemplateData(XElement workingTemplate, XElement source) {
source = source ?? new XElement("Foo");
MergeTemplateData(workingTemplate, source, "Name", "My Project Template Name");
MergeTemplateData(workingTemplate, source, "Description", "A description for this template");
MergeTemplateData(workingTemplate, source, "DefaultName", "Project");
MergeTemplateData(workingTemplate, source, "ProjectType", "CSharp");
MergeTemplateData(workingTemplate, source, "SortOrder", 1000);
MergeTemplateData(workingTemplate, source, "CreateNewFolder", true);
MergeTemplateData(workingTemplate, source, "ProvideDefaultName", true);
MergeTemplateData(workingTemplate, source, "LocationField", "Enabled");
MergeTemplateData(workingTemplate, source, "EnableLocationBrowseButton", true);
MergeTemplateData(workingTemplate, source, "Icon", "logo.png");
MergeTemplateData(workingTemplate, source, "NumberOfParentCategoriesToRollUp", 1);
}
private bool IsPotentialSourceFile(ProjectItemElement item) {
var nonFileTypes = GetNonFileTypesList();
string include = item.Include ?? string.Empty;
return !item.ItemType.StartsWith("_")
&& !item.ItemType.StartsWith("ls-")
&& !nonFileTypes.Contains(item.ItemType)
&& !include.EndsWith(@"/")
&& !include.EndsWith(@"\");
}
private static void Merge(XElement rootElement, IEnumerable<string> filesToMerge) {
var splitFiles = filesToMerge.OrderBy(x => x).Select(x => x.Split('\\'));
foreach (var fileParts in splitFiles) {
var workingElement = rootElement;
for (var i = 0; i < fileParts.Length - 1; ++i) {
var newWorkingElement = workingElement.Elements(XName.Get("Folder", VsTemplateSchema)).FirstOrDefault(x => string.Equals(x.Attribute(XName.Get("TargetFolderName")).Value, fileParts[i], StringComparison.OrdinalIgnoreCase));
if (newWorkingElement == null) {
newWorkingElement = new XElement(XName.Get("Folder", VsTemplateSchema));
newWorkingElement.Add(new XAttribute(XName.Get("Name"), fileParts[i]));
newWorkingElement.Add(new XAttribute(XName.Get("TargetFolderName"), fileParts[i]));
workingElement.Add(newWorkingElement);
workingElement = newWorkingElement;
}
else {
workingElement = newWorkingElement;
}
}
if (!string.IsNullOrWhiteSpace(fileParts[fileParts.Length - 1])) {
var fileElement = new XElement(XName.Get("ProjectItem", VsTemplateSchema), fileParts[fileParts.Length - 1]);
fileElement.Add(new XAttribute(XName.Get("ReplaceParameters"), true));
fileElement.Add(new XAttribute(XName.Get("TargetFileName"), fileParts[fileParts.Length - 1]));
workingElement.Add(fileElement);
}
}
}
private IList<string> GetNonFileTypesList() {
List<string> nonFileTypesList = new List<string>();
if (this.NonFileTypes != null) {
this.NonFileTypes.ToList().ForEach(item => {
string spec = item.ItemSpec;
if (!string.IsNullOrEmpty(spec)) {
string cleanedUpSpec = spec.Trim().TrimStart(';').TrimEnd(';');
nonFileTypesList.Add(cleanedUpSpec);
}
});
}
else {
nonFileTypesList.AddRange(_defaultNonFileTypesList);
}
return nonFileTypesList;
}
}
}