-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSynthesis.csx
241 lines (206 loc) · 6.87 KB
/
Synthesis.csx
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
// Generates Synthesis models
Log.Debug($"Emitting Synthesis templates for {ConfigurationName}...");
public string RenderTemplates()
{
var localCode = new System.Text.StringBuilder();
foreach (var template in Templates)
{
localCode.AppendLine($@"
namespace {template.Namespace}
{{
using global::Sitecore.ContentSearch;
using global::Sitecore.Data;
using global::Sitecore.Data.Items;
using global::System.CodeDom.Compiler;
using global::System.Collections.Generic;
using Synthesis;
using Synthesis.FieldTypes;
using Synthesis.FieldTypes.Interfaces;
using Synthesis.Initializers;
using Synthesis.Synchronization;
using System.CodeDom.Compiler;
/// <summary>Controls the appearance of the inheriting template in site navigation.</summary>
[RepresentsSitecoreTemplateAttribute(""{{{template.Id}}}"", """", ""{ConfigurationName}"")]
[GeneratedCode(""Leprechaun"", ""{Version}"")]
public interface I{template.CodeName}Item : {GetBaseInterfaces(template)}
{{
{RenderInterfaceFields(template)}
}}
/// <summary>Controls the appearance of the inheriting template in site navigation.</summary>
[GeneratedCode(""Leprechaun"", ""{Version}"")]
public class {template.CodeName} : StandardTemplateItem, I{template.CodeName}Item
{{
public {template.CodeName}(Item innerItem) : base(innerItem)
{{
}}
public {template.CodeName}(IDictionary<string, string> searchFields) : base(searchFields)
{{
}}
/// <summary>The name of the Sitecore Template that this class represents</summary>
public static string TemplateName => ""{template.Name}"";
/// <summary>The ID of the Sitecore Template that this class represents</summary>
public static ID ItemTemplateId => new ID(""{{{template.Id}}}"");
/// <summary>The ID of the Sitecore Template that this class represents</summary>
public override ID TemplateId => ItemTemplateId;
{RenderFields(template)}
}}
[GeneratedCode(""Leprechaun"", ""{Version}"")]
public class {template.CodeName}Initializer : ITemplateInitializer
{{
public ID InitializesTemplateId => new ID(""{{{template.Id}}}"");
public IStandardTemplateItem CreateInstance(Item innerItem)
{{
return new {template.CodeName}(innerItem);
}}
public IStandardTemplateItem CreateInstanceFromSearch(IDictionary<string, string> searchFields)
{{
return new {template.CodeName}(searchFields);
}}
}}
}}"
);
}
return localCode.ToString();
}
Code.AppendLine($@"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// ReSharper disable All
{RenderTemplates()}
");
public string GetBaseInterfaces(TemplateCodeGenerationMetadata template)
{
var bases = new System.Collections.Generic.List<string>(template.BaseTemplates.Count + 1);
foreach(var baseTemplate in template.BaseTemplates)
{
bases.Add($"global::{baseTemplate.Namespace}.I{baseTemplate.CodeName}Item");
}
if (bases.Count == 0)
{
// IStandardTemplateItem only needed when no other bases exist otherwise irrelevant by transitive inheritance
bases.Add("IStandardTemplateItem");
}
return string.Join(", ", bases);
}
public string RenderInterfaceFields(TemplateCodeGenerationMetadata template)
{
var localCode = new System.Text.StringBuilder();
foreach (var field in template.OwnFields)
{
localCode.AppendLine($@"
/// <summary>{field.HelpText}</summary>
[IndexFieldAttribute(""{GetSearchFieldName(field)}"")]
I{GetFieldType(field)} {field.CodeName} {{ get; }}");
}
return localCode.ToString();
}
public string RenderFields(TemplateCodeGenerationMetadata template)
{
var localCode = new System.Text.StringBuilder();
foreach (var field in template.AllFields)
{
localCode.AppendLine($@"
private {GetFieldType(field)} {GetBackingFieldName(field)};
/// <summary>{field.HelpText}</summary>
[IndexFieldAttribute(""{GetSearchFieldName(field)}"")]
public I{GetFieldType(field)} {field.CodeName} => {GetBackingFieldName(field)} ?? ({GetBackingFieldName(field)} = new {GetFieldType(field)}(new LazyField(() => InnerItem.Fields[""{{{field.Id}}}""], ""{template.Path}"", ""{field.Name}""), GetSearchFieldValue(""{GetSearchFieldName(field)}"")));
");
}
return localCode.ToString();
}
public string GetFieldType(TemplateFieldCodeGenerationMetadata field)
{
switch(field.Type) {
// Simple Types
case "Checkbox": return "BooleanField";
case "Date": return "DateTimeField";
case "Datetime": return "DateTimeField";
case "File": return "FileField";
case "Image": return "ImageField";
case "Integer": return "IntegerField";
case "Number": return "NumericField";
case "Rich Text": return "RichTextField";
case "Multi-Line Text":
case "Password":
case "Single-Line Text": return "TextField";
// List Types
case "Checklist":
case "Grouped Droplink": return "ItemReferenceField";
case "Droplist":
case "Grouped Droplist": return "TextField";
case "Multilist":
case "Multilist with Search":
case "Treelist":
case "TreelistEx": return "ItemReferenceListField";
case "Name Value List": return "DictionaryField";
// Link Types
case "Droplink":
case "Droptree": return "ItemReferenceField";
case "General Link": return "HyperlinkField";
// System Types
case "Internal Link": return "PathItemReferenceField";
// Developer Types
case "Tristate": return "TristateField";
// Deprecated Types
case "text": return "TextField";
case "memo": return "TextField";
case "lookup":
case "reference":
case "tree": return "ItemReferenceField";
case "tree list": return "ItemReferenceListField";
case "html": return "RichTextField";
case "link": return "HyperlinkField";
default: return "TextField";
}
}
public string GetBackingFieldName(TemplateFieldCodeGenerationMetadata field)
{
return $"_{field.CodeName.Substring(0, 1).ToLowerInvariant()}{field.CodeName.Substring(1)}";
}
public string GetSearchFieldName(TemplateFieldCodeGenerationMetadata field)
{
// not using Solr? Remove the GetSolrFieldTypeSuffix(field.Type)
return field.Name.Replace(" ", "_").ToLowerInvariant() + GetSolrFieldTypeSuffix(field.Type);
}
public string GetSolrFieldTypeSuffix(string typeName)
{
switch (typeName.ToLower())
{
case "checkbox":
return "_b";
case "date":
case "datetime":
return "_tdt";
case "checklist":
case "treelist":
case "treelist with search":
case "treelistex":
case "multilist":
case "multilist with search":
case "tags":
return "_sm";
case "droplink":
case "droptree":
return "_s";
case "general link":
case "general link with search":
case "text":
case "single-line text":
case "multi-line text":
case "rich text":
return "_t";
case "number":
return "_tf";
case "integer":
return "_tl";
default:
return string.Empty;
}
}