代码行数统计工具(Ruby)

#保存扫描结果
class Result
def initialize
self.name = ''
self.count = 0
self.subResult = Array.new
end
attr_reader :name, :count, :subResult
attr_writer :name, :count, :subResult
end

#函数统计类
class LinesCounter
#dir 目标目录名
#fileFilter 文件过滤器,返回false表示过滤掉。
#lineFilter 行过滤器,返回alse表示过滤掉。
def initialize(dir, fileFilter, lineFilter)
raise "#{dir} 不是目录" unless File.directory?(dir)
@dir = dir
@fileFilter, @lineFilter = fileFilter, lineFilter
end

def count
r = Result.new
r.name = @dir
countDir("", @dir, r)
return r
end
private
def getPath(prefix, name)
prefix=='' ? name : prefix + File::SEPARATOR + name
end
def countDir(prefix, dirName, collect)
path = getPath(prefix, dirName)
c = 0
Dir.foreach(path) do
|x|
next if x =~/^(/.|/./.)$/
r = Result.new
r.name = x
subpath = getPath(path, x)
if File.directory?(subpath)
c += countDir(path, x, r)
else
next unless @fileFilter.call(x)
c += countFile(path, x, r)
end
collect.subResult.push(r)
end
collect.count = c
end
def countFile(prefix, fileName, collect)
path = getPath(prefix, fileName)
c = 0
File.foreach(path) do
|x|
next unless @lineFilter.call(x)
c = c + 1
end
collect.count = c
end
end

#打印结果
def printResult(r)
def printR(r, deep)
puts "#{"/t" * deep}#{r.name}(#{r.count})"
r.subResult.each do
|x|
printR(x, deep+1)
end
end
printR(r, 0)
end

lc = LinesCounter.new(ARGV[0],/
proc {|x| x =~ /^.*/.rb$/} , #只统计*.rb文件/
proc {|x| x !~ /^/s*(#|/s*$)/ }) #忽略注释和空行
#这里可以根据自己情况修改
#java可以这样写:
#lc = LinesCounter.new(ARGV[0],/
#proc {|x| x =~ /^.*/.java$/} ,/
#proc {|x| x !~ /^/s*(|/s*$)/ })
printResult(lc.count)

使用方法和运行结果:

D:/ruby>ruby lines.rb D:/ruby
D:/ruby(241)
a.rb(5)
exp.rb(130)
hello(1)
hello.rb(1)
lines.rb(69)
other(20)
pipe.rb(16)
scan.rb(4)
pipe.rb(16)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部