code_metrics 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/code_metrics.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
module CodeMetrics
|
2
|
+
class LineStatistics
|
3
|
+
|
4
|
+
# @param files [Array, FileList, Enumerable]
|
5
|
+
# e.g. FileList["lib/active_record/**/*.rb"]
|
6
|
+
def initialize(files)
|
7
|
+
@files = Array(files).compact
|
8
|
+
end
|
9
|
+
|
10
|
+
# Calculates LOC for each file
|
11
|
+
# Outputs each file and a total LOC
|
12
|
+
def print_loc
|
13
|
+
lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
|
14
|
+
|
15
|
+
@files.each do |file_name|
|
16
|
+
next if file_name =~ /vendor/
|
17
|
+
File.open(file_name, 'r') do |f|
|
18
|
+
while line = f.gets
|
19
|
+
lines += 1
|
20
|
+
next if line =~ /^\s*$/
|
21
|
+
next if line =~ /^\s*#/
|
22
|
+
codelines += 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}"
|
26
|
+
|
27
|
+
total_lines += lines
|
28
|
+
total_codelines += codelines
|
29
|
+
|
30
|
+
lines, codelines = 0, 0
|
31
|
+
end
|
32
|
+
|
33
|
+
puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/code_metrics/version.rb
CHANGED
data/lib/tasks/statistics.rake
CHANGED
@@ -8,6 +8,17 @@ begin
|
|
8
8
|
STATS_DIRECTORIES = CodeMetrics::StatsDirectories.new.directories
|
9
9
|
CodeMetrics::Statistics.new(*STATS_DIRECTORIES).to_s
|
10
10
|
end
|
11
|
+
|
12
|
+
desc "Report LOC for each file, and a total LOC for the list of files. \bUsage rake code_metrics:line_statistics['lib/**/*.rb']"
|
13
|
+
task :line_statistics, :file_pattern do |t, args|
|
14
|
+
file_pattern = args[:file_pattern].to_s
|
15
|
+
raise "No file pattern entered, e.g. :line_statistics['lib/**/*.rb']" if file_pattern.size == 0
|
16
|
+
|
17
|
+
require 'code_metrics/line_statistics'
|
18
|
+
files = FileList[file_pattern]
|
19
|
+
CodeMetrics::LineStatistics.new(files).print_loc
|
20
|
+
end
|
21
|
+
|
11
22
|
end
|
12
23
|
rescue LoadError
|
13
24
|
STDERR.puts "Cannot load rake code_metrics:stats task, rake not available"
|
data/test/rake_test.rb
CHANGED
@@ -4,12 +4,20 @@ require 'code_metrics/railtie'
|
|
4
4
|
|
5
5
|
module CodeMetrics
|
6
6
|
class RakeTest < ActiveSupport::TestCase
|
7
|
+
|
7
8
|
def test_code_metrics_sanity
|
8
9
|
assert_match "Code LOC: 5 Test LOC: 0 Code to Test Ratio: 1:0.0",
|
9
10
|
Dir.chdir(app_path){ `rake code_metrics:stats` }
|
10
11
|
end
|
12
|
+
|
13
|
+
def test_line_metrics_sanity
|
14
|
+
assert_match "L: 5, LOC 3 | app/controllers/application_controller.rb\nTotal: Lines 5, LOC 3",
|
15
|
+
Dir.chdir(app_path){ `rake code_metrics:line_statistics['app/controllers/application_controller.rb']` }
|
16
|
+
end
|
17
|
+
|
11
18
|
def app_path
|
12
19
|
File.expand_path('../dummy', __FILE__)
|
13
20
|
end
|
21
|
+
|
14
22
|
end
|
15
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_metrics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -58,6 +58,7 @@ executables:
|
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
|
+
- lib/code_metrics/line_statistics.rb
|
61
62
|
- lib/code_metrics/profiler.rb
|
62
63
|
- lib/code_metrics/railtie.rb
|
63
64
|
- lib/code_metrics/statistics.rb
|