Ruby-on-rails 如何计算代码行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3954867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:24:35  来源:igfitidea点击:

How to count lines of code?

ruby-on-railsrubyruby-on-rails-3

提问by AnApprentice

I tried rake statsbut that seems highly inaccurate. Perhaps it ignores several directories?

我试过了,rake stats但这似乎非常不准确。也许它忽略了几个目录?

采纳答案by zengr

You can try out these two options:

您可以尝试以下两个选项:

  1. Hack rake stats
  1. 黑客佣金统计

Rakestats snippet from blogpost:

博文中的 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"
  1. metric_fu- A Ruby Gem for Easy Metric Report Generation
  1. metric_fu- 用于轻松生成指标报告的 Ruby Gem

PS: I haven't tried any of the above, but metric_fu sounds interesting, see the screenshots of the output.

PS:我没有尝试过以上任何一种,但是 metric_fu 听起来很有趣,请参阅输出的屏幕截图。

回答by Phrogz

I use the free Perl script cloc. Sample usage:

我使用免费的 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
-------------------------------------------------------------------------------

回答by Amin Ariana

Here's a simple solution. It counts the lines of code in your rails project's app folder - CSS, Ruby, CoffeeScript, and all. At the root of your project, run this command:

这是一个简单的解决方案。它计算 rails 项目的 app 文件夹中的代码行数 - CSS、Ruby、CoffeeScript 等等。在项目的根目录下,运行以下命令:

find ./app -type f | xargs cat | wc -l

EDIT

编辑

Read the comments. Then try this instead:

阅读评论。然后试试这个:

find ./app -type f -name "*.rb" | xargs cat | sed "/^\s*\(#\|$\)/d" | wc -l

回答by user2398029

This one calculates number of files, total lines of code, comments, and average LOC per file. It also excludes files inside directories with "vendor" in their name.

这个计算文件的数量、代码的总行数、注释和每个文件的平均 LOC。它还排除了名称中带有“vendor”的目录中的文件。

Usage:

用法:

count_lines('rb')

Code:

代码:

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