Working on a project where I need to compare the functions in one components to insure that they were present in another and the logical way to accomplish this was to get my methods sorted into alphabetical order.
It's not much code and pretty self explanatory as to what its doing so I am not going to explain it line by line. Feel free to ask questions if you have any.
2<cffile action="read" file="physical/path/to/your/component" variable="cfc">
3<cfscript>
4 function getMethods() {
5 regex = '<cffunction((\s+\w+(\s*=\s*?:".*?"|.*?|[^">\s]+)?)+\s*|\s*)/?>(.*?)</cffunction>';
6 start = 1;
7 length = len(cfc);
8 methods = structNew();
9 do {
10 method = {};
11 matches = REFind(regex,cfc,start,"true");
12 if(matches.pos[1]+matches.len[1] NEQ 0) {
13 name = getName(mid(cfc, matches.pos[2], matches.len[2]));
14 method[name] = mid(cfc, matches.pos[1], matches.len[1]);
15 structAppend(methods,method);
16 start = matches.pos[1]+matches.len[1];
17 } else {
18 start = length;
19 }
20 }
21 while (start NEQ length);
22 return methods;
23 }
24 function getName(s) {
25 var i = 1;
26 for(i=1;i<=listLen(s," "); i++) {
27 if(find("name",listGetAt(s,i," "))) {
28 name = listGetAt(s,i," ");
29 }
30 }
31 name = replace(name,"name=""","");
32 name = replace(name,"""","");
33 return name;
34 }
35</cfscript>
36<cfset methods = getMethods(cfc)>
37<cfset sorted = structSort(methods,'textnocase','asc')>
38</cfsilent>
39<cfoutput>
40<cfloop array="#sorted#" index="function">
41#methods[function]#
42</cfloop>
43</cfoutput>
run the code above and view the source to view the results. May look at making this a Coldfusion Builder Extension at some point.
#1 by Dan Vega on 6/8/11 - 5:37 PM
#2 by Brian on 6/8/11 - 5:43 PM
getMetaData() function will not return the contents of the methods but just the names and attributes. You could use that route but you will still have to manually move the methods around in your component to get them in order.
That is all this is doing is sorting all your methods alphabetically.
#3 by Dan Vega on 6/8/11 - 5:46 PM
#4 by Brian on 6/8/11 - 5:49 PM
#5 by Andy K on 6/8/11 - 9:03 PM