-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathservice.cfc
More file actions
215 lines (195 loc) · 6.09 KB
/
service.cfc
File metadata and controls
215 lines (195 loc) · 6.09 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
/**
* Create a new service 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 service at the same time.
* By default, your new service will be created in `/models` but you can override that with the `directory` param.
* .
* {code:bash}
* coldbox create service UserService --open
* {code}
* .
* {code:bash}
* coldbox create service name=UserService methods="list,save,update" --open
* {code}
*
**/
component extends="coldbox-cli.models.BaseCommand" {
/**
* @name Name of the service to create without the .cfc. For packages, specify name as 'myPackage/MyModel'
* @methods A comma-delimited list of method stubs to generate for you
* @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 service in and creates the directory if it does not exist
* @description The service hint description
* @open Open the file once generated
* @force Force overwrite of existing files
* @componentAnnotations Annotations to add to the component
* @initContent Custom content to add to the init method
* @boxlang Is this a boxlang project?
**/
function run(
required name,
methods = "",
boolean tests = true,
testsDirectory = "tests/specs/unit",
directory = getAppPrefix( getCWD() ) & "models",
description = "I am a new service",
boolean open = false,
boolean force = false,
string componentAnnotations = "",
string initContent = "",
boolean boxlang = isBoxLangProject( getCWD() )
){
// Prepare arguments
var modelTestPath = arguments.directory;
arguments.name = variables.utility.camelCaseUpper( arguments.name );
// 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 service with an empty name." );
}
// Allow dot-delimited paths
arguments.name = replace( arguments.name, ".", "/", "all" );
// Read in Template
var modelContent = fileRead( "#variables.settings.templatesPath#/ServiceContent.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 );
}
// 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 Service [#modelPath#]" );
// 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 );
}
}
}