-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinterceptor.cfc
More file actions
176 lines (161 loc) · 5.31 KB
/
interceptor.cfc
File metadata and controls
176 lines (161 loc) · 5.31 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
/**
* Create a new interceptor 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 interceptor at the same time.
* By default, your new interceptor will be created in /interceptors but you can override that with the directory param.
* Note, even though this command creates the interceptor CFC, you will still need to register it in the interceptors array
* in your ColdBox.cfc config file.
* .
* {code:bash}
* coldbox create interceptor myInterceptor preProcess,postEvent
* {code}
*
**/
component extends="coldbox-cli.models.BaseCommand" {
/**
* @name Name of the interceptor to create without the .cfc
* @points A comma-delimited list of interception points to generate
* @description A description for the interceptor hint
* @tests Generate the unit test component
* @testsDirectory Your unit tests directory. Only used if tests is true
* @directory The base directory to create your interceptor in and creates the directory if it does not exist.
* @open Open the interceptor once generated
* @force Force overwrite of the interceptor if it exists
* @boxlang Is this a boxlang project?
**/
function run(
required name,
points = "",
description = "I am a new interceptor",
boolean tests = true,
testsDirectory = "tests/specs/interceptors",
directory = getAppPrefix( getCWD() ) & "interceptors",
boolean open = false,
boolean force = false,
boolean boxlang = isBoxLangProject( getCWD() )
){
// This will make each directory canonical and absolute
var relativeDirectory = arguments.directory;
arguments.directory = resolvePath( arguments.directory );
arguments.testsDirectory = resolvePath( arguments.testsDirectory );
// Validate directory
if ( !directoryExists( arguments.directory ) ) {
directoryCreate( arguments.directory );
}
// This help readability so the success messages aren't up against the previous command line
print.line();
// Read in Template
var interceptorContent = fileRead( "#variables.settings.templatesPath#/InterceptorContent.txt" );
var interceptorMethod = fileRead( "#variables.settings.templatesPath#/InterceptorMethod.txt" );
var interceptorTestContent = fileRead( "#variables.settings.templatesPath#/testing/InterceptorBDDContent.txt" );
var interceptorTestCase = fileRead( "#variables.settings.templatesPath#/testing/InterceptorBDDCaseContent.txt" );
// Start Replacing
interceptorContent = replaceNoCase(
interceptorContent,
"|Name|",
arguments.name,
"all"
);
if ( arguments.boxlang ) {
interceptorContent = toBoxLangClass( interceptorContent );
}
var interceptorPath = listChangeDelims( relativeDirectory, ".", "/\" ).listAppend( arguments.name, "." );
interceptorTestContent = replaceNoCase(
interceptorTestContent,
"|name|",
interceptorPath,
"all"
);
if ( arguments.boxlang ) {
interceptorTestContent = toBoxLangClass( interceptorTestContent );
}
// Placeholder in case we add this in
interceptorContent = replaceNoCase(
interceptorContent,
"|Description|",
arguments.description,
"all"
);
// Interception Points
if ( len( arguments.points ) ) {
var methodContent = "";
var allTestsCases = "";
var thisTestCase = "";
for ( var thisPoint in listToArray( arguments.points ) ) {
methodContent = methodContent & replaceNoCase(
interceptorMethod,
"|interceptionPoint|",
thisPoint,
"all"
) & CR & CR;
// Are we creating tests cases
if ( arguments.tests ) {
thisTestCase = replaceNoCase(
interceptorTestCase,
"|point|",
thisPoint,
"all"
);
allTestsCases &= thisTestCase & CR & CR;
}
}
interceptorContent = replaceNoCase(
interceptorContent,
"|interceptionPoints|",
methodContent,
"all"
);
interceptorTestContent = replaceNoCase(
interceptorTestContent,
"|TestCases|",
allTestsCases,
"all"
);
} else {
interceptorContent = replaceNoCase(
interceptorContent,
"|interceptionPoints|",
"",
"all"
);
interceptorTestContent = replaceNoCase(
interceptorTestContent,
"|TestCases|",
"",
"all"
);
}
// Write it out.
var interceptorPath = "#arguments.directory#/#arguments.name#.#arguments.boxlang ? "bx" : "cfc"#";
// Confirm it
if (
fileExists( interceptorPath ) && !arguments.force && !confirm(
"The file '#getFileFromPath( interceptorPath )#' already exists, overwrite it (y/n)?"
)
) {
printWarn( "Exiting..." );
return;
}
file action="write" file="#interceptorPath#" mode="777" output="#interceptorContent#";
print.greenLine( "#interceptorPath#" );
if ( tests ) {
var testPath = "#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="#interceptorTestContent#";
printInfo( "Created #testPath#" );
// open file
if ( arguments.open ) {
openPath( testPath );
}
}
// open file
if ( arguments.open ) {
openPath( interceptorPath );
}
}
}