勿忘初心

叹人生、不如意事,十常八九

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

ruby簡單的代碼行統計工具

看代码

encoding: utf-8

class CodeLineStat
attr_reader :code_lines

def initialize
@code_lines = 0
end

def stat(path)
Dir.foreach(path) do |file|
if file != "." && file != ".." then
filePath = path + "/" + file
if File.directory? filePath then
stat(filePath);
elsif file =~ /(.cs|.aspx)$/ then
@code_lines += file_code_line(filePath);
end
end
end
end

private
def file_code_line(filePath)
lines = 0
File.open(filePath,"r:utf-8") do |file|
in_comment = false;
file.each_line do |line|
line.strip!
if line.index("/") then
in_comment = true
elsif line.index("
/") then
in_comment = false
elsif !line.empty? && !in_comment && !line.index("//") then
lines += 1
end
end
end
puts "#{filePath} : #{lines}"
lines
end
end

cl = CodeLineStat.new
cl.stat(ARGV[0])
title = "總共有代碼行:".encode('big5')
puts cl.code_lines

posted on 2015-08-12 17:43  chensss2008  阅读(158)  评论(0)    收藏  举报