是否有用于动作脚本/ flex的好的程序来计算代码行,功能数量,文件,包等

时间:2020-03-05 18:45:18  来源:igfitidea点击:

道格·麦克库纳(Doug McCune)创建了正是我所需要的东西(http://dougmccune.com/blog/2007/05/10/analyze-your-actionscript-code-with-this-apollo-app/),但是可惜的是AIR beta2. 我只是想要一些可以运行的工具,该工具可以提供一些不错的指标……有什么主意吗?

解决方案

回答

为了得到一个大概的估计,我们可以总是运行find。 -type f -exec cat {} \; |如果我们使用的是Mac OS X,请在项目目录中找到wc -l

回答

或者

find . -name '*.as' -or -name '*.mxml' |?xargs wc -l

或者如果我们使用zsh

wc -l **/*.{as,mxml}

它不会为我们提供这些行中的注释或者空白行的比例,但是如果我们仅对一个项目与另一个项目之间的差异感兴趣,并且已经将它们都编写了,这是一个有用的指标。

回答

这是我写的一个小脚本,用于查找ActionScript 3代码中不同源代码元素的出现总数(这是用Python编写的,因为我对此很熟悉,而Perl可能更适合于使用大量正则表达式的脚本像这样):

#!/usr/bin/python

import sys, os, re

# might want to improve on the regexes used here
codeElements = {
'package':{
    'regex':re.compile('^\s*[(private|public|static)\s]*package\s+([A-Za-z0-9_.]+)\s*', re.MULTILINE),
    'numFound':0
    },
'class':{
    'regex':re.compile('^\s*[(private|public|static|dynamic|final|internal|(\[Bindable\]))\s]*class\s', re.MULTILINE),
    'numFound':0
    },
'interface':{
    'regex':re.compile('^\s*[(private|public|static|dynamic|final|internal)\s]*interface\s', re.MULTILINE),
    'numFound':0
    },
'function':{
    'regex':re.compile('^\s*[(private|public|static|protected|internal|final|override)\s]*function\s', re.MULTILINE),
    'numFound':0
    },
'member variable':{
    'regex':re.compile('^\s*[(private|public|static|protected|internal|(\[Bindable\]))\s]*var\s+([A-Za-z0-9_]+)(\s*\:\s*([A-Za-z0-9_]+))*\s*', re.MULTILINE),
    'numFound':0
    },
'todo note':{
    'regex':re.compile('[*\s/][Tt][Oo]\s?[Dd][Oo][\s\-:_/]', re.MULTILINE),
    'numFound':0
    }
}
totalLinesOfCode = 0

filePaths = []
for i in range(1,len(sys.argv)):
    if os.path.exists(sys.argv[i]):
        filePaths.append(sys.argv[i])

for filePath in filePaths:
    thisFile = open(filePath,'r')
    thisFileContents = thisFile.read()
    thisFile.close()
    totalLinesOfCode = totalLinesOfCode + len(thisFileContents.splitlines())
    for codeElementName in codeElements:
        matchSubStrList = codeElements[codeElementName]['regex'].findall(thisFileContents)
        codeElements[codeElementName]['numFound'] = codeElements[codeElementName]['numFound'] + len(matchSubStrList)

for codeElementName in codeElements:
    print str(codeElements[codeElementName]['numFound']) + ' instances of element "'+codeElementName+'" found'
print '---'
print str(totalLinesOfCode) + ' total lines of code'
print ''

将路径传递到项目中所有源代码文件作为此脚本的参数,以使其能够处理所有源代码文件并报告总数。

像这样的命令:

find /path/to/project/root/ -name "*.as" -or -name "*.mxml" | xargs /path/to/script

将输出如下内容:

1589 instances of element "function" found
147 instances of element "package" found
58 instances of element "todo note" found
13 instances of element "interface" found
2033 instances of element "member variable" found
156 instances of element "class" found
---
40822 total lines of code

回答

下面的Enterprise Flex插件中有一个Code Metrics Explorer:

http://www.deitte.com/archives/2008/09/flex_builder_pl.htm

回答

名为LocMetrics的简单工具也可以用于.as文件...

回答

CLOC http://cloc.sourceforge.net/。即使它是基于Windows命令行的,它也可以与AS3.0一起使用,具有我们想要的所有功能,并且文档齐全。这是我正在使用的BAT文件设置:

REM =====================

echo off

cls

REM set variables

set ASDir=C:\root\directory\of\your\AS3\code\

REM run the program

REM See docs for different output formats.

cloc-1.09.exe --by-file-by-lang --force-lang="ActionScript",as --exclude_dir=.svn --ignored=ignoredFiles.txt --report-file=totalLOC.txt %ASDir% 

REM show the output

totalLOC.txt

REM end

pause

REM =====================