Skip to content

Commit

Permalink
Merge pull request #6566 from arunagw/add-javascripts-to-rake-stats-task
Browse files Browse the repository at this point in the history
Add code statistics for Javascript and CoffeeScript files
  • Loading branch information
rafaelfranca committed May 31, 2012
2 parents c51fb02 + a48b3f1 commit 5752b31
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions railties/lib/rails/code_statistics.rb
Expand Up @@ -26,7 +26,7 @@ def calculate_statistics
Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
end

def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }

Dir.foreach(directory) do |file_name|
Expand All @@ -39,6 +39,13 @@ def calculate_directory_statistics(directory, pattern = /.*\.rb$/)

comment_started = false

case file_name
when /.*\.js$/
comment_pattern = /^\s*\/\//
else
comment_pattern = /^\s*#/
end

File.open(directory + "/" + file_name) do |f|
while line = f.gets
stats["lines"] += 1
Expand All @@ -55,7 +62,7 @@ def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
end
stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ comment_pattern
end
end
end
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/tasks/statistics.rake
Expand Up @@ -3,6 +3,7 @@ STATS_DIRECTORIES = [
%w(Helpers app/helpers),
%w(Models app/models),
%w(Mailers app/mailers),
%w(Javascripts app/assets/javascripts),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Integration\ tests test/integration),
Expand Down

0 comments on commit 5752b31

Please sign in to comment.