forked from Glass-lu/Glass.Sitecore.Mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.tt
More file actions
358 lines (285 loc) · 9.69 KB
/
Model.tt
File metadata and controls
358 lines (285 loc) · 9.69 KB
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
// For more information see http://www.glass.lu
// Model.TT version 1.0.2.0
<#@ template hostspecific="True" debug="True" #>
<#@ assembly name="System.Core.dll" #>
<#@ assembly name="%ProgramFiles%\Hedgehog Development\Team Development for Sitecore (VS2010)\HedgehogDevelopment.SitecoreCommon.Data.dll" #>
<#@ assembly name="%ProgramFiles%\Hedgehog Development\Team Development for Sitecore (VS2010)\HedgehogDevelopment.SitecoreCommon.Data.Parser.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="HedgehogDevelopment.SitecoreCommon.Data" #>
<#@ import namespace="HedgehogDevelopment.SitecoreCommon.Data.Items" #>
<#@ import namespace="HedgehogDevelopment.SitecoreCommon.Data.Parser" #>
<#
Init();
GetBoilerplate();
foreach(TemplateItem item in Templates){
switch (GetTypeOfTemplate(item))
{
case Types.Abstract:
GetClassDefinition("partial", item);
break;
case Types.Concrete:
GetClassDefinition("partial", item);
break;
}
}
#>
<#+
//The root namespace for your templates. Must end with a '.'
private string RootNamespace = "Glass.Demo.Application.Models.";
//The root path to the templates to create using the T4 template
private string RootTemplatePath = "/sitecore/templates/";
private string BaseDir;
private List<TemplateItem> Templates, Interfaces, AbstractClasses, AllClasses, ConcreteClasses;
private Dictionary<string, List<TemplateItem>> Namespaces = new Dictionary<string, List<TemplateItem>>();
private void Init()
{
// Put the relative path to your templates node of your TDS project
BaseDir = Host.ResolvePath(@"..\Glass.Sitecore.Mapper.Tutorial.Tds");
SerializedTreeDataSource tds = new SerializedTreeDataSource(BaseDir);
Templates = tds.Templates.Where(t=>t.Path.StartsWith(RootTemplatePath)).OrderBy( t=> t.Path).ToList();
Interfaces = new List<TemplateItem>();
AllClasses = Templates.Except(Interfaces).ToList();
AbstractClasses = AllClasses.Where(t => GetTypeOfTemplate(t) == Types.Abstract).ToList();
ConcreteClasses = Templates.Except(Interfaces).Except(AbstractClasses).ToList();
foreach(TemplateItem t in Templates)
{
string ns = GetNamespace(t);
if (!Namespaces.ContainsKey(ns))
{
Namespaces.Add(ns, new List<TemplateItem>());
}
Namespaces[ns].Add(t);
}
}
#>
<#+
private enum Types {Interface, Abstract, Concrete}
private Types GetTypeOfTemplate(TemplateItem item)
{
if (item.Name.StartsWith("Base"))
return Types.Abstract;
else
return Types.Concrete;
}
// Gets the Content field type for the Sitecore field
public static string GetFieldType(TemplateFieldItem field)
{
if (field != null && field.Fields["Type"] != null)
{
switch(field.Fields["Type"].ToLower())
{
case "tristate":
return "TriState";
case "checkbox":
return "bool";
case "date":
case "datetime":
return "DateTime";
case "number":
return "float";
case "integer":
return "int";
case "treelist":
case "treelistex":
case "treelist descriptive":
case "checklist":
case "multilist":
return string.Empty;
case "grouped droplink":
case "droplink":
case "lookup":
case "droptree":
case "reference":
case "tree":
return "Guid";
case "file":
return "File";
case "image":
return "Image";
case "rich text":
case "html":
return "string";
case "general link":
return "Link";
case "single-line text":
case "multi-line text":
case "frame":
case "text":
case "memo":
case "droplist":
case "grouped droplist":
case "valuelookup":
return "string";
default:
return string.Empty;
}
}
else
{
throw new Exception("There is no 'Type' field on the " + field.Name + " field.");
}
}
private string GetBases(TemplateItem item)
{
List<string> inherits = new List<string>();
// get base templates
var baseTemplates = AllClasses.Where(i=> item.BaseTemplateIds.Contains(i.ID)).Select(i=>GetNamespace(i)+".I"+TitleCase(i.Name) + "");
if (baseTemplates.Count() > 0)
{
// we can only have one base class
inherits.AddRange(baseTemplates);
}
if (inherits.Count > 0)
{
var ints = string.Join(", global::", inherits.Select(x=> x).ToArray());
return ", global::"+ ints;
}
return " ";
}
/// Gets all fields from the current template and its bases grouped by template.section name
private Dictionary<string, List<TemplateFieldItem>> GetAllFieldsForTemplate(TemplateItem item){
Dictionary<string, List<TemplateFieldItem>> dic = new Dictionary<string, List<TemplateFieldItem>>();
// own fields
foreach(TemplateSectionItem si in item.Sections){
string sectionName = string.Concat(item.Name, '-', si.Name);
List<TemplateFieldItem> fields = new List<TemplateFieldItem>();
foreach(TemplateFieldItem ti in si.OwnFields){
fields.Add(ti);
}
dic.Add(sectionName, fields);
}
return dic;
}
private string GetNamespace(TemplateItem item)
{
// trim initial path and replace with default Namspace
string ns = System.Text.RegularExpressions.Regex.Replace(item.Path, RootTemplatePath, RootNamespace, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// turn into segments and remove the item from the path
List<string> segments = ns.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
segments.RemoveAt(segments.Count-1);
ns = string.Join(".", segments);
ns = ns.Trim(".".ToCharArray());
return ns.Replace(" ", string.Empty);
}
public string TitleCase(string name)
{
name = Regex.Replace(name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ");
name = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(name);
name = Regex.Replace(name, @"[^a-zA-Z0-9]", String.Empty);
name = Regex.Replace(name, @"(^[0-9])", "Z$1");
return name;
}
#>
<#+private void CreateField(bool isInterface, TemplateItem item){#>
<#+
if (!isInterface) // for classes go recursively and generate all fields from all inherited interfaces
{
var usedInterfaces = AllClasses.Where(i=> item.BaseTemplateIds.Contains(i.ID));
foreach(var i in usedInterfaces)
{
CreateField(false, i);
}
#>
// fields for template <#= item.Name #>
<#+
}
Dictionary<string, List<TemplateFieldItem>> fieldsBySection = GetAllFieldsForTemplate(item);
foreach(string section in fieldsBySection.Keys){
#>
<#+
foreach (TemplateFieldItem fieldItem in fieldsBySection[section]){
bool hasAttribute = true;
string fieldType = GetFieldType(fieldItem);
if(string.IsNullOrEmpty(fieldType))
continue;
string fieldName = fieldItem.Name;
string propertyName = TitleCase(fieldName);
if (fieldType == "Guid"){
propertyName += "ID";
hasAttribute = false;
}
else if (fieldType == "List<Guid>"){
propertyName += "Ids";
hasAttribute = false;
}
if (hasAttribute){#>
/// <summary>
/// <para>Title: <#= fieldItem.Fields["Title"] ?? string.Empty #></para>
/// <para>Field Name: <#=fieldName#></para>
/// <para>Field Type: <#=fieldItem.Fields["Type"]#></para>
/// <para>Help: <#= fieldItem.Fields["__Short description"] ?? string.Empty#></para>
/// </summary>
[SitecoreField("<#=fieldName#>")]
<#+if(!isInterface){#>public virtual <#+}#><#=fieldType#> <#=propertyName#> { get; set; }
<#+}#>
<#+}#>
<#+}#>
<#+}#>
<#+private void GetClassDefinition(string modifier, TemplateItem item){#>
namespace <#=GetNamespace(item)#>
{
/// <summary>
/// <para>Path: <#= item.Path ?? string.Empty #></para>
/// <para>Help: <#= item.Fields["__Short description"] ?? string.Empty#></para>
/// </summary>
public partial interface I<#=TitleCase(item.Name)#> : global::<#=RootNamespace#>IGlassBase <#=GetBases(item)#>
{
<#+CreateField(true, item); #>
}
/// <summary>
/// <para>Path: <#= item.Path ?? string.Empty #></para>
/// <para>Help: <#= item.Fields["__Short description"] ?? string.Empty#></para>
/// </summary>
[SitecoreClass(TemplateId="<#=item.ID#>")]
public <#=modifier#> class <#=TitleCase(item.Name)#> : global::<#=RootNamespace#>GlassBase, I<#=TitleCase(item.Name)#>
{
<#+CreateField(false, item); #>
}
}
<#+}#>
<#+private void GetBoilerplate(){#>
#pragma warning disable 1591
#pragma warning disable 0108
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Team Development for Sitecore.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Glass.Sitecore.Mapper.Configuration.Attributes;
using Glass.Sitecore.Mapper.Configuration;
using Glass.Sitecore.Mapper.FieldTypes;
using Sitecore.Globalization;
namespace <#= RootNamespace.Substring(0, RootNamespace.Length-1)#>
{
public interface IGlassBase{
[SitecoreId]
Guid Id{ get; }
[SitecoreInfo(SitecoreInfoType.Language)]
Language Language{ get; }
[SitecoreInfo(SitecoreInfoType.Version)]
int Version { get; }
}
public abstract class GlassBase : IGlassBase{
[SitecoreId]
public virtual Guid Id{ get; private set;}
[SitecoreInfo(SitecoreInfoType.Language)]
public virtual Language Language{ get; private set; }
[SitecoreInfo(SitecoreInfoType.Version)]
public virtual int Version { get; private set; }
}
}
<#+}#>