-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathGlassMapper.csx
207 lines (173 loc) · 5.22 KB
/
GlassMapper.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
// Generates GlassMapper models
using System;
using System.Linq;
using System.Collections.Generic;
Log.Debug($"Emitting GlassMapper templates for {ConfigurationName}...");
Code.Append($@"
#pragma warning disable 1591
#pragma warning disable 0108
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Leprechaun.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using global::Glass.Mapper.Sc.Configuration.Attributes;
using global::Glass.Mapper.Sc.Fields;
using System.Diagnostics.CodeAnalysis;
using global::Sitecore.Data;
{RenderTemplates()}
");
public string RenderTemplates()
{
var localCode = new System.Text.StringBuilder();
foreach (var template in Templates)
{
localCode.Append($@"
namespace {template.RootNamespace}
{{");
break;
}
foreach (var template in Templates)
{
localCode.AppendLine($@"
/// <summary>Controls the appearance of the inheriting template in site navigation.</summary>
[SitecoreType(TemplateId = {template.RootNamespace}.{template.CodeName}Constants.TemplateIdString)]
[GeneratedCode(""Leprechaun"", ""{Version}"")]
public partial interface I{template.CodeName} : {GetBaseInterfaces(template)}
{{{RenderInterfaceFields(template)}
}}
[ExcludeFromCodeCoverage]
[GeneratedCode(""Leprechaun"", ""{Version}"")]
public struct {template.CodeName}Constants
{{
public const string TemplateIdString = ""{template.Id}"";
public static readonly ID TemplateId = new ID(TemplateIdString);
public const string TemplateName = ""{template.Name}"";
{ RenderConstantFields(template) }
}}
[ExcludeFromCodeCoverage]
[GeneratedCode(""Leprechaun"", ""{Version}"")]
public partial class {template.CodeName} : GlassBase, I{template.CodeName}
{{{ RenderClassFields(template) }
}}");
}
if (Templates.Any())
{
localCode.Append($@"
}}");
}
return localCode.ToString();
}
public string RenderClassFields(TemplateCodeGenerationMetadata template)
{
var localCode = new System.Text.StringBuilder();
foreach (var field in template.AllFields)
{
localCode.Append($@"
/// <summary>
/// Represents {field.Name} field
/// <para>Path: {field.Path}</para>
/// <para>ID: {field.Id}</para>
/// </summary>
[SitecoreField(FieldName = {template.CodeName}Constants.{field.CodeName}FieldName)]
public virtual {GetFieldType(field)} {field.CodeName} {{ get; set; }}
");
}
return localCode.ToString().TrimEnd();
}
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.RootNamespace}.I{baseTemplate.CodeName}");
}
if (bases.Count == 0)
{
// IGlassBase only needed when no other bases exist otherwise irrelevant by transitive inheritance
bases.Add("IGlassBase");
}
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>
[SitecoreField(FieldName = {template.RootNamespace}.{template.CodeName}Constants.{field.CodeName}FieldName)]
{GetFieldType(field)} {field.CodeName} {{ get; set; }}");
}
return localCode.ToString().TrimEnd();
}
public string RenderConstantFields(TemplateCodeGenerationMetadata template)
{
var localCode = new System.Text.StringBuilder();
foreach (var field in template.AllFields)
{
localCode.AppendLine($@"
public static readonly ID {field.CodeName}FieldId = new ID(""{field.Id}"");
public const string {field.CodeName}FieldName = ""{field.CodeName}"";");
}
return localCode.ToString().TrimEnd();
}
public string GetFieldType(TemplateFieldCodeGenerationMetadata field)
{
switch (field.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":
return "IEnumerable<Guid>";
case "treelistex":
case "treelist descriptive":
case "checklist":
case "multilist":
return "IEnumerable<Guid>";
case "grouped droplink":
case "droplink":
case "lookup":
case "droptree":
case "reference":
case "tree":
return "Guid";
case "file":
return "File";
case "image":
return "ExtendedImage";
case "rich text":
case "html":
return "string";
case "general link":
return "Link";
case "name value list":
return "System.Collections.Specialized.NameValueCollection";
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";
}
}