-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBuild.cfc
313 lines (278 loc) · 7.87 KB
/
Build.cfc
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
/**
* Build process for CommandBox Modules
* Adapt to your needs.
*/
component {
/**
* Constructor
*/
function init(){
// Setup Pathing
variables.cwd = getCWD().reReplace( "\.$", "" );
variables.artifactsDir = cwd & "/.artifacts";
variables.buildDir = cwd & "/.tmp";
variables.apiDocsURL = "http://localhost:60299/apidocs/";
variables.testRunner = "http://localhost:60299/tests/runner.cfm";
variables.exportsDir = "";
variables.moduleName = "coldbox-cli";
// Source Excludes Not Added to final binary: You can use REGEX
variables.excludes = [
"build",
"test\-harness",
"tests",
"docker-compose.yml",
"^\..*"
];
// Cleanup + Init Build Directories
[
variables.buildDir,
variables.artifactsDir
].each( function( item ){
if ( directoryExists( item ) ) {
directoryDelete( item, true );
}
// Create directories
directoryCreate( item, true, true );
} );
// Create Project Dependency Mappings
fileSystemUtil.createMapping( variables.moduleName, variables.cwd );
return this;
}
/**
* Run the build process: test, build source, docs, checksums
*
* @projectName The project name used for resources and slugs
* @version The version you are building
* @buldID The build identifier
* @branch The branch you are building
*/
function run(
required projectName,
version = "1.0.0",
buildID = createUUID(),
branch = "development"
){
// Create project mapping
fileSystemUtil.createMapping( arguments.projectName, variables.cwd );
// Build the source
buildSource( argumentCollection = arguments );
// Build Docs
arguments.outputDir = variables.buildDir & "/apidocs";
docs( argumentCollection = arguments );
// checksums
buildChecksums();
// Finalize Message
variables.print
.line()
.boldMagentaLine( "Build Process is done! Enjoy your build!" )
.toConsole();
}
/**
* Run the test suites
*/
function runTests(){
variables.print
.line()
.boldGreenLine( "------------------------------------------------" )
.boldGreenLine( "Starting to execute your tests..." )
.boldGreenLine( "------------------------------------------------" )
.toConsole();
var sTime = getTickCount();
// Tests First, if they fail then exit
// Run your tests via the `command()` options here.
command( "task run build/Tests.cfc" ).run();
// Check Exit Code?
if ( shell.getExitCode() ) {
return error( "X Cannot continue building, tests failed!" );
} else {
variables.print
.line()
.boldGreenLine( "------------------------------------------------" )
.boldGreenLine( "All tests passed in #getTickCount() - sTime#ms! Ready to go, great job!" )
.boldGreenLine( "------------------------------------------------" )
.toConsole();
}
}
/**
* Build the source
*
* @projectName The project name used for resources and slugs
* @version The version you are building
* @buldID The build identifier
* @branch The branch you are building
*/
function buildSource(
required projectName,
version = "1.0.0",
buildID = createUUID(),
branch = "development"
){
// Build Notice ID
variables.print
.line()
.boldMagentaLine(
"Building #arguments.projectName# v#arguments.version#+#arguments.buildID# from #cwd# using the #arguments.branch# branch."
)
.toConsole();
ensureExportDir( argumentCollection = arguments );
// Project Build Dir
variables.projectBuildDir = variables.buildDir & "/#projectName#";
directoryCreate(
variables.projectBuildDir,
true,
true
);
// Copy source
print.blueLine( "Copying source to build folder..." ).toConsole();
copy(
variables.cwd,
variables.projectBuildDir
);
// Create build ID
fileWrite(
"#variables.projectBuildDir#/#projectName#-#version#+#buildID#",
"Built with love on #dateTimeFormat( now(), "full" )#"
);
// Updating Placeholders
print.greenLine( "Updating version identifier to #arguments.version#" ).toConsole();
command( "tokenReplace" )
.params(
path = "/#variables.projectBuildDir#/**",
token = "@build.version@",
replacement = arguments.version
)
.run();
print.greenLine( "Updating build identifier to #arguments.buildID#" ).toConsole();
command( "tokenReplace" )
.params(
path = "/#variables.projectBuildDir#/**",
token = ( arguments.branch == "master" ? "@build.number@" : "+@build.number@" ),
replacement = ( arguments.branch == "master" ? arguments.buildID : "-snapshot" )
)
.run();
// zip up source
var destination = "#variables.exportsDir#/#projectName#-#version#.zip";
print.greenLine( "Zipping code to #destination#" ).toConsole();
cfzip(
action = "zip",
file = "#destination#",
source = "#variables.projectBuildDir#",
overwrite = true,
recurse = true
);
// Copy box.json for convenience
fileCopy(
"#variables.projectBuildDir#/box.json",
variables.exportsDir
);
}
/**
* Produce the API Docs
*/
function docs(
required projectName,
version = "1.0.0",
outputDir = ".tmp/apidocs"
){
ensureExportDir( argumentCollection = arguments );
// Generate Docs
print.greenLine( "Generating API Docs, please wait..." ).toConsole();
directoryCreate( arguments.outputDir, true, true );
// Generate the docs
command( "docbox generate" )
.params(
"source" = "commands",
"excludes" = "",
"mapping" = variables.moduleName,
"strategy-projectTitle" = "#arguments.projectName# v#arguments.version#",
"strategy-outputDir" = arguments.outputDir
)
.run();
print.greenLine( "API Docs produced at #arguments.outputDir#" ).toConsole();
var destination = "#variables.exportsDir#/#projectName#-docs-#version#.zip";
print.greenLine( "Zipping apidocs to #destination#" ).toConsole();
cfzip(
action = "zip",
file = "#destination#",
source = "#arguments.outputDir#",
overwrite = true,
recurse = true
);
}
/********************************************* PRIVATE HELPERS *********************************************/
/**
* Build Checksums
*/
private function buildChecksums(){
print.greenLine( "Building checksums" ).toConsole();
command( "checksum" )
.params(
path = "#variables.exportsDir#/*.zip",
algorithm = "SHA-512",
extension = "sha512",
write = true
)
.run();
command( "checksum" )
.params(
path = "#variables.exportsDir#/*.zip",
algorithm = "md5",
extension = "md5",
write = true
)
.run();
}
/**
* DirectoryCopy is broken in lucee
*/
private function copy( src, target, recurse = true ){
// process paths with excludes
directoryList(
src,
false,
"path",
function( path ){
var isExcluded = false;
variables.excludes.each( function( item ){
if ( path.replaceNoCase( variables.cwd, "", "all" ).reFindNoCase( item ) ) {
isExcluded = true;
}
} );
return !isExcluded;
}
).each( function( item ){
// Copy to target
if ( fileExists( item ) ) {
print.blueLine( "Copying #item#" ).toConsole();
fileCopy( item, target );
} else {
print.greenLine( "Copying directory #item#" ).toConsole();
directoryCopy(
item,
target & "/" & item.replace( src, "" ),
true
);
}
} );
}
/**
* Gets the last Exit code to be used
**/
private function getExitCode(){
return ( createObject( "java", "java.lang.System" ).getProperty( "cfml.cli.exitCode" ) ?: 0 );
}
/**
* Ensure the export directory exists at artifacts/NAME/VERSION/
*/
private function ensureExportDir(
required projectName,
version = "1.0.0"
){
if ( structKeyExists( variables, "exportsDir" ) && directoryExists( variables.exportsDir ) ) {
return;
}
// Prepare exports directory
variables.exportsDir = variables.artifactsDir & "/#projectName#/#arguments.version#";
directoryCreate( variables.exportsDir, true, true );
}
}