code_stats 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +21 -0
- data/Rakefile +22 -0
- data/VERSION +1 -0
- data/lib/code_stats.rb +82 -0
- data/lib/code_stats/printer.rb +35 -0
- data/lib/code_stats/tasks.rb +37 -0
- metadata +79 -0
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= Code Stats
|
2
|
+
|
3
|
+
== Usage
|
4
|
+
|
5
|
+
In your Rakefile, add
|
6
|
+
|
7
|
+
CodeStats::Tasks.new
|
8
|
+
|
9
|
+
And you get:
|
10
|
+
|
11
|
+
Summary
|
12
|
+
154 Lines
|
13
|
+
123 Code
|
14
|
+
0 Comments
|
15
|
+
0.00 Comments to code %
|
16
|
+
31 Empty
|
17
|
+
|
18
|
+
=== Options
|
19
|
+
|
20
|
+
* <tt>directories</tt>: Takes an array of directories to report on. (default: 'lib')
|
21
|
+
* <tt>reporting_depth</tt>: Depth of subdirectories to report on. 0 is summary mode. (Default: 0)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'code_stats'))
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "code_stats"
|
7
|
+
s.description = s.summary = "Code statistics for your rakefile"
|
8
|
+
s.email = "joshbuddy@gmail.com"
|
9
|
+
s.homepage = "http://github.com/joshbuddy/code_stats"
|
10
|
+
s.authors = ["Joshua Hull"]
|
11
|
+
s.files = FileList["[A-Z]*", "{lib}/**/*"]
|
12
|
+
s.add_dependency 'rainbow'
|
13
|
+
s.add_dependency 'dirge', '>=0.0.3'
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
CodeStats::Tasks.new
|
21
|
+
|
22
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/code_stats.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'code_stats/tasks'
|
3
|
+
require 'code_stats/printer'
|
4
|
+
|
5
|
+
class CodeStats
|
6
|
+
|
7
|
+
class Stats
|
8
|
+
|
9
|
+
attr_reader :file
|
10
|
+
attr_accessor :lines, :comments, :empty
|
11
|
+
|
12
|
+
def initialize(file)
|
13
|
+
@file = file
|
14
|
+
@lines, @comments, @empty = 0, 0, 0
|
15
|
+
|
16
|
+
File.open(file).each_line do |line|
|
17
|
+
@lines += 1
|
18
|
+
case line
|
19
|
+
when /^\s*$/
|
20
|
+
@empty += 1
|
21
|
+
when /^\s*#/
|
22
|
+
@comments += 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def code
|
29
|
+
lines - comments - empty
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class Report
|
35
|
+
|
36
|
+
attr_accessor :lines, :comments, :empty
|
37
|
+
|
38
|
+
def initialize(*directories)
|
39
|
+
@directories = directories
|
40
|
+
@lines, @comments, @empty = 0, 0, 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def <<(stats)
|
44
|
+
@lines += stats.lines
|
45
|
+
@comments += stats.comments
|
46
|
+
@empty += stats.empty
|
47
|
+
end
|
48
|
+
|
49
|
+
def code
|
50
|
+
lines - comments - empty
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_reader :directories, :reporting_depth, :file_extensions
|
56
|
+
|
57
|
+
def initialize(options)
|
58
|
+
@directories = options[:directories] ? Array(options[:directories]) : raise('need to specify directory')
|
59
|
+
@reporting_depth = options[:reporting_depth] || 0
|
60
|
+
@file_extensions = Array(options[:file_extensions] || 'rb')
|
61
|
+
end
|
62
|
+
|
63
|
+
def run
|
64
|
+
reports = Hash.new{|h,k| h[k] = []}
|
65
|
+
directories.each do |dir|
|
66
|
+
Dir.glob(File.join(dir, '**', "*.{#{file_extensions * ','}}")) do |entry|
|
67
|
+
relative_entry = entry[entry.index(dir) + dir.size, entry.size]
|
68
|
+
relative_entry_parts = File.dirname(relative_entry).split(File::SEPARATOR).select{|f| not f.nil? || f.empty?}
|
69
|
+
stats = Stats.new(entry)
|
70
|
+
(0..reporting_depth).each do |depth|
|
71
|
+
if relative_entry_parts.size >= depth
|
72
|
+
reports[dir][depth] ||= {}
|
73
|
+
reports[dir][depth][relative_entry_parts[0, depth]] ||= Report.new(relative_entry_parts[0, depth])
|
74
|
+
reports[dir][depth][relative_entry_parts[0, depth]] << stats
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
reports
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rainbow'
|
2
|
+
|
3
|
+
class CodeStats
|
4
|
+
class Printer
|
5
|
+
|
6
|
+
def initialize(report)
|
7
|
+
@report = report
|
8
|
+
end
|
9
|
+
|
10
|
+
def print
|
11
|
+
@report.each do |dir, depth_reports|
|
12
|
+
puts "========== #{dir}"
|
13
|
+
(1...depth_reports.size).each do |depth|
|
14
|
+
puts "Depth #{depth}"
|
15
|
+
puts ""
|
16
|
+
depth_reports[depth].each do |dir, report|
|
17
|
+
report(dir, report)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
puts ""
|
21
|
+
puts 'Summary'.bright
|
22
|
+
report('', depth_reports[0].values.first)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def report(label, report)
|
27
|
+
puts "%-60s %10s %10s" % [label, report.lines, "Lines".color(:red)]
|
28
|
+
puts "%-60s %10s %10s" % ['', report.code, "Code".color(:blue)]
|
29
|
+
puts "%-60s %10s %10s" % ['', report.comments, "Comments".color(:green)]
|
30
|
+
puts "%-60s %10.2f %10s" % ['', report.comments.to_f / report.code, "Comments to code %"] unless report.code.zero?
|
31
|
+
puts "%-60s %10s %10s" % ['', report.empty, "Empty".color(:magenta)]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'dirge'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/tasklib'
|
4
|
+
|
5
|
+
class CodeStats
|
6
|
+
class Tasks < ::Rake::TaskLib
|
7
|
+
|
8
|
+
attr_accessor :building_block, :options
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
self.options = options
|
12
|
+
root_dir = __DIR_REL__(caller.find{|c| c=~/Rakefile/})
|
13
|
+
|
14
|
+
options[:directories] ||= 'lib'
|
15
|
+
options[:directories] = Array(options[:directories])
|
16
|
+
options[:directories].map! do |dir|
|
17
|
+
File.expand_path(File.join(root_dir, dir))
|
18
|
+
end
|
19
|
+
self.building_block = building_block
|
20
|
+
define
|
21
|
+
end
|
22
|
+
|
23
|
+
def stats
|
24
|
+
@stats ||= CodeStats.new(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def define
|
28
|
+
desc "Code stats - lines of code"
|
29
|
+
task :code_stats do
|
30
|
+
Printer.new(stats.run).print
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_reader :building_block
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: code_stats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Hull
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-29 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rainbow
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: dirge
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.3
|
34
|
+
version:
|
35
|
+
description: Code statistics for your rakefile
|
36
|
+
email: joshbuddy@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- README.rdoc
|
45
|
+
- Rakefile
|
46
|
+
- VERSION
|
47
|
+
- lib/code_stats.rb
|
48
|
+
- lib/code_stats/printer.rb
|
49
|
+
- lib/code_stats/tasks.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/joshbuddy/code_stats
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Code statistics for your rakefile
|
78
|
+
test_files: []
|
79
|
+
|