1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using KY . Generator . AspDotNet . Templates ;
3
5
using KY . Generator . Configuration ;
4
6
using KY . Generator . Csharp . Extensions ;
5
7
using KY . Generator . Templates ;
@@ -9,11 +11,16 @@ namespace KY.Generator.AspDotNet
9
11
{
10
12
internal class GeneratorGenerator : IGenerator
11
13
{
14
+ private readonly List < ITemplate > templates ;
15
+
12
16
public List < FileTemplate > Files { get ; }
13
17
14
18
public GeneratorGenerator ( )
15
19
{
16
20
this . Files = new List < FileTemplate > ( ) ;
21
+ this . templates = new List < ITemplate > ( ) ;
22
+ this . templates . Add ( new DotNetFrameworkTemplate ( ) ) ;
23
+ this . templates . Add ( new DotNetCoreTemplate ( ) ) ;
17
24
}
18
25
19
26
public void Generate ( ConfigurationBase configuration )
@@ -24,6 +31,7 @@ public void Generate(ConfigurationBase configuration)
24
31
{
25
32
return ;
26
33
}
34
+ ITemplate template = this . templates . FirstOrDefault ( x => x . Name == generator . Framework ) ?? this . templates . First ( ) ;
27
35
if ( generator . Controller != null )
28
36
{
29
37
if ( configuration . Language != Csharp . Code . Language )
@@ -35,50 +43,92 @@ public void Generate(ConfigurationBase configuration)
35
43
. AddClass ( "GeneratorController" , Code . Type ( "Controller" ) )
36
44
. WithUsing ( "System" )
37
45
. WithUsing ( "System.Linq" )
38
- . WithUsing ( "System.Web.Mvc" )
39
46
. WithUsing ( "KY.Generator" )
40
47
. WithUsing ( "KY.Generator.Output" ) ;
41
48
42
- MultilineCodeFragment createCode = classTemplate . AddMethod ( "Create" , Code . Type ( "Guid" ) )
43
- . WithAttribute ( "ValidateInput" , Code . Local ( "false" ) )
44
- . WithParameter ( Code . Type ( "string" ) , "configuration" )
45
- . Code ;
49
+ classTemplate . Usings . AddRange ( template . Usings ) ;
50
+
51
+ if ( template . UseOwnCache )
52
+ {
53
+ GenericTypeTemplate type = Code . Generic ( "Dictionary" , Code . Type ( "string" ) , Code . Type ( "MemoryOutput" ) ) ;
54
+ classTemplate . AddField ( "cache" , type )
55
+ . Static ( )
56
+ . DefaultValue = Code . New ( type ) ;
57
+ }
58
+
59
+ MethodTemplate createMethode = classTemplate . AddMethod ( "Create" , Code . Type ( "Guid" ) )
60
+ . WithParameter ( Code . Type ( "string" ) , "configuration" ) ;
61
+ if ( ! template . ValidateInput )
62
+ {
63
+ createMethode . WithAttribute ( "ValidateInput" , Code . Local ( "false" ) ) ;
64
+ }
65
+
66
+ MethodTemplate commandMethode = classTemplate . AddMethod ( "Command" , Code . Type ( "Guid" ) )
67
+ . WithParameter ( Code . Type ( "string" ) , "command" ) ;
68
+
69
+ MultilineCodeFragment createCode = createMethode . Code ;
46
70
createCode . AddLine ( Code . Declare ( Code . Type ( "Guid" ) , "id" , Code . Local ( "Guid" ) . Method ( "NewGuid" ) ) )
47
71
. AddLine ( Code . Declare ( Code . Type ( "MemoryOutput" ) , "output" , Code . New ( Code . Type ( "MemoryOutput" ) ) ) )
48
72
. AddLine ( Code . Declare ( Code . Type ( "Generator" ) , "generator" , Code . New ( Code . Type ( "Generator" ) ) ) )
49
73
. AddLine ( Code . Local ( "generator" ) . Method ( "SetOutput" , Code . Local ( "output" ) ) ) ;
74
+ MultilineCodeFragment commandCode = commandMethode . Code ;
75
+ commandCode . AddLine ( Code . Declare ( Code . Type ( "Guid" ) , "id" , Code . Local ( "Guid" ) . Method ( "NewGuid" ) ) )
76
+ . AddLine ( Code . Declare ( Code . Type ( "MemoryOutput" ) , "output" , Code . New ( Code . Type ( "MemoryOutput" ) ) ) )
77
+ . AddLine ( Code . Declare ( Code . Type ( "Generator" ) , "generator" , Code . New ( Code . Type ( "Generator" ) ) ) )
78
+ . AddLine ( Code . Local ( "generator" ) . Method ( "SetOutput" , Code . Local ( "output" ) ) ) ;
50
79
foreach ( string nameSpace in generator . Controller . Usings )
51
80
{
52
81
classTemplate . AddUsing ( nameSpace ) ;
53
82
}
54
83
foreach ( string moduleType in generator . Controller . PreloadModules )
55
84
{
56
85
createCode . AddLine ( Code . Local ( "generator" ) . GenericMethod ( "PreloadModule" , Code . Type ( moduleType ) ) ) ;
86
+ commandCode . AddLine ( Code . Local ( "generator" ) . GenericMethod ( "PreloadModule" , Code . Type ( moduleType ) ) ) ;
57
87
}
58
88
foreach ( GeneratorConfigurationConfigureModule configure in generator . Controller . Configures )
59
89
{
60
90
createCode . AddLine ( Code . Local ( "generator" ) . Method ( configure . Module , Code . Lambda ( "x" , Csharp . Code . Csharp ( "x." + configure . Action ) ) ) ) ;
91
+ commandCode . AddLine ( Code . Local ( "generator" ) . Method ( configure . Module , Code . Lambda ( "x" , Csharp . Code . Csharp ( "x." + configure . Action ) ) ) ) ;
61
92
}
62
93
createCode . AddLine ( Code . Local ( "generator" ) . Method ( "ParseConfiguration" , Code . Local ( "configuration" ) ) )
63
94
. AddLine ( Code . Local ( "generator" ) . Method ( "Run" ) )
64
- . AddBlankLine ( )
65
- . AddLine ( Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) . Index ( Code . Local ( "id" ) . Method ( "ToString" ) ) . Assign ( Code . Local ( "output" ) ) )
66
- . AddLine ( Code . Return ( Code . Local ( "id" ) ) ) ;
95
+ . AddBlankLine ( ) ;
96
+ commandCode . AddLine ( Code . Local ( "generator" ) . Method ( "ParseCommand" , Code . Local ( "command" ) ) )
97
+ . AddLine ( Code . Local ( "generator" ) . Method ( "Run" ) )
98
+ . AddBlankLine ( ) ;
99
+ if ( template . UseOwnCache )
100
+ {
101
+ createCode . AddLine ( Code . Local ( "cache" ) . Index ( Code . Local ( "id" ) . Method ( "ToString" ) ) . Assign ( Code . Local ( "output" ) ) ) ;
102
+ commandCode . AddLine ( Code . Local ( "cache" ) . Index ( Code . Local ( "id" ) . Method ( "ToString" ) ) . Assign ( Code . Local ( "output" ) ) ) ;
103
+ }
104
+ else
105
+ {
106
+ createCode . AddLine ( Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) . Index ( Code . Local ( "id" ) . Method ( "ToString" ) ) . Assign ( Code . Local ( "output" ) ) ) ;
107
+ commandCode . AddLine ( Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) . Index ( Code . Local ( "id" ) . Method ( "ToString" ) ) . Assign ( Code . Local ( "output" ) ) ) ;
108
+ }
109
+ createCode . AddLine ( Code . Return ( Code . Local ( "id" ) ) ) ;
110
+ commandCode . AddLine ( Code . Return ( Code . Local ( "id" ) ) ) ;
67
111
112
+ ChainedCodeFragment getFromCacheForFilesFragment = template . UseOwnCache
113
+ ? ( ChainedCodeFragment ) Code . Local ( "cache" )
114
+ : Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) ;
68
115
classTemplate . AddMethod ( "GetFiles" , Code . Type ( "string" ) )
69
116
. WithParameter ( Code . Type ( "string" ) , "id" )
70
117
. Code . AddLine ( Code . If ( Code . Local ( "id" ) . Equals ( ) . Null ( ) , x => x . Code . AddLine ( Code . Return ( Code . Null ( ) ) ) ) )
71
- . AddLine ( Code . Declare ( Code . Type ( "MemoryOutput" ) , "output" , Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) . Index ( Code . Local ( "id" ) ) . As ( Code . Type ( "MemoryOutput" ) ) ) )
118
+ . AddLine ( Code . Declare ( Code . Type ( "MemoryOutput" ) , "output" , getFromCacheForFilesFragment . Index ( Code . Local ( "id" ) ) . As ( Code . Type ( "MemoryOutput" ) ) ) )
72
119
. AddLine ( Code . Return ( Code . InlineIf ( Code . Local ( "output" ) . Equals ( ) . Null ( ) ,
73
120
Code . Null ( ) ,
74
121
Code . Local ( "string" ) . Method ( "Join" , Code . Local ( "Environment" ) . Property ( "NewLine" ) ,
75
122
Code . Local ( "output" ) . Property ( "Files" ) . Method ( "Select" , Code . Lambda ( "x" , Code . Local ( "x" ) . Property ( "Key" ) ) ) ) ) ) ) ;
76
123
124
+ ChainedCodeFragment getFromCacheForFileFragment = template . UseOwnCache
125
+ ? ( ChainedCodeFragment ) Code . Local ( "cache" )
126
+ : Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) ;
77
127
classTemplate . AddMethod ( "GetFile" , Code . Type ( "string" ) )
78
128
. WithParameter ( Code . Type ( "string" ) , "id" )
79
129
. WithParameter ( Code . Type ( "string" ) , "path" )
80
130
. Code . AddLine ( Code . If ( Code . Local ( "id" ) . Equals ( ) . Null ( ) , x => x . Code . AddLine ( Code . Return ( Code . Null ( ) ) ) ) )
81
- . AddLine ( Code . Declare ( Code . Type ( "MemoryOutput" ) , "output" , Code . This ( ) . Property ( "HttpContext" ) . Property ( "Cache" ) . Index ( Code . Local ( "id" ) ) . As ( Code . Type ( "MemoryOutput" ) ) ) )
131
+ . AddLine ( Code . Declare ( Code . Type ( "MemoryOutput" ) , "output" , getFromCacheForFileFragment . Index ( Code . Local ( "id" ) ) . As ( Code . Type ( "MemoryOutput" ) ) ) )
82
132
. AddLine ( Code . Return ( Code . InlineIf ( Code . Local ( "output" ) . Equals ( ) . Null ( ) . Or ( ) . Not ( ) . Local ( "output" ) . Property ( "Files" ) . Method ( "ContainsKey" , Code . Local ( "path" ) ) ,
83
133
Code . Null ( ) ,
84
134
Code . Local ( "output" ) . Property ( "Files" ) . Index ( Code . Local ( "path" ) ) ) ) ) ;
0 commit comments