code_statistics 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/code_statistics.gemspec +55 -0
- data/lib/code_statistics.rb +1 -0
- data/lib/code_statistics/code_statistics.rb +109 -0
- data/lib/tasks/code_stats.rb +16 -0
- data/test/code_statistics_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +77 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Devver
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= code_statistics
|
2
|
+
|
3
|
+
This is a port of the rails 'rake stat' so it can be used on none rails projects and have it's features slowly expanded.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Devver. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'lib/tasks/code_stats'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "code_statistics"
|
9
|
+
gem.summary = %Q{Making a gem of the normal rails rake stats method, to make it more robust and work on non rails projects}
|
10
|
+
gem.description = %Q{"This is a port of the rails 'rake stats' method so it can be made more robust and work for non rails projects. New features may eventually be added as well."}
|
11
|
+
gem.email = "dan@devver.net"
|
12
|
+
gem.homepage = "http://github.com/danmayer/code_statistics"
|
13
|
+
gem.authors = ["Dan Mayer"]
|
14
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION')
|
48
|
+
version = File.read('VERSION')
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "code_statistics #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{code_statistics}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dan Mayer"]
|
12
|
+
s.date = %q{2009-10-23}
|
13
|
+
s.description = %q{"This is a port of the rails 'rake stats' method so it can be made more robust and work for non rails projects. New features may eventually be added as well."}
|
14
|
+
s.email = %q{dan@devver.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"code_statistics.gemspec",
|
27
|
+
"lib/code_statistics.rb",
|
28
|
+
"lib/code_statistics/code_statistics.rb",
|
29
|
+
"lib/tasks/code_stats.rb",
|
30
|
+
"test/code_statistics_test.rb",
|
31
|
+
"test/test_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/danmayer/code_statistics}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Making a gem of the normal rails rake stats method, to make it more robust and work on non rails projects}
|
38
|
+
s.test_files = [
|
39
|
+
"test/code_statistics_test.rb",
|
40
|
+
"test/test_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'tasks', 'code_stats')
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module CodeStatistics
|
2
|
+
class CodeStatistics #:nodoc:
|
3
|
+
|
4
|
+
TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests)
|
5
|
+
|
6
|
+
def initialize(*pairs)
|
7
|
+
@pairs = pairs
|
8
|
+
@statistics = calculate_statistics
|
9
|
+
@total = calculate_total if pairs.length > 1
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
print_header
|
14
|
+
@pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
|
15
|
+
print_splitter
|
16
|
+
|
17
|
+
if @total
|
18
|
+
print_line("Total", @total)
|
19
|
+
print_splitter
|
20
|
+
end
|
21
|
+
|
22
|
+
print_code_test_stats
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def calculate_statistics
|
27
|
+
@pairs.inject({}) { |stats, pair| stats[pair.first] = calculate_directory_statistics(pair.last); stats }
|
28
|
+
end
|
29
|
+
|
30
|
+
def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
|
31
|
+
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
|
32
|
+
|
33
|
+
Dir.foreach(directory) do |file_name|
|
34
|
+
if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name)
|
35
|
+
newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
|
36
|
+
stats.each { |k, v| stats[k] += newstats[k] }
|
37
|
+
end
|
38
|
+
|
39
|
+
next unless file_name =~ pattern
|
40
|
+
|
41
|
+
f = File.open(directory + "/" + file_name)
|
42
|
+
|
43
|
+
while line = f.gets
|
44
|
+
stats["lines"] += 1
|
45
|
+
stats["classes"] += 1 if line =~ /class [A-Z]/
|
46
|
+
stats["methods"] += 1 if line =~ /def [a-z]/
|
47
|
+
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
stats
|
52
|
+
end
|
53
|
+
|
54
|
+
def calculate_total
|
55
|
+
total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
|
56
|
+
@statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
|
57
|
+
total
|
58
|
+
end
|
59
|
+
|
60
|
+
def calculate_code
|
61
|
+
code_loc = 0
|
62
|
+
@statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k }
|
63
|
+
code_loc
|
64
|
+
end
|
65
|
+
|
66
|
+
def calculate_tests
|
67
|
+
test_loc = 0
|
68
|
+
@statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k }
|
69
|
+
test_loc
|
70
|
+
end
|
71
|
+
|
72
|
+
def print_header
|
73
|
+
print_splitter
|
74
|
+
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
|
75
|
+
print_splitter
|
76
|
+
end
|
77
|
+
|
78
|
+
def print_splitter
|
79
|
+
puts "+----------------------+-------+-------+---------+---------+-----+-------+"
|
80
|
+
end
|
81
|
+
|
82
|
+
def print_line(name, statistics)
|
83
|
+
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
|
84
|
+
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
|
85
|
+
|
86
|
+
start = if TEST_TYPES.include? name
|
87
|
+
"| #{name.ljust(20)} "
|
88
|
+
else
|
89
|
+
"| #{name.ljust(20)} "
|
90
|
+
end
|
91
|
+
|
92
|
+
puts start +
|
93
|
+
"| #{statistics["lines"].to_s.rjust(5)} " +
|
94
|
+
"| #{statistics["codelines"].to_s.rjust(5)} " +
|
95
|
+
"| #{statistics["classes"].to_s.rjust(7)} " +
|
96
|
+
"| #{statistics["methods"].to_s.rjust(7)} " +
|
97
|
+
"| #{m_over_c.to_s.rjust(3)} " +
|
98
|
+
"| #{loc_over_m.to_s.rjust(5)} |"
|
99
|
+
end
|
100
|
+
|
101
|
+
def print_code_test_stats
|
102
|
+
code = calculate_code
|
103
|
+
tests = calculate_tests
|
104
|
+
|
105
|
+
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
|
106
|
+
puts ""
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
STATS_DIRECTORIES = [
|
2
|
+
%w(Controllers app/controllers),
|
3
|
+
%w(Helpers app/helpers),
|
4
|
+
%w(Models app/models),
|
5
|
+
%w(Libraries lib/),
|
6
|
+
%w(APIs app/apis),
|
7
|
+
%w(Integration\ tests test/integration),
|
8
|
+
%w(Functional\ tests test/functional),
|
9
|
+
%w(Unit\ tests test/unit)
|
10
|
+
].collect { |name, dir| [ name, "#{Dir.pwd}/#{dir}" ] }.select { |name, dir| File.directory?(dir) }
|
11
|
+
|
12
|
+
desc "Report code statistics (KLOCs, etc) from the application"
|
13
|
+
task :stats do
|
14
|
+
require File.join(File.dirname(__FILE__), '..', 'code_statistics', 'code_statistics')
|
15
|
+
CodeStatistics::CodeStatistics.new(*STATS_DIRECTORIES).to_s
|
16
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: code_statistics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Mayer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-23 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: "\"This is a port of the rails 'rake stats' method so it can be made more robust and work for non rails projects. New features may eventually be added as well.\""
|
26
|
+
email: dan@devver.net
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- code_statistics.gemspec
|
42
|
+
- lib/code_statistics.rb
|
43
|
+
- lib/code_statistics/code_statistics.rb
|
44
|
+
- lib/tasks/code_stats.rb
|
45
|
+
- test/code_statistics_test.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/danmayer/code_statistics
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --charset=UTF-8
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.5
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Making a gem of the normal rails rake stats method, to make it more robust and work on non rails projects
|
75
|
+
test_files:
|
76
|
+
- test/code_statistics_test.rb
|
77
|
+
- test/test_helper.rb
|