Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 217ada81ea
Fetching contributors…

Cannot retrieve contributors at this time

file 60 lines (50 sloc) 1.34 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
require 'yard'
include YARD::Parser

file = ARGV[0] || __FILE__

$sloc, $cloc = 0, 0
class SLOC
  attr_accessor :files
  
  def initialize(*args)
    SourceParser.parser_type = :ruby18
    self.files = {}
    log.enter_level(Logger::FATAL) do
      args.each do |file|
        files[file] = {sloc:0, cloc:0}
        parse_string(file, IO.read(file))
      
        print_file(file)
      end

      print_totals
    end
  end
  
  def parse(file, stmts)
    stmts.each do |stmt|
      files[file][:sloc] += 1
      files[file][:cloc] += stmt.comments.size if stmt.comments
      parse_string(file, stmt.block.to_s) if stmt.block
    end
  end
  
  def parse_string(file, str)
    parse(file, YARD::Parser::SourceParser.parse_string(str))
  end
  
  def print_file(file)
    puts "#{file}:"
    puts "SLOC: #{files[file][:sloc]}"
    puts "CLOC: #{files[file][:cloc]}"
    puts
  end
  
  def print_totals
    total_sloc, total_cloc = 0, 0
    avg_sloc, avg_cloc = 0, 0
    files.each do |file, v|
      total_sloc += v[:sloc]
      total_cloc += v[:cloc]
    end
    avg_sloc = total_sloc.to_f / files.size
    avg_cloc = total_cloc.to_f / files.size
    puts "Totals:"
    puts "SLOC (avg): ~#{avg_sloc.round} per file"
    puts "CLOC (avg): ~#{avg_cloc.round} per file"
  end
end

files = ARGV.empty? ? [__FILE__] : ARGV
SLOC.new(*files)
Something went wrong with that request. Please try again.