-
Notifications
You must be signed in to change notification settings - Fork 224
/
IdentityGeneratorFilesConfig.cs
402 lines (349 loc) · 16.5 KB
/
IdentityGeneratorFilesConfig.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
400
401
402
// 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.IO;
using System.Linq;
using Microsoft.VisualStudio.Web.CodeGeneration;
namespace Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Identity
{
public static class IdentityGeneratorFilesConfig
{
public static string[] GetAreaFolders(bool isDataRequired)
{
return isDataRequired
? new string[]
{
"Data",
"Pages"
}
: new string[]
{
"Pages"
};
}
private static IdentityGeneratorFile IdentityHostingStartup = new IdentityGeneratorFile()
{
Name = "IdentityHostingStartup",
SourcePath = "IdentityHostingStartup.cshtml",
OutputPath = "Areas/Identity/IdentityHostingStartup.cs",
IsTemplate = true,
ShouldOverWrite = OverWriteCondition.Never,
ShowInListFiles = false
};
private static IdentityGeneratorFile ReadMe = new IdentityGeneratorFile()
{
Name = "ScaffoldingReadme",
SourcePath = "ScaffoldingReadme.cshtml",
OutputPath = "./ScaffoldingReadme.txt",
IsTemplate = true,
ShouldOverWrite = OverWriteCondition.Always,
ShowInListFiles = false
};
private static IdentityGeneratorFile ViewStart = new IdentityGeneratorFile()
{
Name = "_ViewStart",
SourcePath = "_ViewStart.cshtml",
OutputPath = "Areas/Identity/Pages/_ViewStart.cshtml",
IsTemplate = true,
ShowInListFiles = false,
ShouldOverWrite = OverWriteCondition.Never
};
private static IdentityGeneratorFile[] ViewImports = new[]
{
// Order is important here.
new IdentityGeneratorFile()
{
Name = "Account.Manage._ViewImports",
SourcePath = "Account.Manage._ViewImports.cshtml",
OutputPath = "Areas/Identity/Pages/Account/Manage/_ViewImports.cshtml",
IsTemplate = true,
ShouldOverWrite = OverWriteCondition.Never
},
new IdentityGeneratorFile()
{
Name = "Account._ViewImports",
SourcePath = "Account._ViewImports.cshtml",
OutputPath = "Areas/Identity/Pages/Account/_ViewImports.cshtml",
IsTemplate = true,
ShouldOverWrite = OverWriteCondition.Never
},
new IdentityGeneratorFile()
{
Name= "_ViewImports",
SourcePath= "_ViewImports.cshtml",
OutputPath= "Areas/Identity/Pages/_ViewImports.cshtml",
IsTemplate= true,
ShouldOverWrite = OverWriteCondition.Never
}
};
internal static IdentityGeneratorFile[] GetFilesToGenerate(IEnumerable<string> names, IdentityGeneratorTemplateModel templateModel)
{
if (templateModel == null)
{
throw new ArgumentNullException(nameof(templateModel));
}
List<IdentityGeneratorFile> filesToGenerate = GetDataModelFiles(templateModel);
if (templateModel.GenerateLayout)
{
IdentityGeneratorFile layout = new IdentityGeneratorFile()
{
Name = "_Layout",
SourcePath = "_Layout.cshtml",
OutputPath = Path.Combine(templateModel.SupportFileLocation, "_Layout.cshtml"),
IsTemplate = true,
ShowInListFiles = false
};
filesToGenerate.Add(layout);
}
else
{
IdentityGeneratorFile validationScriptsPartial = new IdentityGeneratorFile()
{
Name = "_ValidationScriptsPartial",
SourcePath = "Pages/_ValidationScriptsPartial.cshtml",
OutputPath = "Areas/Identity/Pages/_ValidationScriptsPartial.cshtml",
IsTemplate = false,
ShouldOverWrite = OverWriteCondition.Never
};
filesToGenerate.Add(validationScriptsPartial);
}
if (!string.IsNullOrEmpty(templateModel.Layout))
{
filesToGenerate.Add(ViewStart);
// if there's a layout file, generate a _ValidationScriptsPartial.cshtml in the same place.
IdentityGeneratorFile layoutPeerValidationScriptsPartial = new IdentityGeneratorFile()
{
Name = "_ValidationScriptsPartial",
SourcePath = "Pages/_ValidationScriptsPartial.cshtml",
OutputPath = Path.Combine(templateModel.SupportFileLocation, "_ValidationScriptsPartial.cshtml"),
IsTemplate = false,
ShouldOverWrite = OverWriteCondition.Never
};
filesToGenerate.Add(layoutPeerValidationScriptsPartial);
}
string contentVersion;
if (templateModel is IdentityGeneratorTemplateModel2 templateModel2)
{
contentVersion = templateModel2.ContentVersion;
}
else
{
contentVersion = IdentityGenerator.ContentVersionDefault;
}
IdentityGeneratorFiles config = GetConfigContentVersion(contentVersion);
if (!templateModel.UseDefaultUI)
{
if (names != null && names.Any())
{
foreach (var name in names)
{
filesToGenerate.AddRange(config.NamedFileConfig[name]);
}
}
else
{
filesToGenerate.AddRange(config.NamedFileConfig
.Where(x => !string.Equals(x.Key, "wwwroot", StringComparison.OrdinalIgnoreCase))
.SelectMany(f => f.Value));
}
}
if (!templateModel.HasExistingNonEmptyWwwRoot)
{
filesToGenerate.AddRange(config.NamedFileConfig["WwwRoot"]);
}
filesToGenerate.Add(IdentityHostingStartup);
filesToGenerate.Add(ReadMe);
return filesToGenerate.Distinct(new IdentityGeneratorFileComparer()).ToArray();
}
/// <summary>
/// Returns a list of ViewImport template files based on the files to generate and also on which _ViewImports already exist in the project.
/// We start from the innermost directory requested, if a _ViewImports file exists at this level, we are done.
/// If not, we add one to be generated and then look in the outer directory.
/// Sequence is:
/// - Account.Manage._ViewImports
/// - Account._ViewImports
/// - _ViewImports
/// </summary>
internal static List<IdentityGeneratorFile> GetViewImports(IEnumerable<IdentityGeneratorFile> files, IFileSystem fileSystem, string rootPath)
{
if (files == null || !files.Any())
{
return new List<IdentityGeneratorFile>();
}
if (fileSystem == null)
{
throw new ArgumentNullException(nameof(fileSystem));
}
if (string.IsNullOrEmpty(rootPath))
{
throw new ArgumentException(nameof(rootPath));
}
var requiredViewImports = new List<IdentityGeneratorFile>();
foreach (var viewImport in ViewImports)
{
var fileNamePrefix = viewImport.Name.Substring(0, viewImport.Name.Length - _ViewImportFileName.Length);
if (files.Any(f => f.Name.StartsWith(fileNamePrefix)))
{
if (fileSystem.FileExists(Path.Combine(rootPath, viewImport.OutputPath)))
{
break;
}
requiredViewImports.Add(viewImport);
}
}
return requiredViewImports;
}
private static readonly string _ViewImportFileName = "_ViewImports";
// Note: "peer" is somewhat of a misnomer, not all of the "peer" files are in the same directory as the layout file.
internal static bool TryGetLayoutPeerFiles(IFileSystem fileSystem, string rootPath, IdentityGeneratorTemplateModel templateModel, out IReadOnlyList<IdentityGeneratorFile> layoutPeerFiles)
{
string viewImportsFileNameWithExtension = string.Concat(_ViewImportFileName, ".cshtml");
if (fileSystem.FileExists(Path.Combine(rootPath, templateModel.SupportFileLocation, viewImportsFileNameWithExtension)))
{
layoutPeerFiles = null;
return false;
}
const string sharedDirName = "Shared";
string outputDirectory;
string checkSupportFileLocation = templateModel.SupportFileLocation;
if (checkSupportFileLocation.EndsWith("\\") || checkSupportFileLocation.EndsWith("/"))
{
checkSupportFileLocation = checkSupportFileLocation.Substring(0, checkSupportFileLocation.Length - 1);
}
if (checkSupportFileLocation.EndsWith(sharedDirName))
{
int directoryLengthWithoutShared = checkSupportFileLocation.Length - sharedDirName.Length;
outputDirectory = checkSupportFileLocation.Substring(0, directoryLengthWithoutShared);
}
else
{
outputDirectory = templateModel.SupportFileLocation;
}
List<IdentityGeneratorFile> peerFiles = new List<IdentityGeneratorFile>();
IdentityGeneratorFile layoutPeerViewImportsFile = new IdentityGeneratorFile()
{
Name = "_ViewImports",
SourcePath = "SupportPages._ViewImports.cshtml",
OutputPath = Path.Combine(outputDirectory, viewImportsFileNameWithExtension),
IsTemplate = true,
ShouldOverWrite = OverWriteCondition.Never
};
peerFiles.Add(layoutPeerViewImportsFile);
IdentityGeneratorFile layoutPeerViewStart = new IdentityGeneratorFile()
{
Name = "_ViewStart",
SourcePath = "SupportPages._ViewStart.cshtml",
OutputPath = Path.Combine(outputDirectory, "_ViewStart.cshtml"),
IsTemplate = true,
ShowInListFiles = false,
ShouldOverWrite = OverWriteCondition.Never
};
peerFiles.Add(layoutPeerViewStart);
layoutPeerFiles = peerFiles;
return true;
}
private static readonly string _CookieConsentPartialFileName = "_CookieConsentPartial.cshtml";
// Look for a cookie consent file in the same location as the layout file. If there isn't one, setup the config to add one there.
internal static bool TryGetCookieConsentPartialFile(IFileSystem fileSystem, string rootPath, IdentityGeneratorTemplateModel templateModel, out IdentityGeneratorFile cookieConsentPartialConfig)
{
string layoutDir = Path.GetDirectoryName(templateModel.Layout);
string cookieConsentCheckLocation = Path.Combine(layoutDir, _CookieConsentPartialFileName);
if (fileSystem.FileExists(cookieConsentCheckLocation))
{
cookieConsentPartialConfig = null;
return false;
}
cookieConsentPartialConfig = new IdentityGeneratorFile()
{
Name = "_CookieConsentPartial",
SourcePath = "SupportPages._CookieConsentPartial.cshtml",
OutputPath = Path.Combine(layoutDir, _CookieConsentPartialFileName),
IsTemplate = false,
ShowInListFiles = false,
ShouldOverWrite = OverWriteCondition.Never
};
return true;
}
private static List<IdentityGeneratorFile> GetDataModelFiles(IdentityGeneratorTemplateModel templateModel)
{
var filesToGenerate = new List<IdentityGeneratorFile>();
if (!templateModel.IsUsingExistingDbContext)
{
// Add DbContext template.
filesToGenerate.Add(new IdentityGeneratorFile()
{
IsTemplate = true,
Name = "ApplicationDbContext",
SourcePath = "ApplicationDbContext.cshtml",
OutputPath = Path.Combine("Areas", "Identity", "Data", $"{templateModel.DbContextClass}.cs"),
ShowInListFiles = false
});
if (templateModel.IsGenerateCustomUser)
{
// Add custom user class template.
filesToGenerate.Add(new IdentityGeneratorFile()
{
IsTemplate = true,
Name = "ApplicationUser",
SourcePath = "ApplicationUser.cshtml",
OutputPath = Path.Combine("Areas", "Identity", "Data", $"{templateModel.UserClass}.cs"),
ShowInListFiles = false
});
}
}
// add the _LoginPartial file. Its config will cause it to not be created if it already exists.
filesToGenerate.Add(new IdentityGeneratorFile()
{
IsTemplate = true,
Name = "_LoginPartial",
SourcePath = "_LoginPartial.cshtml",
OutputPath = Path.Combine(templateModel.SupportFileLocation, "_LoginPartial.cshtml"),
ShouldOverWrite = OverWriteCondition.Never,
ShowInListFiles = false
});
return filesToGenerate;
}
// Maps the valid values for ContentVersion to the filename prefix for the appropriate {prefix}_identitygeneratorfilesconfig.json
private static readonly IReadOnlyDictionary<string, string> _contentVersionToConfigPrefixMap = new Dictionary<string, string>()
{
{ IdentityGenerator.ContentVersionBootstrap3, "bootstrap3" },
{ IdentityGenerator.ContentVersionDefault, "bootstrap4" },
};
// Lazy-Caches the deserialized versions of {prefix}_identitygeneratorfilesconfig.json
private static Dictionary<string, IdentityGeneratorFiles> _versionedConfigCache = new Dictionary<string, IdentityGeneratorFiles>();
// Returns the list of files for the specified content version.
public static IEnumerable<string> GetFilesToList(string contentVersion)
{
IdentityGeneratorFiles config = GetConfigContentVersion(contentVersion);
return config.NamedFileConfig
.Where(c => c.Value.Any(f => f.ShowInListFiles))
.Select(c => c.Key);
}
// Returns the list of files for the default content version.
public static IEnumerable<string> GetFilesToList()
{
return GetFilesToList(IdentityGenerator.ContentVersionDefault);
}
private static IdentityGeneratorFiles GetConfigContentVersion(string contentVersion)
{
IdentityGeneratorFiles config;
if (!_versionedConfigCache.TryGetValue(contentVersion, out config))
{
string configString = string.Empty;
if (_contentVersionToConfigPrefixMap.TryGetValue(contentVersion, out string configFileNamePrefix))
{
string configFileName = $"Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Identity.{configFileNamePrefix}_identitygeneratorfilesconfig.json";
using (var configStream = typeof(IdentityGeneratorFilesConfig).Assembly.GetManifestResourceStream(configFileName))
using (var reader = new StreamReader(configStream))
{
configString = reader.ReadToEnd();
}
}
config = Newtonsoft.Json.JsonConvert.DeserializeObject<IdentityGeneratorFiles>(configString);
_versionedConfigCache[contentVersion] = config;
}
return config;
}
}
}