# encoding: utf-8 module ProjectCodeStatistics module FetchFile def recursion_fetch(dir=nil) dir = @base_dir if dir == nil Dir.foreach(dir).each do |file_dir| dir_path = File.join(dir,file_dir) if File.directory?(dir_path) then recursion_fetch(dir_path) unless file_dir =~ /^\./ else if [/\.java$/,/\.jsp$/,/\.js$/,/\.xml$/].any?{|reg| reg=~file_dir} @all_file_with_row_number[dir_path.gsub(@base_dir,"")] = File.new(dir_path).readlines.size end end end end end class Calculate include FetchFile attr_reader :all_file_with_row_number def initialize(dir) @base_dir, @all_file_with_row_number = dir, {} recursion_fetch end # 分类统计所有文件数量 def file_total_numbers() row_numbers(:xml,:js,:jsp,:java,@all_file_with_row_number) end # 分类统计新加入的文件代码行总量的 def add_file_row_numbers(other) add_file_keys = @all_file_with_row_number.keys - other.all_file_with_row_number.keys add_file_hash = @all_file_with_row_number.find_all{|key,value| add_file_keys.include? key} row_numbers(:xml,:js,:jsp,:java,add_file_hash) end # 分类统计新加入的各种类型文件总量 def compare_add_file_count(other) add_file_keys = @all_file_with_row_number.keys - other.all_file_with_row_number.keys file_numbers(:xml,:js,:jsp,:java,add_file_keys) end # 分类统计修改过的文件 def compare_each_modify_file(other) file_hash = @all_file_with_row_number calculated_hash =other.all_file_with_row_number.find_all.inject({}) do |hash,item| hash[item[0]] = (file_hash[item[0]] - item[1]) if file_hash.include?(item[0]) && file_hash[item[0]] != item[1] hash end row_numbers(:xml,:js,:jsp,:java,calculated_hash) end def file_numbers(*parameter) last = parameter.reverse!.shift parameter.reverse.inject([]) do |total,type| regexp = eval('/' + type.to_s + '$/') total.push(last.find_all{|key,value| regexp =~ key}.size) end end def row_numbers(*parameter) last = parameter.reverse!.shift parameter.reverse.inject([]) do |total,type| regexp = eval('/' + type.to_s + '$/') total.push last.find_all{|key,value| regexp =~ key}.inject(0){|sum,item| sum + item[1] } end end end class Statistics def initialize(new_project,old_project,new_desc="新工程",old_desc="老工程") @new_project = Calculate.new(new_project) @old_project = Calculate.new(old_project) @new_desc,@old_desc = new_desc,old_desc end def print old_total = @old_project.file_total_numbers new_total = @new_project.file_total_numbers puts '系统代码统计' puts "#{@old_desc}代码总量统计,总计:#{old_total.reduce(:+)}行, java:#{old_total[3]}行,jsp: #{old_total[2]}行,js: #{old_total[1]}行,sql: #{old_total[0]}行 " puts "#{@new_desc}代码总量统计,总计:#{new_total.reduce(:+)}行,java:#{new_total[3]}行,jsp: #{new_total[2]}行,js: #{new_total[1]}行,sql: #{new_total[0]}行 " puts "新增总量 #{new_total.reduce(:+) - old_total.reduce(:+)}行" add_files = @new_project.compare_add_file_count(@old_project) new_rows = @new_project.add_file_row_numbers(@old_project) modify_rows = @new_project.compare_each_modify_file(@old_project) total_change = (new_rows.zip modify_rows).inject([]) {|n,a|n<<a.reduce(:+)} puts puts "新增加的文件数量,java:#{add_files[3]}个,jsp: #{add_files[2]}个,js: #{add_files[1]}个, sql: #{add_files[0]}个 " puts puts "新增加的文件代码行,java:#{new_rows[3]}行,jsp: #{new_rows[2]}行,js: #{new_rows[1]}行, sql: #{new_rows[0]}行 " puts "新增加的代码行, java:#{modify_rows[3]}行,jsp: #{modify_rows[2]}行,js: #{modify_rows[1]}行, sql: #{modify_rows[0]}行 " puts "新增加的代码行总计,java:#{total_change[3]}行,jsp: #{total_change[2]}行,js: #{total_change[1]}行, sql: #{total_change[0]}行 " puts "新增总量 #{new_rows.reduce(:+) + modify_rows.reduce(:+)}行" end end end s = ProjectCodeStatistics::Statistics.new('D:\boss\Code\udrm\src','D:\boss\Code\digital20110726\src') s.print?