forked from markmandel/ColdDoc
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
DocBox.cfc
379 lines (344 loc) · 10.8 KB
/
DocBox.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
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
/**
* @author Luis Majano <[email protected]>
*
* Core DocBox documentation class that takes care of generating docs for you.
* You can initialize the object with a strategy and strategy properties or with nothing at all.
* You can then generate the docs according to 1 or more output strategies via the <code>generate()</code> method.
* <hr>
* <small><em>Copyright 2015 Ortus Solutions, Corp <a href="www.ortussolutions.com">www.ortussolutions.com</a></em></small>
*/
component accessors="true" {
/**
* The strategy to use for document generation. Must extend docbox.strategy.AbstractTemplateStrategy
*/
property
name ="strategies"
type ="array"
doc_generic="docbox.strategy.AbstractTemplateStrategy";
/**
* Constructor
*
* @strategy The documentation output strategy to utilize.
* @properties Struct of data properties required for the specific output strategy
*
* @return The DocBox instance
*/
DocBox function init(
any strategy = "",
struct properties = {}
){
variables.strategies = [];
variables.properties = arguments.properties;
// If we have a strategy, then add it in
if ( len( arguments.strategy ) ) {
addStrategy(
strategy = arguments.strategy,
properties = arguments.properties
);
}
return this;
}
/**
* Backwards-compatible setter to add a strategy to the docbox configuration.
*
* @see addStrategy
*
* @return The DocBox instance
*/
DocBox function setStrategy(){
return addStrategy( argumentCollection = arguments );
}
/**
* Add a documentation strategy for output format.
*
* @strategy The optional strategy to generate the documentation with. This can be a class path or an instance of the strategy. If none is passed then
* we create the default strategy of 'docbox.strategy.api.HTMLAPIStrategy'
* @properties The struct of properties to instantiate the strategy with.
*
* @return The DocBox instance
*/
DocBox function addStrategy(
any strategy = "docbox.strategy.api.HTMLAPIStrategy",
struct properties = {}
){
// Set the incomign strategy to store
var newStrategy = arguments.strategy;
// If the strategy is not an object, then look it up
if ( isSimpleValue( newStrategy ) ) {
// Discover the strategy
switch ( uCase( arguments.strategy ) ) {
case "CommandBox":
arguments.strategy = "docbox.strategy.CommandBox.CommandBoxStrategy";
break;
case "HTML":
case "HTMLAPISTRATEGY":
arguments.strategy = "docbox.strategy.api.HTMLAPIStrategy";
break;
case "JSON":
case "JSONAPISTRATEGY":
arguments.strategy = "docbox.strategy.json.JSONAPIStrategy";
break;
case "UML":
case "XMI":
case "XMISTRATEGY":
arguments.strategy = "docbox.strategy.uml2tools.XMIStrategy";
default:
break;
}
// Build it out
newStrategy = new "#arguments.strategy#"( argumentCollection = arguments.properties );
}
setStrategies( getStrategies().append( newStrategy ) );
return this;
}
/**
* Generate the docs
*
* @source Either, the string directory source, OR an array of structs containing 'dir' and 'mapping' key
* @mapping The base mapping for the folder. Only required if the source is a string
* @excludes A regex that will be applied to the input source to exclude from the docs
* @throwOnError Throw an error and halt the generation process if DocBox encounters an invalid component.
*
* @return The DocBox instance
*/
DocBox function generate(
required source,
string mapping = "",
string excludes = "",
boolean throwOnError = false
){
// verify we have at least one strategy defined, if not, auto add the HTML strategy
if ( isNull( getStrategies() ) || !getStrategies().len() ) {
this.addStrategy(
strategy : "HTML",
properties: variables.properties
);
}
// inflate the incoming input and mappings
var thisSource = "";
if ( isSimpleValue( arguments.source ) ) {
thisSource = [
{
dir : arguments.source,
mapping : arguments.mapping
}
];
} else {
thisSource = arguments.source;
}
// build metadata collection
var metadata = buildMetaDataCollection(
thisSource,
arguments.excludes,
arguments.throwOnError
);
getStrategies().each( function( strategy ){
strategy.run( metadata );
} );
return this;
}
/************************************ PRIVATE ******************************************/
/**
* Clean input path
*
* @path The incoming path to clean
* @inputDir The input dir to clean off
*/
private function cleanPath( required path, required inputDir ){
var currentPath = replace(
getDirectoryFromPath( arguments.path ),
arguments.inputDir,
""
);
currentPath = reReplace( currentPath, "^[/\\]", "" );
currentPath = reReplace( currentPath, "[/\\]", ".", "all" );
return reReplace( currentPath, "\.$", "" );
}
/**
* Builds the searchable meta data collection
*
* @inputSource an array of structs containing inputDir and mapping
* @excludes A regex that will be applied to the input source to exclude from the docs
* @throwOnError Throw an error and halt the generation process if DocBox encounters an invalid component.
*/
query function buildMetaDataCollection(
required array inputSource,
string excludes = "",
boolean throwOnError = false
){
var metadata = queryNew( "package,name,extends,metadata,type,implements,fullextends,currentMapping" );
// iterate over input sources
for ( var thisInput in arguments.inputSource ) {
if ( !directoryExists( thisInput.dir ) ) {
throw(
message = "Invalid configuration; source directory not found",
type = "InvalidConfigurationException",
detail = "Configured source #thisInput.dir# does not exist."
);
}
var aFiles = directoryList( thisInput.dir, true, "path", "*.cfc" );
// iterate over files found
for ( var thisFile in aFiles ) {
// Excludes?
// Use relative file path so placement on disk doesn't affect the regex check
var relativeFilePath = replace( thisFile, thisInput.dir, "" );
if ( len( arguments.excludes ) && reFindNoCase( arguments.excludes, relativeFilePath ) ) {
continue;
}
// get current path
var currentPath = cleanPath( thisFile, thisInput.dir );
// calculate package path according to mapping
var packagePath = thisInput.mapping;
if ( len( currentPath ) ) {
packagePath = listAppend( thisInput.mapping, currentPath, "." );
}
// setup cfc name
var cfcName = listFirst( getFileFromPath( thisFile ), "." );
// Core Excludes, don't document the Application.cfc
if ( cfcName == "Application" ) {
continue;
}
try {
// Get component metadatata
var meta = "";
if ( len( packagePath ) ) {
meta = getComponentMetadata( packagePath & "." & cfcName );
} else {
meta = getComponentMetadata( cfcName );
}
// let's do some cleanup, in case CF sucks.
if ( len( packagePath ) AND NOT meta.name contains packagePath ) {
meta.name = packagePath & "." & cfcName;
}
// Add row
queryAddRow( metadata );
// Add contents
querySetCell( metadata, "package", packagePath );
querySetCell( metadata, "name", cfcName );
querySetCell( metadata, "metadata", meta );
querySetCell( metadata, "type", meta.type );
querySetCell(
metadata,
"currentMapping",
thisInput.mapping
);
// Get implements
var implements = getImplements( meta );
implements = listQualify( arrayToList( implements ), ":" );
querySetCell( metadata, "implements", implements );
// Get inheritance
var fullextends = getInheritance( meta );
fullextends = listQualify( arrayToList( fullextends ), ":" );
querySetCell( metadata, "fullextends", fullextends );
// so we cane easily query direct desendents
if ( structKeyExists( meta, "extends" ) ) {
if ( meta.type eq "interface" ) {
querySetCell(
metadata,
"extends",
meta.extends[ structKeyList( meta.extends ) ].name
);
} else {
querySetCell(
metadata,
"extends",
meta.extends.name
);
}
} else {
querySetCell( metadata, "extends", "-" );
}
} catch ( Any e ) {
if ( arguments.throwOnError ) {
throw(
type = "InvalidComponentException",
message = e.message,
detail = e.detail,
extendedInfo = serializeJSON( e )
);
} else {
trace(
type = "warning",
category = "docbox",
inline = "true",
text = "Warning! The following script has errors: " & packagePath & "." & cfcName & ": #e.message & e.detail & e.stacktrace#"
);
}
if ( structKeyExists( server, "lucee" ) ) {
systemOutput(
"Warning! The following script has errors: " & packagePath & "." & cfcName,
true
);
systemOutput( "#e.message & e.detail#", true );
systemOutput( e.stackTrace );
}
}
}
// end qFiles iteration
}
// end input source iteration
return metadata;
}
/**
* Gets an array of the classes that this metadata implements, in order of extension
*
* @metadata The metadata to look at
*
* @return array of component interfaces implemented by some component in this package
*/
private array function getImplements( required struct metadata ){
var interfaces = {};
// check if a cfc
if ( arguments.metadata.type neq "component" ) {
return [];
}
// iterate
while ( structKeyExists( arguments.metadata, "extends" ) ) {
if ( structKeyExists( arguments.metadata, "implements" ) ) {
for ( var key in arguments.metadata.implements ) {
interfaces[ arguments.metadata.implements[ key ].name ] = 1;
}
}
arguments.metadata = arguments.metadata.extends;
}
// get as an array
interfaces = structKeyArray( interfaces );
// sort it
arraySort( interfaces, "textnocase" );
return interfaces;
}
/**
* Gets an array of the classes that this metadata extends, in order of extension
*
* @metadata The metadata to look at
*
* @return array of classes inherited by some component in this package
*/
private array function getInheritance( required struct metadata ){
// ignore top level
var inheritence = [];
while ( structKeyExists( arguments.metadata, "extends" ) ) {
// manage interfaces
if ( arguments.metadata.type == "interface" ) {
arguments.metadata = arguments.metadata.extends[ structKeyList( arguments.metadata.extends ) ];
} else {
arguments.metadata = arguments.metadata.extends;
}
arrayPrepend( inheritence, arguments.metadata.name );
}
return inheritence;
}
/**
* Undocumented function
*
* @deprecated This is no longer in use.
* @param1 param 1
* @param2 param 2
*
* @throws Throws X,Y and Z
*
* @return Nothing
*/
function testFunction( param1, param2 ){
}
}