XCODE 脚本:项目行数统计

本文介绍了一个简单的Ruby脚本工具,用于统计项目中的代码行数。该工具能够计算特定类型的文件,并展示总代码行数及各类文件的分布情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

统计项目行数,从某方面也可以陶醉下自己项目规模。
暴力推荐无聊发呆时候使用。

这个从自己一个最早期Rails项目中剥离出来,虽然简单,不过勉强能用了。
使用方法:打开脚本编辑器。添加新脚本。拷贝代码,OutPut 选择 display in alert

That's all , Enjoy!

#!/usr/bin/env ruby
 
# description: Count code lines in project
# Copyright by [email]xhan87@gmail.com[/email]
# Sep 15 , 2009
 
 
##
##  this is designed to calculate the total lines of codes in your project
##  version 2 @May 24, 2009
##  \- add file filter ,count specific file types add 
##  version 1 @october 8th,2008 
 
 
 
module Enumerable
  # function to get total lines from a file
  def total_lines
    lines = 0
    each_with_index {|content,lines|}
    return lines+1
  end
end
 
 
class CheckLines
  require 'find'
 
  CheckType = %w{m h c cpp mm}.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! check it out!" 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}"}
    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
 
# s=CheckLines.new("/Users/xhan/Codes/plutocms")
 
project_path = `%%%{PBXUtilityScriptsPath}%%%/AskUserForFolderDialog "Select A Project Home Folder"`
#puts project_path
CheckLines.new(project_path[0..-2]) unless project_path.empty?


#!/bin/bash AUTHOR="caizhibiao" #目前ep上的统计开始时间 SINCE_TIME="2025-04-15" UNTIL_TIME="2025-05-21" #--author="caizhibiao" --since="2025-04-15" --until="2025-05-21" scope=(--author="$AUTHOR" --since="$SINCE_TIME" --until="$UNTIL_TIME") # 计算占比(保留两位小数) echo "用户 $AUTHOR 的提交统计:" echo "----------提交次数详情-----------" # 统计总量 TOTAL=$(git log "${scope[@]}" --oneline | wc -l) # 统计 OCT 前缀提交(格式:OCT XXX) OCT=$(git log "${scope[@]}" --oneline --grep="^OCT" | wc -l) # 统计 BYAC 关键字提交(格式:XX BYAC XX) BYAC=$(git log "${scope[@]}" --oneline --grep="BYAC" | wc -l) # 统计 BYMC 关键字提交(格式:XX BYMC XX) BYMC=$(git log "${scope[@]}" --oneline --grep="BYMC" | wc -l) echo "总提交次数 | $TOTAL" # 使用 awk 计算百分比(保留两位小数) echo "BYAC提交次数 | $BYAC → 占比: $(awk -v b=$BYAC -v t=$TOTAL 'BEGIN{printf "%.2f%%", b/t*100}')" echo "BYMC提交次数 | $BYMC → 占比: $(awk -v b=$BYMC -v t=$TOTAL 'BEGIN{printf "%.2f%%", b/t*100}')" echo "OCT 提交次数 | $OCT → 占比: $(awk -v o=$OCT -v t=$TOTAL 'BEGIN{printf "%.2f%%", o/t*100}')" echo "----------代码行数详情-----------" # 安装 cloc(如果尚未安装) # Ubuntu/Debian: sudo apt-get install cloc # MacOS: brew install cloc # 统计指定作者的代码行数(不包含注释) git log "${scope[@]}" --pretty=tformat: --numstat | \ awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "Added lines: %s, Removed lines: %s, Net lines: %s\n", add, subs, loc }' # 如果要排除空行和注释,可以结合 cloc git log "${scope[@]}" --pretty=format: --name-only | \ sort | uniq | grep -vE "^(test|spec|mock)" | \ xargs cloc --quiet --not-match-f=".*\.(json|xml|yml|md)$" --exclude-dir={test,tests,spec,mocks} echo "-----------实际结果--------------" echo "需求关联率 | $(awk -v oct=$OCT -v total=$TOTAL 'BEGIN{printf "%.2f%%", (total-oct)/total*100}')" echo "实际AC | $(awk -v oct=$OCT -v total=$TOTAL 'BEGIN{printf "%.2f%%", (total-oct)/total*100}')" 帮我修改一下可以在mac上运行
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值