Trending: Jawaan


Dynamics AX: Counting Lines of Code – Mohammed Rasheed


It recently downed upon me that I do not have an account of the number of lines of code I have written on my vpc (my Dynamics AX Playground!!).... Unfortunately I could not find an easy way to get an accurate count of the lines of code I wrote.. Hence i wrote some more code :)

Its like writing code to count code!!

Hope this is of help to others out there.. asking themselves the same question.

class mrCountLinesOfCode

{

Map axTypeCount;

Map mrTypeCount;

MapEnumerator axEnumerator;

MapEnumerator mrEnumerator;

str fullPath;

int pos;

int total;

int linesOfCode;

}

void run()

{

TreeNodeiterator tempNodeIterator;

TreeNodeiterator treeNodeIterator;

TreeNodeiterator rootNodeIterator;

TreeNodeiterator childNodeiterator;

str type;

TreeNode treeNode;

TreeNode rootNode;

TreeNode childNode;

TreeNode tempNode;

;

#AOT

rootNode = TreeNode::rootNode();

rootNodeIterator = rootNode.AOTiterator();

rootNode = rootNodeIterator.next();

axTypeCount = new Map(types::String,types::Integer);

mrTypeCount = new Map(types::String,types::Integer);

while(rootNode)

{

type = rootNode.treeNodeName();

rootNodeIterator = rootNode.AOTiterator();

childNode = rootNodeIterator.next();

childNodeIterator = childNode.AOTiterator();

treeNode = childNodeIterator.next();

while(childNode)

{

tempNode = treeNode;

tempNodeiterator = tempNode.AOTiterator();

while(treeNode)

{

// loop till you get to a leaf node - as methods are leafs

while(treeNode.AOTchildNodeCount() > 0)

{

if(treeNode.AOTiterator().next().AOTchildNodeCount()> 0)

treeNode = tempNodeiterator.next();

if(!treeNode)

treeNode = tempNode;

tempNode = treeNode;

treeNode = treeNode.AOTiterator().next();

if(!treeNode)

treeNode = tempNode;

}

// so we found a Lefe. check if its a method

if(sysTreeNode::hasSource(treeNode))

{

tempNodeiterator = tempNode.AOTiterator();

while(treeNode)

{

treeNode = tempNodeiterator.next(); // iterate first.

this.setCount(treeNode,type);

}

treeNode = tempNodeiterator.next();

if(!treeNode)

{

treeNode = childNodeIterator.next();

if(!treeNode)

{

childNode = rootNodeIterator.next();

childNodeIterator = childNode.AOTiterator();

treeNode = childNodeIterator.next();

}

tempNode = treeNode;

tempNodeiterator = tempNode.AOTiterator();

}

}// end if Has Source

else

treeNode = tempNodeiterator.next(); // leaf is not a method.. hence move to the next node

}

//childNode = rootNodeIterator.next();

treeNode = childNodeIterator.next();

if(!treeNode)

{

childNode = rootNodeIterator.next();

while(childNode && !this.checkIfNodeMightHaveCode(childNode.sysNodeType()))

{

childNode = rootNodeIterator.next();

}

if(childNode)

{

childNodeIterator = childNode.AOTiterator();

treeNode = childNodeIterator.next();

}

}

}

childNode = rootNodeIterator.next();

rootNode = childNode;

}// end root while

this.printitOut();

}

void setCount(treeNode _treeNode,str _type)

{

int tempLines;

;

fullPath = sysTreeNode::getPath(_treeNode);

pos = 0;

pos = strScan(fullPath,'\mr',1,strlen(fullPath)); //i always prefix any new elements i create with MR

tempLines = this.getLinesOfCode(_treeNode.AOTgetSource());

if(tempLines > 4)

{

if(pos)

this.countMrLines(tempLines,_type);

else

this.countAXLines(tempLines,_type);

}

}

int getLinesOfCode(Source _source)

{

Source tmpSource;

Line line;

int ptr;

int numberOfLines;

;

if (_source)

{

tmpSource = _source;

while (tmpSource)

{

line++;

ptr = strfind(tmpSource, '\n', 1, maxint());

if (!ptr)

{

ptr = strlen(tmpSource);

}

numberOfLines++;

tmpSource = strdel(tmpSource, 1, ptr);

}

}

return numberOfLines;

}

int getLinesOfCode(Source _source)

{

Source tmpSource;

Line line;

int ptr;

int numberOfLines;

;

if (_source)

{

tmpSource = _source;

while (tmpSource)

{

line++;

ptr = strfind(tmpSource, '\n', 1, maxint());

if (!ptr)

{

ptr = strlen(tmpSource);

}

numberOfLines++;

tmpSource = strdel(tmpSource, 1, ptr);

}

}

return numberOfLines;

}

void countMrLines(int _lines,str _type)

{

;

linesOfCode = 0;

if(mrTypeCount.exists(_type))

linesOfCode = mrTypeCount.lookup(_type);

linesOfCode += _lines;

mrTypeCount.insert(_type,linesOfCode);

}

boolean checkIfNodeMightHaveCode(int _sysNodeType)

{

;

#TreeNodeSysNodeType

switch(_sysNodeType)

{

case #SysNodeTypeTableCollection :

case #SysNodeTypeSystemEnum :

case #NT_DBCOLLECTIONLIST :

case #NT_DBFIELDGROUPLIST :

case #NT_DBENUMLIST :

case #NT_DBREFERENCELIST :

case #NT_CONFIGURATIONKEYLIST :

case #NT_SECURITYKEYLIST :

case #NT_DBLICENSECODELIST :

case #NT_MENU :

case #NT_DBLICENSECODE :

case #NT_CONFIGURATIONKEY :

case #NT_SECURITYKEY :

case #NT_TYPE :

case #NT_REFERENCE :

case #NT_PERSPECTIVE :

case #NT_PERSPECTIVEFIELDLIST :

return false;

default:

return true;

}

// just in case

return true;

}


View the original article here

1 Responses to “Dynamics AX: Counting Lines of Code – Mohammed Rasheed”

Anonymous said...
August 15, 2013 at 6:05 PM

countAXLines and printitOut methods are not posted. It will not work without this.


About Me