我试过rake stats
但这似乎很不准确。也许它忽略了几个目录?如何计算代码行数?
回答
您可以尝试这两种选择:
Rakestats从博文片段:
namespace :spec do
desc "Add files that DHH doesn't consider to be 'code' to stats"
task :statsetup do
require 'code_statistics'
class CodeStatistics
alias calculate_statistics_orig calculate_statistics
def calculate_statistics
@pairs.inject({}) do |stats, pair|
if 3 == pair.size
stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats
else
stats[pair.first] = calculate_directory_statistics(pair.last); stats
end
end
end
end
::STATS_DIRECTORIES << ['Views', 'app/views', /\.(rhtml|erb|rb)$/]
::STATS_DIRECTORIES << ['Test Fixtures', 'test/fixtures', /\.yml$/]
::STATS_DIRECTORIES << ['Email Fixtures', 'test/fixtures', /\.txt$/]
# note, I renamed all my rails-generated email fixtures to add .txt
::STATS_DIRECTORIES << ['Static HTML', 'public', /\.html$/]
::STATS_DIRECTORIES << ['Static CSS', 'public', /\.css$/]
# ::STATS_DIRECTORIES << ['Static JS', 'public', /\.js$/]
# prototype is ~5384 LOC all by itself - very hard to filter out
::CodeStatistics::TEST_TYPES << "Test Fixtures"
::CodeStatistics::TEST_TYPES << "Email Fixtures"
end
end
task :stats => "spec:statsetup"
- metric_fu - 甲红宝石用于Easy Metric报告生成的Gem
PS:我还没有尝试过上述任何一种,但metric_fu听起来很有趣,请参阅输出的屏幕截图。
这是一个简单的解决方案。它统计了您的rails项目的应用程序文件夹中的代码行--CSS,Ruby,CoffeeScript和全部。在项目的根目录下,运行以下命令:
find ./app -type f | xargs cat | wc -l
并且比cloc快几个数量级,如果你不关心你正在计数的行的语义 – pje 2013-08-29 20:01:19
我真的不知道为什么这会得到如此多的投票。它只是对文件行进行计数,包括来自任何源代码,配置文件,日志文件,用于大声哭泣的二进制文件的空行和注释。这与LOC的**非常不同,可以对构成代码行的任何可能定义。在我目前的铁轨项目中,这个报告的数据量为35K,而从精确到19K的数据更准确。 – Thilo 2014-09-11 12:27:00
@Thilo,它实际上不会计数配置文件或二进制文件,因为它正在查找./app文件夹。尽管够公平,但对于一切都有多种解决方案。 – 2014-11-26 22:45:12
我使用免费的Perl脚本cloc。示例用法:
phrogz$ cloc .
180 text files.
180 unique files.
77 files ignored.
http://cloc.sourceforge.net v 1.56 T=1.0 s (104.0 files/s, 19619.0 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Javascript 29 1774 1338 10456
Ruby 61 577 185 4055
CSS 10 118 133 783
HTML 1 13 3 140
DOS Batch 2 6 0 19
Bourne Shell 1 4 0 15
-------------------------------------------------------------------------------
SUM: 104 2492 1659 15468
-------------------------------------------------------------------------------
'brew install cloc' – Chip 2013-08-29 23:42:49
它似乎不支持'erb'视图文件。 'cloc app/views':100个文本文件。 96个独特的文件。忽略100个文件。 **更新:'cloc app/views --force-lang = html,erb' ** – Mifeng 2014-03-30 17:21:09
@Mifeng非常感谢!你可以用玉来做同样的事情,玉也不支持开箱即用。例如。 'cloc example/jadeviews --force-lang = html,jade' – Automatico 2014-12-03 10:50:07
这一个计算文件的数量,代码的总行数,注释和每个文件的平均LOC。它也排除名称中包含“vendor”的目录中的文件。
用法:
count_lines('rb')
代码:
def count_lines(ext)
o = 0 # Number of files
n = 0 # Number of lines of code
m = 0 # Number of lines of comments
files = Dir.glob('./**/*.' + ext)
files.each do |f|
next if f.index('vendor')
next if FileTest.directory?(f)
o += 1
i = 0
File.new(f).each_line do |line|
if line.strip[0] == '#'
m += 1
next
end
i += 1
end
n += i
end
puts "#{o.to_s} files."
puts "#{n.to_s} lines of code."
puts "#{(n.to_f/o.to_f).round(2)} LOC/file."
puts "#{m.to_s} lines of comments."
end
这很简单,它很漂亮' – baash05 2014-01-09 01:08:52
黯然知道。我不想破解或安装一堆宝石。仍在寻找一个简单,优雅的解决方案。 – AnApprentice 2010-10-18 17:04:32