public
Description: A super tight library to add contexts to tests.
Homepage:
Clone URL: git://github.com/jeremymcanally/context.git
Click here to lend your support to: context and make a donation at www.pledgie.com !
context / countloc.rb
100644 66 lines (57 sloc) 1.184 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
61
62
63
64
65
66
def extract_path(argv)
  if argv[1].nil?
    if argv[0] =~ /-a/
      return "/**/*.rb"
    elsif argv[0]
      if argv[0] =~ /\.rb$/
        return argv[0]
      end
      return argv[0] + "/**/*.rb"
    else
      return "/**/*.rb"
    end
  elsif argv[1] =~ /\.rb$/
    return argv[1]
  else
    return argv[1] + "/**/*.rb"
  end
end
 
def all?
  ARGV.join =~ /-a/
end
 
def comment?(line)
  line =~ /^\s*#/
end
 
def blank?(line)
  line =~ /^\s*$/
end
 
def puke(header, locs, comments, blanks)
  puts header + ":"
  puts "#{locs} loc"
  puts "#{comments} lines of comments"
  puts "#{blanks} blank lines"
end
 
dir = File.dirname(__FILE__)
full_path = File.join(dir,extract_path(ARGV))
gloc = gcmts = gblanks = 0
Dir[File.expand_path("#{full_path}")].uniq.each do |file|
  if file =~ /.*\.rb$/
    
    loc = cmts = blanks = 0
 
    File.open(file, "r") do |f|
      while f.gets
        if comment?($_)
          cmts += 1
        elsif blank?($_)
          blanks += 1
        else
          loc += 1
        end
      end
    end
    gcmts += cmts
    gloc += loc
    gblanks += blanks
    puke(file, loc, cmts, blanks) if all?
  end
end
puke("Total", gloc, gcmts, gblanks)