forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultCodeTransformers.cs
More file actions
150 lines (133 loc) · 5.91 KB
/
DefaultCodeTransformers.cs
File metadata and controls
150 lines (133 loc) · 5.91 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
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Razor.Generator;
using ServiceStack.Text;
namespace ServiceStack.Razor.Compilation.CodeTransformers
{
public class AddGeneratedClassAttribute : RazorCodeTransformerBase
{
public override void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod)
{
string tool = "RazorGenerator";
Version version = GetType().Assembly.GetName().Version;
generatedClass.CustomAttributes.Add(
new CodeAttributeDeclaration(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute).FullName,
new CodeAttributeArgument(new CodePrimitiveExpression(tool)),
new CodeAttributeArgument(new CodePrimitiveExpression(version.ToString()))
));
}
}
public class SetImports : RazorCodeTransformerBase
{
private readonly IEnumerable<string> _imports;
private readonly bool _replaceExisting;
public SetImports(IEnumerable<string> imports, bool replaceExisting = false)
{
_imports = imports;
_replaceExisting = replaceExisting;
}
public override void Initialize(RazorPageHost razorHost, IDictionary<string, string> directives)
{
if (_replaceExisting)
{
razorHost.NamespaceImports.Clear();
}
foreach (var import in _imports)
{
razorHost.NamespaceImports.Add(import);
}
}
public override void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod)
{
// Sort imports.
var imports = new List<CodeNamespaceImport>(generatedNamespace.Imports.OfType<CodeNamespaceImport>());
generatedNamespace.Imports.Clear();
generatedNamespace.Imports.AddRange(imports.OrderBy(c => c.Namespace, NamespaceComparer.Instance).ToArray());
}
private class NamespaceComparer : IComparer<string>
{
public static readonly NamespaceComparer Instance = new NamespaceComparer();
public int Compare(string x, string y)
{
if (x == null || y == null)
{
return StringComparer.OrdinalIgnoreCase.Compare(x, y);
}
bool xIsSystem = x.StartsWith("System", StringComparison.OrdinalIgnoreCase);
bool yIsSystem = y.StartsWith("System", StringComparison.OrdinalIgnoreCase);
if (!(xIsSystem ^ yIsSystem))
{
return x.CompareTo(y);
}
else if (xIsSystem)
{
return -1;
}
return 1;
}
}
}
public class MakeTypeStatic : RazorCodeTransformerBase
{
public override string ProcessOutput(string codeContent)
{
return codeContent.Replace("public class", "public static class");
}
}
public class SetBaseType : RazorCodeTransformerBase
{
private const string DefaultModelTypeName = "dynamic";
private readonly bool isGenericType;
private readonly string _typeName;
public SetBaseType(string typeName, bool isGenericType=true)
{
_typeName = typeName.SplitOnLast("`")[0]; //get clean generic name without 'GenericType`1' n args suffix
this.isGenericType = isGenericType;
}
public SetBaseType(Type type)
: this(type.FullName, type.IsGenericType)
{
}
public override void Initialize(RazorPageHost razorHost, IDictionary<string, string> directives)
{
base.Initialize(razorHost, directives);
//string baseClass = razorHost.DefaultBaseClass;
razorHost.DefaultBaseClass = _typeName;
// The CSharpRazorCodeGenerator decides to generate line pragmas based on if the file path is available.
//Set it to an empty string if we do not want to generate them.
//var path = razorHost.EnableLinePragmas ? razorHost.File.RealPath : String.Empty;
//razorHost.CodeGenerator = new CSharpRazorCodeGenerator(razorHost.DefaultClassName, razorHost.DefaultNamespace, path, razorHost)
//{
// GenerateLinePragmas = razorHost.EnableLinePragmas
//};
//razorHost.Parser = new ServiceStackCSharpCodeParser();
}
public override void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod)
{
base.ProcessGeneratedCode(codeCompileUnit, generatedNamespace, generatedClass, executeMethod);
if (generatedClass.BaseTypes.Count > 0)
{
var codeTypeReference = generatedClass.BaseTypes[0];
if (!codeTypeReference.BaseType.Contains('<') && isGenericType)
{
// Use the default model if it wasn't specified by the user.
codeTypeReference.BaseType += '<' + DefaultModelTypeName + '>';
}
}
}
}
public class MakeTypeHelper : RazorCodeTransformerBase
{
public override void Initialize(RazorPageHost razorHost, IDictionary<string, string> directives)
{
razorHost.StaticHelpers = true;
}
public override void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod)
{
generatedClass.Members.Remove(executeMethod);
}
}
}