Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegal committed Jun 17, 2009
1 parent face1db commit 217ada8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.yardoc
doc/
2 changes: 1 addition & 1 deletion mmmail
Submodule mmmail updated from f7fbf0 to b87679
60 changes: 60 additions & 0 deletions sloc.rb
@@ -0,0 +1,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)

0 comments on commit 217ada8

Please sign in to comment.