在软件开发过程中,了解项目的代码量是一个重要的指标。无论是为了评估项目复杂度、工作量,还是进行代码审查,代码统计都是不可或缺的工具。

基础代码统计

最简单的代码行数统计

1
2
# 统计当前目录下所有 C# 文件的行数
Get-ChildItem -Recurse -Include *.cs -Exclude bin,obj,TestResults | Get-Content | Measure-Object -Line

命令详解

让我们逐一解析这个命令的每个部分:

1. Get-ChildItem 命令

1
Get-ChildItem -Recurse -Include *.cs -Exclude bin,obj,TestResults
  • Get-ChildItem: PowerShell 中用于获取文件和文件夹的基础命令,类似于 DOS 的 dir 命令
  • -Recurse: 递归参数,会遍历当前目录及所有子目录
  • -Include *.cs: 包含参数,只匹配扩展名为 .cs 的文件
  • -Exclude bin,obj,TestResults: 排除参数,忽略这些目录中的文件

2. Get-Content 命令

1
Get-Content
  • 读取文件的全部内容,每行作为一个字符串对象返回
  • 通过管道接收 Get-ChildItem 的文件对象

3. Measure-Object 命令

1
Measure-Object -Line
  • Measure-Object: 用于计算对象属性的统计信息
  • -Line: 统计行数参数,计算输入中的行数

常用参数变体

统计不同文件类型:

1
2
3
4
5
6
7
8
# 统计 JavaScript 文件
Get-ChildItem -Recurse -Include *.js | Get-Content | Measure-Object -Line

# 统计 Python 文件
Get-ChildItem -Recurse -Include *.py | Get-Content | Measure-Object -Line

# 统计多种文件类型
Get-ChildItem -Recurse -Include *.cs,*.js,*.ts | Get-Content | Measure-Object -Line

指定特定目录:

1
2
3
4
5
# 统计指定路径下的文件
Get-ChildItem -Path "C:\MyProject" -Recurse -Include *.cs | Get-Content | Measure-Object -Line

# 统计多个目录
Get-ChildItem -Path "C:\Project1","C:\Project2" -Recurse -Include *.cs | Get-Content | Measure-Object -Line

更多排除选项:

1
2
# 排除更多无关目录
Get-ChildItem -Recurse -Include *.cs -Exclude bin,obj,TestResults,packages,node_modules,.git | Get-Content | Measure-Object -Line

输出结果解读

命令执行后会返回类似这样的结果:

1
2
3
Lines Words Characters Property
----- ----- ---------- --------
1250
  • Lines: 总行数(我们关心的主要指标)
  • Words: 单词数(通常为空,因为我们只统计行数)
  • Characters: 字符数(通常为空)

实用技巧

保存结果到变量:

1
2
$result = Get-ChildItem -Recurse -Include *.cs | Get-Content | Measure-Object -Line
Write-Host "项目总行数: $($result.Lines)" -ForegroundColor Green

显示详细信息:

1
2
3
4
5
6
7
8
$files = Get-ChildItem -Recurse -Include *.cs -Exclude bin,obj
$content = $files | Get-Content
$stats = $content | Measure-Object -Line -Word -Character

Write-Host "文件数量: $($files.Count)"
Write-Host "总行数: $($stats.Lines)"
Write-Host "总单词数: $($stats.Words)"
Write-Host "总字符数: $($stats.Characters)"

进阶统计方法

多语言文件统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 统计多种编程语言的代码行数
$extensions = @("*.cs", "*.js", "*.ts", "*.html", "*.css", "*.sql", "*.py")
$totalLines = 0
$results = @()

foreach ($ext in $extensions) {
$files = Get-ChildItem -Recurse -Include $ext -Exclude bin,obj,node_modules,TestResults
if ($files) {
$lines = $files | Get-Content | Measure-Object -Line
$fileCount = $files.Count
$results += [PSCustomObject]@{
Language = $ext
Files = $fileCount
Lines = $lines.Lines
}
$totalLines += $lines.Lines
}
}

# 显示结果
$results | Format-Table -AutoSize
Write-Host "总行数: $totalLines" -ForegroundColor Green

多语言统计命令详解

1. 变量定义和初始化

1
2
3
$extensions = @("*.cs", "*.js", "*.ts", "*.html", "*.css", "*.sql", "*.py")
$totalLines = 0
$results = @()
  • $extensions: 定义一个数组,包含要统计的文件扩展名
  • $totalLines: 初始化总行数计数器
  • $results: 初始化结果数组,用于存储每种语言的统计信息

2. 文件获取和过滤

1
$files = Get-ChildItem -Recurse -Include $ext -Exclude bin,obj,node_modules,TestResults
  • 使用变量 $ext 作为 -Include 参数的值
  • 排除常见的编译输出和依赖目录

3. 条件判断和统计

