`
jsntghf
  • 浏览: 2479140 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

代码行数统计

阅读更多

一、rake stats

 

运行rake stats,这将会得到类似以下的输出:

 

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |    81 |    55 |       3 |      11 |   3 |     3 |
| Helpers              |    19 |    18 |       0 |       1 |   0 |    16 |
| Models               |    17 |    12 |       2 |       0 |   0 |     0 |
| Libraries            |     0 |     0 |       0 |       0 |   0 |     0 |
| Integration tests    |     0 |     0 |       0 |       0 |   0 |     0 |
| Functional tests     |    85 |    72 |       3 |       7 |   2 |     8 |
| Unit tests           |   168 |   144 |       2 |      18 |   9 |     6 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |   370 |   301 |      10 |      37 |   3 |     6 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 85     Test LOC: 216     Code to Test Ratio: 1:2.5

 

二、rake lines

 

到项目路径下执行rake lines即可。

 

task :lines => :environment do
  CheckLines.new(Rails.root.to_s + "/app/controllers")
end

module Enumerable
  def total_lines
    lines = 0
    each_with_index {|_, lines|}
    return lines + 1
  end
end

class CheckLines
  require 'find'

  CheckType = %w{txt rb rhtml erb yml html css xml js}.freeze

  def initialize(directory)
    @total_lines = 0
    if File.directory?(directory)
      @directory = directory
      @contents = {}
      @content_type = {}
      CheckType.each {|ext| @content_type[ext.to_sym] = 0}
      self.go
    else
      puts "#{directory} is not a directory!" and return
    end
  end

  def go
    if @directory
      Find.find @directory do |path|
        pathlite = path.gsub(@directory, '')
        if File.file?(path) && is_check_file(path)
          File.open path do |f|
            tmp_line = f.total_lines
            @contents.store(pathlite, tmp_line)
            @content_type[(path.sub(/.*\./, '').to_sym)] += tmp_line
            @total_lines += tmp_line
          end
        end
      end
      puts "Total Code Lines:#{@total_lines}"
      puts "Total Files #{@contents.keys.size}"
      @content_type.each_pair {|key,value| puts "#{key} : #{value}"}
      details
    end
  end

  def is_check_file file_name
    CheckType.any? do |ext|
      !file_name.scan(/\.#{ext}$/).empty?
    end
  end

  def details
    @contents.each do |key,value|
      puts "#{key} file has lines of #{value}"
    end
  end
end

 

输出如下:

 

Total Code Lines:81
Total Files 3
rb : 81
/application.rb file has lines of 14
/entry_comments_controller.rb file has lines of 26
/entries_controller.rb file has lines of 41

 

 

分享到:
评论

相关推荐

    代码行数统计小工具 代码行数统计小工具

    代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具代码行数统计小工具...

    代码行数统计工具

    代码行数统计工具,所有项目通用,代码行数统计工具,所有项目通用

    代码行数统计工具SourceCount

    代码行数统计工具,特别好用,可以统计代码的注释行数、空行数、代码有效行数。值得拥有。 代码行数统计工具,特别好用,可以统计代码的注释行数、空行数、代码有效行数。值得拥有。

    .net 代码行数统计工具

    .net 代码行数统计工具 可以详细统计出代码行数

    代码行数统计工具.rar

    二、功能介绍 本源码是一个代码行数统计源码,欢迎下载。三、菜单功能 1、点击环境设置,选择路径 2、点击代码行数统计按钮,选择代码文件,点击统计按钮,开始统计相关文件及个数,和行数统计 3、此时点击刷新...

    [代码行数统计工具]SourceCounter

    [代码行数统计工具]SourceCounter,统计源代码的行数

    VBCodeLines 代码行数统计工具

    VBCodeLines 代码行数统计工具,用户统计VB6项目代码行数。

    源代码行数统计器(统计软件工程源代码行数)

    源代码行数统计器 1.5 本软件用于统计软件工程源代码行数,可对指定的子目录下或整个目录树中所有指定类型的源代码文件进行行数统计。 本软件的统计结果包含源代码中的注释行和空行,因为作者认为它们同样也是源...

    代码行数统计工具 source counter + diffcount

    SourceCounter是一个适用性广泛的代码统计工具,通过这个工具能够对30多种代码进行代码行数、注释、空行、文件大小等信息进行统计分析,另外软件还支持对软件开发的各个... 使用工具diffcount可以统计一个项目之间两个...

    SourceCounter代码行数统计

    SourceCounter代码行数统计,统计各种程序类型代码行数,方便!

    程序源代码行数统计工具

    一个不错的程序代码行数统计器,可对cs,vb等进行代码行数统计

    代码行数统计工具代码统计

    代码行统计小工具,支持递归查询。统计内容包括:代码行数、注释行数、空行数、总行数等。网上下载的,与大家分享。来源查看程序,关于

    代码行数统计核心算法程序

    代码行数统计程序的核心算法程序,用于统计*.c或*.h文件的代码行数

    源代码行数统计器 CodeLineCounter

    源代码行数统计器 源代码行数统计器 作者:庄渭峰 版本:V1.5 类型:免费软件 功能简介: 本软件用于统计软件工程源代码行数,可对指定的子目录下或整个目录树中 所有指定类型的源代码文件进行行数统计。 本软件...

    软著申请样例,自动排版代码行数统计软件

    软著申请样例,自动排版代码行数统计软件。软著申请样例,自动排版代码行数统计软件。软著申请样例,自动排版代码行数统计软件。软著申请样例,自动排版代码行数统计软件。软著申请样例,自动排版代码行数统计软件。...

Global site tag (gtag.js) - Google Analytics