-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathorm-virtual-service.cfc
More file actions
146 lines (134 loc) · 4.11 KB
/
orm-virtual-service.cfc
File metadata and controls
146 lines (134 loc) · 4.11 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
/**
* Create a new virtual entity service model 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.
* .
* {code:bash}
* coldbox create orm-virtual-service Contact --open
* {code}
*
**/
component extends="coldbox-cli.models.BaseCommand" {
/**
* @entityName The name of the entity this virtual service will be bound to
* @directory The base directory to create your model in and creates the directory if it does not exist.
* @queryCaching Activate query caching
* @eventHandling Enable the virtual entity service to emit events
* @cacheRegion The cache region the virtual entity service methods will use
* @tests Generate the unit test BDD component
* @testsDirectory Your unit tests directory. Only used if tests is true
* @open Open the file once generated
* @force Force overwrite of existing files
* @boxlang Is this a boxlang project?
**/
function run(
required entityName,
directory = getAppPrefix( getCWD() ) & "models",
boolean queryCaching = false,
boolean eventHandling = true,
cacheRegion = "",
boolean tests = true,
testsDirectory = "tests/specs/unit",
boolean open = false,
boolean force = false,
boolean boxlang = isBoxLangProject( getCWD() )
){
// non-canonical path
var nonCanonicalDirectory = arguments.directory;
// 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 );
}
// Read in Template
var modelContent = fileRead( "#variables.settings.templatesPath#/orm/VirtualEntityService.txt" );
var modelTestContent = fileRead( "#variables.settings.templatesPath#/testing/ModelBDDContent.txt" );
// Query cache Region
if ( !len( arguments.cacheRegion ) ) {
arguments.cacheRegion = "ormservice.#arguments.entityName#";
}
// Basic replacements
modelContent = replaceNoCase(
modelContent,
"|entityName|",
arguments.entityname,
"all"
);
modelContent = replaceNoCase(
modelContent,
"|QueryCaching|",
arguments.QueryCaching,
"all"
);
modelContent = replaceNoCase(
modelContent,
"|cacheRegion|",
arguments.cacheRegion,
"all"
);
modelContent = replaceNoCase(
modelContent,
"|eventHandling|",
arguments.eventHandling,
"all"
);
if ( arguments.boxlang ) {
modelContent = toBoxLangClass( modelContent );
}
modelTestContent = replaceNoCase(
modelTestContent,
"|modelName|",
"#nonCanonicalDirectory#.#arguments.entityName#",
"all"
);
modelTestContent = replaceNoCase(
modelTestContent,
"|TestCases|",
"",
"all"
);
if ( arguments.boxlang ) {
modelTestContent = toBoxLangClass( modelTestContent );
}
// Write out the model
var modelPath = "#arguments.directory#/#arguments.entityName#Service.#arguments.boxlang ? "bx" : "cfc"#";
// Create dir if it doesn't exist
directoryCreate(
getDirectoryFromPath( modelPath ),
true,
true
);
// Confirm it
if (
fileExists( modelPath ) && !arguments.force && !confirm(
"The file '#getFileFromPath( modelPath )#' already exists, overwrite it (y/n)?"
)
) {
printWarn( "Exiting..." );
return;
}
file action="write" file="#modelPath#" mode="777" output="#modelContent#";
printInfo( "Created Virtual Service [#modelPath#]" );
if ( arguments.tests ) {
var testPath = "#arguments.TestsDirectory#/#arguments.entityName#ServiceTest.#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="#modelTestContent#";
// open file
if ( arguments.open ) {
openPath( testPath );
}
printInfo( "Created #testPath#" );
}
// Open file?
if ( arguments.open ) {
openPath( modelPath );
}
}
}