Подтвердить что ты не робот

Как подсчитать строки кода?

Я пробовал rake stats, но это кажется очень неточным. Возможно, он игнорирует несколько каталогов?

4b9b3361

Ответ 1

Вы можете попробовать эти два варианта:

Ракетка из фрагмента блога:

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 - Ruby Gem для создания простого метрического отчета

PS: Я не пробовал ничего из вышеперечисленного, но metric_fu звучит интересно, см. скриншоты выхода.

Ответ 2

Я использую бесплатный Perl script 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
-------------------------------------------------------------------------------

Ответ 3

Здесь простое решение. Он подсчитывает строки кода в папке приложения проекта rails - CSS, Ruby, CoffeeScript и т.д. В корне вашего проекта выполните следующую команду:

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

Ответ 4

Это вычисляет количество файлов, общее количество строк кода, комментарии и среднее значение 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