countloc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ CountLOC is copyrighted free software by Stephen Doyle <stephendoyle75@gmail.com>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see COPYING.txt file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial).
42
+
43
+ 5. The scripts and library files supplied as input to or produced as
44
+ output from the software do not automatically fall under the
45
+ copyright of the software, but belong to whomever generated them,
46
+ and may be sold commercially, and may be aggregated with this
47
+ software.
48
+
49
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
50
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
51
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52
+ PURPOSE.
@@ -0,0 +1,39 @@
1
+ Introduction
2
+ ------------
3
+
4
+ Contents:
5
+ ~~~~~~~~
6
+ 1. Overview
7
+ 2. Installation
8
+ 3. Usage
9
+
10
+
11
+ 1. Overview
12
+ -----------
13
+ CountLOC is a utility program, implemented in Ruby that provides support for
14
+ generating LOC metrics for source code.
15
+
16
+ Initial releases will support counting lines of code in Ruby source code.
17
+ Subsequent releases will add support for other programming languages such as
18
+ Python, C, C++, C#, Java, Perl, etc. ...
19
+
20
+
21
+ 2. Installation
22
+ ---------------
23
+ CountLOC is packaged as a Ruby gem and as a .zip file.
24
+
25
+ Gem:
26
+ ruby gem install countloc.gem
27
+
28
+ zip:
29
+ unzip countloc.zip
30
+
31
+
32
+ 3. Usage
33
+ --------
34
+ For full listing of options and usage:
35
+ countloc --help
36
+
37
+ To get the LOC metrics for a single Ruby file:
38
+ countloc some_file.rb
39
+
@@ -0,0 +1,101 @@
1
+ # = countloc.rb - Ruby line counter.
2
+ #
3
+ # Copyright (C) 2008 Stephen Doyle
4
+ #
5
+ # == Features
6
+ # countloc.rb currently supports generating LOC metrics for Ruby source code.
7
+ #
8
+ # The following commenting styles are supported:
9
+ # * Single line comments - starting from a # until the end of the line.
10
+ # * Multi-line comments - between "=begin" and "=end" tags.
11
+ #
12
+ # == Example
13
+ # countloc.rb --help
14
+ # countloc.rb some_file.rb
15
+ #
16
+
17
+ require 'optparse'
18
+
19
+ class CountLocPatterns
20
+ attr_reader :single_line_full # Entire line is a single line comment
21
+ attr_reader :single_line_mixed # Mixed code and comment on same line
22
+ attr_reader :multi_line_begin # Beginning of a multi-line comment
23
+ attr_reader :multi_line_end # End of a multi-line comment
24
+
25
+ def initialize
26
+ @single_line_full = /^\s*#/
27
+ @single_line_mixed = /#/
28
+ @multi_line_begin = /=begin/
29
+ @multi_line_end = /=end/
30
+ end
31
+ end
32
+
33
+ # Get the lines of code (LOC) metrics for the specified filename(s).
34
+ # Returns: Metrics in the form of a hash of name-value pairs.
35
+ def countloc(filename)
36
+ stats = { 'comments'=>0, 'code'=>0, 'blank'=>0, 'total'=>0 }
37
+ patterns = CountLocPatterns.new
38
+ isMultilineOpen = false
39
+
40
+ srcFile = File.new(filename, 'r').each do |line|
41
+ stats['total'] += 1
42
+
43
+ if isMultilineOpen
44
+ if line =~ patterns.multi_line_end
45
+ isMultilineOpen = false
46
+ end
47
+ stats['comments'] += 1
48
+ next
49
+ end
50
+
51
+ if line =~ /^\s*$/ # Blank line
52
+ stats['blank'] += 1
53
+ next
54
+ end
55
+
56
+ if line =~ patterns.multi_line_begin
57
+ isMultilineOpen = true
58
+ stats['comments'] += 1
59
+ next
60
+ end
61
+
62
+ if line =~ patterns.single_line_full
63
+ stats['comments'] += 1
64
+ next
65
+ end
66
+
67
+ # Process the line to avoid matching comment characters within quoted
68
+ # strings or regular expressions.
69
+ line.gsub!(/\".*?\"/, "X") # Quoted string
70
+ line.gsub!(/\/.*?\//, "X") # Regular expression
71
+
72
+ if line =~ patterns.single_line_mixed
73
+ stats['comments'] += 1
74
+ stats['code'] += 1
75
+ else
76
+ stats['code'] += 1
77
+ end
78
+ end
79
+
80
+ stats['code:comment'] = stats['code'].to_f / stats['comments']
81
+
82
+ return stats
83
+ end
84
+
85
+ # When run as a standalone script ...
86
+ if $0 == __FILE__:
87
+ usage = "Usage: #{$0} [-h --help] <file>"
88
+
89
+ options = {}
90
+ OptionParser.new do |opts|
91
+ opts.banner = usage
92
+ end.parse!
93
+
94
+ if ARGV.length != 1
95
+ puts usage
96
+ exit
97
+ end
98
+
99
+ stats = countloc(ARGV[0])
100
+ stats.each { |k,v| puts "#{k} = #{v}" }
101
+ end
@@ -0,0 +1,7 @@
1
+ =begin
2
+ This is a ruby file that uses multi-line comments.
3
+ It is used for testing countloc
4
+ =end
5
+
6
+ puts "Hello World"
7
+ puts "Blah, blah, blah ..."
@@ -0,0 +1,23 @@
1
+ # This is a sample piece of ruby code to test countloc with single
2
+ # line comments.
3
+ puts "Hello World"
4
+
5
+ # And some more code ...
6
+ 1.upto(10) { |x|puts x}
7
+
8
+ # And yet more ...
9
+ ('a'..'z').each do |alpha|
10
+ puts alpha # This is an example of mixed code and comments.
11
+ end
12
+
13
+ if "abcdefg".include? "#"
14
+ puts "We should never get here!"
15
+ end
16
+
17
+ if "abcdefg" =~ /#/
18
+ puts "We should never get here!"
19
+ end
20
+
21
+ if "abcdefg" =~ /#/ # This is mixed also!
22
+ puts "We should never get here!"
23
+ end
@@ -0,0 +1,28 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ # Copyright (C) 2008 Stephen Doyle
4
+
5
+ require 'test/unit'
6
+ require 'countloc'
7
+
8
+ class RubyTest < Test::Unit::TestCase
9
+
10
+ def test_ruby_single_line_comments
11
+ stats = countloc('ruby_single_line_comments.rb')
12
+ assert_equal(14, stats['code'])
13
+ assert_equal(6, stats['comments'])
14
+ assert_equal(5, stats['blank'])
15
+ assert_equal(23, stats['total'])
16
+ end
17
+
18
+ def test_ruby_multi_line_comments
19
+ stats = countloc('ruby_multiline_comments.rb')
20
+ assert_equal(2, stats['code'])
21
+ assert_equal(4, stats['comments'])
22
+ assert_equal(1, stats['blank'])
23
+ assert_equal(7, stats['total'])
24
+ end
25
+
26
+ end
27
+
28
+
@@ -0,0 +1,6 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ # Copyright (C) 2008 Stephen Doyle
4
+
5
+ require 'test/unit'
6
+ require 'tc_ruby'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: countloc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Doyle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-30 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: LOC metrics generation script implementation in Ruby.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.txt
26
+ - LICENSE.txt
27
+ - lib/countloc.rb
28
+ - test/ruby_multiline_comments.rb
29
+ - test/ruby_single_line_comments.rb
30
+ - test/tc_ruby.rb
31
+ - test/ts_countloc.rb
32
+ has_rdoc: true
33
+ homepage: http://countloc.rubyforge.org/
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --title
37
+ - CountLOC Documentation
38
+ require_paths:
39
+ - lib
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project: countloc
56
+ rubygems_version: 1.0.1
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Ruby line counter - countLOC.
60
+ test_files:
61
+ - test/ts_countloc.rb