1
2
3
4
5
if ($files) {
$lines = $files | Get-Content | Measure-Object -Line
$fileCount = $files.Count
# ...
}
  • if ($files): 检查是否找到了文件(防止空结果报错)
  • $files.Count: 获取文件数量属性

4. 自定义对象创建

1
2
3
4
5
$results += [PSCustomObject]@{
Language = $ext
Files = $fileCount
Lines = $lines.Lines
}
  • [PSCustomObject]: 创建自定义 PowerShell 对象
  • @{}: 哈希表语法,定义对象属性
  • +=: 将新对象添加到结果数组

5. 结果格式化显示

1
2
$results | Format-Table -AutoSize
Write-Host "总行数: $totalLines" -ForegroundColor Green
  • Format-Table -AutoSize: 以表格形式显示,自动调整列宽
  • Write-Host: 在控制台输出彩色文本
  • -ForegroundColor Green: 设置文本为绿色

🎨 输出结果示例

执行后会看到类似这样的输出:

1
2
3
4
5
6
7
8
9
10
11
Language Files Lines
-------- ----- -----
*.cs 45 3250
*.js 12 890
*.ts 8 456
*.html 5 234
*.css 3 123
*.sql 2 89
*.py 0 0

总行数: 5042

自定义扩展用法

添加更多文件类型:

1
2
3
$extensions = @("*.cs", "*.vb", "*.js", "*.ts", "*.jsx", "*.tsx",
"*.html", "*.htm", "*.css", "*.scss", "*.sass",
"*.sql", "*.py", "*.java", "*.cpp", "*.c", "*.h")

按文件大小排序:

1
$results | Sort-Object Lines -Descending | Format-Table -AutoSize

只显示有文件的语言:

1
$results | Where-Object { $_.Files -gt 0 } | Format-Table -AutoSize

保存结果到 CSV 文件:

1
$results | Export-Csv -Path "CodeStats.csv" -NoTypeInformation -Encoding UTF8

实用的快速统计命令

一行命令统计不同语言

1
2
3
4
5
6
7
8
# 统计 C# 文件
(Get-ChildItem -Recurse -Include *.cs -Exclude bin,obj | Get-Content | Measure-Object -Line).Lines

# 统计 JavaScript 文件
(Get-ChildItem -Recurse -Include *.js -Exclude node_modules | Get-Content | Measure-Object -Line).Lines

# 统计 Python 文件
(Get-ChildItem -Recurse -Include *.py | Get-Content | Measure-Object -Line).Lines

文件统计信息

1
2
3
4
5
6
# 显示文件数量和总行数
$files = Get-ChildItem -Recurse -Include *.cs -Exclude bin,obj
$lines = ($files | Get-Content | Measure-Object -Line).Lines
Write-Host "C# 文件数量: $($files.Count)"
Write-Host "总行数: $lines"
Write-Host "平均每文件行数: $([math]::Round($lines / $files.Count, 2))"

按文件类型分组统计

1
2
3
4
5
6
7
# 统计项目中各种文件类型的分布
Get-ChildItem -Recurse -File |
Where-Object { $_.FullName -notlike "*\bin\*" -and $_.FullName -notlike "*\obj\*" } |
Group-Object Extension |
Sort-Object Count -Descending |
Select-Object Name, Count |
Format-Table -AutoSize

查找最大的代码文件

1
2
3
4
5
6
# 找出最大的 5 个代码文件
Get-ChildItem -Recurse -Include *.cs,*.js,*.ts -Exclude bin,obj,node_modules |
Sort-Object Length -Descending |
Select-Object -First 5 |
Select-Object Name, @{Name="Size(KB)"; Expression={[math]::Round($_.Length/1KB, 2)}} |
Format-Table -AutoSize

实际应用场景

日常开发场景

  • 代码审查前: 了解修改的代码量和影响范围
  • 版本发布前: 统计本次版本的代码变更
  • 项目评估: 评估项目复杂度和工作量

项目管理场景

  • 进度跟踪: 定期统计代码增长情况
  • 质量评估: 通过代码行数和文件数评估代码质量

小结

PowerShell 提供了强大的文件处理和文本分析能力,通过组合使用 Get-ChildItemGet-ContentMeasure-Object 等命令,我们可以快速获得项目的各种统计信息。

从简单的行数统计到分类统计,这些命令可以帮助我们:

  • 提高效率: 自动化统计过程,节省手动计算时间
  • 数据驱动: 基于实际数据做出项目决策
  • 快速分析: 一行命令即可获得想要的统计结果

希望这些实用的命令能够帮助你更好地分析项目代码!


小贴士:

  • 可以将常用命令保存为 .ps1 脚本文件,方便重复使用
  • 记得在执行前设置 PowerShell 执行策略:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  • 使用 Get-Help Get-ChildItem -Examples 可以查看更多命令示例