-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhandler.cfc
More file actions
286 lines (264 loc) · 8.8 KB
/
handler.cfc
File metadata and controls
286 lines (264 loc) · 8.8 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
/**
* Create a new handler (controller) in an existing ColdBox application. Make sure you are running this command in the root
* of your app for it to find the correct folder. You can optionally create the views as well as the integration tests for your
* new handler at the same time. By default, your new handler will be created in /handlers but you can override that with the directory param.
* .
* {code:bash}
* coldbox create handler myHandler index,foo,bar --open
* {code}
*
**/
component aliases="coldbox create controller" extends="coldbox-cli.models.BaseCommand" {
static {
HINTS = {
index : "Display a listing of the resource",
new : "Show the form for creating a new resource",
create : "Store a newly created resource in storage",
show : "Display the specified resource",
edit : "Show the form for editing the specified resource",
update : "Update the specified resource in storage",
delete : "Remove the specified resource from storage"
}
}
/**
* @name Name of the handler to create without the .cfc. For packages, specify name as 'myPackage/myHandler'
* @actions A comma-delimited list of actions to generate, by default we generate just an index action
* @views Generate a view for each action
* @viewsDirectory The directory where your views are stored. Only used if views is set to true.
* @appMapping The root location of the application in the web root: ex: /MyApp or / if in the root
* @integrationTests Generate the integration test component
* @testsDirectory Your integration tests directory. Only used if integrationTests is true
* @directory The base directory to create your handler in and creates the directory if it does not exist. Defaults to 'handlers'.
* @description The handler hint description
* @open Open the handler (and test(s) if applicable) once generated
* @rest Make this a REST handler instead of a normal ColdBox Handler
* @force Force overwrite of existing handler
* @resource Generate a resourceful handler with all the actions
* @boxlang Is this a boxlang project? else it is a CFML project
**/
function run(
required name,
actions = "",
boolean views = true,
viewsDirectory = getAppPrefix( getCWD() ) & "views",
boolean integrationTests = true,
appMapping = "/",
testsDirectory = "tests/specs/integration",
directory = getAppPrefix( getCWD() ) & "handlers",
description = "I am a new handler",
boolean open = false,
boolean rest = false,
boolean force = false,
boolean resource = false,
boolean boxlang = isBoxLangProject( getCWD() )
){
// This will make each directory canonical and absolute
arguments.directory = resolvePath( arguments.directory );
arguments.viewsDirectory = resolvePath( arguments.viewsDirectory );
arguments.testsDirectory = resolvePath( arguments.testsDirectory );
// Validate directory
if ( !directoryExists( arguments.directory ) ) {
directoryCreate( arguments.directory );
}
// Allow dot-delimited paths
arguments.name = replace( arguments.name, ".", "/", "all" );
/*******************************************************************
* Read in Templates
*******************************************************************/
// Rest or Normal
var handlerContent = fileRead(
arguments.rest ? "#variables.settings.templatesPath#/RestHandlerContent.txt" : "#variables.settings.templatesPath#/HandlerContent.txt"
);
var handlerTestContent = fileRead( "#variables.settings.templatesPath#/testing/HandlerBDDContent.txt" );
// Start text replacements
handlerContent = replaceNoCase(
handlerContent,
"|handlerName|",
arguments.name,
"all"
);
handlerTestContent = replaceNoCase(
handlerTestContent,
"|appMapping|",
arguments.appMapping,
"all"
);
handlerTestContent = replaceNoCase(
handlerTestContent,
"|handlerName|",
arguments.name,
"all"
);
handlerContent = replaceNoCase(
handlerContent,
"|Description|",
arguments.description,
"all"
);
// BoxLang replacements
if ( arguments.boxlang ) {
handlerContent = toBoxLangClass( handlerContent );
handlerTestContent = toBoxLangClass( handlerTestContent );
}
// Auto Actions Determination if none passed via resource && rest, else empty handler
if ( !len( arguments.actions ) ) {
if ( arguments.resource && !arguments.rest ) {
arguments.actions = "index,new,create,show,edit,update,delete";
} else if ( ( arguments.resource && arguments.rest ) || arguments.rest ) {
arguments.actions = "index,create,show,update,delete";
} else {
arguments.actions = "index";
}
}
// Handle Actions
if ( len( arguments.actions ) ) {
var actionResults = buildActions( argumentCollection = arguments );
handlerContent = replaceNoCase(
handlerContent,
"|EventActions|",
actionResults.actions,
"all"
);
handlerTestContent = replaceNoCase(
handlerTestContent,
"|TestCases|",
actionResults.tests,
"all"
);
} else {
handlerContent = replaceNoCase(
handlerContent,
"|EventActions|",
"",
"all"
);
handlerTestContent = replaceNoCase(
handlerTestContent,
"|TestCases|",
"",
"all"
);
}
// Create dir if it doesn't exist
var handlerPath = resolvePath( "#arguments.directory#/#arguments.name#.#arguments.boxlang ? "bx" : "cfc"#" );
directoryCreate(
getDirectoryFromPath( handlerPath ),
true,
true
);
// Confirm it or Force it
if (
fileExists( handlerPath ) && !arguments.force && !confirm(
"The file '#getFileFromPath( handlerPath )#' already exists, overwrite it (y/n)?"
)
) {
printWarn( "Exiting..." );
return;
}
// Write out the files
file action="write" file="#handlerPath#" mode="777" output="#handlerContent#";
printInfo( "Created Handler [#handlerPath#]" );
// More Tests?
if ( arguments.integrationTests ) {
var testPath = resolvePath( "#arguments.testsDirectory#/#arguments.name#Test.#arguments.boxlang ? "bx" : "cfc"#" );
// Create dir if it doesn't exist
directoryCreate(
getDirectoryFromPath( testPath ),
true,
true
);
// Create the tests
file action="write" file="#testPath#" mode="777" output="#handlerTestContent#";
printInfo( "Created Integration Spec [#testPath#]" );
// open file
if ( arguments.open ) {
openPath( testPath );
}
}
// open file
if ( arguments.open ) {
openPath( handlerPath );
}
}
/**
* Build out the actions
*
* @return struct of { actions : "", tests : ""}
*/
function buildActions(
name,
actions,
boolean views,
viewsDirectory,
boolean integrationTests,
appMapping,
testsDirectory,
directory,
description,
boolean open,
boolean rest,
boolean force,
boolean resource
){
var results = { actions : "", tests : "" }
var actionContent = fileRead(
arguments.rest ? "#variables.settings.templatesPath#/RestActionContent.txt" : "#variables.settings.templatesPath#/ActionContent.txt"
);
var handlerTestCaseContent = fileRead( "#variables.settings.templatesPath#/testing/HandlerBDDCaseContent.txt" );
// Loop Over actions generating their functions
for ( var thisAction in listToArray( arguments.actions ) ) {
thisAction = trim( thisAction );
// Hint Replacement
results.actions &= replaceNoCase(
actionContent,
"|hint|",
static.HINTS[ thisAction ] ?: thisAction,
"all"
);
// Action Replacement
results.actions = replaceNoCase(
results.actions,
"|action|",
thisAction,
"all"
) & repeatString( variables.cr, 1 );
// Are we creating views? But only if we are NOT in rest mode
if ( arguments.views && !arguments.rest && !listFindNoCase( "create,update,delete", thisAction ) ) {
var camelCaseHandlerName = variables.utility.camelCase( arguments.name );
command( "coldbox create view" )
.params(
name : camelCaseHandlerName & "/" & thisAction,
content : "<h1>#camelCaseHandlerName#.#thisAction#</h1>",
directory: arguments.viewsDirectory,
force : arguments.force,
open : arguments.open
)
.run();
}
// Are we creating tests cases on actions
if ( arguments.integrationTests ) {
var thisTestCase = replaceNoCase(
handlerTestCaseContent,
"|action|",
thisAction,
"all"
);
thisTestCase = replaceNoCase(
thisTestCase,
"|event|",
listChangeDelims( arguments.name, ".", "/\" ) & "." & thisAction,
"all"
);
results.tests &= thisTestCase & repeatString( variables.cr, 1 );
}
}
// final replacements
results.actions = replaceNoCase(
results.actions,
"|name|",
arguments.name,
"all"
);
return results;
}
}