#!/usr/bin/env ruby # Author:: Nicolas Despres . # Copyright:: Copyright (c) 2005 Uttk team. All rights reserved. # License:: LGPL # $Id$ require 'pathname' require 'optparse' ME_DIR, ME = Pathname.new($0).split skip_comment = false skip_empty_line = false skip_test = false verbose = true append = false opts = OptionParser.new do |opts| opts.banner = "Usage: #{ME} [options] " opts.separator '' opts.on('-c', '--skip-comment', 'Skip comment') do skip_comment = true end opts.on('-C', '--no-skip-comment', 'Do not skip comment (default)') do skip_comment = false end opts.on('-l', '--skip-empty-line', 'Skip empty line') do skip_empty_line = true end opts.on('-L', '--no-skip-empty-line', 'Do not skip empty line (default)') do skip_empty_line = false end opts.on('-t', '--skip-test', 'Skip unit test suite') do skip_test = true end opts.on('-T', '--no-skip-test', 'Do not skip unit test suite (default)') do skip_test = false end opts.on('-v', '--verbose', 'Be verbose (default)') do verbose = true end opts.on('-V', '--no-verbose', 'Do not be verbose') do verbose = false end opts.on('-a', '--append', 'Append glob pattern to the search list') do append = true end end opts.parse!(ARGV) def merge_arrays! ( dst, src, &block ) src.each_with_index do |x, i| dst[i] = block[dst[i], x] end end GLOBDIRS = [ 'bin/**/*.rb', 'bin/uttk*', 'lib/**/*.{rb,js,css}', 'test/**/*.{rb,yml}', ] files = [] globdirs = GLOBDIRS if append globdirs += ARGV else globdirs = ARGV unless ARGV.empty? end globdirs.each do |glob| Pathname.glob(glob).each do |x| next if x.to_s =~ /~$/ files << x end end if verbose puts "%7s %7s %7s %7s %7s %s" % [ 'lines', 'code', 'comment', 'empty', 'test', 'file' ] end total = [0] * 5 files.each do |f| count = test_count = comment_count = empty_count = code_count = 0 unit_test = false f.each_line do |l| if l =~ /^\s*#/ comment_count += 1 elsif l =~ /^\s*$/ empty_count += 1 elsif l =~ /^(\s*)test_section/ unit_test = /^#{$1}end/ elsif unit_test if l =~ unit_test unit_test = false else test_count += 1 end else code_count += 1 end end count = code_count count += test_count unless skip_test count += comment_count unless skip_comment count += empty_count unless skip_empty_line if f.to_s =~ /\.yml$/ code_count, test_count = 0, code_count end lst = [ count, code_count, comment_count, empty_count, test_count ] merge_arrays!(total, lst) { |x, y| x + y } puts "%7s %7s %7s %7s %7s #{f}" % lst if verbose end if verbose puts "%7s %7s %7s %7s %7s total" % total else puts total[0] end