-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmodel.cfc
More file actions
428 lines (398 loc) · 12.5 KB
/
model.cfc
File metadata and controls
428 lines (398 loc) · 12.5 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* Create a new model CFC 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 unit tests for your new model at the same time.
* By default, your new model will be created in `/models` but you can override that with the directory param.
* Once you create a model you can add a mapping for it in your WireBox binder, or use ColdBox's default scan location and
* just reference it with getModel( 'modelName' ).
* .
* {code:bash}
* coldbox create model myModel --open
* {code}
* .
* {code:bash}
* coldbox create model name=User properties=fname,lname,email --accessors --open
* {code}
*
**/
component extends="coldbox-cli.models.BaseCommand" {
/**
* Constructor
*/
function init(){
// valid persistences
variables.validPersistences = "Transient,Singleton";
variables.migrationsDirectory = "resources/database/migrations";
variables.seedsDirectory = "resources/database/seeds";
return this;
}
/**
* @name Name of the model to create without the .cfc. For packages, specify name as 'myPackage/myModel'
* @methods A comma-delimited list of method stubs to generate for you
* @persistence Specify singleton to have only one instance of this model created
* @persistence.options Transient,Singleton
* @tests Generate the unit test BDD component
* @testsDirectory Your unit tests directory. Only used if tests is true
* @directory The base directory to create your model in and creates the directory if it does not exist.
* @description The model hint description
* @open Open the file once generated
* @accessors Setup accessors to be true in the component
* @properties Enter a list of properties to generate. You can add the type via colon separator. Ex: firstName,age:numeric,wheels:array
* @force Force overwrite of existing files
* @migration Generate a migration file for this model
* @seeder Generate a seeder file for this model
* @handler Generate a handler for this model
* @rest Generate a REST handler for this model
* @resource Generate a resourceful handler with all the actions
* @all Generate all the things: handler, resource, migration, seeder, tests, service
* @componentAnnotations Annotations to add to the component
* @ormTypes Generate ORM types for the properties or normal ColdFusion types
* @propertyContent Custom content to add to the properties
* @initContent Custom content to add to the init method
* @service Generate a service layer for this model
* @boxlang Is this a boxlang project?
**/
function run(
required name,
methods = "",
persistence = "transient",
boolean tests = true,
testsDirectory = "tests/specs/unit",
directory = getAppPrefix( getCWD() ) & "models",
description = "I am a new Model Object",
boolean open = false,
boolean accessors = true,
properties = "id:numeric",
boolean force = false,
boolean migration = false,
boolean seeder = false,
boolean handler = false,
boolean rest = false,
boolean resource = false,
boolean all = false,
string componentAnnotations = "",
boolean ormTypes = false,
string propertyContent = "",
string initContent = "",
boolean service = false,
boolean boxlang = isBoxLangProject( getCWD() )
){
// Prepare arguments
var modelTestPath = arguments.directory;
var modelNamePlural = variables.utility.pluralize( arguments.name.lcase() );
if ( arguments.all ) {
arguments.seeder = true;
arguments.migration = true;
arguments.handler = true;
arguments.resource = true;
arguments.service = true;
}
// This will make each directory canonical and absolute
arguments.directory = resolvePath( arguments.directory );
arguments.testsDirectory = resolvePath( arguments.testsDirectory );
// Validate directory
if ( !directoryExists( arguments.directory ) ) {
directoryCreate( arguments.directory );
}
// Validate persistence
if ( !len( arguments.name ) ) {
error( "Cannot scaffold a model with an empty name." );
}
// Validate persistence
if (
!listFindNoCase(
variables.validPersistences,
arguments.persistence
)
) {
error(
"The persistence value [#arguments.persistence#] is invalid. Valid values are [#listChangeDelims(
variables.validPersistences,
", ",
","
)#]"
);
}
// Exit the command if something above failed
if ( hasError() ) {
return;
}
// Allow dot-delimited paths
arguments.name = replace( arguments.name, ".", "/", "all" );
// Read in Template
var modelContent = fileRead( "#variables.settings.templatesPath#/ModelContent.txt" );
var modelMethodContent = fileRead( "#variables.settings.templatesPath#/ModelMethodContent.txt" );
var modelTestContent = fileRead( "#variables.settings.templatesPath#/testing/ModelBDDContent.txt" );
var modelTestMethodContent = fileRead( "#variables.settings.templatesPath#/testing/ModelBDDMethodContent.txt" );
// Basic replacements
modelContent = replaceNoCase(
modelContent,
"|componentAnnotations|",
arguments.componentAnnotations,
"all"
);
modelContent = replaceNoCase(
modelContent,
"|modelName|",
listLast( arguments.name, "/\" ),
"all"
);
modelContent = replaceNoCase(
modelContent,
"|modelDescription|",
arguments.description,
"all"
);
modelContent = replaceNoCase(
modelContent,
"|initContent|",
arguments.initContent,
"all"
);
if ( arguments.boxlang ) {
modelContent = toBoxLangClass( modelContent );
}
modelTestContent = replaceNoCase(
modelTestContent,
"|modelName|",
listChangeDelims( arguments.name, ".", "/\" ),
"all"
);
modelTestContent = replaceNoCase(
modelTestContent,
"|modelPath|",
listChangeDelims( modelTestPath, ".", "/\" ) & "." & listChangeDelims( arguments.name, ".", "/\" ),
"all"
);
if ( arguments.boxlang ) {
modelTestContent = toBoxLangClass( modelTestContent );
}
// Persistence
switch ( Persistence ) {
case "Transient":
modelContent = replaceNoCase(
modelContent,
"|modelPersistence|",
"",
"all"
);
break;
case "Singleton":
modelContent = replaceNoCase(
modelContent,
"|modelPersistence|",
"singleton ",
"all"
);
}
// Accessors
if ( arguments.accessors ) {
modelContent = replaceNoCase(
modelContent,
"|accessors|",
"accessors=""true""",
"all"
);
} else {
modelContent = replaceNoCase(
modelContent,
"|accessors|",
"",
"all"
);
}
// Generate Model Properties
var properties = listToArray( arguments.properties );
var buffer = createObject( "java", "java.lang.StringBuffer" ).init( arguments.propertyContent );
for ( var thisProperty in properties ) {
var propName = getToken( trim( thisProperty ), 1, ":" );
var propType = getToken( trim( thisProperty ), 2, ":" );
if ( NOT len( propType ) ) {
propType = "string";
}
buffer.append(
"property name=""#propName#"" #arguments.ormTypes ? "ormtype" : "type"#=""#propType#"";#variables.cr & variables.utility.TAB#"
);
}
modelContent = replaceNoCase(
modelContent,
"|properties|",
buffer.toString()
);
// Handle Model Methods
if ( len( arguments.methods ) ) {
var allMethods = "";
var allTestsCases = "";
var methodContent = "";
// Loop Over methods to generate them
for ( var thisMethod in listToArray( arguments.methods ) ) {
if ( thisMethod == "init" ) {
continue;
}
thisMethod = trim( thisMethod );
allMethods = allMethods & replaceNoCase(
modelMethodContent,
"|method|",
thisMethod,
"all"
) & cr & cr;
printInfo( "Generated Method: #thisMethod#()" );
// Are we creating tests cases on methods
if ( arguments.tests ) {
var thisTestCase = replaceNoCase(
modelTestMethodContent,
"|method|",
thisMethod,
"all"
);
allTestsCases &= thisTestCase & CR & CR;
}
}
// final replacement
modelContent = replaceNoCase(
modelContent,
"|methods|",
allMethods,
"all"
);
modelTestContent = replaceNoCase(
modelTestContent,
"|TestCases|",
allTestsCases,
"all"
);
} else {
modelContent = replaceNoCase( modelContent, "|methods|", "", "all" );
modelTestContent = replaceNoCase(
modelTestContent,
"|TestCases|",
"",
"all"
);
}
// Write out the model
var modelPath = "#arguments.directory#/#arguments.name#.#arguments.boxlang ? "bx" : "cfc"#";
// Create dir if it doesn't exist
directoryCreate(
getDirectoryFromPath( modelPath ),
true,
true
);
// Prompt for override
if (
fileExists( modelPath ) && !arguments.force && !confirm(
"The file '#getFileFromPath( modelPath )#' already exists, overwrite it (y/n)?"
)
) {
printWarn( "Exiting..." );
return;
}
// Write out the model
fileWrite( modelPath, trim( modelContent ) );
printInfo( "Created Model: [#modelPath#]" );
// Generate migrations
// No BoxLang here yet, until CommandBox supports it
if ( arguments.migration ) {
var migrationPath = "#resolvePath( variables.migrationsDirectory )#/#dateTimeFormat( now(), "yyyy_mm_dd_HHnnss" )#_create_#modelNamePlural#_table.cfc";
var migrationContent = replaceNoCase(
fileRead( "#variables.settings.templatesPath#/ModelMigrationContent.txt" ),
"|modelName|",
modelNamePlural,
"all"
);
var properties = listToArray( arguments.properties );
var buffer = createObject( "java", "java.lang.StringBuffer" ).init();
for ( var thisProperty in properties ) {
var propName = getToken( trim( thisProperty ), 1, ":" );
var propMethod = getToken( trim( thisProperty ), 2, ":" );
if ( NOT len( propMethod ) ) {
propMethod = "string";
}
buffer.append(
"table.#propMethod#( ""#propName#"" );#variables.cr & repeatString( variables.utility.TAB, 3 )#"
);
}
migrationContent = replaceNoCase(
migrationContent,
"|properties|",
buffer.toString()
);
// Create dir if it doesn't exist
directoryCreate(
getDirectoryFromPath( migrationPath ),
true,
true
);
// Create the migration
file action="write" file="#migrationPath#" mode="777" output="#migrationContent#";
// open file
if ( arguments.open ) {
openPath( migrationPath );
}
printInfo( "Created Migration: [#migrationPath#]" );
}
// Generate Seeder
// No BoxLang here yet, until CommandBox supports it
if ( arguments.seeder ) {
var seederPath = "#resolvePath( variables.seedsDirectory )#/#modelNamePlural#.cfc";
var seedContent = replaceNoCase(
fileRead( "#variables.settings.templatesPath#/ModelSeederContent.txt" ),
"|modelName|",
modelNamePlural,
"all"
);
// Create dir if it doesn't exist
directoryCreate(
getDirectoryFromPath( seederPath ),
true,
true
);
// Create the migration
file action="write" file="#seederPath#" mode="777" output="#seedContent#";
// open file
if ( arguments.open ) {
openPath( seederPath );
}
printInfo( "Created Seeder: [#seederPath#]" );
}
// Generate Service
if ( arguments.service ) {
command( "coldbox create service" )
.params(
name : "#arguments.name#Service",
force : arguments.force,
open : arguments.open,
boxlang: arguments.boxlang
)
.run();
}
// Generate Handler
if ( arguments.handler || arguments.rest || arguments.resource ) {
command( "coldbox create handler" )
.params(
name : modelNamePlural,
force : arguments.force,
open : arguments.open,
rest : arguments.rest,
resource: arguments.resource,
boxlang : arguments.boxlang
)
.run();
}
// Generate Tests
if ( arguments.tests ) {
command( "coldbox create model-test" )
.params(
path : arguments.name,
force : arguments.force,
open : arguments.open,
methods: arguments.methods,
boxlang: arguments.boxlang
)
.run();
}
// Open file?
if ( arguments.open ) {
openPath( modelPath );
}
}
}